2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.versioning;
43
import org.openide.awt.DynamicMenuContent;
44
import org.openide.awt.Mnemonics;
45
import org.openide.windows.TopComponent;
46
import org.netbeans.modules.versioning.spi.VersioningSystem;
47
import org.netbeans.modules.versioning.spi.VCSContext;
48
import org.netbeans.modules.versioning.spi.VCSAnnotator;
51
import javax.swing.event.MenuListener;
52
import javax.swing.event.MenuEvent;
53
import java.awt.event.ActionEvent;
57
* Top level main Versioninng menu.
59
* @author Maros Sandor
61
public class VersioningMainMenu extends AbstractAction implements DynamicMenuContent {
63
public void actionPerformed(ActionEvent e) {
64
// does nothing, this is a popup menu
67
public JComponent[] getMenuPresenters() {
71
public JComponent[] synchMenuPresenters(JComponent[] items) {
75
private JComponent[] createMenu() {
76
List<JComponent> items = new ArrayList<JComponent>(20);
78
final VCSContext ctx = VCSContext.forNodes(TopComponent.getRegistry().getActivatedNodes());
79
List<VersioningSystem> systems = Arrays.asList(VersioningManager.getInstance().getVersioningSystems());
80
VersioningSystem [] vs = VersioningManager.getInstance().getOwners(ctx);
81
VersioningSystem ownerVS = null;
84
if (vs[0].getVCSAnnotator() != null) {
86
List<JComponent> systemItems = actionsToItems(vs[0].getVCSAnnotator().getActions(ctx, VCSAnnotator.ActionDestination.MainMenu));
87
items.addAll(systemItems);
89
items.add(Utils.createJSeparator());
90
} else if (vs.length > 1) {
91
JMenuItem dummy = new JMenuItem("<multiple systems>");
92
dummy.setEnabled(false);
94
items.add(Utils.createJSeparator());
97
Collections.sort(systems, new Comparator<VersioningSystem>() {
98
public int compare(VersioningSystem a, VersioningSystem b) {
99
return Utils.getDisplayName(a).compareTo(Utils.getDisplayName(b));
103
VersioningSystem localHistory = null;
104
for (final VersioningSystem system : systems) {
105
if (Utils.isLocalHistory(system)) {
106
localHistory = system;
108
JMenu menu = createVersioningSystemMenu(system, ctx);
109
if (system == ownerVS) {
110
menu.setEnabled(false);
116
if (localHistory != null) {
117
items.add(Utils.createJSeparator());
118
items.add(createVersioningSystemMenu(localHistory, ctx));
121
return items.toArray(new JComponent[items.size()]);
124
private JMenu createVersioningSystemMenu(final VersioningSystem system, final VCSContext ctx) {
125
final JMenu menu = new JMenu();
126
Mnemonics.setLocalizedText(menu, Utils.getMenuLabel(system));
127
menu.addMenuListener(new MenuListener() {
128
public void menuSelected(MenuEvent e) {
129
if (menu.getItemCount() != 0) return;
130
constructMenu(menu, system, ctx);
133
public void menuDeselected(MenuEvent e) {
136
public void menuCanceled(MenuEvent e) {
142
private void constructMenu(JMenu menu, VersioningSystem system, VCSContext ctx) {
143
if (system.getVCSAnnotator() != null) {
144
List<JComponent> systemItems = actionsToItems(system.getVCSAnnotator().getActions(ctx, VCSAnnotator.ActionDestination.MainMenu));
145
for (JComponent systemItem : systemItems) {
146
menu.add(systemItem);
151
private static List<JComponent> actionsToItems(Action[] actions) {
152
List<JComponent> items = new ArrayList<JComponent>(actions.length);
153
for (Action action : actions) {
154
if (action == null) {
155
items.add(Utils.createJSeparator());
157
if (action instanceof DynamicMenuContent) {
158
DynamicMenuContent dmc = (DynamicMenuContent) action;
159
JComponent [] components = dmc.getMenuPresenters();
160
items.addAll(Arrays.asList(components));
162
JMenuItem item = Utils.toMenuItem(action);