Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/frontend/dynamicview/editPart/ConnectionPart.java

Go to the documentation of this file.
00001 
00018 package m2m.frontend.dynamicview.editPart;
00019 
00020 import java.beans.PropertyChangeEvent;
00021 import java.beans.PropertyChangeListener;
00022 import java.util.Vector;
00023 
00024 import m2m.frontend.dynamicview.ThreadRouteConnections;
00025 import m2m.frontend.dynamicview.editpolicies.EditBendPointPolicy;
00026 import m2m.frontend.dynamicview.model.BendPoint;
00027 import m2m.frontend.dynamicview.model.Connection;
00028 import m2m.frontend.dynamicview.model.GraphicOperation;
00029 import m2m.frontend.dynamicview.model.Schema;
00030 
00031 import org.eclipse.draw2d.BendpointConnectionRouter;
00032 
00033 import org.eclipse.draw2d.ConnectionEndpointLocator;
00034 import org.eclipse.draw2d.IFigure;
00035 import org.eclipse.draw2d.Label;
00036 import org.eclipse.draw2d.PolygonDecoration;
00037 import org.eclipse.draw2d.PolylineConnection;
00038 import org.eclipse.draw2d.RelativeBendpoint;
00039 import org.eclipse.draw2d.geometry.Dimension;
00040 import org.eclipse.gef.EditPart;
00041 import org.eclipse.gef.EditPolicy;
00042 import org.eclipse.gef.editparts.AbstractConnectionEditPart;
00043 import org.eclipse.swt.widgets.Display;
00044 
00045 
00046 public class ConnectionPart extends AbstractConnectionEditPart implements PropertyChangeListener
00047 {     
00052         protected IFigure createFigure()
00053         {
00054                 PolylineConnection connection = (PolylineConnection) super.createFigure();
00055                 
00056                 /* Set a particulaty target decoration if the target model is a GraphicOperation */     
00057                 if(((Connection)getModel()).getTarget() instanceof GraphicOperation)
00058                         connection.setTargetDecoration(new PolygonDecoration());
00059                 
00060                 /* Initialize the style and the router of the connection */     
00061                 connection.setLineStyle(((Connection)getModel()).getLineStyle());
00062                 connection.setConnectionRouter(new BendpointConnectionRouter());
00063                 
00064                 /* Add a label to the source anchor if the source model is a function input connection point */ 
00065                 if(((Connection)getModel()).getSource().getParent() instanceof Schema)
00066                 {
00067                         Label l = new Label();
00068                         ConnectionEndpointLocator cl = new ConnectionEndpointLocator(connection, false);
00069                         cl.setUDistance(-5);
00070                         cl.setVDistance(-10);
00071                         l.setText(((Connection)getModel()).getSource().getName());
00072                         connection.add(l, cl);
00073                 }
00074                 /* Add a label to the source anchor if the source model is a function output connection point */        
00075                 else if(((Connection)getModel()).getTarget().getParent() instanceof Schema)
00076                 {
00077                         Label l = new Label();
00078                         ConnectionEndpointLocator cl = new ConnectionEndpointLocator(connection, true);
00079                         cl.setUDistance(-5);
00080                         cl.setVDistance(-10);
00081                         l.setText(((Connection)getModel()).getTarget().getName());
00082                         connection.add(l, cl);
00083                 }
00084                 
00085                 return connection;
00086         }
00087 
00088         
00092     public void activate() 
00093     {
00094         super.activate();
00095         ((Connection) getModel()).addPropertyChangeListener(this);
00096     }
00097 
00098     
00102     public void deactivate()
00103     {
00104         super.deactivate();
00105         ((Connection) getModel()).removePropertyChangeListener(this);
00106     }
00107         
00108 
00112     protected void refreshVisuals()
00113     { 
00114         /* Get the figure and the model */
00115         IFigure figure = getFigure();
00116         Connection model = (Connection)getModel();
00117         
00118         /* Update the figure with the model */
00119         figure.setVisible(model.getVisible());
00120         refreshBendpoints();
00121     }
00122     
00123     
00127     private void refreshBendpoints() 
00128     {
00129         Vector<RelativeBendpoint> figureConstraint = new Vector<RelativeBendpoint>();
00130         
00131         if (((Connection) getModel()).getBendPoints() != null) 
00132         {
00133                 /* Add all contained bend points in the model to the connection figure */
00134                 for (BendPoint wbp : ((Connection) getModel()).getBendPoints()) 
00135                 {
00136                         RelativeBendpoint rbp = new RelativeBendpoint(getConnectionFigure());
00137                         Dimension first  = new Dimension((Integer) wbp.getWidth()[0],(Integer) wbp.getHeight()[0]);
00138                         Dimension second = new Dimension((Integer) wbp.getWidth()[1],(Integer) wbp.getHeight()[1]);
00139                         rbp.setRelativeDimensions(first, second);
00140                         figureConstraint.add(rbp);
00141                 }
00142                 getConnectionFigure().setRoutingConstraint(figureConstraint);
00143         }
00144     }
00145         
00146         
00151         public void propertyChange(PropertyChangeEvent evt) 
00152         {
00153                  if (evt.getPropertyName().equals(Connection.VISIBLE_CONNECTION)) 
00154                          refreshVisuals();
00155                  else if (evt.getPropertyName().equals(Connection.BENDPOINT_POSITION)) 
00156                          refreshVisuals();
00157                  else if (evt.getPropertyName().equals(Schema.PROPERTY)) 
00158                  {
00159                          Schema schema = (Schema)((SchemaPart)getRoot().getContents()).getModel();
00160                          schema.setSelectedConnection(schema.getSelectedConnection());
00161                  }              
00162                  else if (evt.getPropertyName().equals(Connection.ROUTE_CONNECTION)) 
00163                          Display.getCurrent().asyncExec(new ThreadRouteConnections());
00164         }
00165         
00166 
00170         @Override
00171         public void setSelected(int value)
00172         {
00173                 super.setSelected(value);
00174                 
00175                 if(value != EditPart.SELECTED_NONE)
00176                 {
00177                         Connection con = (Connection) getModel();
00178                         Schema schema = (Schema)((SchemaPart)getRoot().getContents()).getModel();
00179                         schema.setPropertyLock(true);
00180                         schema.setSelectedConnection(con);
00181                 }
00182         }
00183         
00187         @Override
00188         protected void createEditPolicies() 
00189         {
00190                 installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE, new EditBendPointPolicy());
00191         }
00192 }
 All Classes Namespaces Files Functions Variables Enumerations