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: package org.netbeans.modules.versioning;
042:
043: import org.openide.awt.DynamicMenuContent;
044: import org.openide.awt.Mnemonics;
045: import org.openide.windows.TopComponent;
046: import org.netbeans.modules.versioning.spi.VersioningSystem;
047: import org.netbeans.modules.versioning.spi.VCSContext;
048: import org.netbeans.modules.versioning.spi.VCSAnnotator;
049:
050: import javax.swing.*;
051: import javax.swing.event.MenuListener;
052: import javax.swing.event.MenuEvent;
053: import java.awt.event.ActionEvent;
054: import java.util.*;
055:
056: /**
057: * Top level main Versioninng menu.
058: *
059: * @author Maros Sandor
060: */
061: public class VersioningMainMenu extends AbstractAction implements
062: DynamicMenuContent {
063:
064: public void actionPerformed(ActionEvent e) {
065: // does nothing, this is a popup menu
066: }
067:
068: public JComponent[] getMenuPresenters() {
069: return createMenu();
070: }
071:
072: public JComponent[] synchMenuPresenters(JComponent[] items) {
073: return createMenu();
074: }
075:
076: private JComponent[] createMenu() {
077: List<JComponent> items = new ArrayList<JComponent>(20);
078:
079: final VCSContext ctx = VCSContext.forNodes(TopComponent
080: .getRegistry().getActivatedNodes());
081: List<VersioningSystem> systems = Arrays
082: .asList(VersioningManager.getInstance()
083: .getVersioningSystems());
084: VersioningSystem[] vs = VersioningManager.getInstance()
085: .getOwners(ctx);
086:
087: if (vs.length == 1) {
088: if (vs[0].getVCSAnnotator() != null) {
089: List<JComponent> systemItems = actionsToItems(vs[0]
090: .getVCSAnnotator()
091: .getActions(ctx,
092: VCSAnnotator.ActionDestination.MainMenu));
093: items.addAll(systemItems);
094: }
095: items.add(Utils.createJSeparator());
096: } else if (vs.length > 1) {
097: JMenuItem dummy = new JMenuItem("<multiple systems>");
098: dummy.setEnabled(false);
099: items.add(dummy);
100: items.add(Utils.createJSeparator());
101: }
102:
103: Collections.sort(systems, new Comparator<VersioningSystem>() {
104: public int compare(VersioningSystem a, VersioningSystem b) {
105: return Utils.getDisplayName(a).compareTo(
106: Utils.getDisplayName(b));
107: }
108: });
109:
110: VersioningSystem localHistory = null;
111: for (final VersioningSystem system : systems) {
112: if (Utils.isLocalHistory(system)) {
113: localHistory = system;
114: } else {
115: JMenu menu = createVersioningSystemMenu(system, ctx);
116: items.add(menu);
117: }
118: }
119:
120: if (localHistory != null) {
121: items.add(Utils.createJSeparator());
122: items.add(createVersioningSystemMenu(localHistory, ctx));
123: }
124:
125: return items.toArray(new JComponent[items.size()]);
126: }
127:
128: private JMenu createVersioningSystemMenu(
129: final VersioningSystem system, final VCSContext ctx) {
130: final JMenu menu = new JMenu();
131: Mnemonics.setLocalizedText(menu, Utils.getMenuLabel(system));
132: menu.addMenuListener(new MenuListener() {
133: public void menuSelected(MenuEvent e) {
134: if (menu.getItemCount() != 0)
135: return;
136: constructMenu(menu, system, ctx);
137: }
138:
139: public void menuDeselected(MenuEvent e) {
140: }
141:
142: public void menuCanceled(MenuEvent e) {
143: }
144: });
145: return menu;
146: }
147:
148: private void constructMenu(JMenu menu, VersioningSystem system,
149: VCSContext ctx) {
150: if (system.getVCSAnnotator() != null) {
151: List<JComponent> systemItems = actionsToItems(system
152: .getVCSAnnotator().getActions(ctx,
153: VCSAnnotator.ActionDestination.MainMenu));
154: for (JComponent systemItem : systemItems) {
155: menu.add(systemItem);
156: }
157: }
158: }
159:
160: private static List<JComponent> actionsToItems(Action[] actions) {
161: List<JComponent> items = new ArrayList<JComponent>(
162: actions.length);
163: for (Action action : actions) {
164: if (action == null) {
165: items.add(Utils.createJSeparator());
166: } else {
167: if (action instanceof DynamicMenuContent) {
168: DynamicMenuContent dmc = (DynamicMenuContent) action;
169: JComponent[] components = dmc.getMenuPresenters();
170: items.addAll(Arrays.asList(components));
171: } else {
172: JMenuItem item = Utils.toMenuItem(action);
173: items.add(item);
174: }
175: }
176: }
177: return items;
178: }
179: }
|