1 /**
2  * $Id: EditorPalette.java,v 1.9 2012-01-13 12:52:28 david Exp $
3  * Copyright (c) 2007-2012, JGraph Ltd
4  */
5 package com.mxgraph.examples.swing.editor;
6 
7 import java.awt.Color;
8 import java.awt.Dimension;
9 import java.awt.FlowLayout;
10 import java.awt.Font;
11 import java.awt.GradientPaint;
12 import java.awt.Graphics;
13 import java.awt.Graphics2D;
14 import java.awt.Point;
15 import java.awt.Rectangle;
16 import java.awt.datatransfer.DataFlavor;
17 import java.awt.dnd.DnDConstants;
18 import java.awt.dnd.DragGestureEvent;
19 import java.awt.dnd.DragGestureListener;
20 import java.awt.dnd.DragSource;
21 import java.awt.event.MouseEvent;
22 import java.awt.event.MouseListener;
23 
24 import javax.swing.ImageIcon;
25 import javax.swing.JComponent;
26 import javax.swing.JLabel;
27 import javax.swing.JPanel;
28 import javax.swing.TransferHandler;
29 
30 import com.mxgraph.model.mxCell;
31 import com.mxgraph.model.mxGeometry;
32 import com.mxgraph.swing.util.mxGraphTransferable;
33 import com.mxgraph.swing.util.mxSwingConstants;
34 import com.mxgraph.util.mxEvent;
35 import com.mxgraph.util.mxEventObject;
36 import com.mxgraph.util.mxEventSource;
37 import com.mxgraph.util.mxPoint;
38 import com.mxgraph.util.mxRectangle;
39 import com.mxgraph.util.mxEventSource.mxIEventListener;
40 
41 public class EditorPalette extends JPanel
42 {
43 
44 	/**
45 	 *
46 	 */
47 	private static final long serialVersionUID = 7771113885935187066L;
48 
49 	/**
50 	 *
51 	 */
52 	protected JLabel selectedEntry = null;
53 
54 	/**
55 	 *
56 	 */
57 	protected mxEventSource eventSource = new mxEventSource(this);
58 
59 	/**
60 	 *
61 	 */
62 	protected Color gradientColor = new Color(117, 195, 173);
63 
64 	/**
65 	 *
66 	 */
67 	@SuppressWarnings("serial")
EditorPalette()68 	public EditorPalette()
69 	{
70 		setBackground(new Color(149, 230, 190));
71 		setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
72 
73 		// Clears the current selection when the background is clicked
74 		addMouseListener(new MouseListener()
75 		{
76 
77 			/*
78 			 * (non-Javadoc)
79 			 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
80 			 */
81 			public void mousePressed(MouseEvent e)
82 			{
83 				clearSelection();
84 			}
85 
86 			/*
87 			 * (non-Javadoc)
88 			 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
89 			 */
90 			public void mouseClicked(MouseEvent e)
91 			{
92 			}
93 
94 			/*
95 			 * (non-Javadoc)
96 			 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
97 			 */
98 			public void mouseEntered(MouseEvent e)
99 			{
100 			}
101 
102 			/*
103 			 * (non-Javadoc)
104 			 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
105 			 */
106 			public void mouseExited(MouseEvent e)
107 			{
108 			}
109 
110 			/*
111 			 * (non-Javadoc)
112 			 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
113 			 */
114 			public void mouseReleased(MouseEvent e)
115 			{
116 			}
117 
118 		});
119 
120 		// Shows a nice icon for drag and drop but doesn't import anything
121 		setTransferHandler(new TransferHandler()
122 		{
123 			public boolean canImport(JComponent comp, DataFlavor[] flavors)
124 			{
125 				return true;
126 			}
127 		});
128 	}
129 
130 	/**
131 	 *
132 	 */
setGradientColor(Color c)133 	public void setGradientColor(Color c)
134 	{
135 		gradientColor = c;
136 	}
137 
138 	/**
139 	 *
140 	 */
getGradientColor()141 	public Color getGradientColor()
142 	{
143 		return gradientColor;
144 	}
145 
146 	/**
147 	 *
148 	 */
paintComponent(Graphics g)149 	public void paintComponent(Graphics g)
150 	{
151 		if (gradientColor == null)
152 		{
153 			super.paintComponent(g);
154 		}
155 		else
156 		{
157 			Rectangle rect = getVisibleRect();
158 
159 			if (g.getClipBounds() != null)
160 			{
161 				rect = rect.intersection(g.getClipBounds());
162 			}
163 
164 			Graphics2D g2 = (Graphics2D) g;
165 
166 			g2.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), 0,
167 					gradientColor));
168 			g2.fill(rect);
169 		}
170 	}
171 
172 	/**
173 	 *
174 	 */
clearSelection()175 	public void clearSelection()
176 	{
177 		setSelectionEntry(null, null);
178 	}
179 
180 	/**
181 	 *
182 	 */
setSelectionEntry(JLabel entry, mxGraphTransferable t)183 	public void setSelectionEntry(JLabel entry, mxGraphTransferable t)
184 	{
185 		JLabel previous = selectedEntry;
186 		selectedEntry = entry;
187 
188 		if (previous != null)
189 		{
190 			previous.setBorder(null);
191 			previous.setOpaque(false);
192 		}
193 
194 		if (selectedEntry != null)
195 		{
196 			selectedEntry.setBorder(ShadowBorder.getSharedInstance());
197 			selectedEntry.setOpaque(true);
198 		}
199 
200 		eventSource.fireEvent(new mxEventObject(mxEvent.SELECT, "entry",
201 				selectedEntry, "transferable", t, "previous", previous));
202 	}
203 
204 	/**
205 	 *
206 	 */
setPreferredWidth(int width)207 	public void setPreferredWidth(int width)
208 	{
209 		int cols = Math.max(1, width / 55);
210 		setPreferredSize(new Dimension(width,
211 				(getComponentCount() * 55 / cols) + 30));
212 		revalidate();
213 	}
214 
215 	/**
216 	 *
217 	 * @param name
218 	 * @param icon
219 	 * @param style
220 	 * @param width
221 	 * @param height
222 	 * @param value
223 	 */
addEdgeTemplate(final String name, ImageIcon icon, String style, int width, int height, Object value)224 	public void addEdgeTemplate(final String name, ImageIcon icon,
225 			String style, int width, int height, Object value)
226 	{
227 		mxGeometry geometry = new mxGeometry(0, 0, width, height);
228 		geometry.setTerminalPoint(new mxPoint(0, height), true);
229 		geometry.setTerminalPoint(new mxPoint(width, 0), false);
230 		geometry.setRelative(true);
231 
232 		mxCell cell = new mxCell(value, geometry, style);
233 		cell.setEdge(true);
234 
235 		addTemplate(name, icon, cell);
236 	}
237 
238 	/**
239 	 *
240 	 * @param name
241 	 * @param icon
242 	 * @param style
243 	 * @param width
244 	 * @param height
245 	 * @param value
246 	 */
addTemplate(final String name, ImageIcon icon, String style, int width, int height, Object value)247 	public void addTemplate(final String name, ImageIcon icon, String style,
248 			int width, int height, Object value)
249 	{
250 		mxCell cell = new mxCell(value, new mxGeometry(0, 0, width, height),
251 				style);
252 		cell.setVertex(true);
253 
254 		addTemplate(name, icon, cell);
255 	}
256 
257 	/**
258 	 *
259 	 * @param name
260 	 * @param icon
261 	 * @param cell
262 	 */
addTemplate(final String name, ImageIcon icon, mxCell cell)263 	public void addTemplate(final String name, ImageIcon icon, mxCell cell)
264 	{
265 		mxRectangle bounds = (mxGeometry) cell.getGeometry().clone();
266 		final mxGraphTransferable t = new mxGraphTransferable(
267 				new Object[] { cell }, bounds);
268 
269 		// Scales the image if it's too large for the library
270 		if (icon != null)
271 		{
272 			if (icon.getIconWidth() > 32 || icon.getIconHeight() > 32)
273 			{
274 				icon = new ImageIcon(icon.getImage().getScaledInstance(32, 32,
275 						0));
276 			}
277 		}
278 
279 		final JLabel entry = new JLabel(icon);
280 		entry.setPreferredSize(new Dimension(50, 50));
281 		entry.setBackground(EditorPalette.this.getBackground().brighter());
282 		entry.setFont(new Font(entry.getFont().getFamily(), 0, 10));
283 
284 		entry.setVerticalTextPosition(JLabel.BOTTOM);
285 		entry.setHorizontalTextPosition(JLabel.CENTER);
286 		entry.setIconTextGap(0);
287 
288 		entry.setToolTipText(name);
289 		entry.setText(name);
290 
291 		entry.addMouseListener(new MouseListener()
292 		{
293 
294 			/*
295 			 * (non-Javadoc)
296 			 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
297 			 */
298 			public void mousePressed(MouseEvent e)
299 			{
300 				setSelectionEntry(entry, t);
301 			}
302 
303 			/*
304 			 * (non-Javadoc)
305 			 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
306 			 */
307 			public void mouseClicked(MouseEvent e)
308 			{
309 			}
310 
311 			/*
312 			 * (non-Javadoc)
313 			 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
314 			 */
315 			public void mouseEntered(MouseEvent e)
316 			{
317 			}
318 
319 			/*
320 			 * (non-Javadoc)
321 			 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
322 			 */
323 			public void mouseExited(MouseEvent e)
324 			{
325 			}
326 
327 			/*
328 			 * (non-Javadoc)
329 			 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
330 			 */
331 			public void mouseReleased(MouseEvent e)
332 			{
333 			}
334 
335 		});
336 
337 		// Install the handler for dragging nodes into a graph
338 		DragGestureListener dragGestureListener = new DragGestureListener()
339 		{
340 			/**
341 			 *
342 			 */
343 			public void dragGestureRecognized(DragGestureEvent e)
344 			{
345 				e
346 						.startDrag(null, mxSwingConstants.EMPTY_IMAGE, new Point(),
347 								t, null);
348 			}
349 
350 		};
351 
352 		DragSource dragSource = new DragSource();
353 		dragSource.createDefaultDragGestureRecognizer(entry,
354 				DnDConstants.ACTION_COPY, dragGestureListener);
355 
356 		add(entry);
357 	}
358 
359 	/**
360 	 * @param eventName
361 	 * @param listener
362 	 * @see com.mxgraph.util.mxEventSource#addListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
363 	 */
addListener(String eventName, mxIEventListener listener)364 	public void addListener(String eventName, mxIEventListener listener)
365 	{
366 		eventSource.addListener(eventName, listener);
367 	}
368 
369 	/**
370 	 * @return whether or not event are enabled for this palette
371 	 * @see com.mxgraph.util.mxEventSource#isEventsEnabled()
372 	 */
isEventsEnabled()373 	public boolean isEventsEnabled()
374 	{
375 		return eventSource.isEventsEnabled();
376 	}
377 
378 	/**
379 	 * @param listener
380 	 * @see com.mxgraph.util.mxEventSource#removeListener(com.mxgraph.util.mxEventSource.mxIEventListener)
381 	 */
removeListener(mxIEventListener listener)382 	public void removeListener(mxIEventListener listener)
383 	{
384 		eventSource.removeListener(listener);
385 	}
386 
387 	/**
388 	 * @param eventName
389 	 * @param listener
390 	 * @see com.mxgraph.util.mxEventSource#removeListener(java.lang.String, com.mxgraph.util.mxEventSource.mxIEventListener)
391 	 */
removeListener(mxIEventListener listener, String eventName)392 	public void removeListener(mxIEventListener listener, String eventName)
393 	{
394 		eventSource.removeListener(listener, eventName);
395 	}
396 
397 	/**
398 	 * @param eventsEnabled
399 	 * @see com.mxgraph.util.mxEventSource#setEventsEnabled(boolean)
400 	 */
setEventsEnabled(boolean eventsEnabled)401 	public void setEventsEnabled(boolean eventsEnabled)
402 	{
403 		eventSource.setEventsEnabled(eventsEnabled);
404 	}
405 
406 }
407