add load as palette option in palette browser
antoine ELIAS [Fri, 4 Dec 2009 16:49:11 +0000 (17:49 +0100)]
15 files changed:
scilab/CHANGES_5.2.X
scilab/modules/xcos/src/java/org/scilab/modules/graph/ScilabGraph.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/PaletteDiagram.java [new file with mode: 0644]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/Xcos.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/XcosTab.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/LoadAsPalAction.java [new file with mode: 0644]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/BasicBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/SuperBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/BlockPalette.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/XcosPalette.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/XcosPaletteManager.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosComponent.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosConstants.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index d1e27bc..fded3aa 100644 (file)
@@ -1475,3 +1475,5 @@ Bug fixes:
 
 * bug 5469 fixed - Force origin presence on an axes with location property set to 'origin'.
 
+* bug 5546 fixed - Initialization trouble of the variable "compilerpath"
+
index 26aa71f..42b7ebe 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.scilab.modules.graph;
 
+import java.awt.Color;
 import java.util.List;
 
 import org.scilab.modules.gui.tab.Tab;
@@ -39,7 +40,7 @@ public class ScilabGraph extends mxGraph {
     protected mxGraphOutline graphOutline = null;
     protected mxKeyboardHandler keyboardHandler = null;
     protected XcosComponent component = null;
-//    protected mxGraphComponent component = null;
+    //    protected mxGraphComponent component = null;
 
     private String title = Messages.gettext("Untitled");
     private String savedFile = null;
@@ -48,6 +49,9 @@ public class ScilabGraph extends mxGraph {
     private boolean opened = false;
     private boolean redoInAction = false;
     private int undoCounter = 0;
+    private boolean readOnly = false;
+    private Color originalColor = null;
+    private mxRubberband rubberBand;
 
     /**
      * 
@@ -62,8 +66,7 @@ public class ScilabGraph extends mxGraph {
        public void invoke(Object source, mxEventObject evt) {
 
            if (!redoInAction) {
-               undoManager.undoableEditHappened((mxUndoableEdit) evt
-                       .getArgAt(0));
+               undoManager.undoableEditHappened((mxUndoableEdit) evt.getArgAt(0));
                incrementUndoCounter();
            }
        }
@@ -78,7 +81,7 @@ public class ScilabGraph extends mxGraph {
 
     public ScilabGraph() {
        super();
-       
+
        /*
         * Disabling the default connected action and event listeners.
         */
@@ -86,7 +89,7 @@ public class ScilabGraph extends mxGraph {
        mxGraphActions.getSelectPreviousAction().setEnabled(false);
        mxGraphActions.getSelectChildAction().setEnabled(false);
        mxGraphActions.getSelectParentAction().setEnabled(false);
-       
+
        // Undo / Redo capabilities
        getModel().addListener(mxEvent.UNDO, undoHandler);
        getView().addListener(mxEvent.UNDO, undoHandler);
@@ -97,10 +100,10 @@ public class ScilabGraph extends mxGraph {
        undoManager.addListener(mxEvent.REDO, selectionHandler);
 
        component = new XcosComponent(this);
-//     component = new mxGraphComponent(this);
+       //      component = new mxGraphComponent(this);
 
        // Adds rubberband selection
-       new mxRubberband(component);
+       rubberBand = new mxRubberband(component);
 
        // Modified property change
        getModel().addListener(mxEvent.CHANGE, changeTracker);
@@ -118,7 +121,7 @@ public class ScilabGraph extends mxGraph {
        // this.setInvokesStopCellEditing(true);
     }
 
-       public String getSavedFile() {
+    public String getSavedFile() {
        return savedFile;
     }
 
@@ -215,20 +218,48 @@ public class ScilabGraph extends mxGraph {
 
     public void setVisible(boolean visible) {
        if (parentTab != null) {
-           ScilabWindow xcosWindow = (ScilabWindow) UIElementMapper
-                   .getCorrespondingUIElement(parentTab.getParentWindowId());
+           ScilabWindow xcosWindow = (ScilabWindow) UIElementMapper.getCorrespondingUIElement(parentTab.getParentWindowId());
            xcosWindow.setVisible(visible);
        }
     }
 
     public boolean isVisible() {
        if (parentTab != null) {
-           ScilabWindow xcosWindow = (ScilabWindow) UIElementMapper
-                   .getCorrespondingUIElement(parentTab.getParentWindowId());
+           ScilabWindow xcosWindow = (ScilabWindow) UIElementMapper.getCorrespondingUIElement(parentTab.getParentWindowId());
            return xcosWindow.isVisible();
        }
 
        return false;
     }
 
+    public void setReadOnly(boolean readOnly) {
+       this.readOnly = readOnly;
+       
+       setCellsLocked(readOnly);
+       if(isReadonly()) {
+           setOriginalColor(getAsComponent().getBackground());
+           getAsComponent().setBackground(new Color(240, 240, 240));
+       } else {
+           getAsComponent().setBackground(getOriginalColor());
+       }
+    }
+
+    public boolean isReadonly() {
+       return readOnly;
+    }
+
+    public void setOriginalColor(Color originalColor) {
+       this.originalColor = originalColor;
+    }
+
+    public Color getOriginalColor() {
+       if(originalColor != null){
+           return originalColor;
+       }
+       return Color.WHITE;
+    }
+
+    public mxRubberband getRubberBand() {
+        return rubberBand;
+    }
 }
diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/PaletteDiagram.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/PaletteDiagram.java
new file mode 100644 (file)
index 0000000..0ac74ca
--- /dev/null
@@ -0,0 +1,119 @@
+package org.scilab.modules.xcos;
+
+import java.io.File;
+
+import org.scilab.modules.xcos.block.BasicBlock;
+import org.scilab.modules.xcos.block.SplitBlock;
+import org.scilab.modules.xcos.block.TextBlock;
+import org.scilab.modules.xcos.link.BasicLink;
+import org.scilab.modules.xcos.utils.BlockPositioning;
+import org.scilab.modules.xcos.utils.XcosConstants;
+
+import com.mxgraph.model.mxGeometry;
+
+
+public class PaletteDiagram extends XcosDiagram {
+
+    private static int BLOCK_MAX_WIDTH  = (int) (XcosConstants.PALETTE_BLOCK_WIDTH * 0.8); //80% of the max size
+    private static int BLOCK_MAX_HEIGHT = (int) (XcosConstants.PALETTE_BLOCK_HEIGHT * 0.8); //80% of the max size
+    private String name = "";
+    private double windowWidth = 0;
+
+    public PaletteDiagram() {
+       super();
+       
+       setCellsLocked(true);
+       setGridVisible(false);
+       setCellsDeletable(false);
+       setCellsEditable(false);
+    }
+
+    public boolean openDiagramAsPal(String diagramFileName) {
+       File theFile = new File(diagramFileName);
+
+       //int windowHeight = getAsComponent().getHeight();
+       
+       if (theFile.exists()) {
+           transformAndLoadFile(theFile);
+           name = theFile.getName();
+           getRubberBand().setEnabled(false);
+
+           /*change some diagram parameters*/
+           /*delete all links*/
+           for(int i = 0 ; i < getModel().getChildCount(getDefaultParent()) ; i++) {
+               Object obj = getModel().getChildAt(getDefaultParent(), i); 
+               if(obj instanceof BasicLink || obj instanceof SplitBlock || obj instanceof TextBlock) {
+                   getModel().remove(obj);
+                   i--;
+               }
+           }
+           return true;
+       }
+       return false;
+    }
+
+    public void updateDiagram(double newWidth) {
+       
+       if(newWidth == windowWidth) {
+           return;
+       }
+
+       int oldRowItem = (int) (newWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
+       int maxRowItem = (int) (windowWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
+       
+       //only compute for signifiant changes
+       if(oldRowItem == maxRowItem) {
+           return;
+       }
+
+       windowWidth = newWidth;
+       int blockCount = 0;
+
+       for(int i = 0 ; i < getModel().getChildCount(getDefaultParent()) ; i++) {
+           Object obj = getModel().getChildAt(getDefaultParent(), i); 
+           if(obj instanceof BasicBlock){
+               BasicBlock block = (BasicBlock)obj;
+               block.setGeometry(getNewBlockPosition(block.getGeometry(), blockCount));
+               BlockPositioning.updateBlockView(block);
+               blockCount++;
+           }
+       }
+       refresh();
+       undoManager.reset();
+       setModified(false);
+    }
+    
+    private mxGeometry getNewBlockPosition(mxGeometry geom, int blockCount) {
+       
+       int maxRowItem = (int) (windowWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
+       int row = blockCount % maxRowItem;
+       int col = blockCount / maxRowItem;
+       double x = geom.getX();
+       double y = geom.getY();
+       double w = geom.getWidth();
+       double h = geom.getHeight();
+       
+       if(geom.getWidth() > BLOCK_MAX_WIDTH || geom.getHeight() > BLOCK_MAX_HEIGHT) {
+           //update block size to fill "block area"
+           double ratio = Math.min(BLOCK_MAX_HEIGHT / h, BLOCK_MAX_WIDTH / w);
+           w *= ratio;
+           h *= ratio;
+       }
+       
+       x = row * (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
+       x += (XcosConstants.PALETTE_BLOCK_WIDTH - w) / 2;
+       y = col * (XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
+       y += (XcosConstants.PALETTE_BLOCK_HEIGHT - h) / 2;
+       
+       return new mxGeometry(x,y,w,h); 
+    }
+
+    public String getName() {
+       return name;
+    }
+
+    public boolean isCellConnectable(Object cell) {
+       return false;
+    }
+
+}
index 1af8492..1790ed7 100644 (file)
@@ -165,12 +165,7 @@ public class Xcos {
                newSP.setParentDiagram(block.getParentDiagram());
                if (show == true) {
                    newSP.openBlockSettings(null);
-                   // lock cells and change background to gray to show
-                   // read-only
-                   newSP.getChild().setCellsLocked(true);
-                   newSP.getChild().getAsComponent().setBackground(
-                           new Color(204, 204, 204));
-                   // look to disable open setting dialog on double click too
+                   newSP.getChild().setReadOnly(true);
                }
                openedSuperBlock.put(UID, newSP);
                break;
index f8033d8..75c5cb5 100644 (file)
@@ -439,8 +439,7 @@ public class XcosDiagram extends ScilabGraph {
        // Add a listener to track when model is changed
        getModel().addListener(XcosEvent.CHANGE, new ModelTracker());
        
-       setGridEnabled(true);
-       getAsComponent().setGridVisible(true);
+       setGridVisible(true);
        
        ((mxCell) getDefaultParent()).setId((new UID()).toString());
        ((mxCell) getModel().getRoot()).setId((new UID()).toString());
@@ -826,7 +825,7 @@ public class XcosDiagram extends ScilabGraph {
                refresh();
            }
 
-           // Ctrl + Shift + Right Double Click : for debug !!
+           // Ctrl + Shift + Right Middle Click : for debug !!
            if (arg0.getClickCount() >= 2 && SwingUtilities.isMiddleMouseButton(arg0)
                        && arg0.isShiftDown() && arg0.isControlDown())
            {
@@ -835,7 +834,7 @@ public class XcosDiagram extends ScilabGraph {
                    System.err.println("[DEBUG] Click on diagram");
                    System.err.println("Default Parent ID : " + ((mxCell) getDefaultParent()).getId());
                    System.err.println("Model root ID : " + ((mxCell) getModel().getRoot()).getId());
-                   System.err.println("getParentWindow : " + getParentTab().getParentWindow());
+                   System.err.println("getParentWindow : " + (getParentTab() == null ? null : getParentTab().getParentWindow()));
                } else {
                    System.err.println("[DEBUG] Click on : " + cell);
                    System.err.println("[DEBUG] Style : " + ((mxCell) cell).getStyle());
@@ -855,7 +854,7 @@ public class XcosDiagram extends ScilabGraph {
            // Context menu
            if ((arg0.getClickCount() == 1 && SwingUtilities.isRightMouseButton(arg0))
                        || arg0.isPopupTrigger()
-                       || isMacOsPopupTrigger(arg0)) {
+                       || XcosMessages.isMacOsPopupTrigger(arg0)) {
 
                if (cell == null) {
                    // Display diagram context menu
@@ -910,11 +909,9 @@ public class XcosDiagram extends ScilabGraph {
        }
 
        public void mouseEntered(MouseEvent arg0) {
-           // TODO Auto-generated method stub
        }
 
        public void mouseExited(MouseEvent arg0) {
-           // TODO Auto-generated method stub
        }
 
        public void mousePressed(MouseEvent arg0) {
@@ -947,6 +944,13 @@ public class XcosDiagram extends ScilabGraph {
                return true;
        }
        
+       public boolean isCellSelectable(Object cell) {
+           if(cell instanceof BasicPort) {
+               return false;
+           }
+           return super.isCellSelectable(cell);
+       }
+       
        public boolean isCellMovable(Object cell) {
                if (cell instanceof BasicPort) {
                        return false;
@@ -1120,22 +1124,17 @@ public class XcosDiagram extends ScilabGraph {
      * @param status new status
      */
     public void setGridVisible(boolean status) {
-       setGridEnabled(status);
-       getAsComponent().setGridVisible(status);
-       getAsComponent().repaint();
+       setGridEnabled(status);
+       getAsComponent().setGridVisible(status);
+       getAsComponent().repaint();
 
-       // (Un)Check the corresponding menu
-       gridMenu.setChecked(status);
+       // (Un)Check the corresponding menu
+       if(gridMenu != null) {
+           gridMenu.setChecked(status);
+       }
     }
 
-//     public mxRectangle getCellBounds(Object cell, boolean includeEdges,
-//                     boolean includeDescendants, boolean boundingBox) {
-//     //mxRectangle rect = super.getCellBounds(cell, includeEdges, includeDescendants, boundingBox);
-//             mxRectangle rect = super.getCellBounds(cell, includeEdges, false, boundingBox);
-//             return rect;
-//     }
-
-       /**
+    /**
      * Set menu used to manage Grid visibility
      * @param menu the menu
      */
@@ -1458,11 +1457,12 @@ public class XcosDiagram extends ScilabGraph {
        }
     }
     
+
     /**
      * Load a file with different method depending on it extension 
      * @param theFile File to load
      */
-       private void transformAndLoadFile(File theFile) {
+       protected void transformAndLoadFile(File theFile) {
                final File fileToLoad = theFile;
                final XcosFileType filetype = XcosFileType.findFileType(fileToLoad);
                
@@ -1470,16 +1470,10 @@ public class XcosDiagram extends ScilabGraph {
                switch (filetype) {
                case COSF:
                case COS:
-                       Thread transformAction = new Thread() {
-                               public void run() {
-                                       File newFile;
-                                       newFile = filetype.exportToHdf5(fileToLoad);
-                                       System.err.println("export to hdf5 OK");
-                                       transformAndLoadFile(newFile);
-                               }
-                       };
-                       transformAction.start();
-                       break;
+                   File newFile;
+                   newFile = filetype.exportToHdf5(fileToLoad);
+                   transformAndLoadFile(newFile);
+                   break;
 
                case XCOS:
                        Document document = null;
@@ -1743,15 +1737,6 @@ public class XcosDiagram extends ScilabGraph {
                }
            }
        
-       /**
-        * This function checks for the popup menu activation under MacOS with Java version 1.5
-        * Related to Scilab bug #5190
-        * @return true if Java 1.5 and MacOS and mouse clic and ctrl activated
-        */
-       private boolean isMacOsPopupTrigger(MouseEvent e) {
-               return (SwingUtilities.isLeftMouseButton(e) && e.isControlDown() && (System.getProperty("os.name").toLowerCase().indexOf("mac") != -1) && (System.getProperty("java.specification.version").equals("1.5")));
-       }
-       
        public void setContextAction(SetContextAction action) {
                this.action = action;
        }
index 67a6c4f..4e4cff5 100644 (file)
@@ -117,6 +117,7 @@ import com.mxgraph.view.mxGraph;
  */
 public class XcosTab extends SwingScilabTab implements Tab {
 
+    private static final long serialVersionUID = -290453474673387812L;
     /*
      * Static fields
      */
diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/LoadAsPalAction.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/LoadAsPalAction.java
new file mode 100644 (file)
index 0000000..65782b5
--- /dev/null
@@ -0,0 +1,81 @@
+package org.scilab.modules.xcos.actions;
+
+import java.awt.Toolkit;
+import java.awt.event.KeyEvent;
+
+import javax.swing.KeyStroke;
+
+import org.scilab.modules.graph.ScilabGraph;
+import org.scilab.modules.graph.actions.DefaultAction;
+import org.scilab.modules.gui.bridge.filechooser.SwingScilabFileChooser;
+import org.scilab.modules.gui.filechooser.FileChooser;
+import org.scilab.modules.gui.filechooser.ScilabFileChooser;
+import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.gui.pushbutton.PushButton;
+import org.scilab.modules.xcos.palette.XcosPaletteManager;
+import org.scilab.modules.xcos.utils.XcosFileType;
+import org.scilab.modules.xcos.utils.XcosMessages;
+
+public class LoadAsPalAction extends DefaultAction {
+
+    private static final long serialVersionUID = 6292720188130217522L;
+
+    /**
+     * Constructor
+     * @param scilabGraph associated Scilab Graph
+     */
+    private LoadAsPalAction(ScilabGraph scilabGraph) {
+       super(XcosMessages.OPEN, scilabGraph);
+    }
+
+    /**
+     * Create a menu to add in Scilab Graph menu bar
+     * @param scilabGraph associated Scilab Graph
+     * @return the menu
+     */
+    public static MenuItem createMenu(ScilabGraph scilabGraph) {
+       return createMenu(XcosMessages.LOAD_AS_PAL, null, new LoadAsPalAction(scilabGraph),
+               KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
+    }
+
+    /**
+     * Create a button to add in Scilab Graph tool bar
+     * @param scilabGraph associated Scilab Graph
+     * @return the button
+     */
+    public static PushButton createButton(ScilabGraph scilabGraph) {
+       return createButton(XcosMessages.OPEN, "document-open.png", new LoadAsPalAction(scilabGraph));
+    }
+
+    /**
+     * Open file action
+     * @see org.scilab.modules.graph.actions.DefaultAction#doAction()
+     */
+    public void doAction() {
+       FileChooser fc = ScilabFileChooser.createFileChooser();
+
+       /* Standard files */
+       String[] mask = XcosFileType.getValidFileMask();
+       String[] desc = XcosFileType.getValidFileDescription();
+       ((SwingScilabFileChooser) fc.getAsSimpleFileChooser()).addMask(mask , desc);
+
+       fc.setMultipleSelection(false);
+       fc.displayAndWait();
+
+       if (fc.getSelection() == null || fc.getSelection().length == 0 || fc.getSelection()[0].equals("")) {
+           return;
+       }
+
+       XcosPaletteManager.loadUserPalette(fc.getSelection()[0]);
+       
+       //      ConfigXcosManager.saveToRecentOpenedFiles(fc.getSelection()[0]);
+//     if (getGraph(null) == null) { // Called from palettes
+//         //save to recentopenedfile while opening from palettes is handle in Xcos.xcos(filename)
+//         Xcos.xcos(fc.getSelection()[0]);
+//     }
+//     else {
+//         ((XcosDiagram) getGraph(null)).openDiagramFromFile(fc.getSelection()[0]);
+//     }
+//     XcosTab.updateRecentOpenedFilesMenu(((XcosDiagram) getGraph(null)));
+    }
+}
\ No newline at end of file
index 56955f5..c5da2a7 100644 (file)
@@ -31,14 +31,18 @@ import org.scilab.modules.graph.actions.DeleteAction;
 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
 import org.scilab.modules.gui.contextmenu.ContextMenu;
 import org.scilab.modules.gui.contextmenu.ScilabContextMenu;
+import org.scilab.modules.gui.events.callback.CallBack;
 import org.scilab.modules.gui.menu.Menu;
 import org.scilab.modules.gui.menu.ScilabMenu;
 import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.gui.menuitem.ScilabMenuItem;
 import org.scilab.modules.hdf5.scilabTypes.ScilabDouble;
 import org.scilab.modules.hdf5.scilabTypes.ScilabList;
 import org.scilab.modules.hdf5.scilabTypes.ScilabString;
 import org.scilab.modules.hdf5.scilabTypes.ScilabType;
 import org.scilab.modules.hdf5.write.H5Write;
+import org.scilab.modules.xcos.PaletteDiagram;
+import org.scilab.modules.xcos.Xcos;
 import org.scilab.modules.xcos.XcosDiagram;
 import org.scilab.modules.xcos.XcosUIDObject;
 import org.scilab.modules.xcos.actions.AlignBlockAction;
@@ -53,6 +57,7 @@ import org.scilab.modules.xcos.actions.ShowHideShadowAction;
 import org.scilab.modules.xcos.actions.ViewDetailsAction;
 import org.scilab.modules.xcos.io.BasicBlockInfo;
 import org.scilab.modules.xcos.io.BlockReader;
+import org.scilab.modules.xcos.palette.BlockPalette;
 import org.scilab.modules.xcos.port.BasicPort;
 import org.scilab.modules.xcos.port.command.CommandPort;
 import org.scilab.modules.xcos.port.control.ControlPort;
@@ -520,6 +525,10 @@ public class BasicBlock extends XcosUIDObject {
 
     public void openBlockSettings(String context[]) {
        
+       if(getParentDiagram() instanceof PaletteDiagram) {
+           return;
+       }
+       
        //prevent to open twice
        if(isLocked()) {
            return;
@@ -605,56 +614,150 @@ public class BasicBlock extends XcosUIDObject {
     public String getToolTipText() {
        StringBuffer result = new StringBuffer();
        result.append("<html>");
-       //result.append("Block Address : " + this + "<br>");
        result.append("Block Name : "+ getInterfaceFunctionName() + "<br>");
        result.append("Simulation : "+ getSimulationFunctionName() + "<br>");
-       result.append("UID : "+ getId() + "<br>");
-       result.append("Block Style : " + getStyle() + "<br>");
-       result.append("Flip : " + getFlip() + "<br>");
-       result.append("Mirror : " + getMirror() + "<br>");
-       result.append("Input ports : " + BasicBlockInfo.getAllInputPorts(this, false).size() + "<br>");
-       result.append("Output ports : " + BasicBlockInfo.getAllOutputPorts(this, false).size() + "<br>");
-       result.append("Control ports : " + BasicBlockInfo.getAllControlPorts(this, false).size() + "<br>");
-       result.append("Command ports : " + BasicBlockInfo.getAllCommandPorts(this, false).size() + "<br>");
-//     result.append("Diagram : " + getParentDiagram() + "<br>");
-//     //exprs
-//     if (getExprs() != null) {
-//         result.append("Exprs : "+getExprs().toString()+"<br>");
-//     }
-//     else {
-//         result.append("Exprs : (null)<br>");
-//     }
-//     //ipar
-//     if (getIntegerParameters() != null ) {
-//         result.append("Ipar : "+getIntegerParameters().toString()+"<br>");
-//     }
-//     else {
-//         result.append("Ipar : (null)<br>");
-//     }
-//     //rpar
-//     if (getRealParameters() != null) {
-//         result.append("Rpar : "+getRealParameters().toString()+"<br>");
-//     }
-//     else {
-//         result.append("Rpar : (null)<br>");
-//     }
-//     //opar
-//     if (getObjectsParameters() != null) {
-//         result.append("Opar : "+getObjectsParameters().toString()+"<br>");
-//     }
-//     else {
-//         result.append("Opar : (null)<br>");
-//     }
-       
+
+       if(getParentDiagram() instanceof PaletteDiagram) {
+           if(getIntegerParameters() != null) {
+               result.append("Integer parameters : "+ getIntegerParameters() + "<br>");
+           }
+           
+           if(getRealParameters() != null && getRealParameters().getHeight() != 0 && getRealParameters().getWidth() != 0) {
+               result.append("Real parameters : "+ getRealParameters() + "<br>");
+           }
+           
+           if(getObjectsParameters() != null) {
+               result.append("Object parameters : "+ getObjectsParameters() + "<br>");
+           }
+       } else {
+           result.append("UID : "+ getId() + "<br>");
+           result.append("Block Style : " + getStyle() + "<br>");
+           result.append("Flip : " + getFlip() + "<br>");
+           result.append("Mirror : " + getMirror() + "<br>");
+           result.append("Input ports : " + BasicBlockInfo.getAllInputPorts(this, false).size() + "<br>");
+           result.append("Output ports : " + BasicBlockInfo.getAllOutputPorts(this, false).size() + "<br>");
+           result.append("Control ports : " + BasicBlockInfo.getAllControlPorts(this, false).size() + "<br>");
+           result.append("Command ports : " + BasicBlockInfo.getAllCommandPorts(this, false).size() + "<br>");
+       }
+
+       result.append("x : " + getGeometry().getX() + "<br>");
+       result.append("y : " + getGeometry().getY() + "<br>");
+       result.append("w : " + getGeometry().getWidth() + "<br>");
+       result.append("h : " + getGeometry().getHeight() + "<br>");
        result.append("</html>");
        return result.toString();
     }
 
     public void openContextMenu(ScilabGraph graph) {
-       ContextMenu menu = createContextMenu(graph);
+       ContextMenu menu = null;
+       if(getParentDiagram() instanceof PaletteDiagram) {
+           menu = createPaletteContextMenu(graph);
+       } else {
+           menu = createContextMenu(graph);
+       }
        menu.setVisible(true);
     }
 
+    public ContextMenu createPaletteContextMenu(ScilabGraph graph) {
+       ContextMenu menu = ScilabContextMenu.createContextMenu();
+
+       final List<XcosDiagram> allDiagrams = Xcos.getDiagrams();
+
+       if (allDiagrams.size() == 0) {
+           // No diagram opened: should never happen if Xcos opens an empty diagram when it is launched
+           MenuItem addTo = ScilabMenuItem.createMenuItem();
+
+           addTo.setText(XcosMessages.ADDTO_NEW_DIAGRAM);
+           addTo.setCallback(new CallBack(XcosMessages.ADDTO_NEW_DIAGRAM) {
+               private static final long serialVersionUID = 8370536280449900878L;
+
+               public void callBack() {
+                   XcosDiagram theDiagram = Xcos.createEmptyDiagram();
+                   BasicBlock block = (BasicBlock)BasicBlock.this.createClone();
+                   theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
+                   mxGeometry geom = BasicBlock.this.getGeometry();
+                   geom.setX(10);
+                   geom.setY(10);
+                   theDiagram.getModel().setGeometry(block, geom);
+                   BlockPositioning.updateBlockView(block);
+               }
+           });
+
+           menu.add(addTo);
+
+       } else if (allDiagrams.size() == 1) {
+           // A single diagram opened: add to this diagram
+           MenuItem addTo = ScilabMenuItem.createMenuItem();
+
+           addTo.setText(XcosMessages.ADDTO + " " + allDiagrams.get(0).getParentTab().getName());
+           final XcosDiagram theDiagram = allDiagrams.get(0);
+           addTo.setCallback(new CallBack(theDiagram.getTitle()) {
+               private static final long serialVersionUID = -99601763227525686L;
+
+               public void callBack() {
+                   BasicBlock block = (BasicBlock)BasicBlock.this.createClone();
+                   theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
+                   mxGeometry geom = BasicBlock.this.getGeometry();
+                   geom.setX(10);
+                   geom.setY(10);
+                   theDiagram.getModel().setGeometry(block, geom);
+                   BlockPositioning.updateBlockView(block);
+               }
+           });
+
+           menu.add(addTo);
+
+       } else {
+           // The user has to choose
+           Menu addTo = ScilabMenu.createMenu();
+
+           addTo.setText(XcosMessages.ADDTO);
+
+           for (int i = 0; i < allDiagrams.size(); i++) {
+               MenuItem diagram = ScilabMenuItem.createMenuItem();
+               final XcosDiagram theDiagram = allDiagrams.get(i);
+               diagram.setText(allDiagrams.get(i).getParentTab().getName());
+               diagram.setCallback(new CallBack(theDiagram.getTitle()) {
+                   private static final long serialVersionUID = 3345416658377835057L;
+
+                   public void callBack() {
+                       BasicBlock block = (BasicBlock)BasicBlock.this.createClone();
+                       theDiagram.getModel().add(theDiagram.getDefaultParent(), block, 0);
+                       mxGeometry geom = BasicBlock.this.getGeometry();
+                       geom.setX(10);
+                       geom.setY(10);
+                       theDiagram.getModel().setGeometry(block, geom);
+                       BlockPositioning.updateBlockView(block);
+                   }
+               });
+               addTo.add(diagram);
+           }
+
+           menu.add(addTo);
+       }
+
+
+       menu.getAsSimpleContextMenu().addSeparator();
+
+       MenuItem help = ScilabMenuItem.createMenuItem();
+       help.setText(XcosMessages.BLOCK_DOCUMENTATION);
+       help.setCallback(new CallBack(XcosMessages.BLOCK_DOCUMENTATION) {
+           private static final long serialVersionUID = -1480947262397441951L;
+
+           public void callBack() {
+               InterpreterManagement.requestScilabExec("help " + getInterfaceFunctionName());
+           }
+       });
+       menu.add(help);
+
+       menu.setVisible(true);
+
+       ((SwingScilabContextMenu) menu.getAsSimpleContextMenu()).setLocation(
+               MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
+       
+       return menu;
+    }
+
     public ContextMenu createContextMenu(ScilabGraph graph) {
                ContextMenu menu = ScilabContextMenu.createContextMenu();
                Map<Class<? extends DefaultAction>, MenuItem> menuList = new HashMap<Class<? extends DefaultAction>, MenuItem>();
index fece60a..62d9d6c 100644 (file)
@@ -23,6 +23,7 @@ import org.scilab.modules.hdf5.scilabTypes.ScilabDouble;
 import org.scilab.modules.hdf5.scilabTypes.ScilabList;
 import org.scilab.modules.hdf5.scilabTypes.ScilabMList;
 import org.scilab.modules.hdf5.scilabTypes.ScilabString;
+import org.scilab.modules.xcos.PaletteDiagram;
 import org.scilab.modules.xcos.Xcos;
 import org.scilab.modules.xcos.XcosTab;
 import org.scilab.modules.xcos.actions.CodeGenerationAction;
@@ -72,6 +73,10 @@ public class SuperBlock extends BasicBlock {
      * 
      */
     public void openBlockSettings(String[] context) {
+       if(getParentDiagram() instanceof PaletteDiagram) {
+           return;
+       }
+
        if (getChild() == null && getSimulationFunctionType().compareTo(SimulationFunctionType.DEFAULT) != 0) {
            // This means we have a SuperBlock and we generated C code for it.
            this.setLocked(false);
@@ -121,11 +126,15 @@ public class SuperBlock extends BasicBlock {
     }
 
     public void openContextMenu(ScilabGraph graph) {
-       ContextMenu menu = createContextMenu(graph);
-       
-       menu.getAsSimpleContextMenu().addSeparator();
-       menu.add(CodeGenerationAction.createMenu(graph));
-       
+       ContextMenu menu = null;
+
+       if(getParentDiagram() instanceof PaletteDiagram) {
+           menu = createPaletteContextMenu(graph);
+       } else { 
+           menu = createContextMenu(graph);
+           menu.getAsSimpleContextMenu().addSeparator();
+           menu.add(CodeGenerationAction.createMenu(graph));
+       }
        menu.setVisible(true);
     }
     
index a18b4a7..cae6d49 100644 (file)
@@ -7,7 +7,6 @@ import java.awt.Graphics;
 import java.awt.MouseInfo;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
-import java.util.ArrayList;
 import java.util.List;
 
 import javax.swing.ImageIcon;
@@ -30,6 +29,7 @@ import org.scilab.modules.xcos.XcosDiagram;
 import org.scilab.modules.xcos.block.BasicBlock;
 import org.scilab.modules.xcos.block.TextBlock;
 import org.scilab.modules.xcos.io.BlockReader;
+import org.scilab.modules.xcos.utils.XcosConstants;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
 import com.mxgraph.swing.util.mxGraphTransferable;
@@ -37,10 +37,7 @@ import com.mxgraph.util.mxRectangle;
 
 public class BlockPalette extends JLabel {
 
-    private static final long serialVersionUID = 1L;
-    public static int BLOCK_WIDTH = 100;
-    public static int BLOCK_HEIGHT = 100;
-
+    private static final long serialVersionUID = 2045511112700166300L;
     private BasicBlock block;
     private XcosPalette palette;
     private mxGraphTransferable transferable;
@@ -65,7 +62,7 @@ public class BlockPalette extends JLabel {
            public void mouseClicked(MouseEvent e) {
                if ((e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
                        || e.isPopupTrigger()
-                       || isMacOsPopupTrigger(e)) {
+                       || XcosMessages.isMacOsPopupTrigger(e)) {
 
                    ContextMenu menu = ScilabContextMenu.createContextMenu();
 
@@ -232,7 +229,7 @@ public class BlockPalette extends JLabel {
 
     public void setPalette(XcosPalette palette) {
        this.palette = palette;
-       setPreferredSize(new Dimension(BLOCK_WIDTH, BLOCK_HEIGHT));
+       setPreferredSize(new Dimension(XcosConstants.PALETTE_BLOCK_WIDTH, XcosConstants.PALETTE_BLOCK_HEIGHT));
        setBackground(palette.getBackground().brighter());
        setFont(new Font(getFont().getFamily(), 0, 12));
 
@@ -253,12 +250,4 @@ public class BlockPalette extends JLabel {
        this.transferable = transferable;
     }
 
-    /**
-     * This function checks for the popup menu activation under MacOS with Java version 1.5
-     * Related to Scilab bug #5190
-     * @return true if Java 1.5 and MacOS and mouse clic and ctrl activated
-     */
-    private boolean isMacOsPopupTrigger(MouseEvent e) {
-       return (SwingUtilities.isLeftMouseButton(e) && e.isControlDown() && (System.getProperty("os.name").toLowerCase().indexOf("mac") != -1) && (System.getProperty("java.specification.version").equals("1.5")));
-    }
 }
index 8f4021e..165e705 100644 (file)
@@ -37,6 +37,7 @@ import javax.swing.JScrollPane;
 import javax.swing.TransferHandler;
 
 import org.flexdock.plaf.common.border.ShadowBorder;
+import org.scilab.modules.xcos.utils.XcosConstants;
 
 import com.mxgraph.swing.util.mxGraphTransferable;
 import com.mxgraph.util.mxConstants;
@@ -46,10 +47,7 @@ import com.mxgraph.util.mxEventSource;
 import com.mxgraph.util.mxEventSource.mxIEventListener;
 
 public class XcosPalette extends JScrollPane implements ComponentListener {
-    private static final long serialVersionUID = 1L;
-    private static final int HMARGIN = 5;
-    private static final int VMARGIN = 5;
-    private static final int DEFAULT_NB_COLS = 1; /* Updated dynamically at creation */
+    private static final long serialVersionUID = 5693635134906513755L;
 
     private JPanel panel = null;
     private String name;
@@ -67,15 +65,15 @@ public class XcosPalette extends JScrollPane implements ComponentListener {
        setBackground(Color.WHITE);
 
        panel.setBackground(Color.WHITE);
-       panel.setLayout(new FlowLayout(FlowLayout.LEADING, HMARGIN, VMARGIN));
-       panel.setPreferredSize(new Dimension(DEFAULT_NB_COLS * (BlockPalette.BLOCK_WIDTH + HMARGIN), 0));
+       panel.setLayout(new FlowLayout(FlowLayout.LEADING, XcosConstants.PALETTE_HMARGIN, XcosConstants.PALETTE_VMARGIN));
+       panel.setPreferredSize(new Dimension((XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN), 0));
 
-       getVerticalScrollBar().setBlockIncrement(BlockPalette.BLOCK_HEIGHT + VMARGIN);
-       getVerticalScrollBar().setUnitIncrement(BlockPalette.BLOCK_HEIGHT + VMARGIN);
+       getVerticalScrollBar().setBlockIncrement(XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
+       getVerticalScrollBar().setUnitIncrement(XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
 
        //getHorizontalScrollBar().setVisible(false);
-       getHorizontalScrollBar().setBlockIncrement(BlockPalette.BLOCK_WIDTH + HMARGIN);
-       getHorizontalScrollBar().setUnitIncrement(BlockPalette.BLOCK_WIDTH + HMARGIN);
+       getHorizontalScrollBar().setBlockIncrement(XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
+       getHorizontalScrollBar().setUnitIncrement(XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
 
        addComponentListener(this);
        // Clears the current selection when the background is clicked
@@ -289,9 +287,9 @@ public class XcosPalette extends JScrollPane implements ComponentListener {
                panelWidth -=  getVerticalScrollBar().getWidth();
            }
 
-           int numberOfCols = panelWidth / (BlockPalette.BLOCK_WIDTH + HMARGIN);
+           int numberOfCols = panelWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
            double numberOfRows = (double) panel.getComponentCount() / (double) numberOfCols;
-           int preferedHeight = (int) ((BlockPalette.BLOCK_HEIGHT + VMARGIN) * Math.ceil(numberOfRows));
+           int preferedHeight = (int) ((XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN) * Math.ceil(numberOfRows));
 
            panel.setPreferredSize(new Dimension(panelWidth, preferedHeight));
        }
index 236cd95..1f7f063 100644 (file)
 
 package org.scilab.modules.xcos.palette;
 
+import java.awt.Component;
 import java.awt.Dimension;
-import java.io.File;
-import java.util.HashMap;
-import java.util.Map;
+import java.awt.MouseInfo;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
 
 import javax.swing.ImageIcon;
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.JTree;
+import javax.swing.SwingUtilities;
 import javax.swing.event.TreeSelectionEvent;
 import javax.swing.event.TreeSelectionListener;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
-import javax.swing.tree.TreeModel;
+import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
 
+import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
+import org.scilab.modules.gui.console.ScilabConsole;
+import org.scilab.modules.gui.contextmenu.ContextMenu;
+import org.scilab.modules.gui.contextmenu.ScilabContextMenu;
+import org.scilab.modules.gui.events.callback.CallBack;
 import org.scilab.modules.gui.menu.Menu;
 import org.scilab.modules.gui.menu.ScilabMenu;
 import org.scilab.modules.gui.menubar.MenuBar;
 import org.scilab.modules.gui.menubar.ScilabMenuBar;
+import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.gui.menuitem.ScilabMenuItem;
 import org.scilab.modules.gui.tab.ScilabTab;
 import org.scilab.modules.gui.tab.Tab;
 import org.scilab.modules.gui.textbox.ScilabTextBox;
@@ -41,14 +54,19 @@ import org.scilab.modules.gui.toolbar.ToolBar;
 import org.scilab.modules.gui.utils.Size;
 import org.scilab.modules.gui.window.ScilabWindow;
 import org.scilab.modules.gui.window.Window;
+import org.scilab.modules.localization.Messages;
+import org.scilab.modules.xcos.PaletteDiagram;
+import org.scilab.modules.xcos.Xcos;
+import org.scilab.modules.xcos.XcosDiagram;
 import org.scilab.modules.xcos.actions.ClosePalettesAction;
-import org.scilab.modules.xcos.actions.NewDiagramAction;
-import org.scilab.modules.xcos.actions.OpenAction;
+import org.scilab.modules.xcos.actions.LoadAsPalAction;
 import org.scilab.modules.xcos.block.BasicBlock;
-import org.scilab.modules.xcos.block.TextBlock;
-import org.scilab.modules.xcos.io.BlockReader;
+import org.scilab.modules.xcos.utils.BlockPositioning;
+import org.scilab.modules.xcos.utils.XcosComponent;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
+import com.mxgraph.model.mxGeometry;
+
 /**
  * Manage all the Block Palettes.
  */
@@ -57,8 +75,11 @@ public final class XcosPaletteManager {
     private static Thread paletteThread;
     private static boolean paletteLoadStarted;
     private static Tab palettes;
-
-    /**
+    private static JTree paletteTree = null;
+    private static DefaultMutableTreeNode rootNode = null; 
+    private static DefaultMutableTreeNode userDefinedNode = null; 
+    private static DefaultTreeModel paletteTreeModel = null;
+       /**
      * Instantiate all the known names (default configuration)
      */
     private static final PaletteStringDescriptor[] allPalettesStringDescriptor = {
@@ -259,40 +280,112 @@ public final class XcosPaletteManager {
                        JSplitPane.HORIZONTAL_SPLIT);
                final XcosPalette rootPalette = new XcosPalette(
                        XcosMessages.PALETTES);
-               DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
+               rootNode = new DefaultMutableTreeNode(
                        rootPalette);
-               TreeModel paletteTreeModel = new DefaultTreeModel(rootNode);
-               final JTree paletteTree = new JTree(paletteTreeModel);
-
+               paletteTreeModel = new DefaultTreeModel(rootNode);
+               paletteTree = new JTree(paletteTreeModel);
+               
                allpalettes.setRightComponent(rootPalette);
                allpalettes.setLeftComponent(new JScrollPane(paletteTree));
 
                paletteTree.getSelectionModel().setSelectionMode(
                        TreeSelectionModel.SINGLE_TREE_SELECTION);
-               paletteTree
-                       .addTreeSelectionListener(new TreeSelectionListener() {
+               
+               paletteTree.addMouseListener(new MouseListener() {
+                   public void mouseReleased(MouseEvent arg0) {
+                   }
+
+                   public void mousePressed(MouseEvent arg0) {
+                   }
+
+                   public void mouseExited(MouseEvent arg0) {
+                   }
+
+                   public void mouseEntered(MouseEvent arg0) {
+                   }
+
+                   public void mouseClicked(MouseEvent arg0) {
+                       if ((arg0.getClickCount() == 1 && SwingUtilities.isRightMouseButton(arg0))
+                               || arg0.isPopupTrigger()
+                               || XcosMessages.isMacOsPopupTrigger(arg0)) {
+
+                           final TreePath path = XcosPaletteManager.paletteTree.getPathForLocation(arg0.getX(), arg0.getY());
+                           paletteTree.setSelectionPath(path);
+
+                           ContextMenu menu = ScilabContextMenu.createContextMenu();
+
+                           MenuItem addTo = ScilabMenuItem.createMenuItem();
+
+                           addTo.setText(XcosMessages.REMOVE_USER_DEFINED);
+                           addTo.setCallback(new CallBack(XcosMessages.REMOVE_USER_DEFINED) {
+                               private static final long serialVersionUID = 2975508442133933904L;
+
+                               public void callBack() {
+                                   DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)(path.getLastPathComponent());
+                                   paletteTreeModel.removeNodeFromParent(currentNode);
+                                   if(userDefinedNode != null && userDefinedNode.getChildCount() == 0) {
+                                       paletteTreeModel.removeNodeFromParent(userDefinedNode);
+                                       userDefinedNode = null;
+                                   }
+                                   paletteTree.setSelectionRow(0);
+                               }
+                           });
+                           
+                               addTo.setEnabled(false);
+                               if(((DefaultMutableTreeNode)path.getPath()[1]).getUserObject() instanceof String) {
+                               if(path.getPath().length > 2) {
+                                   if(((DefaultMutableTreeNode)path.getPath()[2]).getUserObject() instanceof XcosComponent) {
+                                       addTo.setEnabled(true);
+                                   }
+                               }
+                           }
+
+                           menu.add(addTo);
+                           menu.setVisible(true);
+                           ((SwingScilabContextMenu) menu.getAsSimpleContextMenu()).setLocation(
+                                   MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
+
+                       }
+                   }
+               });
+               
+               paletteTree.addTreeSelectionListener(new TreeSelectionListener() {
 
                            public void valueChanged(TreeSelectionEvent tree) {
-                               DefaultMutableTreeNode node = (DefaultMutableTreeNode) paletteTree
-                                       .getLastSelectedPathComponent();
+                               DefaultMutableTreeNode node = (DefaultMutableTreeNode) paletteTree.getLastSelectedPathComponent();
 
-                               if (node == null)
+                               if (node == null || node.getUserObject() instanceof String) {
                                    // Nothing is selected.
+                                   //allpalettes.setRightComponent(null);
                                    return;
-
-                               Dimension size = allpalettes.getLeftComponent()
-                                       .getSize();
-                               allpalettes.getLeftComponent()
-                                       .setPreferredSize(size);
-                               XcosPalette nodeInfo = (XcosPalette) node
-                                       .getUserObject();
+                               }
+                               
+                               //hide previous palette
+                               Component oldComp = allpalettes.getRightComponent();
+                               if(oldComp instanceof XcosComponent) {
+                                   XcosComponent comp = (XcosComponent)oldComp;
+                                   comp.setVisible(false);
+                               }
+
+                               Dimension size = allpalettes.getLeftComponent().getSize();
+                               allpalettes.getLeftComponent().setPreferredSize(size);
+                               JScrollPane nodeInfo = (JScrollPane) node.getUserObject();
+                               
+                               
+                               //update
                                allpalettes.setRightComponent(nodeInfo);
+                               
+                               //show next palette
+                               if(nodeInfo instanceof XcosComponent) {
+                                   XcosComponent comp = (XcosComponent)nodeInfo;
+                                   comp.setVisible(true);
+                               }
                            }
 
                        });
+               
                // Add a default Java bloc in HashMap
-               ((SwingScilabTab) palettes.getAsSimpleTab())
-                       .setContentPane(allpalettes);
+               ((SwingScilabTab) palettes.getAsSimpleTab()).setContentPane(allpalettes);
 
                allpalettes.setVisible(false);
                palettes.setVisible(true);
@@ -300,8 +393,7 @@ public final class XcosPaletteManager {
                for (PaletteStringDescriptor paletteStringDescriptor : allPalettesStringDescriptor) {
                    /* Doesn't perform UI update */
                    PaletteDescriptor currentDescriptor = new PaletteDescriptor(
-                           paletteStringDescriptor.Name,
-                           createPaletteData(paletteStringDescriptor.Components));
+                           paletteStringDescriptor.Name,createPaletteData(paletteStringDescriptor.Components));
 
                    /* Perform UI update */
                    XcosPalette xcosPalette = new XcosPalette(
@@ -313,13 +405,11 @@ public final class XcosPaletteManager {
                }
 
                paletteLoadStarted = true;
-               palettes.getAsSimpleTab().getInfoBar().setText(
-                       XcosMessages.EMPTY_INFO);
+               palettes.getAsSimpleTab().getInfoBar().setText(XcosMessages.EMPTY_INFO);
 
                /* UI Layout specific operations */
                paletteTree.expandRow(0);
-               allpalettes.getLeftComponent().setMinimumSize(
-                       paletteTree.getPreferredSize());
+               allpalettes.getLeftComponent().setMinimumSize(paletteTree.getPreferredSize());
                paletteTree.setSelectionRow(1);
                paletteTree.setRootVisible(false);
                allpalettes.setContinuousLayout(true);
@@ -331,14 +421,13 @@ public final class XcosPaletteManager {
     private XcosPaletteManager() {
     }
 
-    public static Tab loadPalette() {
+     public static Tab loadPalette() {
        synchronized (paletteThread) {
            paletteThread.notifyAll();
        }
        if (paletteLoadStarted == false) {
            createPaletteWindow();
-           palettes.getAsSimpleTab().getInfoBar().setText(
-                   XcosMessages.LOADING_PALETTES);
+           palettes.getAsSimpleTab().getInfoBar().setText(XcosMessages.LOADING_PALETTES);
            paletteThread.start();
        }
        return palettes;
@@ -348,14 +437,12 @@ public final class XcosPaletteManager {
 
        PaletteData[] xcosPalette = new PaletteData[blocksNames.length];
 
-       final String palImagesPath = System.getenv("SCI")
-               + "/modules/xcos/images/palettes/";
+       final String palImagesPath = System.getenv("SCI") + "/modules/xcos/images/palettes/";
 
        // BasicBlock theBloc = null;
 
        for (int kBlock = 0; kBlock < blocksNames.length; kBlock++) {
-           xcosPalette[kBlock] = new PaletteData(blocksNames[kBlock],
-                   new ImageIcon(palImagesPath + blocksNames[kBlock] + ".jpg"));
+           xcosPalette[kBlock] = new PaletteData(blocksNames[kBlock],new ImageIcon(palImagesPath + blocksNames[kBlock] + ".jpg"));
 
        }
 
@@ -387,13 +474,15 @@ public final class XcosPaletteManager {
        palMenu.setMnemonic('P');
        palMenuBar.add(palMenu);
 
+       palMenu.add(LoadAsPalAction.createMenu(null));
+       palMenu.addSeparator();
        palMenu.add(ClosePalettesAction.createMenu(null));
 
        /** Create the toolbar */
        ToolBar palToolbar = ScilabToolBar.createToolBar();
        palettes.addToolBar(palToolbar);
-       palToolbar.add(NewDiagramAction.createButton(null));
-       palToolbar.add(OpenAction.createButton(null));
+
+       palToolbar.add(LoadAsPalAction.createButton(null));
 
        /** Create the infobar */
        palettes.getAsSimpleTab().setInfoBar(ScilabTextBox.createTextBox());
@@ -407,4 +496,23 @@ public final class XcosPaletteManager {
        }
        return false;
     }
+
+    public static void loadUserPalette(String newPal) {
+       palettes.getAsSimpleTab().getInfoBar().setText(XcosMessages.LOADING_USER_DEFINE);
+       PaletteDiagram paletteDiagram = new PaletteDiagram();
+       paletteDiagram.installListeners();
+       if(paletteDiagram.openDiagramAsPal(newPal) == true) {
+           if(userDefinedNode == null) {
+               userDefinedNode = new DefaultMutableTreeNode(XcosMessages.USER_DEFINED);
+               paletteTreeModel.insertNodeInto(userDefinedNode, rootNode, rootNode.getChildCount());
+           }
+           
+           DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(paletteDiagram.getAsComponent());
+           paletteTreeModel.insertNodeInto(newNode, userDefinedNode, userDefinedNode.getChildCount());
+           paletteDiagram.getAsComponent().setVisible(false);
+           paletteTree.expandRow(rootNode.getIndex(userDefinedNode));
+           paletteTree.setSelectionPath(new TreePath(newNode.getPath()));
+       }
+       palettes.getAsSimpleTab().getInfoBar().setText(XcosMessages.EMPTY_INFO);
+    }
 }
index 70f0a4b..fb6ddda 100644 (file)
@@ -1,19 +1,54 @@
 package org.scilab.modules.xcos.utils;
 
+import java.awt.Dimension;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
+
+import org.scilab.modules.xcos.PaletteDiagram;
+
 import com.mxgraph.swing.mxGraphComponent;
 import com.mxgraph.view.mxGraph;
 
 public class XcosComponent extends mxGraphComponent{
 
-       private static final long serialVersionUID = 1592816691330208090L;
+    private static final long serialVersionUID = 1592816691330208090L;
 
-       public XcosComponent(mxGraph graph) {
-               super(graph);
-       }
+    public XcosComponent(mxGraph graph) {
+       super(graph);
+       
+       addComponentListener(new ComponentListener() {
+           
+           public void componentShown(ComponentEvent arg0) {
+               if(getGraph() instanceof PaletteDiagram) {
+                   ((PaletteDiagram)getGraph()).updateDiagram(getSize().getWidth());
+               }               
+           }
+           
+           public void componentResized(ComponentEvent arg0) {
+               if(getGraph() instanceof PaletteDiagram) {
+                   ((PaletteDiagram)getGraph()).updateDiagram(getSize().getWidth());
+               }               
+           }
+           
+           public void componentMoved(ComponentEvent arg0) {
+           }
+           
+           public void componentHidden(ComponentEvent arg0) {
+           }
+       });
+    }
 
-       public XcosCanvas createCanvas()
-       {
-               return new XcosCanvas();
+    public XcosCanvas createCanvas()
+    {
+       return new XcosCanvas();
+    }
+    
+    public String toString() {
+       if(getGraph() instanceof PaletteDiagram) {
+           String fileName = ((PaletteDiagram)getGraph()).getName();
+           return fileName.substring(0, fileName.lastIndexOf('.'));
+       } else {
+           return super.toString();
        }
-
+    }
 }
index 3e6ffa7..dbea5a3 100644 (file)
@@ -4,14 +4,20 @@ import com.mxgraph.util.mxConstants;
 
 public class XcosConstants extends mxConstants{
 
-       /**
-        * Defines the key for flip image .
-        */
-       public static String STYLE_FLIP = "flip";
+    public static int PALETTE_BLOCK_WIDTH = 100;
+    public static int PALETTE_BLOCK_HEIGHT = 100;
 
-       /**
-        * Defines the key for mirror image .
-        */
-       public static String STYLE_MIRROR = "mirror";
+    public static final int PALETTE_HMARGIN = 5;
+    public static final int PALETTE_VMARGIN = 5;
+
+    /**
+     * Defines the key for flip image .
+     */
+    public static String STYLE_FLIP = "flip";
+
+    /**
+     * Defines the key for mirror image .
+     */
+    public static String STYLE_MIRROR = "mirror";
 
 }
index 9713008..76ced24 100644 (file)
  */
 package org.scilab.modules.xcos.utils;
 
+import java.awt.event.MouseEvent;
+
+import javax.swing.SwingUtilities;
+
 import org.scilab.modules.localization.Messages;
 
 
@@ -48,6 +52,11 @@ public final class XcosMessages {
     public static final String QUIT = Messages.gettext("Quit Xcos");
     public static final String RECENT_FILES = Messages.gettext("Recent Files");
 
+    /** Palette menu in palette browser*/
+    public static final String LOAD_AS_PAL = Messages.gettext("Load as palette") + DOTS;
+    public static final String USER_DEFINED = Messages.gettext("User-Defined");
+    public static final String REMOVE_USER_DEFINED = Messages.gettext("Remove user defined palette");
+
     public static final String DUMP = Messages.gettext("Dump");
     public static final String VIEW_IN_SCICOS = Messages.gettext("View in Scicos");
 
@@ -204,6 +213,7 @@ public final class XcosMessages {
     public static final String SAVING_DIAGRAM = Messages.gettext("Saving diagram" + DOTS);
     public static final String LOADING_DIAGRAM = Messages.gettext("Loading diagram" + DOTS);
     public static final String LOADING_PALETTES = Messages.gettext("Loading palettes" + DOTS);
+    public static final String LOADING_USER_DEFINE = Messages.gettext("Loading user defined palettes" + DOTS);
     public static final String GENERATING_C_CODE = Messages.gettext("Generating C Code for SuperBlock" + DOTS);
     public static final String ERROR_GENERATING_C_CODE = Messages.gettext("A SuperBlock must be selected to generate code");
     public static final String SIMULATION_IN_PROGRESS = Messages.gettext("Simulation in progress" + DOTS);
@@ -221,4 +231,14 @@ public final class XcosMessages {
     public static final String FILE_COS = "Scicos file";
     public static final String FILE_XCOS = "Xcos file";
     public static final String FILE_HDF5 = "Scilab file";
+
+
+    /**
+     * This function checks for the popup menu activation under MacOS with Java version 1.5
+     * Related to Scilab bug #5190
+     * @return true if Java 1.5 and MacOS and mouse clic and ctrl activated
+     */
+    public static boolean isMacOsPopupTrigger(MouseEvent e) {
+       return (SwingUtilities.isLeftMouseButton(e) && e.isControlDown() && (System.getProperty("os.name").toLowerCase().indexOf("mac") != -1) && (System.getProperty("java.specification.version").equals("1.5")));
+    }
 }