1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.AlphaComposite ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dialog ; 26 import java.awt.Dimension ; 27 import java.awt.Graphics ; 28 import java.awt.Graphics2D ; 29 import java.awt.Rectangle ; 30 import java.awt.SystemColor ; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.text.NumberFormat ; 34 import java.util.HashMap ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import java.util.StringTokenizer ; 38 import javax.swing.ComboBoxEditor ; 39 import javax.swing.DefaultComboBoxModel ; 40 import javax.swing.JColorChooser ; 41 import javax.swing.JComboBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JFormattedTextField ; 44 import javax.swing.JFormattedTextField.AbstractFormatter; 45 import javax.swing.JLabel ; 46 import javax.swing.JList ; 47 import javax.swing.ListCellRenderer ; 48 import javax.swing.SwingUtilities ; 49 import javax.swing.text.DefaultFormatter ; 50 import org.openide.util.NbBundle; 51 52 56 class SplashUISupport { 57 private SplashUISupport() {} 58 59 static Rectangle stringToBounds(final String bounds) throws NumberFormatException { 60 StringTokenizer st = new StringTokenizer (bounds, " ,"); int x, y, width, height; 62 x = y = width = height =0; 63 for (int i = 0; i < 4; i++) { 64 if (!st.hasMoreElements()) { 65 throw new NumberFormatException (); 66 } 67 switch (i) { 68 case 0: 69 x = Integer.parseInt(st.nextToken()); 70 break; 71 case 1: 72 y = Integer.parseInt(st.nextToken()); 73 break; 74 case 2: 75 width = Integer.parseInt(st.nextToken()); 76 break; 77 case 3: 78 height = Integer.parseInt(st.nextToken()); 79 break; 80 81 } 82 } 83 return new Rectangle (x,y,width,height); 84 } 85 86 static String boundsToString(final Rectangle bounds) throws NumberFormatException { 87 StringBuffer sb = new StringBuffer (); 88 sb.append(String.valueOf(bounds.x)).append(","); sb.append(String.valueOf(bounds.y)).append(","); sb.append(String.valueOf(bounds.width)).append(","); sb.append(String.valueOf(bounds.height)); return sb.toString(); 93 } 94 95 static Color stringToColor(final String color) throws NumberFormatException { 96 return new Color (Integer.decode(color).intValue()); 97 } 98 99 static String colorToString(final Color color) throws NumberFormatException { 100 return "0x" + Integer.toString((~0xff000000 & color.getRGB()), 16).toUpperCase(Locale.ENGLISH); } 102 103 static int stringToInteger(final String integer) throws NumberFormatException { 104 return Integer.decode(integer).intValue(); 105 } 106 107 static String integerToString(final int integer) throws NumberFormatException { 108 return Integer.toString(integer, 10); 109 } 110 111 static JFormattedTextField getIntegerField() { 112 JFormattedTextField retval = new JFormattedTextField (NumberFormat.getIntegerInstance()); 113 retval = new JFormattedTextField (new FontFormatter(retval.getFormatter())); 114 return retval; 115 } 116 117 static JFormattedTextField getBoundsField() { 118 JFormattedTextField retval = new JFormattedTextField (new BoundsFormatter()); 119 return retval; 120 } 121 122 static SplashUISupport.ColorComboBox getColorComboBox() { 123 SplashUISupport.ColorComboBox retval = new SplashUISupport.ColorComboBox(); 124 return retval; 125 } 126 127 private static class FontFormatter extends DefaultFormatter { 128 private AbstractFormatter deleg; 129 FontFormatter(AbstractFormatter deleg) { 130 setOverwriteMode(false); 131 this.deleg = deleg; 132 } 133 public Object stringToValue(String string) throws java.text.ParseException { 134 Object retval = deleg.stringToValue(string); 135 int i = ((Number )retval).intValue(); 136 if (i < 0) { 137 throw new java.text.ParseException (string,0); 138 } 139 return retval; 140 } 141 142 public String valueToString(Object value) throws java.text.ParseException { 143 return deleg.valueToString(value); 144 } 145 } 146 147 private static class BoundsFormatter extends DefaultFormatter { 148 BoundsFormatter() { 149 setOverwriteMode(false); 150 } 151 public Object stringToValue(String string) throws java.text.ParseException { 152 if (string == null) { 153 return super.stringToValue(string); 154 } else { 155 try { 156 return SplashUISupport.stringToBounds(string); 157 } catch (NumberFormatException ex) { 158 throw new java.text.ParseException (string,0); 159 } 160 } 161 } 162 163 public String valueToString(Object value) throws java.text.ParseException { 164 if (value == null) { 165 return super.valueToString(value); 166 } else { 167 try { 168 return SplashUISupport.boundsToString((Rectangle )value); 169 } catch (NumberFormatException ex) { 170 throw new java.text.ParseException (value.toString(),0); 171 } 172 } 173 } 174 } 175 176 179 static class ColorComboBox extends JComboBox { 180 181 public static final String PROP_COLOR = "color"; public static final Value CUSTOM_COLOR = 183 new Value(loc("Custom"), null); 185 private static Map colorMap = new HashMap (); 186 static { 187 colorMap.put(Color.BLACK, loc("Black")); colorMap.put(Color.BLUE, loc("Blue")); colorMap.put(Color.CYAN, loc("Cyan")); colorMap.put(Color.DARK_GRAY, loc("Dark_Gray")); colorMap.put(Color.GRAY, loc("Gray")); colorMap.put(Color.GREEN, loc("Green")); colorMap.put(Color.LIGHT_GRAY, loc("Light_Gray")); colorMap.put(Color.MAGENTA, loc("Magenta")); colorMap.put(Color.ORANGE, loc("Orange")); colorMap.put(Color.PINK, loc("Pink")); colorMap.put(Color.RED, loc("Red")); colorMap.put(Color.WHITE, loc("White")); colorMap.put(Color.YELLOW, loc("Yellow")); } 201 202 private static Object [] content = new Object [] { 203 new Value(Color.BLACK), 204 new Value(Color.BLUE), 205 new Value(Color.CYAN), 206 new Value(Color.DARK_GRAY), 207 new Value(Color.GRAY), 208 new Value(Color.GREEN), 209 new Value(Color.LIGHT_GRAY), 210 new Value(Color.MAGENTA), 211 new Value(Color.ORANGE), 212 new Value(Color.PINK), 213 new Value(Color.RED), 214 new Value(Color.WHITE), 215 new Value(Color.YELLOW), 216 CUSTOM_COLOR, 217 new Value(NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_none"), null), 218 }; 219 220 221 222 public ColorComboBox() { 223 super(content); 224 setRenderer(new Renderer ()); 225 setEditable(true); 226 setEditor(new Renderer ()); 227 setSelectedItem(new Value(null, null)); 228 addActionListener(new ActionListener () { 229 public void actionPerformed(ActionEvent ev) { 230 if (getSelectedItem() == CUSTOM_COLOR) { 231 Color c = JColorChooser.showDialog( 232 SwingUtilities.getAncestorOfClass 233 (Dialog .class, ColorComboBox.this), 234 loc("SelectColor"), null 236 ); 237 setColor(c); 238 } 239 ColorComboBox.this.firePropertyChange(PROP_COLOR, null, null); 240 } 241 }); 242 } 243 244 public void setDefaultColor(Color color) { 245 Object [] ncontent = new Object [content.length]; 246 System.arraycopy(content, 0, ncontent, 0, content.length); 247 ncontent [content.length - 1] = new Value( 248 NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_default"), color ); 250 setModel(new DefaultComboBoxModel (ncontent)); 251 } 252 253 public void setColor(Color color) { 254 if (color == null) 255 setSelectedIndex(content.length - 1); 256 else 257 setSelectedItem(new Value(color)); 258 } 259 260 public Color getColor() { 261 if (getSelectedIndex() == (content.length - 1)) return null; 262 return ((Value) getSelectedItem()).color; 263 } 264 265 private static String loc(String key) { 266 return NbBundle.getMessage(ColorComboBox.class, key); 267 } 268 269 270 272 public static class Value { 273 String text; 274 Color color; 275 276 Value(Color color) { 277 this.color = color; 278 text = (String ) colorMap.get(color); 279 if (text != null) return; 280 StringBuffer sb = new StringBuffer (); 281 sb.append('[').append(color.getRed()). 282 append(',').append(color.getGreen()). 283 append(',').append(color.getBlue()). 284 append(']'); 285 text = sb.toString(); 286 } 287 288 Value(String text, Color color) { 289 this.text = text; 290 this.color = color; 291 } 292 } 293 294 private static class Editor extends JLabel implements ComboBoxEditor { 295 296 private Object value; 297 298 Editor() { 299 } 301 302 public Component getEditorComponent() { 303 return this; 304 } 305 306 public void setItem(Object anObject) { 307 value = anObject; 308 if (value instanceof String ) { 309 setText(NbBundle.getMessage(SplashUISupport.class, "SplashUISupport_color_default")); 310 super.setForeground(SystemColor.textText); 311 super.setBackground(SystemColor.text); 312 } else { 313 setText(""); 314 super.setBackground((Color ) value); 315 } 316 } 317 318 public Object getItem() { 319 return value; 320 } 321 322 public void setBackground(Color c) {} 323 public void setForeground(Color c) {} 324 325 public void selectAll() {} 326 public void addActionListener(ActionListener l) {} 327 public void removeActionListener(ActionListener l) {} 328 } 329 330 private class Renderer extends JComponent implements 331 ListCellRenderer , ComboBoxEditor { 332 333 private int SIZE = 9; 334 private Value value; 335 336 Renderer() { 337 setPreferredSize(new Dimension ( 338 50, getFontMetrics(ColorComboBox.this.getFont()).getHeight() + 2 339 )); 340 setOpaque(true); 341 } 342 343 public void paint(Graphics g) { 344 Graphics2D g2d = (Graphics2D )g; 345 if (!isEnabled()) { 346 g2d.setComposite(AlphaComposite.getInstance( 347 AlphaComposite.SRC_OVER, 0.3f)); 348 } 349 Color oldColor = g.getColor(); 350 Dimension size = getSize(); 351 g.setColor(getBackground()); 352 g.fillRect(0, 0, size.width, size.height); 353 int i = (size.height - SIZE) / 2; 354 if (value.color != null) { 355 g.setColor(Color.black); 356 g.drawRect(i, i, SIZE, SIZE); 357 g.setColor(value.color); 358 g.fillRect(i + 1, i + 1, SIZE - 1, SIZE - 1); 359 } 360 if (value.text != null) { 361 g.setColor(Color.black); 362 if (value.color != null) 363 g.drawString(value.text, i + SIZE + 5, i + SIZE); 364 else 365 g.drawString(value.text, 5, i + SIZE); 366 } 367 g.setColor(oldColor); 368 } 369 370 public void setEnabled(boolean enabled) { 371 setBackground(enabled ? 372 SystemColor.text : SystemColor.control 373 ); 374 super.setEnabled(enabled); 375 } 376 377 public Component getListCellRendererComponent( 378 JList list, 379 Object value, 380 int index, 381 boolean isSelected, 382 boolean cellHasFocus 383 ) { 384 this.value = (Value) value; 385 setEnabled(list.isEnabled()); 386 return this; 387 } 388 389 public Component getEditorComponent() { 390 setEnabled(ColorComboBox.this.isEnabled()); 391 return this; 392 } 393 394 public void setItem(Object anObject) { 395 this.value = (Value) anObject; 396 } 397 398 public Object getItem() { 399 return value; 400 } 401 public void selectAll() {} 402 public void addActionListener(ActionListener l) {} 403 public void removeActionListener(ActionListener l) {} } 404 } 405 406 } 407 | Popular Tags |