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-2007 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.ui;
043:
044: import java.awt.EventQueue;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.lang.reflect.Method;
048: import java.text.MessageFormat;
049: import java.util.Arrays;
050: import java.util.HashSet;
051: import java.util.Set;
052: import java.util.logging.Logger;
053: import org.netbeans.api.project.FileOwnerQuery;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectUtils;
056: import org.openide.filesystems.FileObject;
057: import org.openide.loaders.DataObject;
058: import org.openide.loaders.TemplateWizard;
059: import org.openide.nodes.Node;
060: import org.openide.util.Lookup;
061: import org.openide.util.NbBundle;
062: import org.openide.util.RequestProcessor;
063: import org.openide.windows.TopComponent;
064: import org.openide.windows.WindowManager;
065:
066: /**
067: * Various hacks that should be solved better later.
068: */
069: public class Hacks {
070:
071: private Hacks() {
072: }
073:
074: private static final String BUILD_NUMBER = System
075: .getProperty("netbeans.buildnumber"); // NOI18N
076:
077: /**
078: * Show name of project corresponding to selection in Main Window title bar.
079: * @author Jesse Glick
080: */
081: static void keepCurrentProjectNameUpdated() {
082: final TopComponent.Registry r = TopComponent.getRegistry();
083: final RequestProcessor.Task task = RequestProcessor
084: .getDefault().create(new Runnable() {
085: public void run() {
086: Node[] sel = r.getActivatedNodes();
087: Set<Project> projects = new HashSet<Project>();
088: boolean noProjectsOpen = OpenProjectList
089: .getDefault().getOpenProjects().length == 0;
090: for (int i = 0; i < sel.length; i++) {
091: Lookup l = sel[i].getLookup();
092: Project p = l.lookup(Project.class);
093: if (p != null) {
094: projects.add(p);
095: if (noProjectsOpen) {
096: Logger
097: .getLogger(
098: Hacks.class
099: .getName())
100: ./*XXX for now*/fine(
101: "Activated node selection "
102: + Arrays
103: .toString(sel)
104: + " contains nonopen project "
105: + p
106: + " though none are open; leak? activated TC: "
107: + r
108: .getActivated()
109: + " current nodes: "
110: + Arrays
111: .toString(r
112: .getCurrentNodes())
113: + " (report in issue #102805)");
114: }
115: } else {
116: DataObject d = l
117: .lookup(DataObject.class);
118: if (d != null) {
119: FileObject f = d.getPrimaryFile();
120: p = FileOwnerQuery.getOwner(f);
121: if (p != null) {
122: projects.add(p);
123: }
124: }
125: }
126: }
127: final String pname;
128: if (projects.size() == 1) {
129: Project p = projects.iterator().next();
130: pname = ProjectUtils.getInformation(p)
131: .getDisplayName();
132: assert pname != null : p;
133: } else if (projects.isEmpty()) {
134: pname = null;
135: } else {
136: pname = NbBundle.getMessage(Hacks.class,
137: "LBL_MultipleProjects");
138: }
139: Project p = OpenProjectList.getDefault()
140: .getMainProject();
141: final String mname = p != null ? ProjectUtils
142: .getInformation(p).getDisplayName()
143: : NbBundle.getMessage(Hacks.class,
144: "LBL_NoMainProject");
145: EventQueue.invokeLater(new Runnable() {
146: public void run() {
147: // depends on exported keys in core/windows
148: String format = NbBundle
149: .getBundle(
150: "org.netbeans.core.windows.view.ui.Bundle")
151: .getString(
152: pname != null ? "CTL_MainWindow_Title"
153: : "CTL_MainWindow_Title_No_Project");
154: String title = pname != null ?
155: // Note that currently mname is ignored.
156: MessageFormat.format(format,
157: BUILD_NUMBER, pname, mname)
158: : MessageFormat.format(format,
159: BUILD_NUMBER, mname);
160: WindowManager.getDefault()
161: .getMainWindow()
162: .setTitle(title);
163: }
164: });
165: }
166: });
167: r.addPropertyChangeListener(new PropertyChangeListener() {
168: public void propertyChange(PropertyChangeEvent ev) {
169: if (TopComponent.Registry.PROP_ACTIVATED_NODES
170: .equals(ev.getPropertyName())) {
171: task.schedule(200);
172: }
173: }
174: });
175: }
176:
177: /** Forces reload of panels in TemplateWizard. Public method updateState doesn't re-read
178: * the new panels from new iterator.
179: * Note: it should be solved better either fixing TemplateWizard or implement
180: * whole NewFileWizard (w/o TemplateWizard).
181: *
182: * @param tw instance of TemplateWizard (thus NewFileWizard)
183: * @param dobj template
184: */
185: static void reloadPanelsInWizard(final TemplateWizard tw,
186: final DataObject dobj) {
187: try {
188: Class<?> twClazz = Class.forName(
189: "org.openide.loaders.TemplateWizard", true, // NOI18N
190: Thread.currentThread().getContextClassLoader());
191: if (twClazz != null) {
192: Method reloadPanels = twClazz.getDeclaredMethod(
193: "reload", DataObject.class); // NOI18N
194: reloadPanels.setAccessible(true);
195: reloadPanels.invoke(tw, dobj);
196: }
197: } catch (Exception e) {
198: // XXX
199: e.printStackTrace();
200: }
201: }
202: }
|