2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.xcos.block;
16 import java.awt.MouseInfo;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
25 import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
27 import org.scilab.modules.graph.ScilabGraph;
28 import org.scilab.modules.graph.actions.CopyAction;
29 import org.scilab.modules.graph.actions.CutAction;
30 import org.scilab.modules.graph.actions.DefaultAction;
31 import org.scilab.modules.graph.actions.DeleteAction;
32 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
33 import org.scilab.modules.gui.contextmenu.ContextMenu;
34 import org.scilab.modules.gui.contextmenu.ScilabContextMenu;
35 import org.scilab.modules.gui.events.callback.CallBack;
36 import org.scilab.modules.gui.menu.Menu;
37 import org.scilab.modules.gui.menu.ScilabMenu;
38 import org.scilab.modules.gui.menuitem.MenuItem;
39 import org.scilab.modules.gui.menuitem.ScilabMenuItem;
40 import org.scilab.modules.hdf5.scilabTypes.ScilabDouble;
41 import org.scilab.modules.hdf5.scilabTypes.ScilabList;
42 import org.scilab.modules.hdf5.scilabTypes.ScilabString;
43 import org.scilab.modules.hdf5.scilabTypes.ScilabType;
44 import org.scilab.modules.hdf5.write.H5Write;
45 import org.scilab.modules.xcos.Xcos;
46 import org.scilab.modules.xcos.XcosUIDObject;
47 import org.scilab.modules.xcos.actions.ShowHideShadowAction;
48 import org.scilab.modules.xcos.block.actions.AlignBlockAction;
49 import org.scilab.modules.xcos.block.actions.BlockDocumentationAction;
50 import org.scilab.modules.xcos.block.actions.BlockParametersAction;
51 import org.scilab.modules.xcos.block.actions.ColorAction;
52 import org.scilab.modules.xcos.block.actions.FlipAction;
53 import org.scilab.modules.xcos.block.actions.MirrorAction;
54 import org.scilab.modules.xcos.block.actions.RegionToSuperblockAction;
55 import org.scilab.modules.xcos.block.actions.RotateAction;
56 import org.scilab.modules.xcos.block.actions.ViewDetailsAction;
57 import org.scilab.modules.xcos.graph.PaletteDiagram;
58 import org.scilab.modules.xcos.graph.SuperBlockDiagram;
59 import org.scilab.modules.xcos.graph.XcosDiagram;
60 import org.scilab.modules.xcos.io.BasicBlockInfo;
61 import org.scilab.modules.xcos.io.BlockReader;
62 import org.scilab.modules.xcos.port.BasicPort;
63 import org.scilab.modules.xcos.port.command.CommandPort;
64 import org.scilab.modules.xcos.port.control.ControlPort;
65 import org.scilab.modules.xcos.port.input.InputPort;
66 import org.scilab.modules.xcos.port.output.OutputPort;
67 import org.scilab.modules.xcos.utils.BlockPositioning;
68 import org.scilab.modules.xcos.utils.StyleMap;
69 import org.scilab.modules.xcos.utils.XcosConstants;
70 import org.scilab.modules.xcos.utils.XcosEvent;
71 import org.scilab.modules.xcos.utils.XcosInterpreterManagement;
72 import org.scilab.modules.xcos.utils.XcosMessages;
73 import org.scilab.modules.xcos.utils.XcosInterpreterManagement.InterpreterException;
75 import com.mxgraph.model.mxGeometry;
76 import com.mxgraph.util.mxConstants;
77 import com.mxgraph.util.mxEventObject;
78 import com.mxgraph.util.mxUtils;
80 public class BasicBlock extends XcosUIDObject {
82 private static final long serialVersionUID = 2189690915516168262L;
83 private String interfaceFunctionName = "xcos_block";
84 private String simulationFunctionName = "xcos_simulate";
85 private SimulationFunctionType simulationFunctionType = SimulationFunctionType.DEFAULT;
86 private transient XcosDiagram parentDiagram = null;
88 private transient int angle = 0;
89 private transient boolean isFlipped = false;
90 private transient boolean isMirrored = false;
93 // TODO : Must make this types evolve, but for now keep a strong link to Scilab
95 // exprs = [] ; rpar = [] ; ipar = [] ; opar = list()
97 //private List<String> exprs = new ArrayList<String>();
98 private ScilabType exprs;
99 //private List<Double> realParameters = new ArrayList<Double>();
100 private ScilabType realParameters;
101 //private List<Integer> integerParameters = new ArrayList<Integer>();
102 private ScilabType integerParameters;
103 //private List objectsParameters = new ArrayList();
104 private ScilabType objectsParameters;
106 private ScilabType nbZerosCrossing = new ScilabDouble();
108 private ScilabType nmode = new ScilabDouble();
110 private ScilabType state = new ScilabDouble();
111 private ScilabType dState = new ScilabDouble();
112 private ScilabType oDState = new ScilabDouble();
114 private ScilabType equations = null;
116 private boolean dependsOnU = false;
117 private boolean dependsOnT = false;
119 private String blockType = "c";
121 private int ordering = 0;
122 private boolean locked = false;
125 * Represent a simulation function type compatible with Scilab/Scicos
126 * function type descriptors.
128 public enum SimulationFunctionType {
129 ESELECT(-2.0), IFTHENELSE(-1.0), DEFAULT(0.0), TYPE_1(1.0), TYPE_2(2.0),
130 TYPE_3(3.0), C_OR_FORTRAN(4.0), SCILAB(5.0), UNKNOWN(5.0);
132 private double value;
135 * Default constructor
138 * Scilab/Scicos function type descriptor
140 private SimulationFunctionType(double scilabValue) {
145 * Get the Java descriptor from the Scilab descriptor.
148 * Scilab/Scicos function type descriptor
149 * @return The corresponding java descriptor
151 public static SimulationFunctionType convertScilabValue(int scilabValue) {
152 for (SimulationFunctionType iter : SimulationFunctionType.values()) {
153 if (iter.getAsDouble() == scilabValue) {
161 * Get the Scilab Descriptor from the Java Descriptor
163 * @return The corresponding Scilab/Scicos descriptor
165 public double getAsDouble() {
170 public BasicBlock() {
176 protected BasicBlock(String label) {
182 protected BasicBlock(String label, String style) {
188 * Initialize the block with the default values
190 protected void setDefaultValues() {
193 setConnectable(false);
194 setGeometry(new mxGeometry(0, 0, 40, 40));
199 public XcosDiagram getParentDiagram() {
200 return parentDiagram;
203 public void setParentDiagram(XcosDiagram parentDiagram) {
204 this.parentDiagram = parentDiagram;
208 public String getInterfaceFunctionName() {
209 return interfaceFunctionName;
212 public void setInterfaceFunctionName(String interfaceFunctionName) {
213 this.interfaceFunctionName = interfaceFunctionName;
216 public void setSimulationFunctionName(String simulationFunctionName) {
217 this.simulationFunctionName = simulationFunctionName;
220 public String getSimulationFunctionName() {
221 return simulationFunctionName;
224 public void setSimulationFunctionType(int scilabValue) {
225 this.simulationFunctionType = SimulationFunctionType.convertScilabValue(scilabValue);
228 public void setSimulationFunctionType(SimulationFunctionType simulationFunctionType) {
229 this.simulationFunctionType = simulationFunctionType;
232 public SimulationFunctionType getSimulationFunctionType() {
233 return simulationFunctionType;
236 public ScilabType getRealParameters() {
237 return realParameters;
240 public void setRealParameters(ScilabType realParameters) {
241 this.realParameters = realParameters;
244 public ScilabType getIntegerParameters() {
245 return integerParameters;
248 public void setIntegerParameters(ScilabType integerParameters) {
249 this.integerParameters = integerParameters;
252 public ScilabType getObjectsParameters() {
253 return objectsParameters;
256 public void setObjectsParameters(ScilabType objectsParameters) {
257 this.objectsParameters = objectsParameters;
260 public void setDependsOnU(boolean dependsOnU) {
261 this.dependsOnU = dependsOnU;
264 public boolean isDependsOnU() {
265 return this.dependsOnU;
268 public boolean dependsOnU() {
272 public void setDependsOnT(boolean dependsOnT) {
273 this.dependsOnT = dependsOnT;
276 public boolean isDependsOnT() {
277 return this.dependsOnT;
279 public boolean dependsOnT() {
283 public void setBlockType(String blockType) {
284 this.blockType = blockType;
287 public String getBlockType() {
291 public void setOrdering(int ordering) {
292 this.ordering = ordering;
295 public int getOrdering() {
299 public void setExprs(ScilabType exprs) {
303 public ScilabType getExprs() {
307 public ScilabType getNbZerosCrossing() {
308 return nbZerosCrossing;
311 public void setNbZerosCrossing(ScilabType nbZerosCrossing) {
312 this.nbZerosCrossing = nbZerosCrossing;
315 public ScilabType getNmode() {
319 public void setNmode(ScilabType nmode) {
323 public ScilabType getState() {
327 public void setState(ScilabType state) {
331 public ScilabType getDState() {
335 public void setDState(ScilabType state) {
339 public ScilabType getODState() {
343 public void setODState(ScilabType state) {
347 public ScilabType getEquations() {
351 public void setEquations(ScilabType equations) {
352 this.equations = equations;
355 public synchronized boolean isLocked() {
359 public synchronized void setLocked(boolean locked) {
360 this.locked = locked;
363 public void removePort(BasicPort port){
364 if(port.getEdgeCount() != 0) {
365 getParentDiagram().removeCells(new Object[]{port.getEdgeAt(0)});
370 public void addPort(InputPort port) {
372 BlockPositioning.updateBlockView(this);
373 port.setOrdering(BasicBlockInfo.getAllInputPorts(this, false).size());
376 public void addPort(OutputPort port) {
378 BlockPositioning.updateBlockView(this);
379 port.setOrdering(BasicBlockInfo.getAllOutputPorts(this, false).size());
382 public void addPort(CommandPort port) {
384 BlockPositioning.updateBlockView(this);
385 port.setOrdering(BasicBlockInfo.getAllCommandPorts(this, false).size());
388 public void addPort(ControlPort port) {
390 BlockPositioning.updateBlockView(this);
391 port.setOrdering(BasicBlockInfo.getAllControlPorts(this, false).size());
394 public ScilabDouble getAllCommandPortsInitialStates() {
395 if (BasicBlockInfo.getAllCommandPorts(this, false).isEmpty()) {
396 return new ScilabDouble();
399 double[][] data = new double[BasicBlockInfo.getAllCommandPorts(this, false).size()][1];
400 for (int i = 0 ; i < BasicBlockInfo.getAllCommandPorts(this, false).size() ; ++i) {
401 data[i][0] = BasicBlockInfo.getAllCommandPorts(this, false).get(i).getInitialState();
404 return new ScilabDouble(data);
407 public ScilabType getSimulationFunctionNameAndType() {
408 if (getSimulationFunctionType() == SimulationFunctionType.DEFAULT) {
409 return new ScilabString(getSimulationFunctionName());
411 ScilabList data = new ScilabList();
413 data.add(new ScilabString(getSimulationFunctionName()));
414 data.add(new ScilabDouble(getSimulationFunctionType().getAsDouble()));
420 * Does the block update and register on the undo manager
421 * @param modifiedBlock the new settings
423 public void updateBlockSettings(BasicBlock modifiedBlock) {
425 /* TODO: emit changes on update */
426 // mxUndoableEdit edit = new mxUndoableEdit(getParentDiagram().getModel()) {
427 // public void dispatch()
429 // ((mxGraphModel) source).fireEvent(mxEvent.CHANGE,
430 // new mxEventObject(new Object[] { changes }));
433 // edit.add(new BlockChange(modifiedBlock, this));
435 doUpdateBlockSettings(modifiedBlock);
439 * Does the block update without using the undo manager
440 * @param modifiedBlock the new settings
442 public void doUpdateBlockSettings(BasicBlock modifiedBlock) {
443 setDependsOnT(modifiedBlock.dependsOnT());
444 setDependsOnU(modifiedBlock.dependsOnU());
445 setExprs(modifiedBlock.getExprs());
447 setRealParameters(modifiedBlock.getRealParameters());
448 setIntegerParameters(modifiedBlock.getIntegerParameters());
449 setObjectsParameters(modifiedBlock.getObjectsParameters());
451 setState(modifiedBlock.getState());
452 setDState(modifiedBlock.getDState());
453 setODState(modifiedBlock.getODState());
455 setEquations(modifiedBlock.getEquations());
458 List<? extends BasicPort> modifiedPorts = null;
459 List<? extends BasicPort> ports = null;
461 // Check if new input port have been added
462 if ((modifiedPorts = BasicBlockInfo.getAllInputPorts(modifiedBlock, false)).size() > (ports = BasicBlockInfo.getAllInputPorts(this, false)).size()) {
463 while((ports = BasicBlockInfo.getAllInputPorts(this, false)).size() < modifiedPorts.size()) {
464 addPort((InputPort)modifiedPorts.get(ports.size()));
467 // Check if input ports have been removed
468 else if ((modifiedPorts = BasicBlockInfo.getAllInputPorts(modifiedBlock, false)).size() < (ports = BasicBlockInfo.getAllInputPorts(this, false)).size()) {
469 while((ports = BasicBlockInfo.getAllInputPorts(this, false)).size() > modifiedPorts.size()) {
470 removePort((BasicPort)ports.get(ports.size() - 1));
474 // Check if new output port have been added
475 if ((modifiedPorts = BasicBlockInfo.getAllOutputPorts(modifiedBlock, false)).size() > (ports = BasicBlockInfo.getAllOutputPorts(this, false)).size()) {
476 while((ports = BasicBlockInfo.getAllOutputPorts(this, false)).size() < modifiedPorts.size()) {
477 addPort((OutputPort)modifiedPorts.get(ports.size()));
480 // Check if output ports have been removed
481 else if ((modifiedPorts = BasicBlockInfo.getAllOutputPorts(modifiedBlock, false)).size() < (ports = BasicBlockInfo.getAllOutputPorts(this, false)).size()) {
482 while((ports = BasicBlockInfo.getAllOutputPorts(this, false)).size() > modifiedPorts.size()) {
483 removePort((BasicPort)ports.get(ports.size() - 1));
488 // Check if new command port have been added
489 if ((modifiedPorts = BasicBlockInfo.getAllCommandPorts(modifiedBlock, false)).size() > (ports = BasicBlockInfo.getAllCommandPorts(this, false)).size()) {
490 while((ports = BasicBlockInfo.getAllCommandPorts(this, false)).size() < modifiedPorts.size()) {
491 addPort((CommandPort)modifiedPorts.get(ports.size()));
494 // Check if command ports have been removed
495 else if ((modifiedPorts = BasicBlockInfo.getAllCommandPorts(modifiedBlock, false)).size() < (ports = BasicBlockInfo.getAllCommandPorts(this, false)).size()) {
496 while((ports = BasicBlockInfo.getAllCommandPorts(this, false)).size() > modifiedPorts.size()) {
497 removePort((BasicPort)ports.get(ports.size() - 1));
501 // Check if new control port have been added
502 if ((modifiedPorts = BasicBlockInfo.getAllControlPorts(modifiedBlock, false)).size() > (ports = BasicBlockInfo.getAllControlPorts(this, false)).size()) {
503 while((ports = BasicBlockInfo.getAllControlPorts(this, false)).size() < modifiedPorts.size()) {
504 addPort((ControlPort)modifiedPorts.get(ports.size()));
507 // Check if control ports have been removed
508 else if ((modifiedPorts = BasicBlockInfo.getAllControlPorts(modifiedBlock, false)).size() < (ports = BasicBlockInfo.getAllControlPorts(this, false)).size()) {
509 while((ports = BasicBlockInfo.getAllControlPorts(this, false)).size() > modifiedPorts.size()) {
510 removePort((BasicPort)ports.get(ports.size() - 1));
515 * If the block is in a superblock then update it.
517 if (getParentDiagram() instanceof SuperBlockDiagram) {
518 SuperBlock parentBlock = ((SuperBlockDiagram) getParentDiagram()).getContainer();
519 parentBlock.getParentDiagram().fireEvent(new mxEventObject(XcosEvent.SUPER_BLOCK_UPDATED, "block", parentBlock));
524 public void openBlockSettings(String context[]) {
526 if(getParentDiagram() instanceof PaletteDiagram) {
530 //prevent to open twice
535 final File tempOutput;
536 final File tempInput;
537 final File tempContext;
539 tempInput = File.createTempFile("xcos",".h5",new File(System.getenv("TMPDIR")));
540 tempInput.deleteOnExit();
543 tempOutput = exportBlockStruct();
545 tempContext = exportContext(context);
549 cmd = "xcosBlockInterface(\""+tempOutput.getAbsolutePath()+"\"";
550 cmd += ", \""+tempInput.getAbsolutePath()+"\"";
551 cmd += ", "+getInterfaceFunctionName();
553 cmd += ", \""+tempContext.getAbsolutePath()+"\");";
555 final BasicBlock currentBlock = this;
557 XcosInterpreterManagement.asynchronousScilabExec(cmd, new ActionListener() {
558 public void actionPerformed(ActionEvent arg0) {
559 // Now read new Block
560 BasicBlock modifiedBlock = BlockReader.readBlockFromFile(tempInput.getAbsolutePath());
561 updateBlockSettings(modifiedBlock);
562 getParentDiagram().fireEvent(new mxEventObject(XcosEvent.ADD_PORTS, "block", currentBlock));
566 } catch (InterpreterException e) {
571 } catch (IOException e) {
576 protected File exportBlockStruct() {
581 tempOutput = File.createTempFile("xcos",".h5",new File(System.getenv("TMPDIR")));
582 tempOutput.deleteOnExit();
583 int file_id = H5Write.createFile(tempOutput.getAbsolutePath());
584 H5Write.writeInDataSet(file_id, "scs_m", BasicBlockInfo.getAsScilabObj(this));
585 H5Write.closeFile(file_id);
587 } catch (IOException e) {
589 } catch (HDF5Exception e) {
595 protected File exportContext(String[] context) {
599 File tempContext = File.createTempFile("xcos",".h5");
600 tempContext.deleteOnExit();
601 int context_file_id = H5Write.createFile(tempContext.getAbsolutePath());
602 H5Write.writeInDataSet(context_file_id, "context", new ScilabString(context));
603 H5Write.closeFile(context_file_id);
605 } catch (IOException e) {
607 } catch (HDF5Exception e) {
613 public String getToolTipText() {
614 StringBuffer result = new StringBuffer();
615 result.append("<html>");
616 result.append("Block Name : "+ getInterfaceFunctionName() + "<br>");
617 result.append("Simulation : "+ getSimulationFunctionName() + "<br>");
619 if(getParentDiagram() instanceof PaletteDiagram) {
620 if(getIntegerParameters() != null) {
621 result.append("Integer parameters : "+ getIntegerParameters() + "<br>");
624 if(getRealParameters() != null && getRealParameters().getHeight() != 0 && getRealParameters().getWidth() != 0) {
625 result.append("Real parameters : "+ getRealParameters() + "<br>");
628 if(getObjectsParameters() != null) {
629 result.append("Object parameters : "+ getObjectsParameters() + "<br>");
632 result.append("UID : "+ getId() + "<br>");
633 result.append("Block Style : " + getStyle() + "<br>");
634 result.append("Flip : " + getFlip() + "<br>");
635 result.append("Mirror : " + getMirror() + "<br>");
636 result.append("Input ports : " + BasicBlockInfo.getAllInputPorts(this, false).size() + "<br>");
637 result.append("Output ports : " + BasicBlockInfo.getAllOutputPorts(this, false).size() + "<br>");
638 result.append("Control ports : " + BasicBlockInfo.getAllControlPorts(this, false).size() + "<br>");
639 result.append("Command ports : " + BasicBlockInfo.getAllCommandPorts(this, false).size() + "<br>");
642 result.append("x : " + getGeometry().getX() + "<br>");
643 result.append("y : " + getGeometry().getY() + "<br>");
644 result.append("w : " + getGeometry().getWidth() + "<br>");
645 result.append("h : " + getGeometry().getHeight() + "<br>");
646 result.append("</html>");
647 return result.toString();
650 public void openContextMenu(ScilabGraph graph) {
651 ContextMenu menu = null;
652 if(getParentDiagram() instanceof PaletteDiagram) {
653 menu = createPaletteContextMenu(graph);
655 menu = createContextMenu(graph);
657 menu.setVisible(true);
660 public ContextMenu createPaletteContextMenu(ScilabGraph graph) {
661 ContextMenu menu = ScilabContextMenu.createContextMenu();
663 final List<XcosDiagram> allDiagrams = Xcos.getDiagrams();
665 if (allDiagrams.size() == 0) {
666 // No diagram opened: should never happen if Xcos opens an empty diagram when it is launched
667 MenuItem addTo = ScilabMenuItem.createMenuItem();
669 addTo.setText(XcosMessages.ADDTO_NEW_DIAGRAM);
670 addTo.setCallback(new CallBack(XcosMessages.ADDTO_NEW_DIAGRAM) {
671 private static final long serialVersionUID = 8370536280449900878L;
673 public void callBack() {
674 XcosDiagram theDiagram = Xcos.createEmptyDiagram();
675 BasicBlock block = (BasicBlock)BlockFactory.createClone(BasicBlock.this);
676 theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
677 mxGeometry geom = BasicBlock.this.getGeometry();
680 theDiagram.getModel().setGeometry(block, geom);
681 BlockPositioning.updateBlockView(block);
687 } else if (allDiagrams.size() == 1) {
688 // A single diagram opened: add to this diagram
689 MenuItem addTo = ScilabMenuItem.createMenuItem();
691 addTo.setText(XcosMessages.ADDTO + " " + allDiagrams.get(0).getParentTab().getName());
692 final XcosDiagram theDiagram = allDiagrams.get(0);
693 addTo.setCallback(new CallBack(theDiagram.getTitle()) {
694 private static final long serialVersionUID = -99601763227525686L;
696 public void callBack() {
697 BasicBlock block = (BasicBlock)BlockFactory.createClone(BasicBlock.this);
698 theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
699 mxGeometry geom = BasicBlock.this.getGeometry();
702 theDiagram.getModel().setGeometry(block, geom);
703 BlockPositioning.updateBlockView(block);
704 block.setParentDiagram(theDiagram);
711 // The user has to choose
712 Menu addTo = ScilabMenu.createMenu();
714 addTo.setText(XcosMessages.ADDTO);
716 for (int i = 0; i < allDiagrams.size(); i++) {
717 MenuItem diagram = ScilabMenuItem.createMenuItem();
718 final XcosDiagram theDiagram = allDiagrams.get(i);
719 diagram.setText(allDiagrams.get(i).getParentTab().getName());
720 diagram.setCallback(new CallBack(theDiagram.getTitle()) {
721 private static final long serialVersionUID = 3345416658377835057L;
723 public void callBack() {
724 BasicBlock block = (BasicBlock)BlockFactory.createClone(BasicBlock.this);
725 theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
726 mxGeometry geom = BasicBlock.this.getGeometry();
729 theDiagram.getModel().setGeometry(block, geom);
730 BlockPositioning.updateBlockView(block);
740 menu.getAsSimpleContextMenu().addSeparator();
742 MenuItem help = ScilabMenuItem.createMenuItem();
743 help.setText(XcosMessages.BLOCK_DOCUMENTATION);
744 help.setCallback(new CallBack(XcosMessages.BLOCK_DOCUMENTATION) {
745 private static final long serialVersionUID = -1480947262397441951L;
747 public void callBack() {
748 XcosInterpreterManagement.requestScilabExec("help " + getInterfaceFunctionName());
753 menu.setVisible(true);
755 ((SwingScilabContextMenu) menu.getAsSimpleContextMenu()).setLocation(
756 MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
761 public ContextMenu createContextMenu(ScilabGraph graph) {
762 ContextMenu menu = ScilabContextMenu.createContextMenu();
763 Map<Class<? extends DefaultAction>, Menu> menuList = new HashMap<Class<? extends DefaultAction>, Menu>();
765 MenuItem value = BlockParametersAction.createMenu(graph);
766 menuList.put(BlockParametersAction.class, value);
769 menu.getAsSimpleContextMenu().addSeparator();
771 value = CutAction.cutMenu(graph);
772 menuList.put(CutAction.class, value);
774 value = CopyAction.copyMenu(graph);
775 menuList.put(CopyAction.class, value);
777 value = DeleteAction.createMenu(graph);
778 menuList.put(DeleteAction.class, value);
781 menu.getAsSimpleContextMenu().addSeparator();
783 value = RegionToSuperblockAction.createMenu(graph);
784 menuList.put(RegionToSuperblockAction.class, value);
786 // Menu mask = ScilabMenu.createMenu();
787 // mask.setText(XcosMessages.SUPERBLOCK_MASK);
789 // mask.add(SuperblockMaskCreateAction.createMenu(graph));
790 // mask.add(SuperblockMaskRemoveAction.createMenu(graph));
792 menu.getAsSimpleContextMenu().addSeparator();
794 Menu format = ScilabMenu.createMenu();
795 format.setText(XcosMessages.FORMAT);
797 value = RotateAction.createMenu(graph);
798 menuList.put(RotateAction.class, value);
800 value = MirrorAction.createMenu(graph);
801 menuList.put(MirrorAction.class, value);
803 value = FlipAction.createMenu(graph);
804 menuList.put(FlipAction.class, value);
806 value = ShowHideShadowAction.createMenu(graph);
807 menuList.put(ShowHideShadowAction.class, value);
810 format.addSeparator();
812 Menu alignMenu = ScilabMenu.createMenu();
813 alignMenu.setText(XcosMessages.ALIGN_BLOCKS);
814 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_LEFT, mxConstants.ALIGN_LEFT));
815 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_CENTER, mxConstants.ALIGN_CENTER));
816 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_RIGHT, mxConstants.ALIGN_RIGHT));
817 alignMenu.addSeparator();
818 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_TOP, mxConstants.ALIGN_TOP));
819 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_MIDDLE, mxConstants.ALIGN_MIDDLE));
820 alignMenu.add(AlignBlockAction.createMenu(graph, XcosMessages.ALIGN_BOTTOM, mxConstants.ALIGN_BOTTOM));
821 menuList.put(AlignBlockAction.class, alignMenu);
822 format.add(alignMenu);
824 format.addSeparator();
826 format.add(ColorAction.createMenu(graph, XcosMessages.BORDER_COLOR, mxConstants.STYLE_STROKECOLOR));
827 format.add(ColorAction.createMenu(graph, XcosMessages.FILL_COLOR, mxConstants.STYLE_FILLCOLOR));
829 menu.getAsSimpleContextMenu().addSeparator();
831 menu.add(ViewDetailsAction.createMenu(graph));
833 menu.getAsSimpleContextMenu().addSeparator();
835 menu.add(BlockDocumentationAction.createMenu(graph));
837 ((SwingScilabContextMenu) menu.getAsSimpleContextMenu()).setLocation(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
839 customizeMenu(menuList);
844 public void setFlip(boolean flip) {
845 if(getParentDiagram() != null) {
848 mxUtils.setCellStyles(getParentDiagram().getModel(), new Object[] {this}, XcosConstants.STYLE_FLIP, "true");
850 mxUtils.setCellStyles(getParentDiagram().getModel(), new Object[] {this}, XcosConstants.STYLE_FLIP, "false");
856 * Override this to customize contextual menu
859 protected void customizeMenu(Map<Class<? extends DefaultAction>, Menu> menuList) {
860 // To be overridden by sub-classes
864 public boolean getMirror(){
868 public void setMirror(boolean mirror) {
869 if(getParentDiagram() != null) {
872 mxUtils.setCellStyles(getParentDiagram().getModel(), new Object[] {this}, XcosConstants.STYLE_MIRROR, "true");
874 mxUtils.setCellStyles(getParentDiagram().getModel(), new Object[] {this}, XcosConstants.STYLE_MIRROR, "false");
879 public boolean getFlip(){
883 public void toggleFlip() {
884 BlockPositioning.toggleFlip(this);
887 public void toggleMirror() {
888 BlockPositioning.toggleMirror(this);
891 public void toggleAntiClockwiseRotation() {
892 BlockPositioning.toggleAntiClockwiseRotation(this);
896 public int getAngle() {
900 public void setAngle(int angle) {
903 if(getParentDiagram() != null) {
904 mxUtils.setCellStyles(getParentDiagram().getModel(), new Object[] {this}, XcosConstants.STYLE_ROTATION, new Integer(angle).toString());
909 * Useful when we need to update local properties with mxCell style properties
911 public void updateFieldsFromStyle() {
912 StyleMap map = new StyleMap(getStyle());
914 angle = Integer.parseInt(map.get(XcosConstants.STYLE_ROTATION));
915 isFlipped = Boolean.parseBoolean(map.get(XcosConstants.STYLE_FLIP));
916 isMirrored = Boolean.parseBoolean(map.get(XcosConstants.STYLE_MIRROR));