KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > Notification


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.autoupdate;
21
22 import java.net.URL JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
29
30 import org.openide.util.NbBundle;
31 import org.openide.DialogDescriptor;
32 import org.openide.DialogDisplayer;
33 import org.openide.awt.HtmlBrowser;
34
35 /** This class performs the notification if found in the XML file
36  *
37  * @author Petr Hrebejk
38  */

39 class Notification extends Object JavaDoc {
40
41     /** Module notification accepted */
42     private static boolean accepted = false;
43     
44     /** The Notification dialog */
45     private static java.awt.Dialog JavaDoc dialog = null;
46     
47     /** This class is a singleton */
48     private Notification() {
49     }
50
51     /** Tests whether XML file contains notification tag. If so opens modal
52     * dialog with the notification.
53     * @return True if there was a notification, false if not.
54     */

55
56     static boolean performNotification( Updates updates, AutoupdateType at ) {
57
58         final String JavaDoc text = updates.getNotificationText();
59         final URL JavaDoc url = updates.getNotificationURL();
60
61         if ( text == null ) {
62             return false;
63         }
64
65         // impl #24786, remeber and check last showed notification
66
Settings settings = Settings.getShared ();
67         if (settings.getAcceptedNotifications () == null) {
68             settings.setAcceptedNotifications (new HashMap JavaDoc ());
69         }
70         Map JavaDoc mapNotifications = settings.getAcceptedNotifications ();
71         Integer JavaDoc lastNotificationId = (Integer JavaDoc)mapNotifications.get (new Integer JavaDoc (at.getName ().hashCode ()));
72         if (lastNotificationId != null && lastNotificationId.intValue () == text.hashCode ()) {
73             // message have been showed previously
74
return false;
75         } else {
76             mapNotifications.put (new Integer JavaDoc (at.getName ().hashCode ()), new Integer JavaDoc (text.hashCode ()));
77             settings.setAcceptedNotifications (mapNotifications);
78         }
79
80         final JButton JavaDoc closeButton = new JButton JavaDoc (
81                                         getBundle("CTL_Notification_Close")
82                                     );
83         closeButton.getAccessibleContext ().setAccessibleName (getBundle("ACS_Notification_Close"));
84         final JButton JavaDoc urlButton = new JButton JavaDoc (
85                                       getBundle("CTL_Notification_URL")
86                                   );
87         urlButton.getAccessibleContext ().setAccessibleName (getBundle("ACS_Notification_URL"));
88
89         JOptionPane JavaDoc pane = new JOptionPane JavaDoc (
90                                text,
91                                JOptionPane.INFORMATION_MESSAGE,
92                                JOptionPane.DEFAULT_OPTION
93                            );
94
95         pane.setOptions (new Object JavaDoc[] {});
96         pane.getAccessibleContext ().setAccessibleName (getBundle( "ACS_Notification_Title" ));
97
98         DialogDescriptor dd = new DialogDescriptor (
99                                   pane,
100                                   getBundle( "CTL_Notification_Title" ),
101                                   true,
102                                   DialogDescriptor.DEFAULT_OPTION,
103                                   DialogDescriptor.OK_OPTION,
104                                   new ActionListener JavaDoc () {
105                                       public void actionPerformed (ActionEvent JavaDoc ev) {
106                                           /*
107                                           dialog.setVisible (false);
108                                           dialog.dispose ();
109                                           dialog = null;
110                                           */

111                                           if (ev.getSource () == urlButton ) {
112                                               // display www browser
113
if ( url != null ) {
114                                                   javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
115                                                                                               public void run() {
116                                                                                                   HtmlBrowser.URLDisplayer.getDefault ().showURL( url );
117                                                                                               }
118                                                                                           } );
119                                               }
120                                           }
121                                       }
122                                   }
123                               );
124
125         dd.setOptions( url != null ? new Object JavaDoc[] {closeButton, urlButton} :
126                        new Object JavaDoc[] {closeButton} );
127         dd.setClosingOptions( null );
128         dialog = DialogDisplayer.getDefault().createDialog( dd );
129         dialog.setVisible(true);
130         return true;
131     }
132
133     static boolean performModuleNotification( String JavaDoc text ) {
134
135         if ( text == null ) {
136             return false;
137         }
138
139         final JButton JavaDoc cancelButton = new JButton JavaDoc (
140                                         getBundle("CTL_Notification_Cancel")
141                                     );
142         cancelButton.getAccessibleContext ().setAccessibleName (getBundle("ACS_Notification_Cancel"));
143         
144         final JButton JavaDoc okButton = new JButton JavaDoc (
145                                       getBundle("CTL_Notification_OK")
146                                   );
147         okButton.getAccessibleContext ().setAccessibleName (getBundle("ACS_Notification_OK"));
148
149         JOptionPane JavaDoc pane = new JOptionPane JavaDoc (
150                                text,
151                                JOptionPane.INFORMATION_MESSAGE,
152                                JOptionPane.DEFAULT_OPTION
153                            );
154         pane.getAccessibleContext ().setAccessibleName (getBundle( "ACS_Notification_Title" ));
155
156         pane.setOptions (new Object JavaDoc[] {});
157
158         DialogDescriptor dd = new DialogDescriptor (
159                                   pane,
160                                   getBundle( "CTL_Notification_Title" ),
161                                   true,
162                                   DialogDescriptor.DEFAULT_OPTION,
163                                   DialogDescriptor.OK_OPTION,
164                                   new ActionListener JavaDoc () {
165                                       public void actionPerformed (ActionEvent JavaDoc ev) {
166                                           if ( ev.getSource() == okButton )
167                                               accepted = true;
168                                           else
169                                               accepted = false;
170
171                                           dialog.setVisible( false );
172                                       }
173                                   }
174                               );
175
176         dd.setOptions( new Object JavaDoc[] {okButton, cancelButton} );
177         dd.setClosingOptions( null );
178         dialog = DialogDisplayer.getDefault().createDialog( dd );
179         accepted = false;
180         dialog.setVisible(true);
181
182         return accepted;
183     }
184     
185     private static String JavaDoc getBundle( String JavaDoc key ) {
186         return NbBundle.getMessage( Notification.class, key );
187     }
188 }
189
Popular Tags