1 19 24 package org.openide.explorer.propertysheet; 25 26 import java.awt.*; 27 import java.awt.event.FocusListener ; 28 import java.awt.geom.AffineTransform ; 29 import java.awt.image.BufferedImage ; 30 31 import javax.swing.*; 32 33 34 48 class ButtonPanel extends javax.swing.JComponent implements InplaceEditor { 49 public static final Object editorActionKey = "openCustomEditor"; 51 52 private final boolean log = PropUtils.isLoggable(ButtonPanel.class); 53 54 56 JComponent comp = null; 57 private ConditionallyFocusableButton button; 58 boolean needLayout = true; 59 private InplaceEditor inplace = null; 60 boolean clearing = false; 61 62 63 public ButtonPanel() { 64 createButton(); 65 setOpaque(true); 66 } 67 68 private void createButton() { 69 button = new ConditionallyFocusableButton(); 70 71 int buttonWidth = PropUtils.getCustomButtonWidth(); 72 button.setBounds(getWidth() - buttonWidth, 0, buttonWidth, getHeight()); 73 button.setIcon(PropUtils.getCustomButtonIcon()); 74 button.setRolloverIcon(PropUtils.getCustomButtonIcon()); 75 button.setMargin(null); 76 button.setName("Custom editor button - editor instance"); button.setText(null); 78 79 button.putClientProperty("hideActionText", Boolean.TRUE); 82 add(button); 86 87 } 89 90 void setButtonAction(Action a) { 91 button.setAction(a); 92 button.setIcon(PropUtils.getCustomButtonIcon()); 93 button.setRolloverIcon(PropUtils.getCustomButtonIcon()); 94 } 95 96 public void setOpaque(boolean b) { 97 if (getInplaceEditor() != null) { 98 getInplaceEditor().getComponent().setOpaque(true); 99 } 100 } 101 102 public void setFont(Font f) { 103 if (comp != null) { 104 comp.setFont(f); 105 } 106 107 super.setFont(f); 108 } 109 110 public InplaceEditor getInplaceEditor() { 111 return inplace; 112 } 113 114 public void setCustomButtonBackground(Color c) { 115 button.setBackground(c); 116 } 117 118 public void setRolloverPoint(Point p) { 119 if (p != null) { 120 if (p.x < (getWidth() - PropUtils.getCustomButtonWidth())) { 121 button.getModel().setRollover(false); 122 123 if (comp instanceof AbstractButton) { 124 ((AbstractButton) comp).getModel().setRollover(true); 125 } 126 } else { 127 button.getModel().setRollover(true); 128 129 if (comp instanceof AbstractButton) { 130 ((AbstractButton) comp).getModel().setRollover(false); 131 } 132 } 133 } else { 134 button.getModel().setRollover(false); 135 136 if (comp instanceof AbstractButton) { 137 ((AbstractButton) comp).getModel().setRollover(false); 138 } 139 } 140 } 141 142 public Dimension getPreferredSize() { 143 Dimension result; 144 145 if (comp != null) { 146 result = new Dimension(comp.getPreferredSize()); 147 result.width += button.getWidth(); 148 result.height = Math.max(result.height, button.getPreferredSize().height); 149 } else { 150 result = new Dimension(button.getPreferredSize()); 151 } 152 153 return result; 154 } 155 156 159 public void setEnabled(boolean val) { 160 super.setEnabled(val); 161 162 if (comp != null) { 163 comp.setEnabled(val); 164 } 165 166 button.setEnabled(true); 167 } 168 169 174 private void setComponent(JComponent c) { 175 if (c == comp) { 176 return; 177 } 178 179 if ((comp != null) && (comp.getParent() == this)) { 180 remove(comp); 181 } 182 183 if (log) { 184 PropUtils.log(ButtonPanel.class, "Button panel setComponent to " + c); 185 } 186 187 comp = c; 188 189 if (comp != null) { 190 comp.setBackground(getBackground()); 191 comp.setForeground(getForeground()); 192 193 if (comp.isEnabled() != isEnabled()) { 194 comp.setEnabled(isEnabled()); 195 } 196 197 add(comp); 198 } 199 200 needLayout = true; 201 } 202 203 public void setBackground(Color c) { 204 super.setBackground(c); 205 206 if (comp != null) { 207 comp.setBackground(c); 208 209 Color bttn = PropUtils.getButtonColor(); 210 211 if (bttn == null) { 212 button.setBackground(c); 213 } else { 214 button.setBackground(bttn); 215 } 216 } 217 } 218 219 public void setForeground(Color c) { 220 super.setForeground(c); 221 222 if (comp != null) { 223 comp.setForeground(c); 224 225 if (PropUtils.getButtonColor() == null) { 226 button.setForeground(c); 227 } 228 } 229 } 230 231 public void paint(Graphics g) { 232 if (isShowing()) { 233 super.paint(g); 234 235 return; 236 } 237 238 if (needLayout) { 239 doLayout(); 240 } 241 242 int width = getWidth(); 243 244 Graphics cg = g.create(0, 0, width - button.getWidth(), getHeight()); 246 247 try { 248 if (comp instanceof InplaceEditor) { 249 comp.paint(cg); 250 251 if (comp.getParent() != this) { 252 add(comp); 253 } 254 } 255 } finally { 256 cg.dispose(); 257 } 258 259 cg = g.create(width - button.getWidth(), 0, button.getWidth(), getHeight()); 260 261 try { 262 button.paint(cg); 263 } finally { 264 cg.dispose(); 265 } 266 267 269 if (getParent() instanceof CellRendererPane) { 270 RepaintManager.currentManager(this).markCompletelyClean(this); 271 } 272 } 273 274 277 @SuppressWarnings ("deprecation") 278 public void reshape(int x, int y, int w, int h) { 279 super.reshape(x, y, w, h); 280 needLayout = true; 281 } 282 283 286 public void requestFocus() { 287 if (comp != null) { 288 comp.requestFocus(); 289 } 290 } 291 292 295 public boolean requestFocusInWindow() { 296 if (comp != null) { 297 return comp.requestFocusInWindow(); 298 } else { 299 return false; 300 } 301 } 302 303 305 public void addFocusListener(FocusListener l) { 306 if (comp != null) { 307 button.addFocusListener(l); 308 comp.addFocusListener(l); 309 } 310 } 311 312 314 public void removeFocusListener(FocusListener l) { 315 if (comp != null) { 316 button.removeFocusListener(l); 317 comp.removeFocusListener(l); 318 } 319 } 320 321 public void setInplaceEditor(InplaceEditor ed) { 322 if (inplace == ed) { 323 if (isAncestorOf(inplace.getComponent())) { 324 return; 325 } 326 } 327 328 if (inplace != null) { 329 setComponent(null); 330 } 331 332 inplace = ed; 333 setComponent(inplace.getComponent()); 334 needLayout = true; 335 } 336 337 public void addActionListener(java.awt.event.ActionListener al) { 339 inplace.addActionListener(al); 340 } 341 342 public void clear() { 343 clearing = true; 344 345 try { 346 inplace.clear(); 347 inplace = null; 348 setComponent(null); 349 } finally { 350 clearing = false; 351 } 352 } 353 354 359 public JComponent getComponent() { 360 return this; 361 } 362 363 public void connect(java.beans.PropertyEditor pe, PropertyEnv env) { 364 inplace.connect(pe, env); 365 } 366 367 public KeyStroke[] getKeyStrokes() { 368 return inplace.getKeyStrokes(); 369 } 370 371 public java.beans.PropertyEditor getPropertyEditor() { 372 return inplace.getPropertyEditor(); 373 } 374 375 public PropertyModel getPropertyModel() { 376 return inplace.getPropertyModel(); 377 } 378 379 public Object getValue() { 380 return inplace.getValue(); 381 } 382 383 public boolean isKnownComponent(Component c) { 384 return (c == this) || inplace.isKnownComponent(c); 386 } 387 388 public void removeActionListener(java.awt.event.ActionListener al) { 389 inplace.removeActionListener(al); 390 } 391 392 public void reset() { 393 inplace.reset(); 394 } 395 396 public void setPropertyModel(PropertyModel pm) { 397 inplace.setPropertyModel(pm); 398 } 399 400 public void setValue(Object o) { 401 inplace.setValue(o); 402 } 403 404 public boolean supportsTextEntry() { 405 return inplace.supportsTextEntry(); 406 } 407 408 public void doLayout() { 409 if (comp != null) { 410 comp.setBounds(0, 0, getWidth() - PropUtils.getCustomButtonWidth(), getHeight()); 411 comp.doLayout(); 412 } 413 414 button.setBounds( 415 getWidth() - PropUtils.getCustomButtonWidth(), 0, PropUtils.getCustomButtonWidth(), getHeight() 416 ); 417 418 if (log) { 419 PropUtils.log( 420 ButtonPanel.class, 421 "Laying out button panel. Bounds" + " are " + getBounds() + ", custom editor button bounds: " + 422 button.getBounds() + " comp is " + comp 423 ); } 425 426 needLayout = false; 427 } 428 429 public Dimension getMinimumSize() { 430 return getPreferredSize(); 431 } 432 433 447 private class ConditionallyFocusableButton extends JButton { 448 private AffineTransform at = AffineTransform.getTranslateInstance(0, 0); 449 private BufferedImage snapshot = null; 450 451 public ConditionallyFocusableButton() { 452 } 453 454 public boolean isFocusable() { 455 return (ButtonPanel.this.getParent() != null) && !clearing; 456 } 457 458 public void paint(Graphics g) { 459 if (PropUtils.useOptimizedCustomButtonPainting() && !hasFocus()) { 460 if (log) { 461 PropUtils.log( 462 ButtonPanel.class, 463 "Blitting custom editor " + "button backing store for button at " + getBounds() + " in " + 464 ((getParent() == null) ? " null parent" : (getParent() + "editor=" + inplace)) 465 ); } 467 468 ((Graphics2D) g).drawRenderedImage(getSnapshot(), at); 469 } else { 470 if (log) { 471 PropUtils.log( 472 ButtonPanel.class, 473 "Painting unoptimized custom editor " + "button button at " + getBounds() + " in " + 474 ((getParent() == null) ? " null parent" : (getParent() + "editor=" + inplace)) 475 ); } 477 478 super.paint(g); 479 } 480 } 481 482 public BufferedImage getSnapshot() { 483 if (snapshot == null) { 484 snapshot = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() 485 .getDefaultConfiguration().createCompatibleImage(getWidth(), getHeight()); 486 487 if (log) { 488 PropUtils.log(ButtonPanel.class, "Created " + snapshot + " custom editor button backing image"); 489 } 490 491 if (snapshot.getAlphaRaster() == null) { 492 snapshot = new BufferedImage (getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); 496 } 497 498 Graphics g = snapshot.getGraphics(); 499 super.paint(g); 500 } 501 502 return snapshot; 503 } 504 } 505 } 506 | Popular Tags |