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