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