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