KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > sharededitor > ui > Dashboard


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharededitor.ui;
5
6 import demo.sharededitor.controls.Dispatcher;
7 import java.awt.BasicStroke JavaDoc;
8 import java.awt.Color JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.Graphics2D JavaDoc;
11 import java.awt.GraphicsEnvironment JavaDoc;
12 import java.awt.GridLayout JavaDoc;
13 import java.awt.image.BufferedImage JavaDoc;
14 import java.awt.Rectangle JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.net.URL JavaDoc;
17 import javax.swing.AbstractAction JavaDoc;
18 import javax.swing.AbstractButton JavaDoc;
19 import javax.swing.ButtonGroup JavaDoc;
20 import javax.swing.filechooser.FileSystemView JavaDoc;
21 import javax.swing.ImageIcon JavaDoc;
22 import javax.swing.JButton JavaDoc;
23 import javax.swing.JColorChooser JavaDoc;
24 import javax.swing.JComboBox JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JToggleButton JavaDoc;
28 import javax.swing.JToolBar JavaDoc;
29
30 public final class Dashboard
31    extends JToolBar JavaDoc
32 {
33     private static final long serialVersionUID = -7801767824425098852L;
34
35     private static boolean transparentMode = false;
36     private Dispatcher dispatcher;
37
38     public Dashboard(Dispatcher dispatcher)
39     {
40        final String JavaDoc IMAGE_PLACEHOLDER = "/images/placeholder.gif";
41        final String JavaDoc IMAGE_SELECTOR = "/images/selector.gif";
42        final String JavaDoc IMAGE_LINE = "/images/line.gif";
43        final String JavaDoc IMAGE_SQUARE = "/images/square.gif";
44        final String JavaDoc IMAGE_FILLEDSQUARE = "/images/filledsquare.gif";
45        final String JavaDoc IMAGE_CIRCLE = "/images/circle.gif";
46        final String JavaDoc IMAGE_FILLEDCIRCLE = "/images/filledcircle.gif";
47        final String JavaDoc IMAGE_TEXT = "/images/text.gif";
48        final String JavaDoc IMAGE_BEDROOM = "/images/bedroom.jpg";
49        
50         this.dispatcher = dispatcher;
51         
52         setOrientation(VERTICAL);
53         setFloatable(false);
54         setLayout(new GridLayout JavaDoc(8, 2));
55
56         ButtonGroup JavaDoc bg = new ButtonGroup JavaDoc();
57         JToggleButton JavaDoc b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Selector"));
58         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_SELECTOR)));
59         b1.setToolTipText("Select shapes");
60         bg.add(b1);
61         add(b1);
62
63         JButton JavaDoc b0 = new JButton JavaDoc();
64         b0.setEnabled(false);
65         b0.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_PLACEHOLDER)));
66         setWithSolidColorIcon(b0, Color.LIGHT_GRAY);
67
68         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Line"));
69         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_LINE)));
70         b1.setToolTipText("Draw lines");
71         bg.add(b1);
72         add(b1);
73
74         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Square", IFillStyleConsts.FILLSTYLE_TRANSPARENT));
75         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_SQUARE)));
76         b1.setToolTipText("Draw transparent squares and rectangular shapes");
77         bg.add(b1);
78         add(b1);
79
80         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Square", IFillStyleConsts.FILLSTYLE_SOLID));
81         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_FILLEDSQUARE)));
82         b1.setToolTipText("Draw solid squares and rectangular shapes");
83         bg.add(b1);
84         add(b1);
85
86         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Circle", IFillStyleConsts.FILLSTYLE_TRANSPARENT));
87         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_CIRCLE)));
88         b1.setToolTipText("Draw transparent circles and oval shapes");
89         bg.add(b1);
90         add(b1);
91
92         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Circle", IFillStyleConsts.FILLSTYLE_SOLID));
93         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_FILLEDCIRCLE)));
94         b1.setToolTipText("Draw solid circles and oval shapes");
95         bg.add(b1);
96         add(b1);
97
98         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Text"));
99         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_TEXT)));
100         b1.setToolTipText("Render text as images");
101         add(b1);
102         bg.add(b1);
103
104         b1 = new JToggleButton JavaDoc(new SetDrawToolAction("Image", IFillStyleConsts.FILLSTYLE_TEXTURED));
105         textureToolButton = b1;
106         b1.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_PLACEHOLDER)));
107         setWithImageIcon(b1, new ImageIcon JavaDoc(getResource(IMAGE_BEDROOM)));
108         b1.setToolTipText("Paint with images");
109         add(b1);
110         bg.add(b1);
111
112         add(new JToolBar.Separator JavaDoc());
113         add(new JToolBar.Separator JavaDoc());
114
115         JButton JavaDoc b2 = new JButton JavaDoc(new SetColorAction("Foreground", Color.DARK_GRAY));
116         b2.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_PLACEHOLDER)));
117         b2.setToolTipText("Select line color");
118         setWithSolidColorIcon(b2, Color.DARK_GRAY);
119         add(b2);
120
121         b2 = new JButton JavaDoc(new SetColorAction("Background", Color.LIGHT_GRAY));
122         b2.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_PLACEHOLDER)));
123         b2.setToolTipText("Select fill color");
124         setWithSolidColorIcon(b2, Color.LIGHT_GRAY);
125         add(b2);
126
127         b2 = new JButton JavaDoc(new SetTextureAction());
128         this.textureSelectButton = b2;
129         b2.setIcon(new ImageIcon JavaDoc(getResource(IMAGE_PLACEHOLDER)));
130         setWithImageIcon(b2, new ImageIcon JavaDoc(getResource(IMAGE_BEDROOM)));
131         b2.setToolTipText("Select the image to use when painting with images");
132         b2.setEnabled(false);
133         add(b2);
134         
135         // default settings
136
dispatcher.setStroke(new BasicStroke JavaDoc(1));
137         dispatcher.setFillStyle(IFillStyleConsts.FILLSTYLE_SOLID);
138         dispatcher.setForeground(Color.DARK_GRAY);
139         dispatcher.setBackground(Color.LIGHT_GRAY);
140         dispatcher.setTexture(new ImageIcon JavaDoc(getResource(IMAGE_BEDROOM)).getImage());
141         dispatcher.setFontName("Courier New");
142         dispatcher.setFontSize(24);
143         dispatcher.setDrawTool("Line");
144     }
145     
146     private AbstractButton JavaDoc textureSelectButton = null;
147     private AbstractButton JavaDoc textureToolButton = null;
148     
149     private void setWithSolidColorIcon(AbstractButton JavaDoc b, Color JavaDoc c)
150     {
151         final int w = b.getIcon().getIconWidth();
152         final int h = b.getIcon().getIconHeight();
153         final BufferedImage JavaDoc icon = new BufferedImage JavaDoc(w, h,
154                 BufferedImage.TYPE_INT_RGB);
155         final Graphics2D JavaDoc g = icon.createGraphics();
156         final Rectangle JavaDoc r = new Rectangle JavaDoc(0, 0, w, h);
157         g.setColor(c);
158         g.setBackground(c);
159         g.fill(r);
160         b.setIcon(new ImageIcon JavaDoc(icon));
161     }
162
163         private URL JavaDoc getResource(String JavaDoc path) {
164           return getClass().getResource(path);
165         }
166
167     private void setWithImageIcon(AbstractButton JavaDoc b, ImageIcon JavaDoc icon)
168     {
169         dispatcher.setTexture(icon.getImage());
170         int w = b.getIcon().getIconWidth();
171         int h = b.getIcon().getIconHeight();
172         BufferedImage JavaDoc scaled = new BufferedImage JavaDoc(w, h, BufferedImage.TYPE_INT_RGB);
173         Graphics2D JavaDoc g = scaled.createGraphics();
174         g.drawImage(icon.getImage(), 0, 0, w, h, null);
175         b.setIcon(new ImageIcon JavaDoc(scaled));
176     }
177
178     class SetDrawToolAction
179        extends AbstractAction JavaDoc
180        implements IFillStyleConsts
181     {
182         private static final long serialVersionUID = 1L;
183
184         private String JavaDoc name;
185         private int fillstyle;
186
187         public SetDrawToolAction(String JavaDoc name)
188         {
189             this.name = name;
190         }
191         
192         public SetDrawToolAction(String JavaDoc name, int fillstyle)
193         {
194            this.name = name;
195            this.fillstyle = fillstyle;
196         }
197
198         public void actionPerformed(ActionEvent JavaDoc e)
199         {
200             dispatcher.setDrawTool(name);
201             dispatcher.setFillStyle(fillstyle);
202             textureSelectButton.setEnabled(FILLSTYLE_TEXTURED == fillstyle);
203         }
204     }
205
206     class SetColorAction
207        extends AbstractAction JavaDoc
208     {
209         private static final long serialVersionUID = 1L;
210
211         private String JavaDoc name;
212
213         private Color JavaDoc color;
214
215         public SetColorAction(String JavaDoc name, Color JavaDoc color)
216         {
217             this.name = name;
218             this.color = color;
219         }
220
221         public void actionPerformed(ActionEvent JavaDoc e)
222         {
223             try
224             {
225                 Method JavaDoc m = dispatcher.getClass().getMethod("set" + name,
226                         new Class JavaDoc[]
227                             { Color JavaDoc.class });
228                 Color JavaDoc selected = JColorChooser.showDialog(Dashboard.this, name,
229                         color);
230
231                 if (selected == null) return;
232
233                 m.invoke(dispatcher, new Object JavaDoc[]
234                     { selected });
235                 Dashboard.this.setWithSolidColorIcon((AbstractButton JavaDoc) e.getSource(),
236                         selected);
237             }
238             catch (Exception JavaDoc ex)
239             {
240                 ex.printStackTrace();
241                 throw new InternalError JavaDoc("Unable to set " + name + " color.");
242             }
243         }
244     }
245     
246     class SetTextureAction
247        extends AbstractAction JavaDoc
248        implements IFillStyleConsts
249     {
250         private static final long serialVersionUID = 1L;
251
252         private String JavaDoc name;
253
254         public void actionPerformed(ActionEvent JavaDoc e)
255         {
256             JFileChooser JavaDoc jc = new JFileChooser JavaDoc(FileSystemView.getFileSystemView());
257          jc.setFileFilter(new ImageFileFilter());
258             
259             if (JFileChooser.APPROVE_OPTION == jc.showOpenDialog(Dashboard.this))
260             {
261                String JavaDoc imagefile = jc.getSelectedFile().getAbsolutePath();
262                 Dashboard.this.setWithImageIcon((AbstractButton JavaDoc) e.getSource(), new ImageIcon JavaDoc(imagefile));
263                 Dashboard.this.setWithImageIcon((AbstractButton JavaDoc) textureToolButton, new ImageIcon JavaDoc(imagefile));
264             }
265         }
266         
267         class ImageFileFilter
268            extends javax.swing.filechooser.FileFilter JavaDoc
269         {
270            public boolean accept(java.io.File JavaDoc f)
271            {
272               return f.getName().endsWith(".jpeg") || f.getName().endsWith(".jpg") || f.getName().endsWith(".gif") || f.getName().endsWith(".png");
273            }
274            
275            public String JavaDoc getDescription()
276            {
277               return "JPEG, JPG, GIF, & PNG Images";
278            }
279         }
280     }
281 }
282
Popular Tags