001: /*
002: * $Id: JGraphpadComboBox.java,v 1.4 2006/02/03 14:32:46 david Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.factory;
011:
012: import java.awt.Color;
013: import java.awt.Component;
014: import java.awt.geom.Rectangle2D;
015: import java.util.Hashtable;
016: import java.util.Map;
017:
018: import javax.swing.JComboBox;
019:
020: import org.jgraph.graph.AbstractCellView;
021: import org.jgraph.graph.GraphConstants;
022: import org.w3c.dom.Node;
023:
024: import com.jgraph.editor.JGraphEditorResources;
025: import com.jgraph.editor.factory.JGraphEditorComboBox;
026: import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
027: import com.jgraph.editor.factory.JGraphEditorComboBox.ComboBoxListener;
028: import com.jgraph.pad.dialog.JGraphpadDialogs;
029: import com.jgraph.pad.graph.JGraphpadGraphConstants;
030: import com.jgraph.pad.graph.JGraphpadVertexRenderer;
031: import com.jgraph.pad.graph.JGraphpadVertexView;
032:
033: /**
034: * Combo box factory methods with special dialogs and custom cell views for
035: * specific attributes (shape).
036: */
037: public class JGraphpadComboBox {
038:
039: /**
040: * Defines the combo box types for {@link ColorComboFactoryMethod}.
041: */
042: public static final int TYPE_BACKGROUND = 0, TYPE_GRADIENT = 1,
043: TYPE_LINECOLOR = 2;
044:
045: /**
046: * Default array of colors.
047: */
048: public static Color[] defaultColors = new Color[] { Color.black,
049: Color.blue, Color.cyan, Color.darkGray, Color.gray,
050: Color.green, Color.lightGray, Color.magenta, Color.orange,
051: Color.pink, Color.red, Color.white, Color.yellow };
052:
053: /**
054: * Default array of shapes.
055: */
056: public static int[] defaultShapes = new int[] {
057: JGraphpadVertexRenderer.SHAPE_RECTANGLE,
058: JGraphpadVertexRenderer.SHAPE_ROUNDED,
059: JGraphpadVertexRenderer.SHAPE_CIRCLE,
060: JGraphpadVertexRenderer.SHAPE_DIAMOND,
061: JGraphpadVertexRenderer.SHAPE_TRIANGLE,
062: JGraphpadVertexRenderer.SHAPE_CYLINDER };
063:
064: /**
065: * Provides a factory method to construct a color combo box.
066: */
067: public static class ColorComboFactoryMethod extends
068: JGraphEditorFactoryMethod {
069:
070: /**
071: * Defines the default name for factory methods of this kind.
072: */
073: public static String NAME = "createBackgroundCombo";
074:
075: /**
076: * Specifies the type of the color combo box.
077: */
078: protected int type = 0;
079:
080: /**
081: * Constructs a color combo box of type {@link #TYPE_BACKGROUND} using
082: * {@link #NAME}.
083: */
084: public ColorComboFactoryMethod() {
085: this (NAME, TYPE_BACKGROUND);
086: }
087:
088: /**
089: * Constructs a color combo box of the specified type.
090: *
091: * @param type
092: * The type of the combo box to be created.
093: */
094: public ColorComboFactoryMethod(String name, int type) {
095: super (name);
096: this .type = type;
097: }
098:
099: /*
100: * (non-Javadoc)
101: */
102: public Component createInstance(Node configuration) {
103: Map[] attrs = new Hashtable[defaultColors.length + 2];
104:
105: // Adds a special entry that displays a color bucket
106: // icon. Below a special listener is installed to handle
107: // clicks on this special entry. The renderer bridge
108: // takes care of displaying the icon.
109: attrs[0] = new Hashtable();
110: GraphConstants.setIcon(attrs[0], JGraphEditorResources
111: .getImage("/com/jgraph/pad/images/color.gif"));
112:
113: // Adds special entries to remove the color properties
114: // from the selection cells.
115: attrs[1] = new Hashtable();
116: if (type == TYPE_BACKGROUND)
117: GraphConstants.setRemoveAttributes(attrs[1],
118: new Object[] { GraphConstants.BACKGROUND,
119: GraphConstants.OPAQUE });
120: else if (type == TYPE_GRADIENT)
121: GraphConstants.setRemoveAttributes(attrs[1],
122: new Object[] { GraphConstants.GRADIENTCOLOR,
123: GraphConstants.OPAQUE });
124: else
125: GraphConstants.setRemoveAttributes(attrs[1],
126: new Object[] { GraphConstants.LINECOLOR });
127: GraphConstants.setIcon(attrs[1], JGraphEditorResources
128: .getImage("/com/jgraph/pad/images/delete.gif"));
129:
130: // Adds the standard entries for each color
131: for (int i = 0; i < defaultColors.length; i++) {
132: Map m = attrs[i + 2] = new Hashtable(4);
133: setColor(m, defaultColors[i]);
134: }
135:
136: // Overrides the default action listener's getSelection hook
137: // to return a specially created map which uses the color
138: // value from a dialog. This behaviour is for the first
139: // entry, which is displayed with a color bucket icon.
140: JGraphEditorComboBox comboBox = new JGraphEditorComboBox(
141: attrs, type == TYPE_LINECOLOR);
142: comboBox.addActionListener(new ComboBoxListener() {
143: protected Object getSelection(JComboBox box) {
144: if (box.getSelectedIndex() == 0) {
145: Color color = JGraphpadDialogs
146: .getSharedInstance()
147: .colorDialog(
148: box,
149: JGraphEditorResources
150: .getString("SelectColor"),
151: null);
152: if (color != null) {
153: Map map = new Hashtable(2);
154: setColor(map, color);
155: return map;
156: } else {
157: return null;
158: }
159: } else if (box.getSelectedIndex() == 1) {
160: Object obj = super .getSelection(box);
161: if (obj instanceof Map) {
162: Map map = new Hashtable((Map) obj);
163: map.remove(GraphConstants.ICON);
164: return map;
165: } else {
166: return obj;
167: }
168: }
169: return super .getSelection(box);
170: }
171: });
172: comboBox.setFocusable(false);
173: return comboBox;
174: }
175:
176: /**
177: * Sets the specified color in <code>map</code> according to the type
178: * of the combo box.
179: *
180: * @param m
181: * The map to set the color in.
182: * @param color
183: * The color to be set.
184: */
185: protected void setColor(Map m, Color color) {
186: if (type == TYPE_GRADIENT) {
187: GraphConstants.setGradientColor(m, color);
188: GraphConstants.setOpaque(m, true);
189: } else if (type == TYPE_LINECOLOR) {
190: GraphConstants.setLineColor(m, color);
191: } else {
192: GraphConstants.setBackground(m, color);
193: GraphConstants.setOpaque(m, true);
194: }
195: }
196:
197: }
198:
199: /**
200: * Provides a factory method to construct a shape combo box.
201: */
202: public static class VertexShapeComboFactoryMethod extends
203: JGraphEditorFactoryMethod {
204:
205: /**
206: * Defines the default name for factory methods of this kind.
207: */
208: public static String NAME = "createVertexShapeCombo";
209:
210: /**
211: * Constructs a new border combo factory method using {@link #NAME}.
212: */
213: public VertexShapeComboFactoryMethod() {
214: super (NAME);
215: }
216:
217: /*
218: * (non-Javadoc)
219: */
220: public Component createInstance(Node configuration) {
221: Map[] attrs = new Hashtable[defaultShapes.length];
222: for (int i = 0; i < defaultShapes.length; i++) {
223: attrs[i] = new Hashtable(2);
224: JGraphpadGraphConstants.setVertexShape(attrs[i],
225: defaultShapes[i]);
226: }
227: AbstractCellView view = new JGraphpadVertexView("");
228: GraphConstants.setBorderColor(view.getAttributes(),
229: Color.BLACK);
230: Rectangle2D bounds = new Rectangle2D.Double(0, 0, 14, 14);
231: GraphConstants.setBounds(view.getAttributes(), bounds);
232: JGraphEditorComboBox comboBox = new JGraphEditorComboBox(
233: attrs, view, false);
234: comboBox
235: .addActionListener(new JGraphEditorComboBox.ComboBoxListener());
236: comboBox.setFocusable(false);
237: return comboBox;
238: }
239:
240: }
241:
242: }
|