Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.web.clientproject.browser; |
43 |
|
44 |
import java.awt.Component; |
45 |
import java.awt.Toolkit; |
46 |
import java.awt.event.ActionEvent; |
47 |
import java.awt.image.BufferedImage; |
48 |
import java.beans.PropertyChangeEvent; |
49 |
import java.beans.PropertyChangeListener; |
50 |
import java.io.IOException; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Arrays; |
53 |
import java.util.LinkedHashSet; |
54 |
import java.util.List; |
55 |
import java.util.Set; |
56 |
import java.util.logging.Level; |
57 |
import java.util.logging.Logger; |
58 |
import javax.swing.AbstractAction; |
59 |
import javax.swing.Action; |
60 |
import javax.swing.ButtonGroup; |
61 |
import javax.swing.ImageIcon; |
62 |
import javax.swing.JButton; |
63 |
import javax.swing.JMenu; |
64 |
import javax.swing.JMenuItem; |
65 |
import javax.swing.JPopupMenu; |
66 |
import javax.swing.JRadioButtonMenuItem; |
67 |
import javax.swing.JSeparator; |
68 |
import javax.swing.event.MenuEvent; |
69 |
import javax.swing.event.MenuListener; |
70 |
import javax.swing.event.PopupMenuEvent; |
71 |
import javax.swing.event.PopupMenuListener; |
72 |
import org.netbeans.api.annotations.common.NonNull; |
73 |
import org.netbeans.api.annotations.common.NullAllowed; |
74 |
import org.netbeans.api.project.FileOwnerQuery; |
75 |
import org.netbeans.api.project.Project; |
76 |
import org.netbeans.api.project.ui.OpenProjects; |
77 |
import org.netbeans.modules.web.browser.api.WebBrowser; |
78 |
import org.netbeans.modules.web.browser.spi.ProjectBrowserProvider; |
79 |
import org.netbeans.spi.project.ActionProvider; |
80 |
import org.openide.awt.ActionID; |
81 |
import org.openide.awt.ActionReference; |
82 |
import org.openide.awt.ActionReferences; |
83 |
import org.openide.awt.ActionRegistration; |
84 |
import org.openide.awt.DropDownButtonFactory; |
85 |
import org.openide.filesystems.FileObject; |
86 |
import org.openide.loaders.DataObject; |
87 |
import org.openide.nodes.Node; |
88 |
import org.openide.util.Exceptions; |
89 |
import org.openide.util.HelpCtx; |
90 |
import org.openide.util.ImageUtilities; |
91 |
import org.openide.util.Lookup; |
92 |
import org.openide.util.LookupEvent; |
93 |
import org.openide.util.LookupListener; |
94 |
import org.openide.util.NbBundle; |
95 |
import org.openide.util.RequestProcessor; |
96 |
import org.openide.util.WeakListeners; |
97 |
import org.openide.util.actions.CallableSystemAction; |
98 |
import org.openide.util.lookup.ProxyLookup; |
99 |
import org.openide.windows.TopComponent; |
100 |
|
101 |
/** |
102 |
* |
103 |
*/ |
104 |
@ActionID(id="org.netbeans.modules.web.clientproject.browser.ActiveBrowserAction", category="Project") |
105 |
@ActionRegistration(displayName="#ActiveBrowserAction.reg.name", lazy=false) |
106 |
@ActionReferences({ |
107 |
@ActionReference(path="Menu/BuildProject", position=307), |
108 |
@ActionReference(path="Toolbars/Build", position=87) |
109 |
}) |
110 |
public class ActiveBrowserAction extends CallableSystemAction implements LookupListener { |
111 |
|
112 |
private final Lookup lookup; |
113 |
private @NullAllowed Project currentProject; |
114 |
private @NullAllowed ProjectBrowserProvider currentBrowserProvider; |
115 |
private final PropertyChangeListener currentBrowserProviderListener; |
116 |
private JButton toolbarButton; |
117 |
private JMenu menuAction; |
118 |
|
119 |
private static final RequestProcessor RP = new RequestProcessor(ActiveBrowserAction.class); |
120 |
|
121 |
public ActiveBrowserAction() { |
122 |
lookup = LastActivatedWindowLookup.INSTANCE; |
123 |
Lookup.Result<Project> resultPrj = lookup.lookupResult(Project.class); |
124 |
Lookup.Result<DataObject> resultDO = lookup.lookupResult(DataObject.class); |
125 |
resultPrj.addLookupListener(WeakListeners.create(LookupListener.class, this, resultPrj)); |
126 |
resultDO.addLookupListener(WeakListeners.create(LookupListener.class, this, resultDO)); |
127 |
currentBrowserProviderListener = new PropertyChangeListener() { |
128 |
public @Override void propertyChange(PropertyChangeEvent evt) { |
129 |
if (ProjectBrowserProvider.PROP_BROWSER_ACTIVE.equals(evt.getPropertyName())) { |
130 |
updateButton(ActiveBrowserAction.this.getBrowserProvider()); |
131 |
} |
132 |
} |
133 |
}; |
134 |
refreshView(); |
135 |
} |
136 |
|
137 |
@Override |
138 |
public void performAction() { |
139 |
Toolkit.getDefaultToolkit().beep(); |
140 |
} |
141 |
|
142 |
@Override |
143 |
@NbBundle.Messages({ |
144 |
"ActiveBrowserAction.name=Set Project Browser" |
145 |
}) |
146 |
public String getName() { |
147 |
return Bundle.ActiveBrowserAction_name(); |
148 |
} |
149 |
|
150 |
@Override |
151 |
public HelpCtx getHelpCtx() { |
152 |
return new HelpCtx("org.netbeans.modules.web.clientproject.browser.ActiveBrowserAction"); |
153 |
} |
154 |
|
155 |
@Override |
156 |
public void resultChanged(LookupEvent ev) { |
157 |
refreshViewLater(); |
158 |
} |
159 |
|
160 |
@Override |
161 |
public JMenuItem getMenuPresenter() { |
162 |
JMenu m = new JMenu(Bundle.ActiveBrowserAction_name()); |
163 |
m.addMenuListener(new MenuListener() { |
164 |
@Override |
165 |
public void menuSelected(MenuEvent e) { |
166 |
JMenu m = (JMenu)e.getSource(); |
167 |
m.removeAll(); |
168 |
ProjectBrowserProvider pbp = ActiveBrowserAction.this.getBrowserProvider(); |
169 |
for (Component mi : createMenuItems(pbp != null ? pbp.getActiveBrowser() : null)) { |
170 |
if (mi instanceof JSeparator) { |
171 |
m.addSeparator(); |
172 |
} else { |
173 |
m.add(mi); |
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
@Override |
179 |
public void menuDeselected(MenuEvent e) { |
180 |
} |
181 |
|
182 |
@Override |
183 |
public void menuCanceled(MenuEvent e) { |
184 |
} |
185 |
}); |
186 |
menuAction = m; |
187 |
return m; |
188 |
} |
189 |
|
190 |
private ProjectBrowserProvider getBrowserProvider() { |
191 |
synchronized (this) { |
192 |
return currentBrowserProvider; |
193 |
} |
194 |
} |
195 |
|
196 |
@NbBundle.Messages({ |
197 |
"ActiveBrowserAction.customize=Customize" |
198 |
}) |
199 |
private List<Component> createMenuItems(WebBrowser selectedWebBrowser) { |
200 |
List<Component> l = new ArrayList<Component>(); |
201 |
final ProjectBrowserProvider pbp = getBrowserProvider(); |
202 |
if (pbp != null) { |
203 |
ButtonGroup group = new ButtonGroup(); |
204 |
for (WebBrowser wb : pbp.getBrowsers()) { |
205 |
JRadioButtonMenuItem mi = new JRadioButtonMenuItem(new SelectBrowserAction(pbp, wb)); |
206 |
group.add(mi); |
207 |
if (selectedWebBrowser != null && wb.getId().equals(selectedWebBrowser.getId())) { |
208 |
mi.setSelected(true); |
209 |
} |
210 |
l.add(mi); |
211 |
} |
212 |
if (pbp.hasCustomizer()) { |
213 |
l.add(new JSeparator()); |
214 |
Action customizeAction = new AbstractAction(Bundle.ActiveBrowserAction_customize()) { |
215 |
@Override |
216 |
public void actionPerformed(ActionEvent e) { |
217 |
pbp.customize(); |
218 |
} |
219 |
}; |
220 |
JMenuItem mi = new JMenuItem(customizeAction); |
221 |
l.add(mi); |
222 |
} |
223 |
} |
224 |
return l; |
225 |
} |
226 |
|
227 |
private static class SelectBrowserAction extends AbstractAction { |
228 |
|
229 |
private ProjectBrowserProvider pbp; |
230 |
private WebBrowser wb; |
231 |
|
232 |
public SelectBrowserAction(ProjectBrowserProvider pbp, WebBrowser wb) { |
233 |
super(wb.getName(), new ImageIcon(wb.getIconImage())); |
234 |
this.pbp = pbp; |
235 |
this.wb = wb; |
236 |
} |
237 |
|
238 |
@Override |
239 |
public void actionPerformed(ActionEvent e) { |
240 |
try { |
241 |
pbp.setActiveBrowser(wb); |
242 |
} catch (IllegalArgumentException ex) { |
243 |
Exceptions.printStackTrace(ex); |
244 |
} catch (IOException ex) { |
245 |
Exceptions.printStackTrace(ex); |
246 |
} |
247 |
} |
248 |
|
249 |
} |
250 |
|
251 |
@Override |
252 |
public JMenuItem getPopupPresenter() { |
253 |
return super.getPopupPresenter(); //To change body of generated methods, choose Tools | Templates. |
254 |
} |
255 |
|
256 |
@Override |
257 |
public Component getToolbarPresenter() { |
258 |
JPopupMenu menu = new JPopupMenu(); |
259 |
JButton button = DropDownButtonFactory.createSimpleDropDownButton( |
260 |
new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)), menu); |
261 |
button.setDisabledIcon(new ImageIcon(ImageUtilities.loadImage("org/netbeans/modules/web/browser/ui/resources/browser-disabled.png"))); |
262 |
button.setEnabled(false); |
263 |
menu.add(new JMenuItem("xxx")); // NOI18N |
264 |
ProjectBrowserProvider pbp = getBrowserProvider(); |
265 |
toolbarButton = button; |
266 |
updateButton(pbp); |
267 |
menu.addPopupMenuListener(new PopupMenuListener() { |
268 |
|
269 |
@Override |
270 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
271 |
JPopupMenu menu = (JPopupMenu) e.getSource(); |
272 |
menu.removeAll(); |
273 |
ProjectBrowserProvider pbp = ActiveBrowserAction.this.getBrowserProvider(); |
274 |
for (Component mi : createMenuItems(pbp.getActiveBrowser())) { |
275 |
if (mi instanceof JSeparator) { |
276 |
menu.addSeparator(); |
277 |
} else { |
278 |
menu.add(mi); |
279 |
} |
280 |
} |
281 |
} |
282 |
|
283 |
@Override |
284 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
285 |
} |
286 |
|
287 |
@Override |
288 |
public void popupMenuCanceled(PopupMenuEvent e) { |
289 |
} |
290 |
}); |
291 |
return button; |
292 |
} |
293 |
|
294 |
private void refreshViewLater() { |
295 |
RP.post(new Runnable() { |
296 |
public @Override void run() { |
297 |
refreshView(); |
298 |
} |
299 |
}); |
300 |
} |
301 |
|
302 |
private void refreshView() { |
303 |
Project p = OpenProjects.getDefault().getMainProject(); |
304 |
if (p != null) { |
305 |
activeProjectChanged(p); |
306 |
} else { |
307 |
Project[] selected = getProjectsFromLookup(lookup, null); |
308 |
if (selected.length == 1) { |
309 |
activeProjectChanged(selected[0]); |
310 |
} else { |
311 |
Project[] open = OpenProjects.getDefault().getOpenProjects(); |
312 |
if (open.length == 1) { |
313 |
activeProjectChanged(open[0]); |
314 |
} else { |
315 |
activeProjectChanged(null); |
316 |
} |
317 |
} |
318 |
} |
319 |
} |
320 |
|
321 |
private void activeProjectChanged(Project p) { |
322 |
ProjectBrowserProvider pbp = null; |
323 |
synchronized (this) { |
324 |
if (currentProject == p) { |
325 |
return; |
326 |
} |
327 |
if (currentBrowserProvider != null) { |
328 |
currentBrowserProvider.removePropertyChangeListener(currentBrowserProviderListener); |
329 |
currentBrowserProvider = null; |
330 |
} |
331 |
currentProject = p; |
332 |
if (currentProject != null) { |
333 |
currentBrowserProvider = currentProject.getLookup().lookup(ProjectBrowserProvider.class); |
334 |
pbp = currentBrowserProvider; |
335 |
if (currentBrowserProvider != null) { |
336 |
currentBrowserProvider.addPropertyChangeListener(currentBrowserProviderListener); |
337 |
} |
338 |
} |
339 |
} |
340 |
updateButton(pbp); |
341 |
} |
342 |
|
343 |
private void updateButton(ProjectBrowserProvider pbp) { |
344 |
JButton tb = toolbarButton; |
345 |
if (tb != null) { |
346 |
if (pbp == null) { |
347 |
tb.setIcon(new ImageIcon(new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB))); |
348 |
tb.setToolTipText(null); |
349 |
} else { |
350 |
WebBrowser wb = pbp.getActiveBrowser(); |
351 |
if (wb != null) { |
352 |
tb.setIcon(new ImageIcon(wb.getIconImage())); |
353 |
tb.setToolTipText(wb.getName()); |
354 |
} else { |
355 |
tb.setIcon(new ImageIcon(ImageUtilities.loadImage("org/netbeans/modules/web/browser/ui/resources/browser-generic.png"))); |
356 |
tb.setToolTipText(null); |
357 |
} |
358 |
} |
359 |
tb.setEnabled(pbp != null); |
360 |
} |
361 |
JMenu ma = menuAction; |
362 |
if (ma != null) { |
363 |
ma.setEnabled(pbp != null); |
364 |
} |
365 |
} |
366 |
|
367 |
|
368 |
|
369 |
|
370 |
// XXX: copy&pasted from project.ui.actions.LookupSensitiveAction.LastActivatedWindowLookup |
371 |
/** |
372 |
* #120721: do not want to use Utilities.actionsGlobalContext since that does not survive focus change, |
373 |
* and we would like to mimic the selection tracking behavior of Hacks.keepCurrentProjectNameUpdated. |
374 |
*/ |
375 |
static final class LastActivatedWindowLookup extends ProxyLookup implements PropertyChangeListener { |
376 |
|
377 |
static final Lookup INSTANCE = new LastActivatedWindowLookup(); |
378 |
|
379 |
private final TopComponent.Registry reg = TopComponent.getRegistry(); |
380 |
|
381 |
LastActivatedWindowLookup() { |
382 |
reg.addPropertyChangeListener(this); |
383 |
updateLookups(); |
384 |
} |
385 |
|
386 |
private void updateLookups() { |
387 |
Node[] nodes = reg.getActivatedNodes(); |
388 |
Lookup[] delegates = new Lookup[nodes.length]; |
389 |
for (int i = 0; i < nodes.length; i++) { |
390 |
delegates[i] = nodes[i].getLookup(); |
391 |
} |
392 |
setLookups(delegates); |
393 |
} |
394 |
|
395 |
@Override |
396 |
public void propertyChange(PropertyChangeEvent ev) { |
397 |
if (TopComponent.Registry.PROP_ACTIVATED_NODES.equals(ev.getPropertyName())) { |
398 |
updateLookups(); |
399 |
} |
400 |
} |
401 |
|
402 |
} |
403 |
|
404 |
// XXX: copied from project.ui.actions.ActionsUtil: |
405 |
@NonNull |
406 |
public static Project[] getProjectsFromLookup( Lookup lookup, String command ) { |
407 |
// First find out whether there is a project directly in the Lookup |
408 |
Set<Project> result = new LinkedHashSet<Project>(); // XXX or use OpenProjectList.projectByDisplayName? |
409 |
for (Project p : lookup.lookupAll(Project.class)) { |
410 |
result.add(p); |
411 |
} |
412 |
// Now try to guess the project from dataobjects |
413 |
for (DataObject dObj : lookup.lookupAll(DataObject.class)) { |
414 |
FileObject fObj = dObj.getPrimaryFile(); |
415 |
Project p = FileOwnerQuery.getOwner(fObj); |
416 |
if ( p != null ) { |
417 |
result.add( p ); |
418 |
} |
419 |
} |
420 |
Project[] projectsArray = result.toArray(new Project[result.size()]); |
421 |
|
422 |
if ( command != null ) { |
423 |
// All projects have to have the command enabled |
424 |
for (Project p : projectsArray) { |
425 |
if (!commandSupported(p, command, lookup)) { |
426 |
return new Project[0]; |
427 |
} |
428 |
} |
429 |
} |
430 |
|
431 |
return projectsArray; |
432 |
} |
433 |
|
434 |
// XXX: copied from project.ui.actions.ActionsUtil: |
435 |
public static boolean commandSupported( Project project, String command, Lookup context ) { |
436 |
//We have to look whether the command is supported by the project |
437 |
ActionProvider ap = project.getLookup().lookup(ActionProvider.class); |
438 |
if ( ap != null ) { |
439 |
List<String> commands = Arrays.asList(ap.getSupportedActions()); |
440 |
if ( commands.contains( command ) ) { |
441 |
try { |
442 |
if (context == null || ap.isActionEnabled(command, context)) { |
443 |
//System.err.println("cS: true project=" + project + " command=" + command + " context=" + context); |
444 |
return true; |
445 |
} |
446 |
} catch (IllegalArgumentException x) { |
447 |
Logger.getLogger(ActiveBrowserAction.class.getName()).log(Level.INFO, "#213589: possible race condition in MergedActionProvider", x); |
448 |
} |
449 |
} |
450 |
} |
451 |
//System.err.println("cS: false project=" + project + " command=" + command + " context=" + context); |
452 |
return false; |
453 |
} |
454 |
} |