AC_JAVA_CHECK_VERSION_PACKAGE(NAME, IMPORTS, JAR, MIN_VERSION, CURRENT_VERSION)
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / XcosPalette.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4  * 
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
10  *
11  */
12
13 package org.scilab.modules.xcos.palette;
14
15 import java.awt.Color;
16 import java.awt.Dimension;
17 import java.awt.FlowLayout;
18 import java.awt.GradientPaint;
19 import java.awt.Graphics;
20 import java.awt.Graphics2D;
21 import java.awt.Point;
22 import java.awt.Rectangle;
23 import java.awt.datatransfer.DataFlavor;
24 import java.awt.dnd.DnDConstants;
25 import java.awt.dnd.DragGestureEvent;
26 import java.awt.dnd.DragGestureListener;
27 import java.awt.dnd.DragSource;
28 import java.awt.event.ComponentEvent;
29 import java.awt.event.ComponentListener;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32
33 import javax.swing.ImageIcon;
34 import javax.swing.JComponent;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.TransferHandler;
38
39 import org.flexdock.plaf.common.border.ShadowBorder;
40 import org.scilab.modules.xcos.utils.XcosConstants;
41
42 import com.mxgraph.swing.util.mxGraphTransferable;
43 import com.mxgraph.util.mxConstants;
44 import com.mxgraph.util.mxEvent;
45 import com.mxgraph.util.mxEventObject;
46 import com.mxgraph.util.mxEventSource;
47 import com.mxgraph.util.mxEventSource.mxIEventListener;
48
49 public class XcosPalette extends JScrollPane implements ComponentListener {
50     private static final long serialVersionUID = 5693635134906513755L;
51
52     private JPanel panel = null;
53     private String name;
54
55     protected BlockPalette selectedEntry;
56
57     protected mxEventSource eventSource = new mxEventSource(this);
58
59     protected Color gradientColor = Color.LIGHT_GRAY;
60
61     public XcosPalette(String name) {
62         super(new JPanel());
63         panel = (JPanel) getViewport().getComponent(0);
64         this.name = name;
65         setBackground(Color.WHITE);
66
67         panel.setBackground(Color.WHITE);
68         panel.setLayout(new FlowLayout(FlowLayout.LEADING, XcosConstants.PALETTE_HMARGIN, XcosConstants.PALETTE_VMARGIN));
69         panel.setPreferredSize(new Dimension((XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN), 0));
70
71         getVerticalScrollBar().setBlockIncrement(XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
72         getVerticalScrollBar().setUnitIncrement(XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
73
74         //getHorizontalScrollBar().setVisible(false);
75         getHorizontalScrollBar().setBlockIncrement(XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
76         getHorizontalScrollBar().setUnitIncrement(XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
77
78         addComponentListener(this);
79         // Clears the current selection when the background is clicked
80         addMouseListener(new MouseListener()
81         {
82
83             /*
84              * (non-Javadoc)
85              * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
86              */
87             public void mousePressed(MouseEvent e) {
88                 clearSelection();
89             }
90
91             /*
92              * (non-Javadoc)
93              * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
94              */
95             public void mouseClicked(MouseEvent e) {
96             }
97
98             /*
99              * (non-Javadoc)
100              * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
101              */
102             public void mouseEntered(MouseEvent e) {
103             }
104
105             /*
106              * (non-Javadoc)
107              * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
108              */
109             public void mouseExited(MouseEvent e) {
110             }
111
112             /*
113              * (non-Javadoc)
114              * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
115              */
116             public void mouseReleased(MouseEvent e) {
117             }
118
119         });
120
121         // Shows a nice icon for drag and drop but doesn't import anything
122         setTransferHandler(new TransferHandler() {
123             private static final long serialVersionUID = 1L;
124
125             public boolean canImport(JComponent comp, DataFlavor[] flavors) {
126                 return true;
127             }
128         });
129     }
130
131     /**
132      * 
133      */
134     public void setGradientColor(Color c) {
135         gradientColor = c;
136     }
137
138     /**
139      * 
140      */
141     public Color getGradientColor() {
142         return gradientColor;
143     }
144
145     /**
146      * 
147      */
148     public void paintComponent(Graphics g) {
149         if (gradientColor == null) {
150             super.paintComponent(g);
151         } else {
152             Rectangle rect = getVisibleRect();
153
154             if (g.getClipBounds() != null) {
155                 rect = rect.intersection(g.getClipBounds());
156             }
157
158             Graphics2D g2 = (Graphics2D) g;
159
160             g2.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), 0,
161                     gradientColor));
162             g2.fill(rect);
163         }
164     }
165
166     /**
167      * 
168      */
169     public void clearSelection() {
170         setSelectionEntry(null, null);
171     }
172
173     /**
174      * 
175      */
176     public void setSelectionEntry(BlockPalette entry, mxGraphTransferable t) {
177         BlockPalette last = selectedEntry;
178         selectedEntry = entry;
179
180         if (last != null) {
181             last.setBorder(null);
182             last.setOpaque(false);
183         }
184
185         if (selectedEntry != null) {
186             selectedEntry.setBorder(new ShadowBorder());
187             selectedEntry.setOpaque(true);
188         }
189
190         eventSource.fireEvent(mxEvent.SELECT, new mxEventObject(new Object[] {
191                 selectedEntry, t, last }));
192     }
193
194
195
196     /**
197      * 
198      * @param name
199      * @param icon
200      * @param style
201      * @param width
202      * @param height
203      * @param value
204      */
205     public void addTemplate(final String name, ImageIcon icon) {
206
207         final BlockPalette entry = new BlockPalette(icon);
208
209         entry.setPalette(this);
210         entry.setToolTipText(name);
211         entry.setText(name);
212
213         // Install the handler for dragging nodes into a graph
214         DragGestureListener dragGestureListener = new DragGestureListener()
215         {
216             /**
217              * 
218              */
219             public void dragGestureRecognized(DragGestureEvent e) {
220                 e.startDrag(null, mxConstants.EMPTY_IMAGE, new Point(), entry.getTransferable(), null);
221             }
222
223         };
224
225         DragSource dragSource = new DragSource();
226         dragSource.createDefaultDragGestureRecognizer(entry,
227                 DnDConstants.ACTION_COPY, dragGestureListener);
228
229         panel.add(entry);
230     }
231
232     /**
233      * @param eventName
234      * @param listener
235      * @see com.mxgraph.util.mxEventSource#addListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
236      */
237     public void addListener(String eventName, mxIEventListener listener) {
238         eventSource.addListener(eventName, listener);
239     }
240
241     /**
242      * @return
243      * @see com.mxgraph.util.mxEventSource#isEventsEnabled()
244      */
245     public boolean isEventsEnabled() {
246         return eventSource.isEventsEnabled();
247     }
248
249     /**
250      * @param listener
251      * @see com.mxgraph.util.mxEventSource#removeListener(com.mxgraph.util.mxEventSource.mxIEventListener)
252      */
253     public void removeListener(mxIEventListener listener) {
254         eventSource.removeListener(listener);
255     }
256
257     /**
258      * @param eventName
259      * @param listener
260      * @see com.mxgraph.util.mxEventSource#removeListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
261      */
262     public void removeListener(mxIEventListener listener, String eventName) {
263         eventSource.removeListener(listener, eventName);
264     }
265
266     /**
267      * @param eventsEnabled
268      * @see com.mxgraph.util.mxEventSource#setEventsEnabled(boolean)
269      */
270     public void setEventsEnabled(boolean eventsEnabled) {
271         eventSource.setEventsEnabled(eventsEnabled);
272     }
273
274     public void componentHidden(ComponentEvent arg0) {
275     }
276
277     public void componentMoved(ComponentEvent arg0) {
278     }
279
280     public void componentResized(ComponentEvent arg0) {
281         if (arg0.getSource() instanceof XcosPalette) {
282             XcosPalette palette = ((XcosPalette) arg0.getSource());
283             int panelWidth = (int) palette.getSize().getWidth() - 3;
284
285             //take care if VerticalScrollBar is visible to compute visible area
286             if (getVerticalScrollBar().isVisible()) {
287                 panelWidth -=  getVerticalScrollBar().getWidth();
288             }
289
290             int numberOfCols = panelWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
291             double numberOfRows = (double) panel.getComponentCount() / (double) numberOfCols;
292             int preferedHeight = (int) ((XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN) * Math.ceil(numberOfRows));
293
294             panel.setPreferredSize(new Dimension(panelWidth, preferedHeight));
295         }
296     }
297
298     public void componentShown(ComponentEvent arg0) {
299     }
300
301     public String toString() {
302         return this.name;
303     }
304
305     public BlockPalette getSelectedEntry() {
306         return selectedEntry;
307     }
308
309     public void setSelectedEntry(BlockPalette selectedEntry) {
310         this.selectedEntry = selectedEntry;
311     }
312
313 }