1 19 20 package org.netbeans.core.windows.services; 21 22 23 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.DefaultKeyboardFocusManager ; 27 import java.awt.Dialog ; 28 import java.awt.Dimension ; 29 import java.awt.FlowLayout ; 30 import java.awt.Frame ; 31 import java.awt.GridBagConstraints ; 32 import java.awt.GridBagLayout ; 33 import java.awt.Insets ; 34 import java.awt.KeyboardFocusManager ; 35 import java.awt.Rectangle ; 36 import java.awt.event.*; 37 import java.beans.PropertyChangeListener ; 38 import java.lang.reflect.*; 39 import java.util.*; 40 import java.util.logging.*; 41 import javax.swing.*; 42 import javax.swing.event.*; 43 import javax.swing.plaf.basic.BasicLookAndFeel ; 44 import javax.swing.plaf.metal.MetalLookAndFeel ; 45 import org.openide.*; 46 import org.openide.awt.Mnemonics; 47 import org.openide.util.*; 48 49 50 52 56 class NbPresenter extends JDialog 57 implements PropertyChangeListener , WindowListener, Mutex.Action<Void >, Comparator<Object > { 58 59 60 public static NbPresenter currentModalDialog; 61 private static final Set<ChangeListener > listeners = new HashSet<ChangeListener >(); 62 63 protected NotifyDescriptor descriptor; 64 65 private final JButton stdYesButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("YES_OPTION_CAPTION")); private final JButton stdNoButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("NO_OPTION_CAPTION")); private final JButton stdOKButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("OK_OPTION_CAPTION")); private final JButton stdCancelButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("CANCEL_OPTION_CAPTION")); private final JButton stdClosedButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("CLOSED_OPTION_CAPTION")); private final JButton stdHelpButton = new JButton(); 71 private final JButton stdDetailButton = new JButton(NbBundle.getBundle(NbPresenter.class).getString("HELP_OPTION_CAPTION")); { 73 stdYesButton.setDefaultCapable(true); 74 stdOKButton.setDefaultCapable(true); 75 stdNoButton.setDefaultCapable(false); 76 stdCancelButton.setDefaultCapable(false); 77 stdCancelButton.setVerifyInputWhenFocusTarget(false); 78 stdClosedButton.setDefaultCapable(false); 79 stdHelpButton.setDefaultCapable(false); 80 stdDetailButton.setDefaultCapable(false); 81 Mnemonics.setLocalizedText (stdHelpButton, NbBundle.getBundle(NbPresenter.class).getString("HELP_OPTION_CAPTION")); 83 84 initAccessibility(); 85 } 86 private final static String ESCAPE_COMMAND = "Escape"; 88 private Component currentMessage; 89 private JScrollPane currentScrollPane; 90 private boolean leaf = false; 91 private JPanel currentButtonsPanel; 92 private Component [] currentPrimaryButtons; 93 private Component [] currentSecondaryButtons; 94 95 96 private int currentAlign; 97 98 private ButtonListener buttonListener; 99 100 private transient HelpCtx currentHelp = null; 101 102 private transient boolean haveCalledInitializeButtons = false; 103 104 private static Logger LOG = Logger.getLogger(NbPresenter.class.getName()); 105 106 static final long serialVersionUID =-4508637164126678997L; 107 108 112 public NbPresenter(NotifyDescriptor d, Frame owner, boolean modal) { 113 super(owner, d.getTitle(), modal); initialize(d); 115 } 116 117 121 public NbPresenter(NotifyDescriptor d, Dialog owner, boolean modal) { 122 super(owner, d.getTitle(), modal); initialize(d); 124 } 125 126 boolean isLeaf () { 127 return leaf; 128 } 129 130 private void initAccessibility(){ 131 132 ResourceBundle bundle; 133 bundle = NbBundle.getBundle(NbPresenter.class); 134 135 stdYesButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_YES_OPTION_NAME")); stdYesButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_YES_OPTION_DESC")); 138 stdNoButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_NO_OPTION_NAME")); stdNoButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_NO_OPTION_DESC")); 141 stdOKButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_OK_OPTION_NAME")); stdOKButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_OK_OPTION_DESC")); 144 stdCancelButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_CANCEL_OPTION_NAME")); stdCancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CANCEL_OPTION_DESC")); 147 stdClosedButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_CLOSED_OPTION_NAME")); stdClosedButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CLOSED_OPTION_DESC")); 150 stdHelpButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_HELP_OPTION_NAME")); stdHelpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_HELP_OPTION_DESC")); 153 stdDetailButton.getAccessibleContext().setAccessibleName(bundle.getString("ACS_HELP_OPTION_NAME")); stdDetailButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_HELP_OPTION_DESC")); } 156 157 private void initialize(NotifyDescriptor d) { 158 getRootPane().setOpaque(true); 163 164 if (d instanceof WizardDescriptor) { 165 setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSE); 167 } else { 168 setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); 170 } 171 172 descriptor = d; 173 174 buttonListener = new ButtonListener(); 175 leaf = d instanceof DialogDescriptor ? ((DialogDescriptor)d).isLeaf () : true; 177 178 getRootPane().registerKeyboardAction( 179 buttonListener, 180 ESCAPE_COMMAND, 181 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 182 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 183 ); 184 185 initializePresenter(); 186 187 pack(); 188 setBounds(Utilities.findCenterBounds(getSize())); 189 } 190 191 194 private void requestFocusForMessage() { 195 Component comp = currentMessage; 196 197 if(comp == null) { 198 return; 199 } 200 201 if(!(comp instanceof JComponent) 202 || !((JComponent)comp).requestDefaultFocus()) { 203 204 comp.requestFocus(); 205 } 206 } 207 208 private void initializeMessage() { 209 Object newMessage = descriptor.getMessage(); 210 if ((currentMessage == null) || !currentMessage.equals(newMessage)) { 212 uninitializeMessage(); 213 214 if (descriptor.getMessageType() == NotifyDescriptor.PLAIN_MESSAGE && 215 (newMessage instanceof Component )) { 216 currentMessage = (Component )newMessage; 218 } else { 219 currentMessage = createOptionPane(); 220 } 221 Dimension prefSize = currentMessage.getPreferredSize(); 222 final Rectangle screenBounds = Utilities.getUsableScreenBounds(); 223 224 if (prefSize.width > screenBounds.width - 100 225 || prefSize.height > screenBounds.height- 100 226 ) { 227 currentScrollPane = new JScrollPane() { 228 public Dimension getPreferredSize() { 229 Dimension sz = new Dimension (super.getPreferredSize()); 230 if (sz.width > screenBounds.width - 100) { 231 sz.width = screenBounds.width * 3 / 4; 232 } 233 if (sz.height > screenBounds.height - 100) 234 sz.height = screenBounds.height * 3 / 4; 235 return sz; 236 } 237 }; 238 currentScrollPane.setViewportView(currentMessage); 239 getContentPane().add(currentScrollPane, BorderLayout.CENTER); 240 } 241 else { 242 getContentPane().add(currentMessage, BorderLayout.CENTER); 243 } 244 } 245 } 246 247 private void uninitializeMessage() { 248 if (currentMessage != null) { 249 if (currentScrollPane != null) { 250 getContentPane().remove(currentScrollPane); 251 currentScrollPane = null; 252 } 253 else { 254 getContentPane().remove(currentMessage); 255 } 256 currentMessage = null; 257 } 258 } 259 260 private void initializePresenter() { 261 if (currentMessage != null) 262 return; 263 264 initializeMessage(); 265 266 updateHelp(); 267 268 initializeButtons(); 269 haveCalledInitializeButtons = true; 270 271 descriptor.addPropertyChangeListener(this); 272 addWindowListener(this); 273 274 initializeClosingOptions (); 275 } 276 277 280 private void uninitializePresenter() { 281 descriptor.removePropertyChangeListener(this); 282 uninitializeMessage(); 283 uninitializeButtons(); 284 uninitializeClosingOptions (); 285 } 286 287 private final HackTypeAhead hack = new HackTypeAhead(); 288 public void addNotify() { 289 super.addNotify(); 290 initializePresenter(); 291 292 hack.activate(); 293 } 294 295 public void removeNotify() { 296 super.removeNotify(); 297 uninitializePresenter(); 298 299 } 300 301 303 private JOptionPane createOptionPane() { 304 Object msg = descriptor.getMessage(); 305 boolean override = true; 306 String strMsg = null, strMsgLower; 307 308 if (msg instanceof String ) { 309 msg = org.openide.util.Utilities.replaceString((String )msg, "\t", " "); msg = org.openide.util.Utilities.replaceString((String )msg, "\r", ""); strMsg = (String )msg; 315 strMsgLower = strMsg.toLowerCase(); 316 override = !strMsgLower.startsWith("<html>"); } 318 if (msg instanceof javax.accessibility.Accessible ) { 319 strMsg = ((javax.accessibility.Accessible )msg).getAccessibleContext().getAccessibleDescription(); 320 } 321 322 JOptionPane optionPane; 323 if (override) { 324 optionPane = new JOptionPane( 326 msg, 327 descriptor.getMessageType(), 328 0, null, new Object [0], null ) { 333 public int getMaxCharactersPerLineCount() { 334 return 100; 335 } 336 }; 337 } else { 338 optionPane = new JOptionPane( 340 msg, 341 descriptor.getMessageType(), 342 0, null, new Object [0], null ); 347 } 348 349 if (UIManager.getLookAndFeel().getClass() == MetalLookAndFeel .class || 350 UIManager.getLookAndFeel().getClass() == BasicLookAndFeel .class) { 351 optionPane.setUI(new javax.swing.plaf.basic.BasicOptionPaneUI () { 352 public Dimension getMinimumOptionPaneSize() { 353 if (minimumSize == null) { 354 return new Dimension (MinimumWidth, 50); 357 } 358 return new Dimension (minimumSize.width, 50); 359 } 360 }); 361 } 362 optionPane.setWantsInput(false); 363 optionPane.getAccessibleContext().setAccessibleDescription(strMsg); 364 365 return optionPane; 366 } 367 368 private void uninitializeButtons() { 369 if (currentButtonsPanel != null) { 370 if (currentPrimaryButtons != null) { 371 for (int i = 0; i < currentPrimaryButtons.length; i++) { 372 modifyListener(currentPrimaryButtons[i], buttonListener, false); 373 } 374 } 375 if (currentSecondaryButtons != null) { 376 for (int i = 0; i < currentSecondaryButtons.length; i++) { 377 modifyListener(currentSecondaryButtons[i], buttonListener, false); 378 } 379 } 380 381 getContentPane().remove(currentButtonsPanel); 382 currentButtonsPanel = null; 383 } 384 } 385 386 private void initializeClosingOptions (boolean init) { 387 Object [] options = getClosingOptions (); 388 389 if (options == null) return ; 390 for (int i = 0; i < options.length; i++) { 391 modifyListener (options[i], buttonListener, init); 392 } 393 } 394 395 private void initializeClosingOptions () { 396 initializeClosingOptions (true); 397 } 398 399 private void uninitializeClosingOptions () { 400 initializeClosingOptions (false); 401 } 402 403 408 public int compare (Object a, Object b) { 409 boolean isDefaultButton = a.equals(descriptor.getDefaultValue ()); 410 int result; 411 if (a.equals(NotifyDescriptor.OK_OPTION) || a.equals(NotifyDescriptor.YES_OPTION)) { 412 result = 1; 413 } else { 414 result = 0; 415 } 416 417 if (isDefaultButton) { 418 result++; 419 } 420 return result; 421 } 422 423 protected final void initializeButtons() { 424 427 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getFocusOwner (); 428 429 boolean dontShowHelp = ( descriptor instanceof WizardDescriptor && ( Boolean.FALSE.equals (((WizardDescriptor)descriptor).getProperty ("WizardPanel_helpDisplayed")) )); boolean helpButtonShown = 431 stdHelpButton.isShowing() || ( descriptor instanceof WizardDescriptor && !dontShowHelp ); 432 433 434 uninitializeButtons(); 435 436 Object [] primaryOptions = descriptor.getOptions(); 437 Object [] secondaryOptions = descriptor.getAdditionalOptions(); 438 currentAlign = getOptionsAlign(); 439 440 443 currentPrimaryButtons = null; 444 currentSecondaryButtons = null; 445 446 boolean isAqua = "Aqua".equals (UIManager.getLookAndFeel().getID()) || "true".equalsIgnoreCase (System.getProperty ("xtest.looks_as_mac")); 448 if (isAqua) { 449 currentAlign = DialogDescriptor.BOTTOM_ALIGN; 451 } 452 453 if (primaryOptions != null) { 458 if (isAqua) { 459 Arrays.sort(primaryOptions, this); 460 } 461 currentPrimaryButtons = new Component [primaryOptions.length]; 462 for (int i = 0; i < primaryOptions.length; i++) { 463 if (primaryOptions[i] == NotifyDescriptor.YES_OPTION) { 464 currentPrimaryButtons[i] = stdYesButton; 465 } else if (primaryOptions[i] == NotifyDescriptor.NO_OPTION) { 466 currentPrimaryButtons[i] = stdNoButton; 467 } else if (primaryOptions[i] == NotifyDescriptor.OK_OPTION) { 468 currentPrimaryButtons[i] = stdOKButton; 469 stdOKButton.setEnabled(descriptor.isValid()); 470 } else if (primaryOptions[i] == NotifyDescriptor.CANCEL_OPTION) { 471 currentPrimaryButtons[i] = stdCancelButton; 472 } else if (primaryOptions[i] == NotifyDescriptor.CLOSED_OPTION) { 473 currentPrimaryButtons[i] = stdClosedButton; 474 } else if (primaryOptions[i] instanceof Component ) { 475 currentPrimaryButtons[i] = (Component ) primaryOptions [i]; 476 } else if (primaryOptions [i] instanceof Icon) { 477 JButton button = new JButton((Icon)primaryOptions [i]); 478 button.setDefaultCapable(false); 480 currentPrimaryButtons[i] = button; 481 } else { 482 JButton button = new JButton(); 483 Mnemonics.setLocalizedText (button, primaryOptions [i].toString ()); 484 button.setDefaultCapable(primaryOptions[i].equals(descriptor.getDefaultValue ())); 485 currentPrimaryButtons[i] = button; 486 } 487 } 488 } else { switch (descriptor.getOptionType()) { 490 case NotifyDescriptor.YES_NO_OPTION: 491 if (isAqua) { 492 currentPrimaryButtons = new Component [2]; 493 currentPrimaryButtons[0] = stdNoButton; 494 currentPrimaryButtons[1] = stdYesButton; 495 } else { 496 currentPrimaryButtons = new Component [2]; 497 currentPrimaryButtons[0] = stdYesButton; 498 currentPrimaryButtons[1] = stdNoButton; 499 } 500 break; 501 case NotifyDescriptor.YES_NO_CANCEL_OPTION: 502 currentPrimaryButtons = new Component [3]; 503 if (isAqua) { 504 currentPrimaryButtons[0] = stdCancelButton; 505 currentPrimaryButtons[1] = stdNoButton; 506 currentPrimaryButtons[2] = stdYesButton; 507 } else { 508 currentPrimaryButtons[0] = stdYesButton; 509 currentPrimaryButtons[1] = stdNoButton; 510 currentPrimaryButtons[2] = stdCancelButton; 511 } 512 break; 513 case NotifyDescriptor.OK_CANCEL_OPTION: 514 default: 515 if (isAqua) { 516 currentPrimaryButtons = new Component [2]; 517 currentPrimaryButtons[0] = stdCancelButton; 518 currentPrimaryButtons[1] = stdOKButton; 519 } else { 520 currentPrimaryButtons = new Component [2]; 521 currentPrimaryButtons[0] = stdOKButton; 522 currentPrimaryButtons[1] = stdCancelButton; 523 } 524 stdOKButton.setEnabled(descriptor.isValid()); 525 break; 526 } 527 } 528 529 531 if (!dontShowHelp && (currentHelp != null || helpButtonShown)) { 532 if (currentPrimaryButtons == null) currentPrimaryButtons = new Component [] { }; 533 Component [] cPB2 = new Component [currentPrimaryButtons.length + 1]; 534 if (isAqua) { System.arraycopy(currentPrimaryButtons, 0, cPB2, 1, currentPrimaryButtons.length); 537 cPB2[0] = stdHelpButton; 538 } else { 539 System.arraycopy(currentPrimaryButtons, 0, cPB2, 0, currentPrimaryButtons.length); 540 cPB2[currentPrimaryButtons.length] = stdHelpButton; 541 } 542 currentPrimaryButtons = cPB2; 543 544 stdHelpButton.setEnabled(currentHelp != null); 545 } 546 547 if ((secondaryOptions != null) && (secondaryOptions.length != 0)) { 548 currentSecondaryButtons = new Component [secondaryOptions.length]; 549 Arrays.sort (secondaryOptions, this); 550 for (int i = 0; i < secondaryOptions.length; i++) { 551 if (secondaryOptions[i] == NotifyDescriptor.YES_OPTION) { 552 currentSecondaryButtons[i] = stdYesButton; 553 } else if (secondaryOptions[i] == NotifyDescriptor.NO_OPTION) { 554 currentSecondaryButtons[i] = stdNoButton; 555 } else if (secondaryOptions[i] == NotifyDescriptor.OK_OPTION) { 556 currentSecondaryButtons[i] = stdOKButton; 557 stdOKButton.setEnabled(descriptor.isValid()); 558 } else if (secondaryOptions[i] == NotifyDescriptor.CANCEL_OPTION) { 559 currentSecondaryButtons[i] = stdCancelButton; 560 } else if (secondaryOptions[i] == NotifyDescriptor.CLOSED_OPTION) { 561 currentSecondaryButtons[i] = stdClosedButton; 562 } else if (secondaryOptions[i] instanceof Component ) { 563 currentSecondaryButtons[i] = (Component ) secondaryOptions [i]; 564 } else if (secondaryOptions [i] instanceof Icon) { 565 JButton button = new JButton((Icon)secondaryOptions [i]); 566 currentSecondaryButtons[i] = button; 567 } else { 568 JButton button = new JButton(); 569 Mnemonics.setLocalizedText (button, secondaryOptions [i].toString ()); 570 currentSecondaryButtons[i] = button; 571 } 572 } 573 } 574 575 578 if (currentAlign == DialogDescriptor.BOTTOM_ALIGN || currentAlign == -1) { 579 580 JPanel panelForPrimary = null; 581 JPanel panelForSecondary = null; 582 583 584 if (currentPrimaryButtons != null) { 585 panelForPrimary = new JPanel(); 586 587 if (currentAlign == -1) { 588 panelForPrimary.setLayout(new org.openide.awt.EqualFlowLayout()); 589 } else { 590 panelForPrimary.setLayout(new org.openide.awt.EqualFlowLayout(FlowLayout.RIGHT)); 591 } 592 for (int i = 0; i < currentPrimaryButtons.length; i++) { 593 modifyListener(currentPrimaryButtons[i], buttonListener, true); panelForPrimary.add(currentPrimaryButtons[i]); 595 } 596 } 597 598 if (currentSecondaryButtons != null) { 599 panelForSecondary = new JPanel(); 600 panelForSecondary.setLayout(new org.openide.awt.EqualFlowLayout(FlowLayout.LEFT)); 601 for (int i = 0; i < currentSecondaryButtons.length; i++) { 602 modifyListener(currentSecondaryButtons[i], buttonListener, true); panelForSecondary.add(currentSecondaryButtons[i]); 604 } 605 } 606 607 if ((panelForPrimary != null) && (panelForSecondary != null)) { 609 currentButtonsPanel = new JPanel(); 610 currentButtonsPanel.setLayout(new BorderLayout ()); 611 currentButtonsPanel.add(panelForPrimary, BorderLayout.EAST); 612 currentButtonsPanel.add(panelForSecondary, BorderLayout.WEST); 613 } else if (panelForPrimary != null) { 614 currentButtonsPanel = panelForPrimary; 615 } else { 616 currentButtonsPanel = panelForSecondary; 617 } 618 619 if ((currentButtonsPanel != null)&&(currentButtonsPanel.getComponentCount() != 0)) { 621 if (currentButtonsPanel.getBorder() == null) { 622 currentButtonsPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (11, 6, 5, 5))); 623 } 624 getContentPane().add(currentButtonsPanel, BorderLayout.SOUTH); 625 } 626 627 } else if (currentAlign == DialogDescriptor.RIGHT_ALIGN) { 628 currentButtonsPanel = new JPanel(); 629 currentButtonsPanel.setLayout(new GridBagLayout ()); 630 GridBagConstraints gbc = new GridBagConstraints (); 631 gbc.gridwidth = GridBagConstraints.REMAINDER; 632 gbc.weightx = 1.0f; 633 gbc.insets = new Insets (5, 4, 2, 5); 634 gbc.fill = GridBagConstraints.HORIZONTAL; 635 636 if (currentPrimaryButtons != null) { 637 for (int i = 0; i < currentPrimaryButtons.length; i++) { 638 modifyListener(currentPrimaryButtons[i], buttonListener, true); currentButtonsPanel.add(currentPrimaryButtons[i], gbc); 640 } 641 } 642 643 GridBagConstraints padding = new GridBagConstraints (); 644 padding.gridwidth = GridBagConstraints.REMAINDER; 645 padding.weightx = 1.0f; 646 padding.weighty = 1.0f; 647 padding.fill = GridBagConstraints.BOTH; 648 currentButtonsPanel.add(new JPanel(), padding); 649 650 gbc.insets = new Insets (2, 4, 5, 5); 651 if (currentSecondaryButtons != null) { 652 for (int i = 0; i < currentSecondaryButtons.length; i++) { 653 modifyListener(currentSecondaryButtons[i], buttonListener, true); currentButtonsPanel.add(currentSecondaryButtons[i], gbc); 655 } 656 } 657 658 if (currentButtonsPanel != null) { 660 if (currentButtonsPanel.getBorder() == null) { 661 currentButtonsPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (6, 7, 5, 5))); 662 } 663 getContentPane().add(currentButtonsPanel, BorderLayout.EAST); 664 } 665 666 } 667 updateDefaultButton(); 668 669 670 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getFocusOwner (); 671 672 if (fo != focusOwner && focusOwner != null) { 673 focusOwner.requestFocus(); 674 } 675 } 676 677 679 private void updateDefaultButton() { 680 if (descriptor.getDefaultValue () != null && descriptor.getDefaultValue () instanceof JButton) { 682 JButton b = (JButton)descriptor.getDefaultValue (); 683 if (b.isVisible() && b.isEnabled () && b.isDefaultCapable ()) { 684 getRootPane ().setDefaultButton (b); 685 return ; 686 } 687 } else { 688 } 690 if (currentPrimaryButtons != null) { 691 for (int i = 0; i < currentPrimaryButtons.length; i++) { 693 if (currentPrimaryButtons[i] instanceof JButton) { 694 JButton b = (JButton)currentPrimaryButtons[i]; 695 if (b.isVisible() && b.isEnabled() && b.isDefaultCapable()) { 696 getRootPane().setDefaultButton(b); 697 return; 698 } 699 } 700 } 701 } 702 getRootPane().setDefaultButton(null); 704 } 705 706 708 private void updateOKButton(boolean valid) { 709 if (currentPrimaryButtons != null) { 710 for (int i = 0; i < currentPrimaryButtons.length; i++) { 711 if (currentPrimaryButtons[i] instanceof JButton) { 712 JButton b = (JButton)currentPrimaryButtons[i]; 713 if ((b == stdOKButton) && b.isVisible()) { 714 b.setEnabled(valid); 715 } 716 } 717 } 718 } 719 if (currentSecondaryButtons != null) { 720 for (int i = 0; i < currentSecondaryButtons.length; i++) { 721 if (currentSecondaryButtons[i] instanceof JButton) { 722 JButton b = (JButton)currentSecondaryButtons[i]; 723 if ((b == stdOKButton) && b.isVisible()) { 724 b.setEnabled(valid); 725 } 726 } 727 } 728 } 729 } 730 731 private void modifyListener(Object comp, ButtonListener l, boolean add) { 732 if (comp instanceof JButton) { 734 JButton b = (JButton)comp; 735 if (add) { 736 List listeners; 737 listeners = Arrays.asList (b.getActionListeners ()); 738 if (!listeners.contains (l)) { 739 b.addActionListener(l); 740 } 741 listeners = Arrays.asList (b.getComponentListeners ()); 742 if (!listeners.contains (l)) { 743 b.addComponentListener(l); 744 } 745 listeners = Arrays.asList (b.getPropertyChangeListeners ()); 746 if (!listeners.contains (l)) { 747 b.addPropertyChangeListener(l); 748 } 749 } else { 750 b.removeActionListener(l); 751 b.removeComponentListener(l); 752 b.removePropertyChangeListener(l); 753 } 754 return; 755 } else { 756 java.lang.reflect.Method m = null; 759 try { 760 m = comp.getClass().getMethod(add ? "addActionListener" : "removeActionListener", new Class [] { ActionListener.class }); try { 762 m.setAccessible (true); 763 } catch (SecurityException se) { 764 m = null; } 766 } catch (NoSuchMethodException e) { 767 m = null; } catch (SecurityException e2) { 769 m = null; } 771 if (m != null) { 772 try { 773 m.invoke(comp, new Object [] { l }); 774 } catch (Exception e) { 775 } 777 } 778 } 779 } 780 781 783 private void superShow() { 784 assert SwingUtilities.isEventDispatchThread () : "Invoked super.show() in AWT event thread."; super.show(); 786 } 787 788 @Override @Deprecated 789 public void show() { 790 if (isModal()) { 792 Mutex.EVENT.readAccess(this); 793 } else { 794 if (SwingUtilities.isEventDispatchThread()) { 795 doShow(); 796 } else { 797 SwingUtilities.invokeLater(new Runnable () { 798 public void run () { 799 doShow(); 800 } 801 }); 802 } 803 } 804 } 805 806 public Void run() { 807 doShow(); 808 return null; 809 } 810 811 private void doShow () { 812 NbPresenter prev = null; 813 if (isModal()) { 814 prev = currentModalDialog; 815 currentModalDialog = this; 816 fireChangeEvent(); 817 } 818 819 superShow(); 820 821 if (currentModalDialog != prev) { 822 currentModalDialog = prev; 823 fireChangeEvent(); 824 } 825 } 826 827 public void propertyChange(final java.beans.PropertyChangeEvent evt) { 828 boolean update = false; 829 830 if (DialogDescriptor.PROP_OPTIONS.equals(evt.getPropertyName())) { 831 initializeButtons(); 832 update = true; 833 } else if (DialogDescriptor.PROP_OPTION_TYPE.equals(evt.getPropertyName())) { 834 initializeButtons(); 835 update = true; 836 } else if (DialogDescriptor.PROP_OPTIONS_ALIGN.equals(evt.getPropertyName())) { 837 initializeButtons(); 838 update = true; 839 } else if (DialogDescriptor.PROP_MESSAGE.equals(evt.getPropertyName())) { 840 initializeMessage(); 841 requestFocusForMessage(); 842 updateHelp(); 844 update = true; 845 } else if (DialogDescriptor.PROP_MESSAGE_TYPE.equals(evt.getPropertyName())) { 846 initializeMessage(); 847 requestFocusForMessage(); 848 update = true; 849 } else if (DialogDescriptor.PROP_TITLE.equals(evt.getPropertyName())) { 850 setTitle(descriptor.getTitle()); 851 } else if (DialogDescriptor.PROP_HELP_CTX.equals(evt.getPropertyName())) { 852 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getFocusOwner (); 854 updateHelp(); 855 currentButtonsPanel.revalidate(); 857 currentButtonsPanel.repaint(); 858 if (fo != null) fo.requestFocus(); 859 } else if (DialogDescriptor.PROP_VALID.equals(evt.getPropertyName())) { 860 updateOKButton(((Boolean )(evt.getNewValue())).booleanValue()); 861 } 862 863 if (update) { 864 Dimension sz = getSize(); 865 Dimension prefSize = getPreferredSize(); 866 if (prefSize.width > sz.width || prefSize.height > sz.height) { 867 setSize(Math.max(prefSize.width, sz.width), 868 Math.max(prefSize.height, sz.height)); 869 } 870 871 validate(); 872 repaint(); 873 } 874 } 875 876 private void updateHelp() { 877 HelpCtx help = getHelpCtx(); 879 if (HelpCtx.DEFAULT_HELP.equals(help)) { 882 Object msg = descriptor.getMessage(); 883 if (msg instanceof Component ) { 884 help = HelpCtx.findHelp((Component ) msg); 885 } 886 if (HelpCtx.DEFAULT_HELP.equals(help)) help = null; 887 } 888 if (! Utilities.compareObjects(currentHelp, help)) { 889 currentHelp = help; 890 if (help != null && help.getHelpID() != null) { 891 HelpCtx.setHelpIDString(getRootPane(), help.getHelpID()); 893 } 894 if (haveCalledInitializeButtons) initializeButtons(); 896 } 897 } 898 899 901 protected int getOptionsAlign() { 902 return -1; 903 } 904 905 907 protected ActionListener getButtonListener() { 908 return null; 909 } 910 911 913 protected Object [] getClosingOptions() { 914 return null; 915 } 916 917 919 protected HelpCtx getHelpCtx() { 920 return null; 921 } 922 923 924 public void windowDeactivated(final java.awt.event.WindowEvent p1) { 925 } 926 public void windowClosed(final java.awt.event.WindowEvent p1) { 927 } 928 public void windowDeiconified(final java.awt.event.WindowEvent p1) { 929 } 930 public void windowOpened(final java.awt.event.WindowEvent p1) { 931 } 932 public void windowIconified(final java.awt.event.WindowEvent p1) { 933 } 934 public void windowClosing(final java.awt.event.WindowEvent p1) { 935 if (! (descriptor instanceof WizardDescriptor)) { 937 descriptor.setValue(NotifyDescriptor.CLOSED_OPTION); 938 } 939 } 940 public void windowActivated(final java.awt.event.WindowEvent p1) { 941 } 942 943 public static void addChangeListener(ChangeListener l) { 945 synchronized (listeners) { 946 listeners.add(l); 947 } 948 } 949 public static void removeChangeListener(ChangeListener l) { 950 synchronized (listeners) { 951 listeners.remove(l); 952 } 953 } 954 private static void fireChangeEvent() { 955 SwingUtilities.invokeLater(new Runnable () { 956 public void run() { 957 Iterator<ChangeListener > it; 958 synchronized (listeners) { 959 it = new HashSet<ChangeListener >(listeners).iterator(); 960 } 961 ChangeEvent ev = new ChangeEvent(NbPresenter.class); 962 while (it.hasNext()) { 963 it.next().stateChanged(ev); 964 } 965 } 966 }); 967 } 968 969 971 private class ButtonListener implements ActionListener, ComponentListener, PropertyChangeListener { 972 ButtonListener() {} 973 public void actionPerformed(ActionEvent evt) { 974 boolean isAqua = "Aqua".equals (UIManager.getLookAndFeel().getID()) || "true".equalsIgnoreCase (System.getProperty ("xtest.looks_as_mac")); 976 977 Object pressedOption = evt.getSource(); 978 if (ESCAPE_COMMAND.equals (evt.getActionCommand ())) { 980 pressedOption = NotifyDescriptor.CLOSED_OPTION; 981 } else { 982 if (evt.getSource() == stdHelpButton) { 984 org.netbeans.core.NbTopManager.get().showHelp(currentHelp); 985 return; 986 } 987 988 Object [] options = descriptor.getOptions(); 989 if (isAqua && options != null) { 990 Arrays.sort (options, NbPresenter.this); 991 } 992 993 if ( 994 options != null && 995 currentPrimaryButtons != null && 996 options.length == (currentPrimaryButtons.length - 997 ((currentHelp != null) ? 1 : 0)) 998 ) { 999 int offset = currentHelp != null && isAqua ? 1000 -1 : 0; 1001 for (int i = 0; i < currentPrimaryButtons.length; i++) { 1002 if (evt.getSource() == currentPrimaryButtons[i]) { 1003 pressedOption = options[i + offset]; 1004 } 1005 } 1006 } 1007 1008 options = descriptor.getAdditionalOptions(); 1009 if (isAqua && options != null) { 1010 Arrays.sort (options, NbPresenter.this); 1011 } 1012 1013 if ( 1014 options != null && 1015 currentSecondaryButtons != null && 1016 options.length == currentSecondaryButtons.length 1017 ) { 1018 for (int i = 0; i < currentSecondaryButtons.length; i++) { 1019 if (evt.getSource() == currentSecondaryButtons[i]) { 1020 pressedOption = options[i]; 1021 } 1022 } 1023 } 1024 1025 if (evt.getSource() == stdYesButton) { 1026 pressedOption = NotifyDescriptor.YES_OPTION; 1027 } else if (evt.getSource() == stdNoButton) { 1028 pressedOption = NotifyDescriptor.NO_OPTION; 1029 } else if (evt.getSource() == stdCancelButton) { 1030 pressedOption = NotifyDescriptor.CANCEL_OPTION; 1031 } else if (evt.getSource() == stdClosedButton) { 1032 pressedOption = NotifyDescriptor.CLOSED_OPTION; 1033 } else if (evt.getSource() == stdOKButton) { 1034 pressedOption = NotifyDescriptor.OK_OPTION; 1035 } 1036 } 1037 1038 descriptor.setValue(pressedOption); 1039 1040 ActionListener al = getButtonListener(); 1041 if (al != null) { 1042 1043 if (pressedOption == evt.getSource()) { 1044 al.actionPerformed(evt); 1045 } else { 1046 al.actionPerformed(new ActionEvent( 1047 pressedOption, evt.getID(), evt.getActionCommand(), evt.getModifiers() 1048 )); 1049 } 1050 } 1051 1052 Object [] arr = getClosingOptions(); 1053 if (arr == null || pressedOption == NotifyDescriptor.CLOSED_OPTION) { 1054 dispose(); 1056 } else { 1057 java.util.List l = java.util.Arrays.asList(arr); 1058 1059 if (l.contains(pressedOption)) { 1060 dispose(); 1061 } 1062 } 1063 } 1064 public void componentShown(final java.awt.event.ComponentEvent p1) { 1065 updateDefaultButton(); 1066 } 1067 public void componentResized(final java.awt.event.ComponentEvent p1) { 1068 } 1069 1070 public void componentHidden(final java.awt.event.ComponentEvent p1) { 1071 updateDefaultButton(); 1072 } 1073 1074 public void componentMoved(final java.awt.event.ComponentEvent p1) { 1075 } 1076 1077 public void propertyChange(final java.beans.PropertyChangeEvent p1) { 1078 if ("enabled".equals(p1.getPropertyName())) { 1079 updateDefaultButton(); 1080 } 1081 } 1082 } 1083 1084 public javax.accessibility.AccessibleContext getAccessibleContext() { 1085 if (accessibleContext == null) { 1086 accessibleContext = new AccessibleNbPresenter(); 1087 } 1088 return accessibleContext; 1089 } 1090 1091 private static String getMessageTypeDescription(int messageType) { 1092 switch(messageType) { 1093 case NotifyDescriptor.ERROR_MESSAGE: 1094 return NbBundle.getBundle(NbPresenter.class).getString("ACSD_ErrorMessage"); case NotifyDescriptor.WARNING_MESSAGE: 1096 return NbBundle.getBundle(NbPresenter.class).getString("ACSD_WarningMessage"); case NotifyDescriptor.QUESTION_MESSAGE: 1098 return NbBundle.getBundle(NbPresenter.class).getString("ACSD_QuestionMessage"); case NotifyDescriptor.INFORMATION_MESSAGE: 1100 return NbBundle.getBundle(NbPresenter.class).getString("ACSD_InformationMessage"); case NotifyDescriptor.PLAIN_MESSAGE: 1102 return NbBundle.getBundle(NbPresenter.class).getString("ACSD_PlainMessage"); } 1104 return ""; } 1106 1107 private class AccessibleNbPresenter extends AccessibleJDialog { 1108 AccessibleNbPresenter() {} 1109 public String getAccessibleName() { 1110 if (accessibleName != null) { 1111 return accessibleName; 1112 } else { 1113 if (currentMessage instanceof javax.accessibility.Accessible 1114 && currentMessage.getAccessibleContext().getAccessibleName() != null) { 1115 return currentMessage.getAccessibleContext().getAccessibleName(); 1116 } else { 1117 return super.getAccessibleName(); 1118 } 1119 } 1120 } 1121 public String getAccessibleDescription() { 1122 if (accessibleDescription != null) { 1123 return accessibleDescription; 1124 } else { 1125 if (currentMessage instanceof javax.accessibility.Accessible 1126 && currentMessage.getAccessibleContext().getAccessibleDescription() != null) { 1127 return java.text.MessageFormat.format( 1128 getMessageTypeDescription(descriptor.getMessageType()), 1129 new Object [] { 1130 currentMessage.getAccessibleContext().getAccessibleDescription() 1131 } 1132 ); 1133 } else { 1134 return super.getAccessibleDescription(); 1135 } 1136 } 1137 } 1138 } 1139 1140 static Field markers; 1141 static Method dequeue; 1142 static { 1143 if (Boolean.getBoolean("netbeans.hack.50423")) { try { 1145 markers = DefaultKeyboardFocusManager .class.getDeclaredField("typeAheadMarkers"); markers.setAccessible(true); 1147 dequeue = DefaultKeyboardFocusManager .class.getDeclaredMethod("dequeueKeyEvents", new Class [] { Long.TYPE, java.awt.Component .class }); 1148 dequeue.setAccessible(true); 1149 } catch (Throwable ex) { 1150 LOG.log(Level.WARNING, "Not activating workaround for #50423", ex); } 1152 } 1153 } 1154 1155 private final class HackTypeAhead implements Runnable { 1156 private RequestProcessor.Task task = RequestProcessor.getDefault().create(this); 1157 1158 1159 public HackTypeAhead() { 1160 } 1161 1162 public void activate() { 1163 if (markers != null) { 1164 task.schedule(1000); 1165 } 1166 } 1167 1168 public void run() { 1169 if (!SwingUtilities.isEventDispatchThread()) { 1170 SwingUtilities.invokeLater(this); 1171 return; 1172 } 1173 1174 KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 1175 Collection result = null; 1176 try { 1177 result = (Collection) markers.get(fm); 1178 } catch (Exception ex) { 1179 Logger.getLogger(NbPresenter.class.getName()).log(Level.WARNING, null, ex); 1180 } 1181 1182 if (result == null || result.isEmpty()) { 1183 return; 1184 } 1185 1186 LOG.warning("Symptoms of #50423: There is something in type ahead: " + result + " requesting focus change"); try { 1188 dequeue.invoke(fm, new Object [] { Long.valueOf(-1), NbPresenter.this }); 1189 } catch (Exception ex) { 1190 Logger.getLogger(NbPresenter.class.getName()).log(Level.WARNING, null, ex); 1191 } 1192 } 1193 } } 1195 | Popular Tags |