KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > ProjectMenuItem


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 package org.netbeans.modules.versioning;
20
21 import org.openide.util.actions.Presenter;
22 import org.openide.util.Lookup;
23 import org.openide.nodes.Node;
24 import org.openide.awt.DynamicMenuContent;
25 import org.openide.awt.Mnemonics;
26 import org.openide.windows.TopComponent;
27 import org.netbeans.modules.versioning.spi.VersioningSystem;
28 import org.netbeans.modules.versioning.spi.VCSContext;
29 import org.netbeans.modules.versioning.spi.VCSAnnotator;
30
31 import javax.swing.*;
32 import java.io.File JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.util.*;
35
36 /**
37  * Appears in a project's popup menu.
38  *
39  * @author Maros Sandor
40  */

41 public class ProjectMenuItem extends AbstractAction implements Presenter.Popup {
42
43     public JMenuItem getPopupPresenter() {
44         return new DynamicDummyItem();
45     }
46     
47     public void actionPerformed(ActionEvent JavaDoc e) {
48         // dummy, not used
49
}
50
51     private JComponent [] createItems() {
52         Node [] nodes = getActivatedNodes();
53         if (nodes.length > 0) {
54             VersioningSystem owner = VersioningManager.getInstance().getOwner(toFile(nodes[0]));
55             for (int i = 1; i < nodes.length; i++) {
56                 Node node = nodes[i];
57                 VersioningSystem vs = VersioningManager.getInstance().getOwner(toFile(node));
58                 if (vs != owner) {
59                     return new JComponent[0];
60                 }
61             }
62             List<JComponent> popups = new ArrayList<JComponent>();
63             if (owner != null) {
64                 JMenu menu = createVersioningSystemPopup(owner, nodes);
65                 if (menu != null) {
66                     popups.add(menu);
67                 }
68                 VersioningSystem localHistory = VersioningManager.getInstance().getLocalHistory(toFile(nodes[0]));
69                 if(localHistory != null) {
70                     JMenu localHistoryMenu = createVersioningSystemPopup(localHistory, nodes);
71                     if(localHistoryMenu != null) {
72                         popups.add(localHistoryMenu);
73                     }
74                 }
75             } else {
76                 Lookup.Result<VersioningSystem> result = Lookup.getDefault().lookup(new Lookup.Template<VersioningSystem>(VersioningSystem.class));
77                 Collection<? extends VersioningSystem> vcs = result.allInstances();
78                 for (VersioningSystem vs : vcs) {
79                     JMenu menu = createVersioningSystemPopup(vs, nodes);
80                     if (menu != null) popups.add(menu);
81                 }
82             }
83             return popups.toArray(new JComponent[popups.size()]);
84         }
85         return new JComponent[0];
86     }
87
88     private JMenu createVersioningSystemPopup(VersioningSystem owner, Node[] nodes) {
89         VCSAnnotator an = owner.getVCSAnnotator();
90         if (an == null) return null;
91         JMenu menu = new JMenu(owner.getDisplayName());
92         VCSContext ctx = VCSContext.forNodes(nodes);
93         Action [] actions = an.getActions(ctx, VCSAnnotator.ActionDestination.PopupMenu);
94         JComponent [] items = new JComponent[actions.length];
95         int i = 0;
96         for (Action action : actions) {
97             if (action == null) {
98                 items[i++] = new JSeparator();
99             } else {
100                 JMenuItem item = new JMenuItem(action);
101                 Mnemonics.setLocalizedText(item, (String JavaDoc) action.getValue(Action.NAME));
102                 items[i++] = item;
103             }
104             menu.add(items[i-1]);
105         }
106         return menu;
107     }
108
109     private Node[] getActivatedNodes() {
110         return TopComponent.getRegistry().getActivatedNodes();
111     }
112
113     private File JavaDoc toFile(Node node) {
114         VCSContext ctx = VCSContext.forNodes(new Node [] { node });
115         return ctx.getRootFiles().iterator().next();
116     }
117
118     private class DynamicDummyItem extends JMenuItem implements DynamicMenuContent {
119         public JComponent[] getMenuPresenters() {
120             return createItems();
121         }
122
123         public JComponent[] synchMenuPresenters(JComponent[] items) {
124             return createItems();
125         }
126     }
127 }
128
Popular Tags