a18b4a71248a74253cfcdf63369a1efcd35a2c9c
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / BlockPalette.java
1 package org.scilab.modules.xcos.palette;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Font;
6 import java.awt.Graphics;
7 import java.awt.MouseInfo;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import javax.swing.ImageIcon;
14 import javax.swing.JLabel;
15 import javax.swing.SwingUtilities;
16 import javax.swing.border.Border;
17
18 import org.flexdock.plaf.common.border.ShadowBorder;
19 import org.scilab.modules.action_binding.InterpreterManagement;
20 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
21 import org.scilab.modules.gui.contextmenu.ContextMenu;
22 import org.scilab.modules.gui.contextmenu.ScilabContextMenu;
23 import org.scilab.modules.gui.events.callback.CallBack;
24 import org.scilab.modules.gui.menu.Menu;
25 import org.scilab.modules.gui.menu.ScilabMenu;
26 import org.scilab.modules.gui.menuitem.MenuItem;
27 import org.scilab.modules.gui.menuitem.ScilabMenuItem;
28 import org.scilab.modules.xcos.Xcos;
29 import org.scilab.modules.xcos.XcosDiagram;
30 import org.scilab.modules.xcos.block.BasicBlock;
31 import org.scilab.modules.xcos.block.TextBlock;
32 import org.scilab.modules.xcos.io.BlockReader;
33 import org.scilab.modules.xcos.utils.XcosMessages;
34
35 import com.mxgraph.swing.util.mxGraphTransferable;
36 import com.mxgraph.util.mxRectangle;
37
38 public class BlockPalette extends JLabel {
39
40     private static final long serialVersionUID = 1L;
41     public static int BLOCK_WIDTH = 100;
42     public static int BLOCK_HEIGHT = 100;
43
44     private BasicBlock block;
45     private XcosPalette palette;
46     private mxGraphTransferable transferable;
47
48     public BlockPalette(ImageIcon icon) {
49         super(icon);
50
51         addMouseListener(new MouseListener()
52         {
53             /*
54              * (non-Javadoc)
55              * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
56              */
57             public void mousePressed(MouseEvent e) {
58                 getPalette().setSelectionEntry(BlockPalette.this, getTransferable());
59             }
60
61             /*
62              * (non-Javadoc)
63              * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
64              */
65             public void mouseClicked(MouseEvent e) {
66                 if ((e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
67                         || e.isPopupTrigger()
68                         || isMacOsPopupTrigger(e)) {
69
70                     ContextMenu menu = ScilabContextMenu.createContextMenu();
71
72                     final List<XcosDiagram> allDiagrams = Xcos.getDiagrams();
73
74                     if (allDiagrams.size() == 0) {
75                         // No diagram opened: should never happen if Xcos opens an empty diagram when it is launched
76                         MenuItem addTo = ScilabMenuItem.createMenuItem();
77
78                         addTo.setText(XcosMessages.ADDTO_NEW_DIAGRAM);
79                         addTo.setCallback(new CallBack(BlockPalette.this.getText()) {
80                             private static final long serialVersionUID = 1185879440137756636L;
81
82                             public void callBack() {
83                                 BasicBlock block = (BasicBlock) BlockPalette.this.getBlock().createClone();
84                                 block.getGeometry().setX(10);
85                                 block.getGeometry().setY(10);
86                                 Xcos.createEmptyDiagram().addCell(block);
87                             }
88                         });
89
90                         menu.add(addTo);
91
92                     } else if (allDiagrams.size() == 1) {
93                         // A single diagram opened: add to this diagram
94                         MenuItem addTo = ScilabMenuItem.createMenuItem();
95
96                         addTo.setText(XcosMessages.ADDTO + " " + allDiagrams.get(0).getParentTab().getName());
97                         final XcosDiagram theDiagram = allDiagrams.get(0);
98                         addTo.setCallback(new CallBack(BlockPalette.this.getText()) {
99                             private static final long serialVersionUID = 1185879440137756636L;
100
101                             public void callBack() {
102                                 BasicBlock block = (BasicBlock) BlockPalette.this.getBlock().createClone();
103                                 block.getGeometry().setX(10);
104                                 block.getGeometry().setY(10);
105                                 theDiagram.addCell(block);
106                             }
107                         });
108
109                         menu.add(addTo);
110
111                     } else {
112                         // The user has to choose
113                         Menu addTo = ScilabMenu.createMenu();
114
115                         addTo.setText(XcosMessages.ADDTO);
116
117                         for (int i = 0; i < allDiagrams.size(); i++) {
118                             MenuItem diagram = ScilabMenuItem.createMenuItem();
119                             final XcosDiagram theDiagram = allDiagrams.get(i);
120                             diagram.setText(allDiagrams.get(i).getParentTab().getName());
121                             diagram.setCallback(new CallBack(BlockPalette.this.getText()) {
122                                 private static final long serialVersionUID = -3138430622029406470L;
123
124                                 public void callBack() {
125                                     BasicBlock block = (BasicBlock) BlockPalette.this.getBlock().createClone();
126                                     block.getGeometry().setX(10);
127                                     block.getGeometry().setY(10);
128                                     theDiagram.addCell(block);
129                                 }
130                             });
131                             addTo.add(diagram);
132                         }
133
134                         menu.add(addTo);
135                     }
136
137
138                     menu.getAsSimpleContextMenu().addSeparator();
139
140                     MenuItem help = ScilabMenuItem.createMenuItem();
141                     help.setText("Block help");
142                     help.setCallback(new CallBack(BlockPalette.this.getText()) {
143                         private static final long serialVersionUID = -8720228686621887887L;
144
145                         public void callBack() {
146                             InterpreterManagement.requestScilabExec("help " + BlockPalette.this.getText());
147                         }
148                     });
149                     menu.add(help);
150
151                     menu.setVisible(true);
152
153                     ((SwingScilabContextMenu) menu.getAsSimpleContextMenu()).setLocation(
154                             MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
155                 }
156             }
157
158             /*
159              * (non-Javadoc)
160              * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
161              */
162             public void mouseEntered(MouseEvent e) {
163             }
164
165             /*
166              * (non-Javadoc)
167              * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
168              */
169             public void mouseExited(MouseEvent e) {
170             }
171
172             /*
173              * (non-Javadoc)
174              * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
175              */
176             public void mouseReleased(MouseEvent e) {
177             }
178
179         });
180     }
181
182     public BasicBlock getBlock() {
183         
184         if(block == null) {
185             System.err.println("loading : " + getText());
186         } else {
187             System.err.println("Already loaded : " + getText());
188         }
189         
190         if(block == null) {
191             if (getText().compareTo("TEXT_f") != 0) {
192                 String blocksPath = System.getenv("SCI") + "/modules/scicos_blocks/blocks/";
193
194                 // Search the bloc in global hashmap
195                 block = BlockReader.readBlockFromFile(blocksPath + getText() + ".h5");
196
197                 if (block.getStyle().compareTo("") == 0) {
198                     block.setStyle(block.getInterfaceFunctionName());
199                     block.setValue(block.getInterfaceFunctionName());
200                 }
201             } else {
202                 block = new TextBlock("Edit me!!!");
203             }
204         }
205         return block;
206     }
207
208     public void setBlock(BasicBlock block) {
209         this.block = block;
210     }
211     public void paint2(Graphics g) {
212         boolean opaque = isOpaque();
213         Color bg = getBackground();
214         Border br = getBorder();
215
216         if (getPalette().getSelectedEntry() == this) {
217             setBackground(getPalette().getBackground().brighter());
218             setBorder(new ShadowBorder());
219             setOpaque(true);
220         }
221
222         super.paint(g);
223
224         setBorder(br);
225         setBackground(bg);
226         setOpaque(opaque);
227     }
228
229     public XcosPalette getPalette() {
230         return palette;
231     }
232
233     public void setPalette(XcosPalette palette) {
234         this.palette = palette;
235         setPreferredSize(new Dimension(BLOCK_WIDTH, BLOCK_HEIGHT));
236         setBackground(palette.getBackground().brighter());
237         setFont(new Font(getFont().getFamily(), 0, 12));
238
239         setVerticalTextPosition(JLabel.BOTTOM);
240         setHorizontalTextPosition(JLabel.CENTER);
241         setIconTextGap(5);
242     }
243
244     public mxGraphTransferable getTransferable() {
245         if(transferable == null) {
246             transferable = new mxGraphTransferable(new Object[] {BlockPalette.this.getBlock()},
247                         (mxRectangle) BlockPalette.this.getBlock().getGeometry().clone());          
248         }
249         return transferable;
250     }
251
252     public void setTransferable(mxGraphTransferable transferable) {
253         this.transferable = transferable;
254     }
255
256     /**
257      * This function checks for the popup menu activation under MacOS with Java version 1.5
258      * Related to Scilab bug #5190
259      * @return true if Java 1.5 and MacOS and mouse clic and ctrl activated
260      */
261     private boolean isMacOsPopupTrigger(MouseEvent e) {
262         return (SwingUtilities.isLeftMouseButton(e) && e.isControlDown() && (System.getProperty("os.name").toLowerCase().indexOf("mac") != -1) && (System.getProperty("java.specification.version").equals("1.5")));
263     }
264 }