Math2mat

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

Go to the documentation of this file.
00001 
00018 package m2m.frontend.dynamicview.editPart;
00019 
00020 import java.beans.PropertyChangeEvent;
00021 import java.util.Vector;
00022 
00023 import org.eclipse.draw2d.ActionEvent;
00024 import org.eclipse.draw2d.ActionListener;
00025 import org.eclipse.draw2d.IFigure;
00026 import org.eclipse.draw2d.geometry.Rectangle;
00027 import org.eclipse.gef.EditPolicy;
00028 import org.eclipse.jface.dialogs.MessageDialog;
00029 import org.eclipse.swt.widgets.Display;
00030 import org.eclipse.ui.PlatformUI;
00031 
00032 import m2m.backend.structure.Function;
00033 import m2m.frontend.dynamicview.ThreadRouteConnections;
00034 import m2m.frontend.dynamicview.editpolicies.EditLayoutPolicy;
00035 import m2m.frontend.dynamicview.figure.FunctionFigure;
00036 import m2m.frontend.dynamicview.figure.PointConnectionFigure;
00037 import m2m.frontend.dynamicview.model.Connection;
00038 import m2m.frontend.dynamicview.model.GraphicFunction;
00039 import m2m.frontend.dynamicview.model.GraphicLoop;
00040 import m2m.frontend.dynamicview.model.Node;
00041 import m2m.frontend.dynamicview.model.PointConnection;
00042 import m2m.frontend.view.Editor;
00043 
00044 
00045 
00046 public class FunctionPart extends AbstractEditPart implements ActionListener
00047 {
00051         private int redInY;
00055         private int redOutY;
00056         
00057         
00062     @Override
00063     protected IFigure createFigure() 
00064     {
00065         IFigure figure = new FunctionFigure();
00066         ((FunctionFigure) figure).getBtnMinimize().addActionListener(this);
00067         ((FunctionFigure) figure).addMouseMotionListener(this);
00068         ((FunctionFigure) figure).addMouseListener(this);
00069         return figure;
00070     }
00071 
00072     
00076     @Override
00077     protected void createEditPolicies() 
00078     {
00079         installEditPolicy(EditPolicy.LAYOUT_ROLE, new EditLayoutPolicy()); 
00080     }
00081     
00082     
00086     protected void refreshVisuals()
00087     { 
00088         /* Get the figure and the model */
00089         FunctionFigure figure = (FunctionFigure)getFigure();
00090         GraphicFunction model = (GraphicFunction)getModel();
00091 
00092         /* Update the figure with the model */
00093         figure.setName(model.getName());
00094         figure.setLayout(model.getLayout());
00095         figure.setVisible(model.getVisible());
00096     }
00097 
00098     
00103     public Vector<Node> getModelChildren()
00104     {
00105         return ((GraphicFunction)getModel()).getChildrenArray();
00106     }
00107     
00108     
00109     
00114         @Override
00115         public void propertyChange(PropertyChangeEvent evt) 
00116         {
00117                  if (evt.getPropertyName().equals(Node.LAYOUT)) 
00118                          refreshVisuals();
00119                  else if(evt.getPropertyName().equals(Node.VISIBLE_COMPONENT))
00120                          refreshVisuals();
00121         }
00122 
00123 
00127         @Override
00128         public void actionPerformed(ActionEvent arg0) 
00129         {               
00130                 ((GraphicFunction)getModel()).setNotReduct(!((GraphicFunction)getModel()).getNotReduct());
00131                 ((GraphicFunction)getModel()).setNotReduct(reductionWindow(((GraphicFunction)getModel()).getNotReduct()));
00132         }
00133         
00134         
00140         public boolean reductionWindow(boolean visible)
00141         {
00142         int x;
00143         int y;  
00144         
00145                 /* Initialize the y reduction position of input and output connection point */
00146                 redInY  = FunctionFigure.SPACE_REDUCTION;
00147                 redOutY = FunctionFigure.SPACE_REDUCTION;
00148                 int maxInOut;
00149                         
00150                 // Get the function model
00151         GraphicFunction model = (GraphicFunction)getModel();
00152         
00153         // Check if the function can be maximize
00154         if(!visible || (visible && model.CanSetLayout(new Rectangle(model.getLayout().x, model.getLayout().y, model.getLayoutReduction().width, model.getLayoutReduction().height))))
00155         {               
00156                 Vector<Node> nodes = new Vector<Node>();
00157                 
00158                 /* Reverse the children vector of the function */
00159                         for(Node node : model.getChildrenArray()) 
00160                                 nodes.add(0, node);
00161                         
00162                         for(Node node : nodes)
00163                         {
00164                                 /* Mask/Unmask the nodes who has no connections outside the function */
00165                                 if(!hasExternConnection(node, visible))
00166                                         node.setVisible(visible);
00167                                 
00168                                 if(node instanceof GraphicLoop && !((GraphicLoop)node).getIsReduct())
00169                                         ((GraphicLoop)node).setConnectionsVisible(visible);
00170                                 else
00171                                 {
00172                                         /* Mask/Unmask the sources connections inside the function */
00173                                         for(Connection conn : node.getSourceConnections())
00174                                                 if((children.contains(conn.getTarget()) || !(conn.getTarget().getParent() == conn.getSource().getParent().getParent())))
00175                                                         conn.setVisible(visible);
00176                                                 
00177                                         /* Mask/Unmask the targets connections inside the function */
00178                                         for(Connection conn : node.getTargetConnections())
00179                                                 if( (children.contains(conn.getSource()) || !(conn.getSource().getParent() == conn.getTarget().getParent().getParent())))
00180                                                         conn.setVisible(visible);
00181                                 }
00182                         }               
00183                         
00184                         int space = 1;
00185                         
00186                         /* Set the layout of the output connection points and the function */
00187                         if(!visible)
00188                         {                               
00189                                 for(PointConnection pc : model.getOutputPointsConnection())
00190                                 {
00191                                 x =  model.getLayout().x + model.getReductSizeWidth() + 15;
00192                                 y =  model.getLayout().y + FunctionFigure.SPACE_REDUCTION*space + 2;                    
00193                                 pc.setLayoutReduction(new Rectangle(x, y, 10, 10));
00194                                 space++;
00195                                 }
00196                                 maxInOut = Math.max(model.getOutputPointsConnection().size(), ((Function)model.getElement()).getInput().size())+1;
00197                                 model.setSize(model.getReductSizeWidth(), maxInOut*FunctionFigure.SPACE_REDUCTION+model.getReductSizeHeight()); 
00198                         }               
00199                         else
00200                         {
00201                         for(PointConnection pc : model.getOutputPointsConnection())
00202                                 {
00203                                         x =  pc.getLayoutReduction().x + model.getLayout().x - model.getLayoutReduction().x;
00204                                 y =  pc.getLayoutReduction().y + model.getLayout().y - model.getLayoutReduction().y;
00205                                 pc.setLayout(new Rectangle(x, y, PointConnectionFigure.WIDTH, PointConnectionFigure.HEIGHT));
00206                                 pc.removeLayoutReduction();
00207                                 }       
00208                         model.setSize(model.getFullSizeWidth(), model.getFullSizeHeight());     
00209                         }
00210                         
00211                         if(model instanceof GraphicLoop)
00212                                 ((GraphicLoop)model).setIsReduct(!((GraphicLoop)model).getIsReduct());
00213                         
00214                         // Route Connections
00215                         Display.getCurrent().asyncExec(new ThreadRouteConnections());
00216                         return visible;
00217         }
00218        
00219         Editor editor = (Editor)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
00220             MessageDialog.openError(editor.getSite().getShell(), "Error", "This action is not allowed!");
00221             
00222         return !visible;        
00223         }
00224         
00225         
00233         public boolean hasExternConnection(Node node, boolean visible)
00234         {
00235                 // Get the list of children of the function
00236                 Vector<Node> children = ((GraphicFunction) getModel()).getChildrenArray();      
00237                                 
00238                 /* Check if the node has extern source connection */
00239                 for(Connection conn : node.getSourceConnections())
00240                 {
00241                         if(!children.contains(conn.getTarget()) && (conn.getTarget().getParent() == conn.getSource().getParent().getParent()))
00242                         {
00243                                 /* Set the new layout of the PointConnection */
00244                                 if(!visible)
00245                                 {
00246                                         node.setLayoutReduction(new Rectangle(((GraphicFunction) getModel()).getReductSizeWidth()-PointConnectionFigure.WIDTH-12, redOutY, 10, 10));                            
00247                                         redOutY += FunctionFigure.SPACE_REDUCTION;
00248                                 }
00249                                 else
00250                                 {
00251                                         node.setLayout(node.getLayoutReduction());              
00252                                         node.removeLayoutReduction();
00253                                 }
00254                                 return true;
00255                         }
00256                 }
00257                 
00258                 /* Check if the node has extern target connection */
00259                 for(Connection conn : node.getTargetConnections())
00260                 {
00261                         if(!children.contains(conn.getSource()) && (conn.getSource().getParent() == conn.getTarget().getParent().getParent()))
00262                         {
00263                                 /* Set the new layout of the PointConnection */
00264                                 if(!visible)
00265                                 {
00266                                         node.setLayoutReduction(new Rectangle(0, redInY, 10, 10));                              
00267                                         redInY += FunctionFigure.SPACE_REDUCTION;
00268                                 }
00269                                 else
00270                                 {
00271                                         node.setLayout(node.getLayoutReduction());              
00272                                         node.removeLayoutReduction();
00273                                 }
00274                                 return true;
00275                         }
00276                 }
00277                 
00278                 return false;
00279         }
00280 }
00281 
00282 
00283 
 All Classes Namespaces Files Functions Variables Enumerations