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