KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.versioning;
20
21 import org.openide.awt.DynamicMenuContent;
22 import org.openide.awt.Mnemonics;
23 import org.openide.windows.TopComponent;
24 import org.netbeans.modules.versioning.spi.VersioningSystem;
25 import org.netbeans.modules.versioning.spi.VCSContext;
26 import org.netbeans.modules.versioning.spi.VCSAnnotator;
27 import org.netbeans.modules.versioning.spi.LocalHistory;
28 import org.netbeans.modules.versioning.util.Utils;
29
30 import javax.swing.*;
31 import javax.swing.event.MenuListener JavaDoc;
32 import javax.swing.event.MenuEvent JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.util.*;
35
36 /**
37  * Top level main Versioninng menu.
38  *
39  * @author Maros Sandor
40  */

41 public class VersioningMainMenu extends AbstractAction implements DynamicMenuContent {
42
43     public void actionPerformed(ActionEvent JavaDoc e) {
44         // does nothing, this is a popup menu
45
}
46
47     public JComponent[] getMenuPresenters() {
48         return createMenu();
49     }
50
51     public JComponent[] synchMenuPresenters(JComponent[] items) {
52         return createMenu();
53     }
54     
55     private JComponent[] createMenu() {
56         List<JComponent> items = new ArrayList<JComponent>(20);
57
58         final VCSContext ctx = VCSContext.forNodes(TopComponent.getRegistry().getActivatedNodes());
59         List<VersioningSystem> systems = Arrays.asList(VersioningManager.getInstance().getVersioningSystems());
60         VersioningSystem [] vs = VersioningManager.getInstance().getOwners(ctx);
61         VersioningSystem ownerVS = null;
62
63         if (vs.length == 1) {
64             if (vs[0].getVCSAnnotator() != null) {
65                 ownerVS = vs[0];
66                 List<JComponent> systemItems = actionsToItems(vs[0].getVCSAnnotator().getActions(ctx, VCSAnnotator.ActionDestination.MainMenu));
67                 items.addAll(systemItems);
68             }
69             items.add(new JSeparator());
70         } else if (vs.length > 1) {
71             JMenuItem dummy = new JMenuItem("<multiple systems>");
72             dummy.setEnabled(false);
73             items.add(dummy);
74             items.add(new JSeparator());
75         }
76         
77         Collections.sort(systems, new Comparator<VersioningSystem>() {
78             public int compare(VersioningSystem a, VersioningSystem b) {
79                 return a.getDisplayName().compareTo(b.getDisplayName());
80             }
81         });
82
83         VersioningSystem localHistory = null;
84         for (final VersioningSystem system : systems) {
85             if (system instanceof LocalHistory) {
86                 localHistory = system;
87             } else {
88                 JMenu menu = createVersioningSystemMenu(system, ctx);
89                 if (system == ownerVS) {
90                     menu.setEnabled(false);
91                 }
92                 items.add(menu);
93             }
94         }
95         
96         if (localHistory != null) {
97             items.add(new JSeparator());
98             items.add(createVersioningSystemMenu(localHistory, ctx));
99         }
100
101         return items.toArray(new JComponent[items.size()]);
102     }
103
104     private JMenu createVersioningSystemMenu(final VersioningSystem system, final VCSContext ctx) {
105         final JMenu menu = new JMenu();
106         Mnemonics.setLocalizedText(menu, "&" + system.getDisplayName());
107         menu.addMenuListener(new MenuListener JavaDoc() {
108             public void menuSelected(MenuEvent JavaDoc e) {
109                 if (menu.getItemCount() != 0) return;
110                 constructMenu(menu, system, ctx);
111             }
112     
113             public void menuDeselected(MenuEvent JavaDoc e) {
114             }
115     
116             public void menuCanceled(MenuEvent JavaDoc e) {
117             }
118         });
119         return menu;
120     }
121
122     private void constructMenu(JMenu menu, VersioningSystem system, VCSContext ctx) {
123         if (system.getVCSAnnotator() != null) {
124             List<JComponent> systemItems = actionsToItems(system.getVCSAnnotator().getActions(ctx, VCSAnnotator.ActionDestination.MainMenu));
125             for (JComponent systemItem : systemItems) {
126                 menu.add(systemItem);
127             }
128         }
129     }
130
131     private static List<JComponent> actionsToItems(Action[] actions) {
132         List<JComponent> items = new ArrayList<JComponent>(actions.length);
133         for (Action action : actions) {
134             if (action == null) {
135                 items.add(new JSeparator());
136             } else {
137                 if (action instanceof DynamicMenuContent) {
138                     DynamicMenuContent dmc = (DynamicMenuContent) action;
139                     JComponent [] components = dmc.getMenuPresenters();
140                     items.addAll(Arrays.asList(components));
141                 } else {
142                     JMenuItem item = Utils.toMenuItem(action);
143                     items.add(item);
144                 }
145             }
146         }
147         return items;
148     }
149 }
150
Popular Tags