Tools for Domain-Specific Modeling
by Steven Kelly

Listing One

/* Abridged version of LED.java, showing parts related to property value.
 * Whole file is 3 times as long. In total, the 4 LED classes are 663 lines.
 */

package org.eclipse.gef.examples.logicdesigner.model;

import org.eclipse.gef.examples.logicdesigner.LogicMessages;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

public class LED
    extends LogicSubpart
{
public static String P_VALUE = "value";  
protected static IPropertyDescriptor[] newDescriptors = null;
static{
    PropertyDescriptor pValueProp = new TextPropertyDescriptor(P_VALUE, 
            LogicMessages.PropertyDescriptor_LED_Value);
    pValueProp.setValidator(LogicNumberCellEditorValidator.instance());
    if(descriptors!=null){
        newDescriptors = new IPropertyDescriptor[descriptors.length+1];
        for(int i=0;i<descriptors.length;i++)
            newDescriptors[i] = descriptors[i];
        newDescriptors[descriptors.length] = pValueProp;
    } else
        newDescriptors = new IPropertyDescriptor[]{pValueProp};
}
public Object getPropertyValue(Object propName) {
    if (P_VALUE.equals(propName))
        return new Integer(getValue()).toString();
    if( ID_SIZE.equals(propName)){
        return new String("("+getSize().width+","+getSize().height+")");
    }
    return super.getPropertyValue(propName);
}
public void resetPropertyValue(Object id){
    if (P_VALUE.equals(id))
        setValue(0);
    super.resetPropertyValue(id);
}
public void setPropertyValue(Object id, Object value){
    if (P_VALUE.equals(id))
        setValue(Integer.parseInt((String)value));
    else
        super.setPropertyValue(id,value);
}
}


Listing Two
/* Abridged version of LEDFigure.java, showing just unselected symbol display.
 * Whole file is 4 times as long, plus 65 lines for dragged symbol display.
 */
protected void paintFigure(Graphics g) {
    Rectangle r = getBounds().getCopy();
    g.translate(r.getLocation());
    g.setBackgroundColor(LogicColorConstants.logicGreen);
    g.setForegroundColor(LogicColorConstants.connectorGreen);
    g.fillRectangle(0, 2, r.width, r.height - 4);   
    int right = r.width - 1;
    g.drawLine(0, Y1, right, Y1);
    g.drawLine(0, Y1, 0, Y2);
    
    g.setForegroundColor(LogicColorConstants.connectorGreen);
    g.drawLine(0, Y2, right, Y2);
    g.drawLine(right, Y1, right, Y2);

    // Draw the gaps for the connectors
    g.setForegroundColor(ColorConstants.listBackground);
    for (int i = 0; i < 4; i++) {
        g.drawLine(GAP_CENTERS_X[i] - 2, Y1, GAP_CENTERS_X[i] + 3, Y1);
        g.drawLine(GAP_CENTERS_X[i] - 2, Y2, GAP_CENTERS_X[i] + 3, Y2);
    }
    // Draw the connectors
    g.setForegroundColor(LogicColorConstants.connectorGreen);
    g.setBackgroundColor(LogicColorConstants.connectorGreen);
    for (int i = 0; i < 4; i++) {
        connector.translate(GAP_CENTERS_X[i], 0);
        g.fillPolygon(connector);
        g.drawPolygon(connector);
        connector.translate(-GAP_CENTERS_X[i], 0);
        
        bottomConnector.translate(GAP_CENTERS_X[i], r.height - 1);
        g.fillPolygon(bottomConnector);
        g.drawPolygon(bottomConnector);
        bottomConnector.translate(-GAP_CENTERS_X[i], -r.height + 1);
    }
    // Draw the display
    g.setBackgroundColor(LogicColorConstants.logicHighlight);
    g.fillRectangle(displayHighlight);
    g.setBackgroundColor(DISPLAY_SHADOW);
    g.fillRectangle(displayShadow);
    g.setBackgroundColor(ColorConstants.black);
    g.fillRectangle(displayRectangle);
    
    // Draw the value
    g.setFont(DISPLAY_FONT);
    g.setForegroundColor(DISPLAY_TEXT);
    g.drawText(value, valuePoint);
}






3


