KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > java > project > support > ui > BrokenReferencesSupport


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.spi.java.project.support.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Frame JavaDoc;
24 import java.awt.event.WindowAdapter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.api.project.ProjectUtils;
31 import org.netbeans.modules.java.project.BrokenReferencesAlertPanel;
32 import org.netbeans.modules.java.project.BrokenReferencesCustomizer;
33 import org.netbeans.modules.java.project.BrokenReferencesModel;
34 import org.netbeans.modules.java.project.JavaProjectSettings;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.ReferenceHelper;
37 import org.openide.DialogDescriptor;
38 import org.openide.DialogDisplayer;
39 import org.openide.ErrorManager;
40 import org.openide.util.NbBundle;
41 import org.openide.windows.WindowManager;
42
43 /**
44  * Support for managing broken project references. Project freshly checkout from
45  * VCS can has broken references of several types: reference to other project,
46  * reference to a foreign file, reference to an external source root, reference
47  * to a Java Library or reference to a Java Platform. This class has helper
48  * methods for detection of these problems and for fixing them.
49  * <div class="nonnormative">
50  * Typical usage of this class it to check whether the project has some broken
51  * references and if it has then providing an action on project's node which
52  * allows to correct these configuration problems by showing broken references
53  * customizer.
54  * </div>
55  * @author David Konecny
56  */

57 public class BrokenReferencesSupport {
58
59     /** Last time in ms when the Broken References alert was shown. */
60     private static long brokenAlertLastTime = 0;
61     
62     /** Is Broken References alert shown now? */
63     private static boolean brokenAlertShown = false;
64
65     /** Timeout within which request to show alert will be ignored. */
66     private static int BROKEN_ALERT_TIMEOUT = 1000;
67     
68     private BrokenReferencesSupport() {}
69
70     /**
71      * Checks whether the project has some broken references or not.
72      * @param projectHelper AntProjectHelper associated with the project.
73      * @param referenceHelper ReferenceHelper associated with the project.
74      * @param properties array of property names which values hold
75      * references which may be broken. For example for J2SE project
76      * the property names will be: "javac.classpath", "run.classpath", etc.
77      * @param platformProperties array of property names which values hold
78      * name of the platform(s) used by the project. These platforms will be
79      * checked for existence. For example for J2SE project the property
80      * name is one and it is "platform.active". The name of the default
81      * platform is expected to be "default_platform" and this platform
82      * always exists.
83      * @return true if some problem was found and it is necessary to give
84      * user a chance to fix them
85      */

86     public static boolean isBroken(AntProjectHelper projectHelper,
87             ReferenceHelper referenceHelper, String JavaDoc[] properties, String JavaDoc[] platformProperties) {
88         return BrokenReferencesModel.isBroken(projectHelper,
89             projectHelper.getStandardPropertyEvaluator(), properties, platformProperties);
90     }
91     
92     /**
93      * Shows UI customizer which gives users chance to fix encountered problems.
94      * @param projectHelper AntProjectHelper associated with the project.
95      * @param referenceHelper ReferenceHelper associated with the project.
96      * @param properties array of property names which values hold
97      * references which may be broken. For example for J2SE project
98      * the property names will be: "javac.classpath", "run.classpath", etc.
99      * @param platformProperties array of property names which values hold
100      * name of the platform(s) used by the project. These platforms will be
101      * checked for existence. For example for J2SE project the property
102      * name is one and it is "platform.active". The name of the default
103      * platform is expected to be "default_platform" and this platform
104      * always exists.
105      */

106     public static void showCustomizer(AntProjectHelper projectHelper,
107             ReferenceHelper referenceHelper, String JavaDoc[] properties, String JavaDoc[] platformProperties) {
108         BrokenReferencesModel model = new BrokenReferencesModel(projectHelper, referenceHelper, properties, platformProperties);
109         BrokenReferencesCustomizer customizer = new BrokenReferencesCustomizer(model);
110         JButton JavaDoc close = new JButton JavaDoc (NbBundle.getMessage(BrokenReferencesCustomizer.class,"LBL_BrokenLinksCustomizer_Close")); // NOI18N
111
close.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(BrokenReferencesCustomizer.class,"ACSD_BrokenLinksCustomizer_Close")); // NOI18N
112
String JavaDoc projectDisplayName = "???"; // NOI18N
113
try {
114             Project project = ProjectManager.getDefault().findProject(projectHelper.getProjectDirectory());
115             if (project != null) {
116                 projectDisplayName = ProjectUtils.getInformation(project).getDisplayName();
117             }
118         } catch (IOException JavaDoc e) {
119             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
120         }
121         DialogDescriptor dd = new DialogDescriptor(customizer,
122             NbBundle.getMessage(BrokenReferencesCustomizer.class,
123             "LBL_BrokenLinksCustomizer_Title", projectDisplayName), // NOI18N
124
true, new Object JavaDoc[] {close}, close, DialogDescriptor.DEFAULT_ALIGN, null, null);
125         Dialog JavaDoc dlg = null;
126         try {
127             dlg = DialogDisplayer.getDefault().createDialog(dd);
128             dlg.setVisible(true);
129         } finally {
130             if (dlg != null)
131                 dlg.dispose();
132         }
133     }
134
135     /**
136      * Show alert message box informing user that a project has broken
137      * references. This method can be safely called from any thread, e.g. during
138      * the project opening, and it will take care about showing message box only
139      * once for several subsequent calls during a timeout.
140      * The alert box has also "show this warning again" check box.
141      */

