001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.ui;
042:
043: import org.netbeans.modules.profiler.ProfilerIDESettings;
044: import org.netbeans.modules.profiler.utils.IDEUtils;
045: import org.openide.DialogDescriptor;
046: import org.openide.DialogDisplayer;
047: import org.openide.NotifyDescriptor;
048: import org.openide.util.NbBundle;
049: import java.awt.*;
050: import java.awt.event.ComponentAdapter;
051: import java.awt.event.ComponentEvent;
052: import java.awt.event.ComponentListener;
053: import java.util.concurrent.CountDownLatch;
054: import java.util.logging.Level;
055: import java.util.logging.Logger;
056: import javax.swing.*;
057:
058: public final class ProfilerDialogs {
059: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
060:
061: public static final class DNSAConfirmation extends
062: NotifyDescriptor.Confirmation {
063: //~ Instance fields ------------------------------------------------------------------------------------------------------
064:
065: private final String dnsaKey;
066: private String dnsaMessage = DONT_SHOW_AGAIN_MSG;
067: private boolean dnsaDefault = true;
068:
069: //~ Constructors ---------------------------------------------------------------------------------------------------------
070:
071: /**
072: * Create a yes/no/cancel question with default title.
073: *
074: * @param message the message object
075: * @see org.openide.NotifyDescriptor#NotifyDescriptor
076: */
077: public DNSAConfirmation(final String key, final Object message) {
078: super (message);
079: this .dnsaKey = key;
080: }
081:
082: /**
083: * Create a yes/no/cancel question.
084: *
085: * @param message the message object
086: * @param title the dialog title
087: * @see org.openide.NotifyDescriptor#NotifyDescriptor
088: */
089: public DNSAConfirmation(final String key, final Object message,
090: final String title) {
091: super (message, title);
092: this .dnsaKey = key;
093: }
094:
095: /**
096: * Create a question with default title.
097: *
098: * @param message the message object
099: * @param optionType the type of options to display to the user
100: * @see org.openide.NotifyDescriptor#NotifyDescriptor
101: */
102: public DNSAConfirmation(final String key, final Object message,
103: final int optionType) {
104: super (message, optionType);
105: this .dnsaKey = key;
106: }
107:
108: /**
109: * Create a question.
110: *
111: * @param message the message object
112: * @param title the dialog title
113: * @param optionType the type of options to display to the user
114: * @see org.openide.NotifyDescriptor#NotifyDescriptor
115: */
116: public DNSAConfirmation(final String key, final Object message,
117: final String title, final int optionType) {
118: super (message, title, optionType);
119: this .dnsaKey = key;
120: }
121:
122: /**
123: * Create a confirmation with default title.
124: *
125: * @param message the message object
126: * @param optionType the type of options to display to the user
127: * @param messageType the type of message to use
128: * @see org.openide.NotifyDescriptor#NotifyDescriptor
129: */
130: public DNSAConfirmation(final String key, final Object message,
131: final int optionType, final int messageType) {
132: super (message, optionType, messageType);
133: this .dnsaKey = key;
134: }
135:
136: /**
137: * Create a confirmation.
138: *
139: * @param message the message object
140: * @param title the dialog title
141: * @param optionType the type of options to display to the user
142: * @param messageType the type of message to use
143: * @see org.openide.NotifyDescriptor#NotifyDescriptor
144: */
145: public DNSAConfirmation(final String key, final Object message,
146: final String title, final int optionType,
147: final int messageType) {
148: super (message, title, optionType, messageType);
149: this .dnsaKey = key;
150: }
151:
152: //~ Methods --------------------------------------------------------------------------------------------------------------
153:
154: public void setDNSADefault(boolean value) {
155: this .dnsaDefault = value;
156: }
157:
158: public boolean getDNSADefault() {
159: return dnsaDefault;
160: }
161:
162: public String getDNSAKey() {
163: return dnsaKey;
164: }
165:
166: public void setDNSAMessage(String value) {
167: this .dnsaMessage = value;
168: }
169:
170: public String getDNSAMessage() {
171: return dnsaMessage;
172: }
173: }
174:
175: // If No is selected the Don't show again is reset
176: public static final class DNSAConfirmationChecked extends
177: NotifyDescriptor.Confirmation {
178: //~ Instance fields ------------------------------------------------------------------------------------------------------
179:
180: private final String dnsaKey;
181: private String dnsaMessage = DONT_SHOW_AGAIN_MSG;
182: private boolean dnsaDefault = false;
183:
184: //~ Constructors ---------------------------------------------------------------------------------------------------------
185:
186: /**
187: * Create a yes/no/cancel question with default title.
188: *
189: * @param message the message object
190: * @see org.openide.NotifyDescriptor#NotifyDescriptor
191: */
192: public DNSAConfirmationChecked(final String key,
193: final Object message) {
194: super (message);
195: this .dnsaKey = key;
196: }
197:
198: /**
199: * Create a yes/no/cancel question.
200: *
201: * @param message the message object
202: * @param title the dialog title
203: * @see org.openide.NotifyDescriptor#NotifyDescriptor
204: */
205: public DNSAConfirmationChecked(final String key,
206: final Object message, final String title) {
207: super (message, title);
208: this .dnsaKey = key;
209: }
210:
211: /**
212: * Create a question with default title.
213: *
214: * @param message the message object
215: * @param optionType the type of options to display to the user
216: * @see org.openide.NotifyDescriptor#NotifyDescriptor
217: */
218: public DNSAConfirmationChecked(final String key,
219: final Object message, final int optionType) {
220: super (message, optionType);
221: this .dnsaKey = key;
222: }
223:
224: /**
225: * Create a question.
226: *
227: * @param message the message object
228: * @param title the dialog title
229: * @param optionType the type of options to display to the user
230: * @see org.openide.NotifyDescriptor#NotifyDescriptor
231: */
232: public DNSAConfirmationChecked(final String key,
233: final Object message, final String title,
234: final int optionType) {
235: super (message, title, optionType);
236: this .dnsaKey = key;
237: }
238:
239: /**
240: * Create a confirmation with default title.
241: *
242: * @param message the message object
243: * @param optionType the type of options to display to the user
244: * @param messageType the type of message to use
245: * @see org.openide.NotifyDescriptor#NotifyDescriptor
246: */
247: public DNSAConfirmationChecked(final String key,
248: final Object message, final int optionType,
249: final int messageType) {
250: super (message, optionType, messageType);
251: this .dnsaKey = key;
252: }
253:
254: /**
255: * Create a confirmation.
256: *
257: * @param message the message object
258: * @param title the dialog title
259: * @param optionType the type of options to display to the user
260: * @param messageType the type of message to use
261: * @see org.openide.NotifyDescriptor#NotifyDescriptor
262: */
263: public DNSAConfirmationChecked(final String key,
264: final Object message, final String title,
265: final int optionType, final int messageType) {
266: super (message, title, optionType, messageType);
267: this .dnsaKey = key;
268: }
269:
270: //~ Methods --------------------------------------------------------------------------------------------------------------
271:
272: public void setDNSADefault(boolean value) {
273: this .dnsaDefault = value;
274: }
275:
276: public boolean getDNSADefault() {
277: return dnsaDefault;
278: }
279:
280: public String getDNSAKey() {
281: return dnsaKey;
282: }
283:
284: public void setDNSAMessage(String value) {
285: this .dnsaMessage = value;
286: }
287:
288: public String getDNSAMessage() {
289: return dnsaMessage;
290: }
291: }
292:
293: public static final class DNSAMessage extends
294: NotifyDescriptor.Message {
295: //~ Instance fields ------------------------------------------------------------------------------------------------------
296:
297: private final String dnsaKey;
298: private String dnsaMessage = DONT_SHOW_AGAIN_MSG;
299: private boolean dnsaDefault = true;
300:
301: //~ Constructors ---------------------------------------------------------------------------------------------------------
302:
303: /**
304: * Create an informational report about the results of a command.
305: *
306: * @param message the message object
307: * @see org.openide.NotifyDescriptor#NotifyDescriptor
308: */
309: public DNSAMessage(final String key, final Object message) {
310: super (message);
311: this .dnsaKey = key;
312: }
313:
314: /**
315: * Create a report about the results of a command.
316: *
317: * @param message the message object
318: * @param messageType the type of message to be displayed
319: * @see org.openide.NotifyDescriptor#NotifyDescriptor
320: */
321: public DNSAMessage(final String dnsaKey, final Object message,
322: final int messageType) {
323: super (message, messageType);
324: this .dnsaKey = dnsaKey;
325: }
326:
327: //~ Methods --------------------------------------------------------------------------------------------------------------
328:
329: public void setDNSADefault(boolean value) {
330: this .dnsaDefault = value;
331: }
332:
333: public boolean getDNSADefault() {
334: return dnsaDefault;
335: }
336:
337: public String getDNSAKey() {
338: return dnsaKey;
339: }
340:
341: public void setDNSAMessage(String value) {
342: this .dnsaMessage = value;
343: }
344:
345: public String getDNSAMessage() {
346: return dnsaMessage;
347: }
348: }
349:
350: public static final class MessageWithDetails extends
351: DialogDescriptor {
352: //~ Instance fields ------------------------------------------------------------------------------------------------------
353:
354: private JButton showDetailsButton = new JButton(
355: SHOW_DETAILS_BUTTON_TEXT);
356: private Object detailsMsg;
357:
358: //~ Constructors ---------------------------------------------------------------------------------------------------------
359:
360: /**
361: * Create an informational report about the results of a command.
362: *
363: * @param shortMsg the short message object
364: * @param detailsMsg the details message object
365: * @see org.openide.NotifyDescriptor#NotifyDescriptor
366: */
367: public MessageWithDetails(final Object shortMsg,
368: final Object detailsMsg, final boolean respectParent) {
369: super (
370: shortMsg,
371: NotifyDescriptor
372: .getTitleForType(NotifyDescriptor.INFORMATION_MESSAGE));
373: this .detailsMsg = detailsMsg;
374: showDetailsButton.setDefaultCapable(false);
375: setOptions(new Object[] { OK_OPTION });
376: setAdditionalOptions(new Object[] { showDetailsButton });
377: setLeaf(!respectParent);
378: }
379:
380: /**
381: * Create a report about the results of a command.
382: *
383: * @param shortMsg the message object
384: * @param detailsMsg the details message object
385: * @param messageType the type of message to be displayed
386: * @see org.openide.NotifyDescriptor#NotifyDescriptor
387: */
388: public MessageWithDetails(final Object shortMsg,
389: final Object detailsMsg, final int messageType,
390: final boolean respectParent) {
391: super (shortMsg, NotifyDescriptor
392: .getTitleForType(messageType));
393: this .setMessageType(messageType);
394: this .detailsMsg = detailsMsg;
395: showDetailsButton.setDefaultCapable(false);
396: setOptions(new Object[] { OK_OPTION });
397: setAdditionalOptions(new Object[] { showDetailsButton });
398: setLeaf(!respectParent);
399: }
400:
401: //~ Methods --------------------------------------------------------------------------------------------------------------
402:
403: public Object getDetailsMessage() {
404: return detailsMsg;
405: }
406:
407: public Object getShortMessage() {
408: return this .getMessage();
409: }
410:
411: public JButton getShowDetailsButton() {
412: return showDetailsButton;
413: }
414: }
415:
416: private static final class DNSAPanel extends JPanel {
417: //~ Instance fields ------------------------------------------------------------------------------------------------------
418:
419: private final JCheckBox check;
420:
421: //~ Constructors ---------------------------------------------------------------------------------------------------------
422:
423: DNSAPanel(final Object message, final boolean defaultSel,
424: final String dnsaMessage) {
425: Component messageComponent = null;
426:
427: if (message instanceof Component) {
428: messageComponent = (Component) message;
429: } else if (message instanceof String) {
430: messageComponent = new NBHTMLLabel((String) message);
431: messageComponent.setFocusable(false);
432: }
433:
434: setLayout(new BorderLayout(0, 10));
435: check = new JCheckBox(dnsaMessage, defaultSel);
436:
437: if (messageComponent != null) {
438: add(messageComponent, BorderLayout.CENTER);
439: }
440:
441: add(check, BorderLayout.SOUTH);
442: }
443:
444: //~ Methods --------------------------------------------------------------------------------------------------------------
445:
446: public boolean isAutoChecked() {
447: return check.isSelected();
448: }
449: }
450:
451: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
452:
453: private static final Logger LOGGER = Logger
454: .getLogger(ProfilerDialogs.class.getName());
455:
456: // -----
457: // I18N String constants
458: private static final String DONT_SHOW_AGAIN_MSG = NbBundle
459: .getMessage(ProfilerDialogs.class,
460: "ProfilerDialogs_DontShowAgainMsg"); //NOI18N
461: private static final String SHOW_DETAILS_BUTTON_TEXT = NbBundle
462: .getMessage(ProfilerDialogs.class,
463: "ProfilerDialogs_ShowDetailsButtonText"); //NOI18N
464: // -----
465: private static boolean DEBUG = System
466: .getProperty("org.netbeans.modules.profiler.ui.ProfilerDialogs") != null; //NOI18N
467: private static final DialogDisplayer standard = DialogDisplayer
468: .getDefault();
469: private static boolean silent = false;
470:
471: //~ Constructors -------------------------------------------------------------------------------------------------------------
472:
473: private ProfilerDialogs() {
474: } // avoid direct instance creation
475:
476: //~ Methods ------------------------------------------------------------------------------------------------------------------
477:
478: public static void setSilent(boolean value) {
479: silent = value;
480: }
481:
482: public static void close(final Dialog dialog) {
483: final CountDownLatch latch = new CountDownLatch(1);
484:
485: final ComponentListener listener = new ComponentAdapter() {
486: @Override
487: public void componentHidden(ComponentEvent e) {
488: latch.countDown();
489: }
490:
491: @Override
492: public void componentShown(ComponentEvent e) {
493: super .componentShown(e);
494: }
495: };
496:
497: dialog.addComponentListener(listener);
498: IDEUtils.runInEventDispatchThread(new Runnable() {
499: public void run() {
500: dialog.setVisible(false);
501: }
502: });
503:
504: try {
505: latch.await();
506: dialog.removeComponentListener(listener);
507: } catch (InterruptedException e) {
508: }
509: }
510:
511: public static Dialog createDialog(final DialogDescriptor descriptor) {
512: descriptor.setLeaf(true);
513:
514: return standard.createDialog(descriptor);
515: }
516:
517: public static void display(final Dialog dialog) {
518: final CountDownLatch latch = new CountDownLatch(1);
519:
520: final ComponentListener listener = new ComponentAdapter() {
521: @Override
522: public void componentHidden(ComponentEvent e) {
523: super .componentHidden(e);
524: }
525:
526: @Override
527: public void componentShown(ComponentEvent e) {
528: latch.countDown();
529: }
530: };
531:
532: dialog.addComponentListener(listener);
533: IDEUtils.runInEventDispatchThread(new Runnable() {
534: public void run() {
535: dialog.setVisible(true);
536: }
537: });
538:
539: try {
540: latch.await();
541: dialog.removeComponentListener(listener);
542: } catch (InterruptedException e) {
543: }
544: }
545:
546: public static Object notify(final NotifyDescriptor descriptor) {
547: if (silent) {
548: if (LOGGER.isLoggable(Level.FINEST)) {
549: LOGGER.finest("Silently closed message: "
550: + descriptor.getMessage()); //NOI18N
551: }
552:
553: return NotifyDescriptor.CLOSED_OPTION;
554: }
555:
556: if (LOGGER.isLoggable(Level.FINEST)) {
557: LOGGER.finest("notify: " + descriptor.getClass().getName()); //NOI18N
558: }
559:
560: if (descriptor instanceof DNSAMessage) {
561: final DNSAMessage dm = (DNSAMessage) descriptor;
562: Object autoAccept = stringToNDOption(ProfilerIDESettings
563: .getInstance().getDoNotShowAgain(dm.getDNSAKey()));
564:
565: if (LOGGER.isLoggable(Level.FINEST)) {
566: LOGGER.finest("DNSAMessage key: " + dm.getDNSAKey()
567: + ", autoAccept: " + autoAccept); //NOI18N
568: }
569:
570: if (autoAccept != null) {
571: return autoAccept;
572: }
573:
574: final DNSAPanel dp = new DNSAPanel(dm.getMessage(), dm
575: .getDNSADefault(), dm.getDNSAMessage());
576: dm.setMessage(dp);
577:
578: final Object ret = standard.notify(descriptor);
579:
580: if ((ret != DialogDescriptor.CANCEL_OPTION)
581: && (ret != DialogDescriptor.CLOSED_OPTION)) {
582: if (dp.isAutoChecked()) {
583: if (LOGGER.isLoggable(Level.FINEST)) {
584: LOGGER
585: .finest("DNSAMessage key: "
586: + dm.getDNSAKey()
587: + ", setting autoAccept: "
588: + autoAccept); //NOI18N
589: }
590:
591: ProfilerIDESettings.getInstance()
592: .setDoNotShowAgain(dm.getDNSAKey(),
593: ndOptionToString(ret));
594: }
595: }
596:
597: return ret;
598: } else if (descriptor instanceof DNSAConfirmation) {
599: final DNSAConfirmation dm = (DNSAConfirmation) descriptor;
600: Object autoAccept = stringToNDOption(ProfilerIDESettings
601: .getInstance().getDoNotShowAgain(dm.getDNSAKey()));
602:
603: if (LOGGER.isLoggable(Level.FINEST)) {
604: LOGGER.finest("DNSAConfirmation key: "
605: + dm.getDNSAKey() + ", autoAccept: "
606: + autoAccept); //NOI18N
607: }
608:
609: if (autoAccept != null) {
610: return autoAccept;
611: }
612:
613: final DNSAPanel dp = new DNSAPanel(dm.getMessage(), dm
614: .getDNSADefault(), dm.getDNSAMessage());
615: dm.setMessage(dp);
616:
617: final Object ret = standard.notify(descriptor);
618:
619: if ((ret != DialogDescriptor.CANCEL_OPTION)
620: && (ret != DialogDescriptor.CLOSED_OPTION)) {
621: if (dp.isAutoChecked()) {
622: if (LOGGER.isLoggable(Level.FINEST)) {
623: LOGGER
624: .finest("ProfilerDialogs: DNSAConfirmation key: "
625: + dm.getDNSAKey()
626: + ", setting autoAccept: "
627: + autoAccept); //NOI18N
628: }
629:
630: ProfilerIDESettings.getInstance()
631: .setDoNotShowAgain(dm.getDNSAKey(),
632: ndOptionToString(ret));
633: }
634: }
635:
636: return ret;
637: } else if (descriptor instanceof DNSAConfirmationChecked) {
638: final DNSAConfirmationChecked dm = (DNSAConfirmationChecked) descriptor;
639: Object autoAccept = stringToNDOption(ProfilerIDESettings
640: .getInstance().getDoNotShowAgain(dm.getDNSAKey()));
641:
642: if (LOGGER.isLoggable(Level.FINEST)) {
643: LOGGER.finest("DNSAConfirmationChecked key: "
644: + dm.getDNSAKey() + ", autoAccept: "
645: + autoAccept); //NOI18N
646: }
647:
648: if (autoAccept != null) {
649: return autoAccept;
650: }
651:
652: final DNSAPanel dp = new DNSAPanel(dm.getMessage(), dm
653: .getDNSADefault(), dm.getDNSAMessage());
654: dm.setMessage(dp);
655:
656: final Object ret = standard.notify(descriptor);
657:
658: if ((ret != DialogDescriptor.CANCEL_OPTION)
659: && (ret != DialogDescriptor.CLOSED_OPTION)) {
660: if (dp.isAutoChecked()
661: && (ret != DialogDescriptor.NO_OPTION)) {
662: if (LOGGER.isLoggable(Level.FINEST)) {
663: LOGGER
664: .finest("ProfilerDialogs: DNSAConfirmationChecked key: "
665: + dm.getDNSAKey()
666: + ", setting autoAccept: "
667: + autoAccept); //NOI18N
668: }
669:
670: ProfilerIDESettings.getInstance()
671: .setDoNotShowAgain(dm.getDNSAKey(),
672: ndOptionToString(ret));
673: }
674: }
675:
676: return ret;
677: } else if (descriptor instanceof MessageWithDetails) {
678: if (LOGGER.isLoggable(Level.FINEST)) {
679: LOGGER.finest("MessageWithDetails message:"
680: + ((MessageWithDetails) descriptor)
681: .getDetailsMessage()); //NOI18N
682: }
683:
684: final MessageWithDetails dm = (MessageWithDetails) descriptor;
685: Object returnObj = standard.notify(dm);
686:
687: if (returnObj == dm.getShowDetailsButton()) {
688: returnObj = standard
689: .notify(new NotifyDescriptor.Message(dm
690: .getDetailsMessage(), dm
691: .getMessageType()));
692: }
693:
694: return returnObj;
695: } else {
696: return standard.notify(descriptor);
697: }
698: }
699:
700: // Maps org.openide.NotifyDescriptor option to String
701: private static String ndOptionToString(Object option) {
702: if (option == NotifyDescriptor.CANCEL_OPTION) {
703: return "CANCEL_OPTION"; // NOI18N
704: } else if (option == NotifyDescriptor.CLOSED_OPTION) {
705: return "CLOSED_OPTION"; // NOI18N
706: } else if (option == NotifyDescriptor.NO_OPTION) {
707: return "NO_OPTION"; // NOI18N
708: } else if (option == NotifyDescriptor.OK_OPTION) {
709: return "OK_OPTION"; // NOI18N
710: } else if (option == NotifyDescriptor.YES_OPTION) {
711: return "YES_OPTION"; // NOI18N
712: }
713:
714: return null;
715: }
716:
717: // Maps String to org.openide.NotifyDescriptor option
718: private static Object stringToNDOption(String string) {
719: if (string == null) {
720: return null;
721: }
722:
723: if (string.equals("CANCEL_OPTION")) { //NOI18N
724:
725: return NotifyDescriptor.CANCEL_OPTION;
726: } else if (string.equals("CLOSED_OPTION")) { //NOI18N
727:
728: return NotifyDescriptor.CLOSED_OPTION;
729: } else if (string.equals("NO_OPTION")) { //NOI18N
730:
731: return NotifyDescriptor.NO_OPTION;
732: } else if (string.equals("OK_OPTION")) { //NOI18N
733:
734: return NotifyDescriptor.OK_OPTION;
735: } else if (string.equals("YES_OPTION")) { //NOI18N
736:
737: return NotifyDescriptor.YES_OPTION;
738: }
739:
740: return null;
741: }
742: }
|