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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.uiapi;
043:
044: import java.awt.Dialog;
045: import java.awt.Dimension;
046: import java.awt.Frame;
047: import java.awt.Rectangle;
048: import java.awt.event.ActionEvent;
049: import java.awt.event.ActionListener;
050: import java.awt.event.WindowAdapter;
051: import java.awt.event.WindowEvent;
052: import java.beans.PropertyChangeEvent;
053: import java.beans.PropertyChangeListener;
054: import java.io.IOException;
055: import java.util.Arrays;
056: import java.util.LinkedList;
057: import java.util.List;
058: import javax.swing.JButton;
059: import javax.swing.JComponent;
060: import javax.swing.JDialog;
061: import javax.swing.SwingUtilities;
062: import org.netbeans.api.progress.ProgressHandle;
063: import org.netbeans.api.progress.ProgressHandleFactory;
064: import org.netbeans.api.project.Project;
065: import org.netbeans.api.project.ProjectManager;
066: import org.netbeans.spi.project.ui.support.ProjectCustomizer;
067: import org.openide.DialogDescriptor;
068: import org.openide.DialogDisplayer;
069: import org.openide.util.Exceptions;
070: import org.openide.util.HelpCtx;
071: import org.openide.util.Lookup;
072: import org.openide.util.Mutex;
073: import org.openide.util.NbBundle;
074: import org.openide.util.RequestProcessor;
075: import org.openide.windows.WindowManager;
076:
077: /** Implementation of standard customizer dialog.
078: *
079: * @author Petr Hrebejk
080: */
081: public class CustomizerDialog {
082:
083: /** Factory class only
084: */
085: private CustomizerDialog() {
086: }
087:
088: // Option indexes
089: private static final int OPTION_OK = 0;
090: private static final int OPTION_CANCEL = OPTION_OK + 1;
091:
092: // Option command names
093: private static final String COMMAND_OK = "OK"; // NOI18N
094: private static final String COMMAND_CANCEL = "CANCEL"; // NOI18N
095:
096: public static Dialog createDialog(ActionListener okOptionListener,
097: ActionListener storeListener,
098: final CustomizerPane innerPane,
099: HelpCtx helpCtx,
100: final ProjectCustomizer.Category[] categories,
101: //#97998 related
102: ProjectCustomizer.CategoryComponentProvider componentProvider) {
103:
104: ListeningButton okButton = new ListeningButton(NbBundle
105: .getMessage(CustomizerDialog.class,
106: "LBL_Customizer_Ok_Option"), // NOI18N
107: categories);
108: okButton.setEnabled(CustomizerDialog.checkValidity(categories));
109:
110: // Create options
111: JButton options[] = {
112: okButton,
113: new JButton(NbBundle.getMessage(CustomizerDialog.class,
114: "LBL_Customizer_Cancel_Option")), // NOI18N
115: };
116:
117: // Set commands
118: options[OPTION_OK].setActionCommand(COMMAND_OK);
119: options[OPTION_CANCEL].setActionCommand(COMMAND_CANCEL);
120:
121: //A11Y
122: options[OPTION_OK].getAccessibleContext()
123: .setAccessibleDescription(
124: NbBundle.getMessage(CustomizerDialog.class,
125: "AD_Customizer_Ok_Option")); // NOI18N
126: options[OPTION_CANCEL].getAccessibleContext()
127: .setAccessibleDescription(
128: NbBundle.getMessage(CustomizerDialog.class,
129: "AD_Customizer_Cancel_Option")); // NOI18N
130:
131: // RegisterListener
132: ActionListener optionsListener = new OptionListener(
133: okOptionListener, storeListener, categories,
134: componentProvider);
135: options[OPTION_OK].addActionListener(optionsListener);
136: options[OPTION_CANCEL].addActionListener(optionsListener);
137:
138: innerPane.getAccessibleContext().setAccessibleName(
139: NbBundle.getMessage(CustomizerDialog.class,
140: "AN_ProjectCustomizer")); //NOI18N
141: innerPane.getAccessibleContext().setAccessibleDescription(
142: NbBundle.getMessage(CustomizerDialog.class,
143: "AD_ProjectCustomizer")); //NOI18N
144:
145: if (helpCtx == null) {
146: helpCtx = HelpCtx.DEFAULT_HELP;
147: }
148:
149: DialogDescriptor dialogDescriptor = new DialogDescriptor(
150: innerPane, // innerPane
151: NbBundle.getMessage(CustomizerDialog.class,
152: "LBL_Customizer_Title"), // NOI18N // displayName
153: false, // modal
154: options, // options
155: options[OPTION_OK], // initial value
156: DialogDescriptor.BOTTOM_ALIGN, // options align
157: helpCtx, // helpCtx
158: null); // listener
159:
160: innerPane.addPropertyChangeListener(new HelpCtxChangeListener(
161: dialogDescriptor, helpCtx));
162: if (innerPane instanceof HelpCtx.Provider) {
163: dialogDescriptor.setHelpCtx(((HelpCtx.Provider) innerPane)
164: .getHelpCtx());
165: }
166: dialogDescriptor.setClosingOptions(new Object[] {
167: options[OPTION_OK], options[OPTION_CANCEL] });
168:
169: Dialog dialog = DialogDisplayer.getDefault().createDialog(
170: dialogDescriptor);
171:
172: dialog.addWindowListener(new WindowAdapter() {
173: public void windowClosed(WindowEvent e) {
174: innerPane.clearPanelComponentCache();
175: List<ProjectCustomizer.Category> queue = new LinkedList<ProjectCustomizer.Category>(
176: Arrays.asList(categories));
177:
178: while (!queue.isEmpty()) {
179: ProjectCustomizer.Category category = queue
180: .remove(0);
181:
182: Utilities.removeCategoryChangeSupport(category);
183:
184: if (category.getSubcategories() != null) {
185: queue.addAll(Arrays.asList(category
186: .getSubcategories()));
187: }
188: }
189: }
190: });
191:
192: return dialog;
193:
194: }
195:
196: /** Returns whether all given categories are valid or not. */
197: private static boolean checkValidity(
198: ProjectCustomizer.Category[] categories) {
199: for (ProjectCustomizer.Category c : categories) {
200: if (!c.isValid()) {
201: return false;
202: }
203: ProjectCustomizer.Category[] subCategories = c
204: .getSubcategories();
205: if (subCategories != null) {
206: if (!checkValidity(subCategories)) {
207: return false;
208: }
209: }
210: }
211: return true;
212: }
213:
214: /** Listens to the actions on the Customizer's option buttons */
215: private static class OptionListener implements ActionListener {
216:
217: private ActionListener okOptionListener;
218: private ActionListener storeListener;
219: private ProjectCustomizer.Category[] categories;
220: private Lookup.Provider prov;
221:
222: OptionListener(
223: ActionListener okOptionListener,
224: ActionListener storeListener,
225: ProjectCustomizer.Category[] categs,
226: ProjectCustomizer.CategoryComponentProvider componentProvider) {
227: this .okOptionListener = okOptionListener;
228: this .storeListener = storeListener;
229: categories = categs;
230: //#97998 related
231: if (componentProvider instanceof Lookup.Provider) {
232: prov = (Lookup.Provider) componentProvider;
233: }
234: }
235:
236: public void actionPerformed(final ActionEvent e) {
237: String command = e.getActionCommand();
238:
239: if (COMMAND_OK.equals(command)) {
240: // Call the OK option listener
241: ProjectManager.mutex().writeAccess(
242: new Mutex.Action<Object>() {
243: public Object run() {
244: okOptionListener.actionPerformed(e); // XXX maybe create new event
245: actionPerformed(e, categories);
246: return null;
247: }
248: });
249:
250: final ProgressHandle handle = ProgressHandleFactory
251: .createHandle(NbBundle.getMessage(
252: CustomizerDialog.class,
253: "LBL_Saving_Project_data_progress"));
254: JComponent component = ProgressHandleFactory
255: .createProgressComponent(handle);
256: Frame mainWindow = WindowManager.getDefault()
257: .getMainWindow();
258: final JDialog dialog = new JDialog(mainWindow, NbBundle
259: .getMessage(CustomizerDialog.class,
260: "LBL_Saving_Project_data"), true);
261: SavingProjectDataPanel panel = new SavingProjectDataPanel(
262: component);
263:
264: dialog.getContentPane().add(panel);
265: dialog
266: .setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
267: dialog.pack();
268:
269: Rectangle bounds = mainWindow.getBounds();
270: int middleX = bounds.x + bounds.width / 2;
271: int middleY = bounds.y + bounds.height / 2;
272: Dimension size = dialog.getPreferredSize();
273: dialog.setBounds(middleX - size.width / 2, middleY
274: - size.height / 2, size.width, size.height);
275:
276: // Call storeListeners out of AWT EQ
277: RequestProcessor.getDefault().post(new Runnable() {
278: public void run() {
279: try {
280: ProjectManager.mutex().writeAccess(
281: new Mutex.Action<Object>() {
282: public Object run() {
283: handle.start();
284: if (storeListener != null) {
285: storeListener
286: .actionPerformed(e);
287: }
288: storePerformed(e,
289: categories);
290: // #97998 related
291: saveModifiedProject();
292: return null;
293: }
294: });
295: } finally {
296: SwingUtilities.invokeLater(new Runnable() {
297: public void run() {
298: dialog.setVisible(false);
299: dialog.dispose();
300: }
301: });
302: }
303: }
304: });
305:
306: dialog.setVisible(true);
307:
308: }
309: }
310:
311: private void actionPerformed(ActionEvent e,
312: ProjectCustomizer.Category[] categs) {
313: for (ProjectCustomizer.Category category : categs) {
314: ActionListener list = category.getOkButtonListener();
315: if (list != null) {
316: list.actionPerformed(e);// XXX maybe create new event
317: }
318: if (category.getSubcategories() != null) {
319: actionPerformed(e, category.getSubcategories());
320: }
321: }
322: }
323:
324: private void storePerformed(ActionEvent e,
325: ProjectCustomizer.Category[] categories) {
326: for (ProjectCustomizer.Category category : categories) {
327: ActionListener listener = category.getStoreListener();
328: if (listener != null) {
329: listener.actionPerformed(e); // XXX maybe create new event
330: }
331: if (category.getSubcategories() != null) {
332: storePerformed(e, category.getSubcategories());
333: }
334: }
335: }
336:
337: private void saveModifiedProject() {
338: if (prov != null) {
339: Project prj = prov.getLookup().lookup(Project.class);
340: if (ProjectManager.getDefault().isModified(prj)) {
341: try {
342: ProjectManager.getDefault().saveProject(prj);
343: } catch (IOException ex) {
344: Exceptions.printStackTrace(ex);
345: } catch (IllegalArgumentException ex) {
346: Exceptions.printStackTrace(ex);
347: }
348: }
349: }
350: }
351:
352: }
353:
354: private static class HelpCtxChangeListener implements
355: PropertyChangeListener {
356:
357: DialogDescriptor dialogDescriptor;
358: HelpCtx defaultHelpCtx;
359:
360: HelpCtxChangeListener(DialogDescriptor dialogDescriptor,
361: HelpCtx defaultHelpCtx) {
362: this .dialogDescriptor = dialogDescriptor;
363: this .defaultHelpCtx = defaultHelpCtx;
364: }
365:
366: public void propertyChange(PropertyChangeEvent evt) {
367:
368: if (CustomizerPane.HELP_CTX_PROPERTY.equals(evt
369: .getPropertyName())) {
370: HelpCtx newHelp = (HelpCtx) evt.getNewValue();
371: dialogDescriptor
372: .setHelpCtx(newHelp == null
373: || newHelp == HelpCtx.DEFAULT_HELP ? defaultHelpCtx
374: : newHelp);
375: }
376:
377: }
378:
379: }
380:
381: private static class ListeningButton extends JButton implements
382: PropertyChangeListener {
383:
384: private ProjectCustomizer.Category[] categories;
385:
386: public ListeningButton(String label,
387: ProjectCustomizer.Category[] categories) {
388: super (label);
389: this .categories = categories;
390: for (ProjectCustomizer.Category c : categories) {
391: Utilities.getCategoryChangeSupport(c)
392: .addPropertyChangeListener(this );
393: }
394:
395: }
396:
397: public void propertyChange(PropertyChangeEvent evt) {
398: if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) {
399: boolean valid = (Boolean) evt.getNewValue();
400: // enable only if all categories are valid
401: setEnabled(valid
402: && CustomizerDialog.checkValidity(categories));
403: }
404: }
405:
406: }
407:
408: }
|