Search in sources :

Example 1 with MemoryException

use of net.sourceforge.usbdm.peripherals.model.MemoryException 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);
        }
    }
}
Also used : RegisterModel(net.sourceforge.usbdm.peripherals.model.RegisterModel) MemoryException(net.sourceforge.usbdm.peripherals.model.MemoryException) Enumeration(net.sourceforge.usbdm.peripheralDatabase.Enumeration) StyleRange(org.eclipse.swt.custom.StyleRange) FieldModel(net.sourceforge.usbdm.peripherals.model.FieldModel)

Aggregations

Enumeration (net.sourceforge.usbdm.peripheralDatabase.Enumeration)1 FieldModel (net.sourceforge.usbdm.peripherals.model.FieldModel)1 MemoryException (net.sourceforge.usbdm.peripherals.model.MemoryException)1 RegisterModel (net.sourceforge.usbdm.peripherals.model.RegisterModel)1 StyleRange (org.eclipse.swt.custom.StyleRange)1