KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > OptionsDisplayerImpl


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
21 package org.netbeans.modules.options;
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.WindowEvent JavaDoc;
28 import java.awt.event.WindowListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.lang.ref.WeakReference JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import javax.swing.AbstractButton JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import org.netbeans.api.options.OptionsDisplayer;
38 import org.netbeans.spi.options.OptionsPanelController;
39
40 import org.openide.DialogDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.NotifyDescriptor;
43 import org.openide.NotifyDescriptor.Confirmation;
44 import org.openide.awt.Mnemonics;
45 import org.openide.util.Exceptions;
46 import org.openide.util.Lookup;
47 import org.openide.util.NbBundle;
48 import org.openide.util.RequestProcessor;
49 import org.openide.util.Utilities;
50 import org.openide.util.actions.CallableSystemAction;
51 import org.openide.util.actions.SystemAction;
52
53
54 public class OptionsDisplayerImpl {
55     /** Link to dialog, if its opened. */
56     private static Dialog JavaDoc dialog;
57     /** weak link to options dialog DialogDescriptor. */
58     private static WeakReference JavaDoc<DialogDescriptor> descriptorRef = new WeakReference JavaDoc<DialogDescriptor> (null);
59     private static String JavaDoc title = loc("CTL_Options_Dialog_Title");
60     private static Logger JavaDoc log = Logger.getLogger(OptionsDisplayerImpl.class.getName ());
61     private boolean modal;
62     
63     public OptionsDisplayerImpl (boolean modal) {
64         this.modal = modal;
65     }
66     
67     public boolean isOpen() {
68         return dialog != null;
69     }
70     
71     public void showOptionsDialog (String JavaDoc categoryID) {
72         if (isOpen()) {
73             // dialog already opened
74
dialog.setVisible (true);
75             dialog.toFront ();
76             log.fine("Front Options Dialog"); //NOI18N
77
return;
78         }
79                 
80         DialogDescriptor descriptor = (DialogDescriptor)
81             descriptorRef.get ();
82         
83         OptionsPanel optionsPanel = null;
84         if (descriptor == null) {
85             optionsPanel = categoryID == null ? new OptionsPanel () : new OptionsPanel(categoryID);
86             JButton JavaDoc bOK = (JButton JavaDoc) loc(new JButton JavaDoc(), "CTL_OK");//NOI18N
87
JButton JavaDoc bClassic = (JButton JavaDoc) loc(new JButton JavaDoc(), "CTL_Classic");//NOI18N
88
boolean isMac = Utilities.isMac();
89             Object JavaDoc[] options = new Object JavaDoc[2];
90             options[0] = isMac ? DialogDescriptor.CANCEL_OPTION : bOK;
91             options[1] = isMac ? bOK : DialogDescriptor.CANCEL_OPTION;
92             descriptor = new DialogDescriptor(optionsPanel,title,modal,options,DialogDescriptor.OK_OPTION,DialogDescriptor.DEFAULT_ALIGN, null, null);
93             descriptor.setAdditionalOptions(new Object JavaDoc[] {bClassic});
94             descriptor.setHelpCtx(optionsPanel.getHelpCtx());
95             OptionsPanelListener listener = new OptionsPanelListener(descriptor, optionsPanel, bOK, bClassic);
96             descriptor.setButtonListener(listener);
97             optionsPanel.addPropertyChangeListener(listener);
98             descriptorRef = new WeakReference JavaDoc<DialogDescriptor>(descriptor);
99             log.fine("Create new Options Dialog"); //NOI18N
100
} else {
101             optionsPanel = (OptionsPanel) descriptor.getMessage ();
102             //TODO:
103
//just in case that switched from advanced
104
optionsPanel.update();
105             log.fine("Reopen Options Dialog"); //NOI18N
106
}
107         
108         dialog = DialogDisplayer.getDefault ().createDialog (descriptor);
109         optionsPanel.initCurrentCategory(categoryID);
110         dialog.addWindowListener (new MyWindowListener (optionsPanel));
111         dialog.setVisible (true);
112     }
113     
114     private static String JavaDoc loc (String JavaDoc key) {
115         return NbBundle.getMessage (OptionsDisplayerImpl.class, key);
116     }
117     
118     private static Component JavaDoc loc (Component JavaDoc c, String JavaDoc key) {
119         if (c instanceof AbstractButton JavaDoc)
120             Mnemonics.setLocalizedText (
121                 (AbstractButton JavaDoc) c,
122                 loc (key)
123             );
124         else
125             Mnemonics.setLocalizedText (
126                 (JLabel JavaDoc) c,
127                 loc (key)
128             );
129         return c;
130     }
131     
132     private class OptionsPanelListener implements PropertyChangeListener JavaDoc,
133     ActionListener JavaDoc {
134         private DialogDescriptor descriptor;
135         private OptionsPanel optionsPanel;
136         private JButton JavaDoc bOK;
137         private JButton JavaDoc bClassic;
138         
139         
140         OptionsPanelListener (
141             DialogDescriptor descriptor,
142             OptionsPanel optionsPanel,
143             JButton JavaDoc bOK,
144             JButton JavaDoc bClassic
145         ) {
146             this.descriptor = descriptor;
147             this.optionsPanel = optionsPanel;
148             this.bOK = bOK;
149             this.bClassic = bClassic;
150         }
151         
152         public void propertyChange (PropertyChangeEvent JavaDoc ev) {
153             if (ev.getPropertyName ().equals ("buran" + OptionsPanelController.PROP_HELP_CTX)) { //NOI18N
154
descriptor.setHelpCtx (optionsPanel.getHelpCtx ());
155             } else if (ev.getPropertyName ().equals ("buran" + OptionsPanelController.PROP_VALID)) { //NOI18N
156
bOK.setEnabled (optionsPanel.dataValid ());
157             }
158         }
159         
160         @SuppressWarnings JavaDoc("unchecked")
161         public void actionPerformed (ActionEvent JavaDoc e) {
162             if (!isOpen()) return; //WORKARROUND for some bug in NbPresenter
163
// listener is called twice ...
164
if (e.getSource () == bOK) {
165                 log.fine("Options Dialog - Ok pressed."); //NOI18N
166
Dialog JavaDoc d = dialog;
167                 dialog = null;
168                 d.dispose ();
169                 RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
170                    public void run () {
171                         optionsPanel.save ();
172                    }
173                 });
174             } else
175             if (e.getSource () == DialogDescriptor.CANCEL_OPTION ||
176                 e.getSource () == DialogDescriptor.CLOSED_OPTION
177             ) {
178                 log.fine("Options Dialog - Cancel pressed."); //NOI18N
179
Dialog JavaDoc d = dialog;
180                 dialog = null;
181                 d.dispose ();
182                 RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
183                    public void run () {
184                         optionsPanel.cancel ();
185                    }
186                 });
187             } else
188             if (e.getSource () == bClassic) {
189                 log.fine("Options Dialog - Classic pressed."); //NOI18N
190
Dialog JavaDoc d = dialog;
191                 dialog = null;
192                 if (optionsPanel.isChanged ()) {
193                     Confirmation descriptor = new Confirmation (
194                         loc ("CTL_Some_values_changed"),
195                         NotifyDescriptor.YES_NO_CANCEL_OPTION,
196                         NotifyDescriptor.QUESTION_MESSAGE
197                     );
198                     Object JavaDoc result = DialogDisplayer.getDefault ().
199                         notify (descriptor);
200                     if (result == NotifyDescriptor.YES_OPTION) {
201                         d.dispose ();
202                         RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
203                            public void run () {
204                                 optionsPanel.save ();
205                            }
206                         });
207                     } else
208                     if (result == NotifyDescriptor.NO_OPTION) {
209                         d.dispose ();
210                         RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
211                            public void run () {
212                                 optionsPanel.cancel ();
213                            }
214                         });
215                     } else {
216                         dialog = d;
217                         return;
218                     }
219                 } else {
220                     d.dispose ();
221                     RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
222                        public void run () {
223                             optionsPanel.cancel ();
224                        }
225                     });
226                 }
227                 try {
228                     ClassLoader JavaDoc cl = (ClassLoader JavaDoc) Lookup.getDefault ().lookup (ClassLoader JavaDoc.class);
229                     Class JavaDoc<CallableSystemAction> clz = (Class JavaDoc<CallableSystemAction>)cl.loadClass("org.netbeans.core.actions.OptionsAction");
230                     CallableSystemAction a = SystemAction.findObject(clz, true);
231                     a.putValue ("additionalActionName", loc ("CTL_Modern"));
232                     a.putValue ("optionsDialogTitle", loc ("CTL_Classic_Title"));
233                     a.putValue ("additionalActionListener", new OpenOptionsListener ()
234                     );
235                     a.performAction ();
236                 } catch (Exception JavaDoc ex) {
237                     Exceptions.printStackTrace(ex);
238                 }
239             } // classic
240
}
241     }
242     
243     private class MyWindowListener implements WindowListener JavaDoc {
244         private OptionsPanel optionsPanel;
245         private Dialog JavaDoc originalDialog;
246         
247                 
248         MyWindowListener (OptionsPanel optionsPanel) {
249             this.optionsPanel = optionsPanel;
250             this.originalDialog = dialog;
251         }
252         
253         public void windowClosing (WindowEvent JavaDoc e) {
254             if (dialog == null) return;
255             log.fine("Options Dialog - windowClosing "); //NOI18N
256
RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
257                public void run () {
258                     optionsPanel.cancel ();
259                }
260             });
261             if (this.originalDialog == dialog) {
262                 dialog = null;
263             }
264         }
265
266         public void windowClosed(WindowEvent JavaDoc e) {
267             optionsPanel.storeUserSize();
268             if (optionsPanel.needsReinit()) {
269                 descriptorRef = new WeakReference JavaDoc<DialogDescriptor>(null);
270             }
271             if (this.originalDialog == dialog) {
272                 dialog = null;
273             }
274             log.fine("Options Dialog - windowClosed"); //NOI18N
275
}
276         public void windowDeactivated (WindowEvent JavaDoc e) {}
277         public void windowOpened (WindowEvent JavaDoc e) {}
278         public void windowIconified (WindowEvent JavaDoc e) {}
279         public void windowDeiconified (WindowEvent JavaDoc e) {}
280         public void windowActivated (WindowEvent JavaDoc e) {}
281     }
282     
283     class OpenOptionsListener implements ActionListener JavaDoc {
284         public void actionPerformed(ActionEvent JavaDoc e) {
285             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
286                 public void run() {
287                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
288                         public void run() {
289                             log.fine("Options Dialog - Back to modern."); //NOI18N
290
//OptionsDisplayerImpl.this.showOptionsDialog(null);
291
OptionsDisplayer.getDefault().open();
292                         }
293                     });
294                 }
295             });
296         }
297     }
298 }
299
300
Popular Tags