142     public static synchronized void showAlert() {
143         // Do not show alert if it is already shown or if it was shown
144
// in last BROKEN_ALERT_TIMEOUT milliseconds or if user do not wish it.
145
if (brokenAlertShown ||
146                 brokenAlertLastTime+BROKEN_ALERT_TIMEOUT > System.currentTimeMillis() ||
147                 !JavaProjectSettings.isShowAgainBrokenRefAlert()) {
148             return;
149         }
150         brokenAlertShown = true;
151         final Runnable JavaDoc task = new Runnable JavaDoc() {
152             public void run() {
153                 try {
154                     JButton JavaDoc closeOption = new JButton JavaDoc (NbBundle.getMessage(BrokenReferencesAlertPanel.class, "CTL_Broken_References_Close"));
155                     closeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrokenReferencesAlertPanel.class, "AD_Broken_References_Close"));
156                     DialogDescriptor dd = new DialogDescriptor(new BrokenReferencesAlertPanel(),
157                         NbBundle.getMessage(BrokenReferencesAlertPanel.class, "MSG_Broken_References_Title"),
158                         true,
159                         new Object JavaDoc[] {closeOption},
160                         closeOption,
161                         DialogDescriptor.DEFAULT_ALIGN,
162                         null,
163                         null); // NOI18N
164
dd.setMessageType(DialogDescriptor.WARNING_MESSAGE);
165                     Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(dd);
166                     dlg.setVisible(true);
167                 } finally {
168                     synchronized (BrokenReferencesSupport.class) {
169                         brokenAlertLastTime = System.currentTimeMillis();
170                         brokenAlertShown = false;
171                     }
172                 }
173             }
174         };
175         SwingUtilities.invokeLater(new Runnable JavaDoc() {
176             public void run () {
177                 Frame JavaDoc f = WindowManager.getDefault().getMainWindow();
178                 if (f == null || f.isShowing()) {
179                     task.run();
180                 }
181                 else {
182                     new MainWindowListener (f,task);
183                 }
184             }
185         });
186     }
187     
188     
189     private static class MainWindowListener extends WindowAdapter JavaDoc {
190         
191         private Frame JavaDoc frame;
192         private Runnable JavaDoc task;
193         
194         /**
195          * Has to be called by the event thread!
196          *
197          */

198         public MainWindowListener (Frame JavaDoc frame, Runnable JavaDoc task) {
199             assert frame != null && task != null;
200             assert SwingUtilities.isEventDispatchThread();
201             this.frame = frame;
202             this.task = task;
203             frame.addWindowListener(this);
204         }
205         
206         public /*@Override*/ void windowOpened(java.awt.event.WindowEvent JavaDoc e) {
207             MainWindowListener.this.frame.removeWindowListener (this);
208             SwingUtilities.invokeLater(this.task);
209         }
210     }
211     
212     
213 }
214
Popular Tags