1 19 24 package org.openide.explorer.propertysheet; 25 26 import org.openide.util.WeakSet; 27 28 import java.awt.Color ; 29 import java.awt.Component ; 30 import java.awt.Dimension ; 31 import java.awt.FontMetrics ; 32 import java.awt.Graphics ; 33 import java.awt.Insets ; 34 import java.awt.Point ; 35 import java.awt.Rectangle ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.ActionListener ; 38 import java.awt.event.InputEvent ; 39 import java.awt.event.MouseEvent ; 40 41 import java.beans.PropertyEditor ; 42 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 import javax.swing.ButtonGroup ; 48 import javax.swing.JComponent ; 49 import javax.swing.JPanel ; 50 import javax.swing.JRadioButton ; 51 import javax.swing.KeyStroke ; 52 import javax.swing.SwingUtilities ; 53 import javax.swing.border.TitledBorder ; 54 55 56 63 class RadioInplaceEditor extends JPanel implements InplaceEditor, ActionListener { 64 private transient List <ActionListener > actionListenerList; 65 protected transient PropertyEditor editor = null; 66 protected transient PropertyEnv env = null; 67 protected transient PropertyModel mdl = null; 68 protected transient ButtonGroup group = null; 69 private boolean tableUI = false; 70 boolean isFirstEvent = false; 71 private WeakSet<InvRadioButton> buttonCache = new WeakSet<InvRadioButton>(); 72 private boolean useTitle = false; 73 74 public RadioInplaceEditor(boolean tableUI) { 75 setLayout(new AutoGridLayout(false)); 76 this.tableUI = tableUI; 77 setOpaque(true); 78 } 79 80 public void clear() { 81 editor = null; 82 env = null; 83 mdl = null; 84 group = null; 85 86 Component [] c = getComponents(); 87 88 for (int i = 0; i < c.length; i++) { 89 if (c[i] instanceof JRadioButton ) { 90 ((JRadioButton ) c[i]).removeActionListener(this); 91 } 92 } 93 94 removeAll(); 95 setEnabled(true); 96 } 97 98 99 public Dimension getPreferredSize() { 100 if (getLayout() != null) { 101 return getLayout().preferredLayoutSize(this); 102 } else { 103 return super.getPreferredSize(); 104 } 105 } 106 107 public void addNotify() { 108 super.addNotify(); 109 isFirstEvent = true; 110 } 111 112 private InvRadioButton[] getButtons(int count) { 113 InvRadioButton[] result = new InvRadioButton[count]; 114 115 Iterator <InvRadioButton> i = buttonCache.iterator(); 116 int idx = 0; 117 118 while (i.hasNext() && (idx < count)) { 119 result[idx] = i.next(); 120 121 if (result[idx] != null) { 122 result[idx].setEnabled(true); 123 result[idx].setSelected(false); 124 idx++; 125 } 126 } 127 128 for (; idx < count; idx++) { 129 result[idx] = createButton(); 130 buttonCache.add(result[idx]); 131 } 132 133 return result; 134 } 135 136 public void setEnabled(boolean val) { 137 super.setEnabled(val); 139 140 Component [] c = getComponents(); 141 142 for (int i = 0; i < c.length; i++) { 143 c[i].setEnabled(val); 144 } 145 } 146 147 public void setBackground(Color col) { 148 super.setBackground(col); 149 150 Component [] c = getComponents(); 151 152 for (int i = 0; i < c.length; i++) { 153 c[i].setBackground(col); 154 } 155 } 156 157 public void setForeground(Color col) { 158 super.setForeground(col); 159 160 Component [] c = getComponents(); 161 162 for (int i = 0; i < c.length; i++) { 163 c[i].setForeground(col); 164 } 165 } 166 167 171 public void requestFocus() { 172 Component [] c = getComponents(); 173 174 if (c.length > 0) { 175 for (int i = 0; i < c.length; i++) { 176 if (c[i] instanceof InvRadioButton && ((InvRadioButton) c[i]).isSelected()) { 177 c[i].requestFocus(); 178 179 return; 180 } 181 } 182 183 c[0].requestFocus(); 184 } else { 185 super.requestFocus(); 186 } 187 } 188 189 public boolean requestFocusInWindow() { 190 Component [] c = getComponents(); 191 192 for (int i = 0; i < c.length; i++) { 193 if (c[i] instanceof InvRadioButton && ((InvRadioButton) c[i]).isSelected()) { 194 return c[i].requestFocusInWindow(); 195 } 196 } 197 198 return super.requestFocusInWindow(); 199 } 200 201 public void setUseTitle(boolean val) { 202 if (useTitle != val) { 203 useTitle = val; 204 205 if (env != null) { 206 setBorder(new TitledBorder (env.getFeatureDescriptor().getDisplayName())); 207 } 208 } 209 } 210 211 public void connect(PropertyEditor pe, PropertyEnv env) { 212 if (!tableUI && (env != null) && useTitle) { 213 setBorder(new TitledBorder (env.getFeatureDescriptor().getDisplayName())); 214 } else { 215 setBorder(null); 216 } 217 218 editor = pe; 219 220 String [] tags = editor.getTags(); 221 group = new ButtonGroup (); 222 223 InvRadioButton[] buttons = getButtons(tags.length); 224 225 if (env != null) { 226 setEnabled(env.isEditable()); 227 } 228 229 for (int i = 0; i < tags.length; i++) { 230 InvRadioButton jr = buttons[i]; 231 configureButton(jr, tags[i]); 232 add(jr); 233 } 234 } 235 236 238 protected InvRadioButton createButton() { 239 return new InvRadioButton(); 240 } 241 242 243 protected void configureButton(InvRadioButton ire, String txt) { 244 ire.addActionListener(this); 245 246 if (editor.getTags().length == 1) { 247 ire.setEnabled(false); 248 } else { 249 ire.setEnabled(isEnabled()); 250 } 251 252 if (tableUI) { 253 ire.setFocusable(false); 254 } else { 255 ire.setFocusable(true); 256 } 257 258 ire.setText(txt); 259 260 if (txt.equals(editor.getAsText())) { 261 ire.setSelected(true); 262 } else { 263 ire.setSelected(false); 264 } 265 266 ire.setFont(getFont()); 267 ire.setBackground(getBackground()); 268 ire.setForeground(getForeground()); 269 group.add(ire); 270 } 271 272 public JComponent getComponent() { 273 return this; 274 } 275 276 public KeyStroke [] getKeyStrokes() { 277 return null; 278 } 279 280 public PropertyEditor getPropertyEditor() { 281 return editor; 282 } 283 284 public PropertyModel getPropertyModel() { 285 return mdl; 286 } 287 288 public Object getValue() { 289 Component [] c = getComponents(); 290 291 for (int i = 0; i < c.length; i++) { 293 if (c[i] instanceof JRadioButton ) { 294 if (group.getSelection() == ((JRadioButton ) c[i]).getModel()) { 295 String result = ((JRadioButton ) c[i]).getText(); 296 297 return result; 298 } 299 } 300 } 301 302 return null; 303 } 304 305 public void handleInitialInputEvent(InputEvent e) { 306 System.err.println("HandleInitialInputEvent"); 307 getLayout().layoutContainer(this); 308 309 if (e instanceof MouseEvent ) { 310 Point p = SwingUtilities.convertPoint((JComponent ) e.getSource(), ((MouseEvent ) e).getPoint(), this); 311 Component c = getComponentAt(p); 312 313 if (c instanceof JRadioButton ) { 314 ((JRadioButton ) c).setSelected(true); 315 c.requestFocus(); 316 fireActionPerformed(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, InplaceEditor.COMMAND_SUCCESS)); 317 } 318 } else { 319 Component [] c = getComponents(); 320 321 for (int i = 0; i < c.length; i++) { 322 if (c[i] instanceof JRadioButton ) { 323 if (((JRadioButton ) c[i]).isSelected()) { 324 c[i].requestFocusInWindow(); 325 } 326 } 327 } 328 } 329 } 330 331 public boolean isKnownComponent(Component c) { 332 return (c != null) && ((c == this) || c instanceof InvRadioButton); 333 } 334 335 public void reset() { 336 setValue(editor.getAsText()); 337 } 338 339 public void setPropertyModel(PropertyModel pm) { 340 mdl = pm; 341 } 342 343 public void setValue(Object o) { 344 Component [] c = getComponents(); 345 346 for (int i = 0; i < c.length; i++) { 347 if (c[i] instanceof JRadioButton ) { 348 if (((JRadioButton ) c[i]).getText().equals(o)) { 349 ((JRadioButton ) c[i]).setSelected(true); 350 } else { 351 ((JRadioButton ) c[i]).setSelected(false); 353 } 354 } 355 } 356 } 357 358 public boolean supportsTextEntry() { 359 return false; 360 } 361 362 public synchronized void addActionListener(java.awt.event.ActionListener listener) { 363 if (actionListenerList == null) { 364 actionListenerList = new java.util.ArrayList <ActionListener >(); 365 } 366 367 actionListenerList.add(listener); 368 } 369 370 public synchronized void removeActionListener(java.awt.event.ActionListener listener) { 371 if (actionListenerList != null) { 372 actionListenerList.remove(listener); 373 } 374 } 375 376 private void fireActionPerformed(final java.awt.event.ActionEvent event) { 377 java.util.List list; 379 380 synchronized (this) { 381 if (actionListenerList == null) { 382 return; 383 } 384 385 list = (List ) ((ArrayList ) actionListenerList).clone(); 386 } 387 388 final java.util.List theList = list; 389 390 if (tableUI) { 395 SwingUtilities.invokeLater( 396 new Runnable () { 397 public void run() { 398 for (int i = 0; i < theList.size(); i++) { 399 ((java.awt.event.ActionListener ) theList.get(i)).actionPerformed(event); 400 } 401 } 402 } 403 ); 404 } else { 405 for (int i = 0; i < list.size(); i++) { 406 ((java.awt.event.ActionListener ) theList.get(i)).actionPerformed(event); 407 } 408 } 409 } 410 411 public void actionPerformed(ActionEvent e) { 412 ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, InplaceEditor.COMMAND_SUCCESS); 413 fireActionPerformed(ae); 414 } 415 416 public void paint(Graphics g) { 417 if (isShowing()) { 418 super.paint(g); 419 } else { 420 getLayout().layoutContainer(this); 421 422 Component [] c = getComponents(); 423 Color col = g.getColor(); 424 425 try { 426 g.setColor(getBackground()); 427 g.fillRect(0, 0, getWidth(), getHeight()); 428 429 for (int i = 0; i < c.length; i++) { 430 Rectangle r = c[i].getBounds(); 431 432 if (g.hitClip(r.x, r.y, r.width, r.height)) { 433 Graphics g2 = g.create(r.x, r.y, r.width, r.height); 434 435 try { 436 c[i].paint(g2); 437 } finally { 438 g2.dispose(); 439 } 440 } 441 } 442 443 if (getBorder() != null) { 444 super.paintBorder(g); 445 } 446 } finally { 447 g.setColor(col); 448 } 449 } 450 } 451 452 public void processMouseEvent(MouseEvent me) { 453 if (isFirstEvent) { 454 handleInitialInputEvent(me); 455 isFirstEvent = false; 456 } else { 457 super.processMouseEvent(me); 458 } 459 } 460 461 public Component getComponentAt(int x, int y) { 462 getLayout().layoutContainer(this); 463 464 Component result = super.getComponentAt(x, y); 465 System.err.println("getComponentAt " + x + "," + y + " returning " + result.getName()); 466 467 return result; 468 } 469 470 472 class InvRadioButton extends JRadioButton { 473 public InvRadioButton() { 474 super(); 475 } 476 477 public String getName() { 478 return "InvRadioButton - " + getText(); } 480 481 public void processKeyEvent(java.awt.event.KeyEvent ke) { 482 super.processKeyEvent(ke); 483 484 if ( 485 ((ke.getKeyCode() == ke.VK_ENTER) || (ke.getKeyCode() == ke.VK_ESCAPE)) && 486 (ke.getID() == ke.KEY_PRESSED) 487 ) { 488 RadioInplaceEditor.this.fireActionPerformed( 489 new ActionEvent ( 490 this, ActionEvent.ACTION_PERFORMED, 491 (ke.getKeyCode() == ke.VK_ENTER) ? COMMAND_SUCCESS : COMMAND_FAILURE 492 ) 493 ); 494 } 495 } 496 497 public Dimension getPreferredSize() { 498 int w = 0; 499 int h = 0; 500 Graphics g = PropUtils.getScratchGraphics(this); 501 FontMetrics fm = g.getFontMetrics(getFont()); 502 503 if (getIcon() != null) { 504 w = getIcon().getIconWidth(); 505 h = getIcon().getIconHeight(); 506 } 507 508 if (getBorder() != null) { 509 Insets ins = getBorder().getBorderInsets(this); 510 w += (ins.left + ins.right); 511 h += (ins.bottom + ins.top); 512 } 513 514 w += (fm.stringWidth(getText()) + 22); 515 h = Math.max(fm.getHeight(), h) + 2; 516 517 return new Dimension (w, h); 518 } 519 } 520 } 521 | Popular Tags |