use of net.sourceforge.usbdm.peripherals.model.RegisterModel in project usbdm-eclipse-plugins by podonoghue.
the class PeripheralsInformationPanel method updateContent.
/**
* Updates the peripheralsInformationPanel according to the current tree selection
*/
public void updateContent() {
// ITreeSelection selection = (ITreeSelection) fPeripheralsTreeViewer.getSelection();
// Object uModel = selection.getFirstElement();
// if ((uModel == null) || !(uModel instanceof BaseModel)) {
// return;
// }
// fCurrentModel = (BaseModel) uModel;
// System.err.println("PeripheralsInformationPanel.updateContent()");
setText("");
if (fCurrentModel == null) {
return;
}
String basicDescription = fCurrentModel.getDescription();
String valueString = "";
if (fCurrentModel instanceof RegisterModel) {
RegisterModel model = (RegisterModel) fCurrentModel;
valueString = String.format(" (%s,%s,%s)", model.getValueAsDecimalString(), model.getValueAsHexString(), model.getValueAsBinaryString());
} else if (fCurrentModel instanceof FieldModel) {
FieldModel model = (FieldModel) fCurrentModel;
valueString = String.format(" (%s,%s,%s)", model.getValueAsDecimalString(), model.getValueAsHexString(), model.getValueAsBinaryString());
}
StringBuffer description = new StringBuffer();
StyleRange valueStyleRange = null;
int splitAt = basicDescription.indexOf("\n");
if (!valueString.isEmpty()) {
if (splitAt != -1) {
description.append(basicDescription.substring(0, splitAt));
valueStyleRange = new StyleRange(description.length(), valueString.length(), Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE), null, SWT.NORMAL);
description.append(valueString);
description.append(basicDescription.substring(splitAt));
} else {
description.append(basicDescription);
valueStyleRange = new StyleRange(description.length(), valueString.length(), Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE), null, SWT.NORMAL);
description.append(valueString);
}
} else {
description.append(basicDescription);
}
StyleRange styleRange = new StyleRange(0, description.length(), null, null, SWT.BOLD);
if (fCurrentModel instanceof FieldModel) {
FieldModel uField = (FieldModel) fCurrentModel;
// Start of enumeration
int enumerationIndex = description.length();
// text
// Length of enumeration text
int enumerationlength = 0;
// Start of highlighted enumeration
int selectionIndex = 0;
// Length of highlighted enumeration
int selectionLength = 0;
long enumerationValue = 0;
boolean enumerationValid = false;
try {
enumerationValue = uField.getValue();
enumerationValid = true;
} catch (MemoryException e) {
}
for (Enumeration enumeration : uField.getEnumeratedDescription()) {
description.append("\n");
String enumerationValueDescription = enumeration.getName() + ": " + enumeration.getCDescription();
if ((selectionIndex == 0) && (enumerationValid && enumeration.isSelected(enumerationValue))) {
// Highlight first matching enumeration
selectionIndex = description.length();
selectionLength = enumerationValueDescription.length();
}
enumerationlength += enumerationValueDescription.length();
description.append(enumerationValueDescription);
}
setText(description.toString());
setStyleRange(styleRange);
if (valueStyleRange != null) {
setStyleRange(valueStyleRange);
}
styleRange = new StyleRange(enumerationIndex, enumerationlength, null, null, SWT.NORMAL);
setStyleRange(styleRange);
styleRange = new StyleRange(selectionIndex, selectionLength, Display.getCurrent().getSystemColor(SWT.COLOR_RED), null, SWT.NORMAL);
setStyleRange(styleRange);
} else {
setText(description.toString());
setStyleRange(styleRange);
if (valueStyleRange != null) {
setStyleRange(valueStyleRange);
}
}
}
use of net.sourceforge.usbdm.peripherals.model.RegisterModel in project usbdm-eclipse-plugins by podonoghue.
the class PeripheralsInformationPanel method selectionChanged.
/**
* Handles changes in selection of peripheral, register or field in tree view
*
* Updates description in infoPanel
* Attaches change listener to selected element
*
* @param event
*/
public void selectionChanged(SelectionChangedEvent event) {
Object source = event.getSource();
// System.err.println("PeripheralsInformationPanel.selectionChanged(), source = " + source.getClass());
if (source == fPeripheralsTreeViewer) {
ITreeSelection selection = (ITreeSelection) fPeripheralsTreeViewer.getSelection();
Object uModel = selection.getFirstElement();
// Detach from current peripheral
if (fPeripheralModel != null) {
fPeripheralModel.removeListener(this);
fPeripheralModel = null;
}
if ((uModel == null) || !(uModel instanceof BaseModel)) {
fCurrentModel = null;
updateContent();
return;
}
// Save model element to get values/description from
fCurrentModel = (BaseModel) uModel;
// Find peripheral that owns selected model
BaseModel model = fCurrentModel;
if (model instanceof FieldModel) {
// System.err.println("PeripheralsInformationPanel.selectionChanged(), traversing field = " + model);
model = model.getParent();
}
if (model instanceof RegisterModel) {
// System.err.println("PeripheralsInformationPanel.selectionChanged(), traversing register = " + model);
model = model.getParent();
}
if (model instanceof PeripheralModel) {
// Attach listener to peripheral
fPeripheralModel = (PeripheralModel) model;
fPeripheralModel.addListener(this);
// System.err.println("PeripheralsInformationPanel.selectionChanged(), listening to = " + model);
}
// else {
// System.err.println("PeripheralsInformationPanel.selectionChanged(), ignoring = " + model);
// System.err.println("PeripheralsInformationPanel.selectionChanged(), ignoring = " + model.getClass());
// }
updateContent();
}
}
use of net.sourceforge.usbdm.peripherals.model.RegisterModel in project usbdm-eclipse-plugins by podonoghue.
the class PeripheralsViewSorter method compare.
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
BaseModel element1 = (BaseModel) e1;
BaseModel element2 = (BaseModel) e2;
int diff = 0;
// Fields - sort by bitOffset then name
if (e1 instanceof FieldModel) {
diff = ((FieldModel) e2).getBitOffset() - ((FieldModel) e1).getBitOffset();
if (diff != 0) {
return diff;
}
diff = ((FieldModel) e2).getBitWidth() - ((FieldModel) e1).getBitWidth();
if (diff != 0) {
return diff;
}
diff = element1.getName().compareTo(element2.getName());
return diff;
}
// Registers sorted by address then name
if ((e1 instanceof RegisterModel) || (e1 instanceof ClusterModel)) {
if (element1.getAddress() > element2.getAddress()) {
diff = 1;
} else if (element1.getAddress() < element2.getAddress()) {
diff = -1;
} else {
diff = element1.getName().compareTo(element2.getName());
}
return diff;
}
// Sort criteria affects how peripherals are sorted only
switch(criteria) {
case PeripheralNameOrder:
// Sort by name then address (unlikely!)
diff = element1.getName().compareTo(element2.getName());
if (diff == 0) {
if (element1.getAddress() > element2.getAddress()) {
diff = 1;
} else if (element1.getAddress() < element2.getAddress()) {
diff = -1;
} else {
diff = element1.getName().compareTo(element2.getName());
}
}
break;
case AddressOrder:
// Sort by address then name
if (element1.getAddress() > element2.getAddress()) {
diff = 1;
} else if (element1.getAddress() < element2.getAddress()) {
diff = -1;
} else {
diff = element1.getName().compareTo(element2.getName());
}
break;
}
return diff;
}
use of net.sourceforge.usbdm.peripherals.model.RegisterModel in project usbdm-eclipse-plugins by podonoghue.
the class PeripheralsViewCellModifier method modify.
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
*/
@Override
public void modify(Object element, String property, Object value) {
// System.err.println("PeripheralsViewCellModifier.modify("+element.getClass()+", "+value.toString()+")");
if (element instanceof TreeItem) {
// Update element and tree model
TreeItem treeItem = (TreeItem) element;
Object treeItemData = treeItem.getData();
if (treeItemData instanceof RegisterModel) {
// System.err.println("PeripheralsViewCellModifier.modify(RegisterModel, "+value.toString()+")");
RegisterModel registerModel = (RegisterModel) treeItemData;
try {
String s = value.toString().trim();
if (s.startsWith("0b")) {
registerModel.setValue(Long.parseLong(s.substring(2, s.length()), 2));
} else {
registerModel.setValue(Long.decode(s));
}
} catch (NumberFormatException e) {
// System.err.println("PeripheralsViewCellModifier.modify(RegisterModel, ...) - format error");
}
} else if (treeItemData instanceof FieldModel) {
FieldModel fieldModel = (FieldModel) treeItemData;
try {
String s = value.toString().trim();
if (s.startsWith("0b")) {
fieldModel.setValue(Long.parseLong(s.substring(2, s.length()), 2));
} else {
fieldModel.setValue(Long.decode(s));
}
} catch (NumberFormatException e) {
// System.err.println("PeripheralsViewCellModifier.modify(FieldModel, ...) - format error");
}
}
}
}
use of net.sourceforge.usbdm.peripherals.model.RegisterModel in project usbdm-eclipse-plugins by podonoghue.
the class UsbdmDevicePeripheralsView method createPartControl.
// /**
// * Provides the editor for the tree elements
// *
// * Does minor modifications to the default editor.
// */
// private class PeripheralsViewTextCellEditor extends TextCellEditor {
//
// private int minHeight;
//
// public PeripheralsViewTextCellEditor(Tree tree) {
// super(tree, SWT.BORDER);
// Text txt = (Text) getControl();
//
// Font fnt = txt.getFont();
// FontData[] fontData = fnt.getFontData();
// if (fontData != null && fontData.length > 0) {
// minHeight = fontData[0].getHeight() + 10;
// }
// }
//
// public LayoutData getLayoutData() {
// LayoutData data = super.getLayoutData();
// if (minHeight > 0)
// data.minimumHeight = minHeight;
// return data;
// }
// }
/**
* Callback that creates the viewer and initialises it.
*
* The View consists of a tree and a information panel
*/
public void createPartControl(Composite parent) {
// Create the manager and bind to main composite
resManager = new LocalResourceManager(JFaceResources.getResources(), parent);
parent.setLayoutData(new FillLayout());
SashForm form = new SashForm(parent, SWT.VERTICAL);
form.setLayout(new FillLayout());
// Make sash visible
form.setSashWidth(4);
form.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
// =============================
fPeripheralsTreeViewer = new CheckboxTreeViewer(form, SWT.MULTI | SWT.V_SCROLL | SWT.FULL_SELECTION);
Tree tree = fPeripheralsTreeViewer.getTree();
tree.setLinesVisible(true);
tree.setHeaderVisible(true);
ColumnViewerToolTipSupport.enableFor(fPeripheralsTreeViewer);
// // Suppress tree expansion on double-click
// // see http://www.eclipse.org/forums/index.php/t/257325/
// peripheralsTreeViewer.getControl().addListener(SWT.MeasureItem, new Listener(){
// @Override
// public void handleEvent(Event event) {
// }});
fPeripheralsTreeViewer.setColumnProperties(fTreeProperties);
fPeripheralsTreeViewer.setCellEditors(new CellEditor[] { null, new TextCellEditor(fPeripheralsTreeViewer.getTree()), null });
// peripheralsTreeViewer.setCellEditors(new CellEditor[] { null, new PeripheralsViewTextCellEditor(peripheralsTreeViewer.getTree()), null });
fPeripheralsTreeViewer.setCellModifier(new PeripheralsViewCellModifier(this));
/*
* Name column
*/
TreeColumn column;
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultNameColumnWidth);
column.setText("Name");
// Add listener to column so peripherals are sorted by name when clicked
column.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
fPeripheralsTreeViewer.setComparator(new PeripheralsViewSorter(PeripheralsViewSorter.SortCriteria.PeripheralNameOrder));
}
});
/*
* Value column
*/
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultValueColumnWidth);
column.setText("Value");
column.setResizable(fDefaultValueColumnWidth != 0);
/*
* Field column
*/
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultFieldColumnWidth);
column.setText("Field");
column.setResizable(fDefaultFieldColumnWidth != 0);
/*
* Mode column
*/
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultModeWidth);
column.setText("Mode");
column.setResizable(fDefaultModeWidth != 0);
/*
* Location column
*/
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultLocationColumnWidth);
column.setText("Location");
column.setResizable(fDefaultLocationColumnWidth != 0);
// Add listener to column so peripheral are sorted by address when clicked
column.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
fPeripheralsTreeViewer.setComparator(new PeripheralsViewSorter(PeripheralsViewSorter.SortCriteria.AddressOrder));
}
});
/*
* Description column
*/
column = new TreeColumn(fPeripheralsTreeViewer.getTree(), SWT.NONE);
column.setWidth(fDefaultDescriptionColumnWidth);
column.setText("Description");
column.setResizable(fDefaultDescriptionColumnWidth != 0);
// Default to sorted by Peripheral name
fPeripheralsTreeViewer.setComparator(new PeripheralsViewSorter(PeripheralsViewSorter.SortCriteria.PeripheralNameOrder));
// Noting filtered
fPeripheralsTreeViewer.addFilter(new PeripheralsViewFilter(PeripheralsViewFilter.SelectionCriteria.SelectAll));
// Label provider
fPeripheralsTreeViewer.setLabelProvider(new PeripheralsViewCellLabelProvider(this));
// Content provider
fPeripheralsTreeViewer.setContentProvider(new PeripheralsViewContentProvider(this));
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(fPeripheralsTreeViewer) {
@Override
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
// ||
return (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION);
// (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR);
}
};
TreeViewerEditor.create(fPeripheralsTreeViewer, actSupport, TreeViewerEditor.DEFAULT);
// Create the help context id for the viewer's control
// PlatformUI.getWorkbench().getHelpSystem().setHelp(treeViewer.getControl(),
// "usbdmMemory.viewer");
// =============================
fPeripheralsInformationPanel = new PeripheralsInformationPanel(form, SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY, this.fPeripheralsTreeViewer);
form.setWeights(new int[] { 80, 20 });
// Tree expansion/collapse
fPeripheralsTreeViewer.addTreeListener(new ITreeViewerListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
Object element = event.getElement();
// System.err.println("treeExpanded() => event.getElement().getClass() = " + element.getClass());
if (element instanceof RegisterModel) {
((RegisterModel) element).update();
}
if (element instanceof PeripheralModel) {
((PeripheralModel) element).update();
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
});
// When user checks a checkbox in the tree, check all its children
fPeripheralsTreeViewer.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
// peripheralsTreeViewer.expandToLevel(event.getElement(), 1);
fPeripheralsTreeViewer.setSubtreeChecked(event.getElement(), event.getChecked());
}
});
// Create the actions
makeActions();
// Add selected actions to context menu
hookContextMenu();
// Add selected actions to menu bar
contributeToActionBars();
}
Aggregations