1 19 20 21 package org.netbeans.modules.options; 22 23 import java.awt.Component ; 24 import java.awt.Dialog ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.WindowEvent ; 28 import java.awt.event.WindowListener ; 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.lang.ref.WeakReference ; 32 import java.util.logging.Logger ; 33 import javax.swing.AbstractButton ; 34 import javax.swing.JButton ; 35 import javax.swing.JLabel ; 36 import javax.swing.SwingUtilities ; 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 56 private static Dialog dialog; 57 58 private static WeakReference <DialogDescriptor> descriptorRef = new WeakReference <DialogDescriptor> (null); 59 private static String title = loc("CTL_Options_Dialog_Title"); 60 private static Logger 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 categoryID) { 72 if (isOpen()) { 73 dialog.setVisible (true); 75 dialog.toFront (); 76 log.fine("Front Options Dialog"); 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 bOK = (JButton ) loc(new JButton (), "CTL_OK"); JButton bClassic = (JButton ) loc(new JButton (), "CTL_Classic"); boolean isMac = Utilities.isMac(); 89 Object [] options = new Object [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 [] {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 <DialogDescriptor>(descriptor); 99 log.fine("Create new Options Dialog"); } else { 101 optionsPanel = (OptionsPanel) descriptor.getMessage (); 102 optionsPanel.update(); 105 log.fine("Reopen Options Dialog"); } 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 loc (String key) { 115 return NbBundle.getMessage (OptionsDisplayerImpl.class, key); 116 } 117 118 private static Component loc (Component c, String key) { 119 if (c instanceof AbstractButton ) 120 Mnemonics.setLocalizedText ( 121 (AbstractButton ) c, 122 loc (key) 123 ); 124 else 125 Mnemonics.setLocalizedText ( 126 (JLabel ) c, 127 loc (key) 128 ); 129 return c; 130 } 131 132 private class OptionsPanelListener implements PropertyChangeListener , 133 ActionListener { 134 private DialogDescriptor descriptor; 135 private OptionsPanel optionsPanel; 136 private JButton bOK; 137 private JButton bClassic; 138 139 140 OptionsPanelListener ( 141 DialogDescriptor descriptor, 142 OptionsPanel optionsPanel, 143 JButton bOK, 144 JButton 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 ev) { 153 if (ev.getPropertyName ().equals ("buran" + OptionsPanelController.PROP_HELP_CTX)) { descriptor.setHelpCtx (optionsPanel.getHelpCtx ()); 155 } else if (ev.getPropertyName ().equals ("buran" + OptionsPanelController.PROP_VALID)) { bOK.setEnabled (optionsPanel.dataValid ()); 157 } 158 } 159 160 @SuppressWarnings ("unchecked") 161 public void actionPerformed (ActionEvent e) { 162 if (!isOpen()) return; if (e.getSource () == bOK) { 165 log.fine("Options Dialog - Ok pressed."); Dialog d = dialog; 167 dialog = null; 168 d.dispose (); 169 RequestProcessor.getDefault ().post (new Runnable () { 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."); Dialog d = dialog; 180 dialog = null; 181 d.dispose (); 182 RequestProcessor.getDefault ().post (new Runnable () { 183 public void run () { 184 optionsPanel.cancel (); 185 } 186 }); 187 } else 188 if (e.getSource () == bClassic) { 189 log.fine("Options Dialog - Classic pressed."); Dialog 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 result = DialogDisplayer.getDefault (). 199 notify (descriptor); 200 if (result == NotifyDescriptor.YES_OPTION) { 201 d.dispose (); 202 RequestProcessor.getDefault ().post (new Runnable () { 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 () { 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 () { 222 public void run () { 223 optionsPanel.cancel (); 224 } 225 }); 226 } 227 try { 228 ClassLoader cl = (ClassLoader ) Lookup.getDefault ().lookup (ClassLoader .class); 229 Class <CallableSystemAction> clz = (Class <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 ex) { 237 Exceptions.printStackTrace(ex); 238 } 239 } } 241 } 242 243 private class MyWindowListener implements WindowListener { 244 private OptionsPanel optionsPanel; 245 private Dialog originalDialog; 246 247 248 MyWindowListener (OptionsPanel optionsPanel) { 249 this.optionsPanel = optionsPanel; 250 this.originalDialog = dialog; 251 } 252 253 public void windowClosing (WindowEvent e) { 254 if (dialog == null) return; 255 log.fine("Options Dialog - windowClosing "); RequestProcessor.getDefault ().post (new Runnable () { 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 e) { 267 optionsPanel.storeUserSize(); 268 if (optionsPanel.needsReinit()) { 269 descriptorRef = new WeakReference <DialogDescriptor>(null); 270 } 271 if (this.originalDialog == dialog) { 272 dialog = null; 273 } 274 log.fine("Options Dialog - windowClosed"); } 276 public void windowDeactivated (WindowEvent e) {} 277 public void windowOpened (WindowEvent e) {} 278 public void windowIconified (WindowEvent e) {} 279 public void windowDeiconified (WindowEvent e) {} 280 public void windowActivated (WindowEvent e) {} 281 } 282 283 class OpenOptionsListener implements ActionListener { 284 public void actionPerformed(ActionEvent e) { 285 RequestProcessor.getDefault().post(new Runnable () { 286 public void run() { 287 SwingUtilities.invokeLater(new Runnable () { 288 public void run() { 289 log.fine("Options Dialog - Back to modern."); OptionsDisplayer.getDefault().open(); 292 } 293 }); 294 } 295 }); 296 } 297 } 298 } 299 300 | Popular Tags |