KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > Hacks


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ui;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.openide.filesystems.FileObject;
33 import org.openide.loaders.DataObject;
34 import org.openide.loaders.TemplateWizard;
35 import org.openide.nodes.Node;
36 import org.openide.util.Lookup;
37 import org.openide.util.NbBundle;
38 import org.openide.util.RequestProcessor;
39 import org.openide.windows.TopComponent;
40 import org.openide.windows.WindowManager;
41
42 /**
43  * Various hacks that should be solved better later.
44  */

45 public class Hacks {
46     
47     private static final String JavaDoc BUILD_NUMBER = System.getProperty("netbeans.buildnumber"); // NOI18N
48

49     /**
50      * Show name of project corresponding to selection in Main Window title bar.
51      * @author Jesse Glick
52      */

53     static void keepCurrentProjectNameUpdated() {
54         final TopComponent.Registry r = TopComponent.getRegistry();
55         final RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
56             public void run() {
57                 Node[] sel = r.getActivatedNodes();
58                 Set JavaDoc<Project> projects = new HashSet JavaDoc<Project>();
59                 for (int i = 0; i < sel.length; i++) {
60                     Lookup l = sel[i].getLookup();
61                     Project p = l.lookup(Project.class);
62                     if (p != null) {
63                         projects.add(p);
64                     } else {
65                         DataObject d = l.lookup(DataObject.class);
66                         if (d != null) {
67                             FileObject f = d.getPrimaryFile();
68                             p = FileOwnerQuery.getOwner(f);
69                             if (p != null) {
70                                 projects.add(p);
71                             }
72                         }
73                     }
74                 }
75                 final String JavaDoc pname;
76                 if (projects.size() == 1) {
77                     Project p = projects.iterator().next();
78                     pname = ProjectUtils.getInformation(p).getDisplayName();
79                     assert pname != null : p;
80                 } else if (projects.isEmpty()) {
81                     pname = null;
82                 } else {
83                     pname = NbBundle.getMessage(Hacks.class, "LBL_MultipleProjects");
84                 }
85                 Project p = OpenProjectList.getDefault().getMainProject();
86                 final String JavaDoc mname = p != null?
87                     ProjectUtils.getInformation(p).getDisplayName():
88                     NbBundle.getMessage(Hacks.class, "LBL_NoMainProject");
89                 EventQueue.invokeLater(new Runnable JavaDoc() {
90                     public void run() {
91                         // depends on exported keys in core/windows
92
String JavaDoc format = NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").
93                                 getString(pname != null ? "CTL_MainWindow_Title" : "CTL_MainWindow_Title_No_Project");
94                         String JavaDoc title = pname != null?
95                             // Note that currently mname is ignored.
96
MessageFormat.format(format, BUILD_NUMBER, pname, mname) :
97                             MessageFormat.format(format, BUILD_NUMBER, mname);
98                         WindowManager.getDefault().getMainWindow().setTitle(title);
99                     }
100                 });
101             }
102         });
103         r.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
104             public void propertyChange(PropertyChangeEvent JavaDoc ev) {
105                 if (TopComponent.Registry.PROP_ACTIVATED_NODES.equals(ev.getPropertyName())) {
106                     task.schedule(200);
107                 }
108             }
109         });
110     }
111     
112     
113     /** Forces reload of panels in TemplateWizard. Public method updateState doesn't re-read
114      * the new panels from new iterator.
115      * Note: it should be solved better either fixing TemplateWizard or implement
116      * whole NewFileWizard (w/o TemplateWizard).
117      *
118      * @param tw instance of TemplateWizard (thus NewFileWizard)
119      * @param dobj template
120      */

121     static void reloadPanelsInWizard (final TemplateWizard tw, final DataObject dobj) {
122         try {
123             Class JavaDoc<?> twClazz = Class.forName(
124                 "org.openide.loaders.TemplateWizard", true, // NOI18N
125
Thread.currentThread().getContextClassLoader());
126             if (twClazz != null) {
127                 Method JavaDoc reloadPanels = twClazz.getDeclaredMethod("reload", DataObject.class); // NOI18N
128
reloadPanels.setAccessible (true);
129                 reloadPanels.invoke(tw, dobj);
130             }
131         } catch (Exception JavaDoc e) {
132             // XXX
133
e.printStackTrace();
134         }
135     }
136 }
137
Popular Tags