001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.ui;
005:
006: import demo.sharededitor.controls.Dispatcher;
007: import java.awt.BasicStroke;
008: import java.awt.Color;
009: import java.awt.event.ActionEvent;
010: import java.awt.Graphics2D;
011: import java.awt.GridLayout;
012: import java.awt.image.BufferedImage;
013: import java.awt.Rectangle;
014: import java.lang.reflect.Method;
015: import java.net.URL;
016: import javax.swing.AbstractAction;
017: import javax.swing.AbstractButton;
018: import javax.swing.ButtonGroup;
019: import javax.swing.filechooser.FileSystemView;
020: import javax.swing.ImageIcon;
021: import javax.swing.JButton;
022: import javax.swing.JColorChooser;
023: import javax.swing.JFileChooser;
024: import javax.swing.JToggleButton;
025: import javax.swing.JToolBar;
026:
027: public final class Dashboard extends JToolBar {
028: private static final long serialVersionUID = -7801767824425098852L;
029: // private static boolean transparentMode = false;
030: private Dispatcher dispatcher;
031:
032: public Dashboard(Dispatcher dispatcher) {
033: final String IMAGE_PLACEHOLDER = "/images/placeholder.gif";
034: final String IMAGE_SELECTOR = "/images/selector.gif";
035: final String IMAGE_LINE = "/images/line.gif";
036: final String IMAGE_SQUARE = "/images/square.gif";
037: final String IMAGE_FILLEDSQUARE = "/images/filledsquare.gif";
038: final String IMAGE_CIRCLE = "/images/circle.gif";
039: final String IMAGE_FILLEDCIRCLE = "/images/filledcircle.gif";
040: final String IMAGE_TEXT = "/images/text.gif";
041: final String IMAGE_BEDROOM = "/images/bedroom.jpg";
042:
043: this .dispatcher = dispatcher;
044:
045: setOrientation(VERTICAL);
046: setFloatable(false);
047: setLayout(new GridLayout(8, 2));
048:
049: ButtonGroup bg = new ButtonGroup();
050: JToggleButton b1 = new JToggleButton(new SetDrawToolAction(
051: "Selector"));
052: b1.setIcon(new ImageIcon(getResource(IMAGE_SELECTOR)));
053: b1.setToolTipText("Select shapes");
054: bg.add(b1);
055: add(b1);
056:
057: JButton b0 = new JButton();
058: b0.setEnabled(false);
059: b0.setIcon(new ImageIcon(getResource(IMAGE_PLACEHOLDER)));
060: setWithSolidColorIcon(b0, Color.LIGHT_GRAY);
061:
062: b1 = new JToggleButton(new SetDrawToolAction("Line"));
063: b1.setIcon(new ImageIcon(getResource(IMAGE_LINE)));
064: b1.setToolTipText("Draw lines");
065: bg.add(b1);
066: add(b1);
067:
068: b1 = new JToggleButton(new SetDrawToolAction("Square",
069: FillStyleConsts.FILLSTYLE_TRANSPARENT));
070: b1.setIcon(new ImageIcon(getResource(IMAGE_SQUARE)));
071: b1
072: .setToolTipText("Draw transparent squares and rectangular shapes");
073: bg.add(b1);
074: add(b1);
075:
076: b1 = new JToggleButton(new SetDrawToolAction("Square",
077: FillStyleConsts.FILLSTYLE_SOLID));
078: b1.setIcon(new ImageIcon(getResource(IMAGE_FILLEDSQUARE)));
079: b1.setToolTipText("Draw solid squares and rectangular shapes");
080: bg.add(b1);
081: add(b1);
082:
083: b1 = new JToggleButton(new SetDrawToolAction("Circle",
084: FillStyleConsts.FILLSTYLE_TRANSPARENT));
085: b1.setIcon(new ImageIcon(getResource(IMAGE_CIRCLE)));
086: b1.setToolTipText("Draw transparent circles and oval shapes");
087: bg.add(b1);
088: add(b1);
089:
090: b1 = new JToggleButton(new SetDrawToolAction("Circle",
091: FillStyleConsts.FILLSTYLE_SOLID));
092: b1.setIcon(new ImageIcon(getResource(IMAGE_FILLEDCIRCLE)));
093: b1.setToolTipText("Draw solid circles and oval shapes");
094: bg.add(b1);
095: add(b1);
096:
097: b1 = new JToggleButton(new SetDrawToolAction("Text"));
098: b1.setIcon(new ImageIcon(getResource(IMAGE_TEXT)));
099: b1.setToolTipText("Render text as images");
100: add(b1);
101: bg.add(b1);
102:
103: b1 = new JToggleButton(new SetDrawToolAction("Image",
104: FillStyleConsts.FILLSTYLE_TEXTURED));
105: textureToolButton = b1;
106: b1.setIcon(new ImageIcon(getResource(IMAGE_PLACEHOLDER)));
107: setWithImageIcon(b1, new ImageIcon(getResource(IMAGE_BEDROOM)));
108: b1.setToolTipText("Paint with images");
109: add(b1);
110: bg.add(b1);
111:
112: add(new JToolBar.Separator());
113: add(new JToolBar.Separator());
114:
115: JButton b2 = new JButton(new SetColorAction("Foreground",
116: Color.DARK_GRAY));
117: b2.setIcon(new ImageIcon(getResource(IMAGE_PLACEHOLDER)));
118: b2.setToolTipText("Select line color");
119: setWithSolidColorIcon(b2, Color.DARK_GRAY);
120: add(b2);
121:
122: b2 = new JButton(new SetColorAction("Background",
123: Color.LIGHT_GRAY));
124: b2.setIcon(new ImageIcon(getResource(IMAGE_PLACEHOLDER)));
125: b2.setToolTipText("Select fill color");
126: setWithSolidColorIcon(b2, Color.LIGHT_GRAY);
127: add(b2);
128:
129: b2 = new JButton(new SetTextureAction());
130: this .textureSelectButton = b2;
131: b2.setIcon(new ImageIcon(getResource(IMAGE_PLACEHOLDER)));
132: setWithImageIcon(b2, new ImageIcon(getResource(IMAGE_BEDROOM)));
133: b2
134: .setToolTipText("Select the image to use when painting with images");
135: b2.setEnabled(false);
136: add(b2);
137:
138: // default settings
139: dispatcher.setStroke(new BasicStroke(1));
140: dispatcher.setFillStyle(FillStyleConsts.FILLSTYLE_SOLID);
141: dispatcher.setForeground(Color.DARK_GRAY);
142: dispatcher.setBackground(Color.LIGHT_GRAY);
143: dispatcher.setTexture(new ImageIcon(getResource(IMAGE_BEDROOM))
144: .getImage());
145: dispatcher.setFontName("Courier New");
146: dispatcher.setFontSize(24);
147: dispatcher.setDrawTool("Line");
148: }
149:
150: private AbstractButton textureSelectButton = null;
151:
152: private AbstractButton textureToolButton = null;
153:
154: private void setWithSolidColorIcon(AbstractButton b, Color c) {
155: final int w = b.getIcon().getIconWidth();
156: final int h = b.getIcon().getIconHeight();
157: final BufferedImage icon = new BufferedImage(w, h,
158: BufferedImage.TYPE_INT_RGB);
159: final Graphics2D g = icon.createGraphics();
160: final Rectangle r = new Rectangle(0, 0, w, h);
161: g.setColor(c);
162: g.setBackground(c);
163: g.fill(r);
164: b.setIcon(new ImageIcon(icon));
165: }
166:
167: private URL getResource(String path) {
168: return getClass().getResource(path);
169: }
170:
171: private void setWithImageIcon(AbstractButton b, ImageIcon icon) {
172: dispatcher.setTexture(icon.getImage());
173: int w = b.getIcon().getIconWidth();
174: int h = b.getIcon().getIconHeight();
175: BufferedImage scaled = new BufferedImage(w, h,
176: BufferedImage.TYPE_INT_RGB);
177: Graphics2D g = scaled.createGraphics();
178: g.drawImage(icon.getImage(), 0, 0, w, h, null);
179: b.setIcon(new ImageIcon(scaled));
180: }
181:
182: class SetDrawToolAction extends AbstractAction implements
183: FillStyleConsts {
184: private static final long serialVersionUID = 1L;
185:
186: private String name;
187:
188: private int fillstyle;
189:
190: public SetDrawToolAction(String name) {
191: this .name = name;
192: }
193:
194: public SetDrawToolAction(String name, int fillstyle) {
195: this .name = name;
196: this .fillstyle = fillstyle;
197: }
198:
199: public void actionPerformed(ActionEvent e) {
200: dispatcher.setDrawTool(name);
201: dispatcher.setFillStyle(fillstyle);
202: textureSelectButton
203: .setEnabled(FILLSTYLE_TEXTURED == fillstyle);
204: }
205: }
206:
207: class SetColorAction extends AbstractAction {
208: private static final long serialVersionUID = 1L;
209:
210: private String name;
211:
212: private Color color;
213:
214: public SetColorAction(String name, Color color) {
215: this .name = name;
216: this .color = color;
217: }
218:
219: public void actionPerformed(ActionEvent e) {
220: try {
221: Method m = dispatcher.getClass().getMethod(
222: "set" + name, new Class[] { Color.class });
223: Color selected = JColorChooser.showDialog(
224: Dashboard.this , name, color);
225:
226: if (selected == null)
227: return;
228:
229: m.invoke(dispatcher, new Object[] { selected });
230: Dashboard.this .setWithSolidColorIcon((AbstractButton) e
231: .getSource(), selected);
232: } catch (Exception ex) {
233: ex.printStackTrace();
234: throw new InternalError("Unable to set " + name
235: + " color.");
236: }
237: }
238: }
239:
240: class SetTextureAction extends AbstractAction implements
241: FillStyleConsts {
242: private static final long serialVersionUID = 1L;
243:
244: //private String name;
245: public void actionPerformed(ActionEvent e) {
246: JFileChooser jc = new JFileChooser(FileSystemView
247: .getFileSystemView());
248: jc.setFileFilter(new ImageFileFilter());
249:
250: if (JFileChooser.APPROVE_OPTION == jc
251: .showOpenDialog(Dashboard.this )) {
252: String imagefile = jc.getSelectedFile()
253: .getAbsolutePath();
254: Dashboard.this .setWithImageIcon((AbstractButton) e
255: .getSource(), new ImageIcon(imagefile));
256: Dashboard.this .setWithImageIcon(
257: (AbstractButton) textureToolButton,
258: new ImageIcon(imagefile));
259: }
260: }
261:
262: class ImageFileFilter extends
263: javax.swing.filechooser.FileFilter {
264: public boolean accept(java.io.File f) {
265: final String filename = f.getName().toLowerCase();
266: return filename.endsWith(".jpeg")
267: || filename.endsWith(".jpg")
268: || filename.endsWith(".gif")
269: || filename.endsWith(".png");
270: }
271:
272: public String getDescription() {
273: return "JPEG, JPG, GIF, & PNG Images";
274: }
275: }
276: }
277: }
|