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-2006 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.util.actions.Presenter;
044: import org.openide.util.actions.SystemAction;
045: import org.openide.util.Lookup;
046: import org.openide.util.NbBundle;
047: import org.openide.nodes.Node;
048: import org.openide.awt.DynamicMenuContent;
049: import org.openide.awt.Mnemonics;
050: import org.openide.windows.TopComponent;
051: import org.netbeans.modules.versioning.spi.VersioningSystem;
052: import org.netbeans.modules.versioning.spi.VCSContext;
053: import org.netbeans.modules.versioning.spi.VCSAnnotator;
054: import org.netbeans.modules.diff.PatchAction;
055:
056: import javax.swing.*;
057: import java.io.File;
058: import java.awt.event.ActionEvent;
059: import java.util.*;
060:
061: /**
062: * Appears in a project's popup menu.
063: *
064: * @author Maros Sandor
065: */
066: public class ProjectMenuItem extends AbstractAction implements
067: Presenter.Popup {
068:
069: public JMenuItem getPopupPresenter() {
070: return new DynamicDummyItem();
071: }
072:
073: public void actionPerformed(ActionEvent e) {
074: // dummy, not used
075: }
076:
077: private JComponent[] createItems() {
078: Node[] nodes = getActivatedNodes();
079: if (nodes.length > 0) {
080: Set<VersioningSystem> owners = getOwners(nodes);
081: if (owners.size() != 1) {
082: return new JComponent[0];
083: }
084: VersioningSystem owner = owners.iterator().next();
085: VersioningSystem localHistory = getLocalHistory(nodes);
086: List<JComponent> popups = new ArrayList<JComponent>();
087: if (owner != null) {
088: JMenu menu = createVersioningSystemPopup(owner, nodes);
089: if (menu != null) {
090: popups.add(menu);
091: }
092: } else {
093: JMenu vmenu = new JMenu(NbBundle.getMessage(
094: ProjectMenuItem.class,
095: "CTL_MenuItem_VersioningMenu"));
096: Lookup.Result<VersioningSystem> result = Lookup
097: .getDefault().lookup(
098: new Lookup.Template<VersioningSystem>(
099: VersioningSystem.class));
100: Collection<? extends VersioningSystem> vcs = result
101: .allInstances();
102: for (VersioningSystem vs : vcs) {
103: if (vs
104: .getProperty(VersioningSystem.PROP_LOCALHISTORY_VCS) != null)
105: continue;
106: JComponent[] items = createVersioningSystemItems(
107: vs, nodes);
108: if (items != null) {
109: for (JComponent item : items) {
110: vmenu.add(item);
111: }
112: }
113: }
114: vmenu.addSeparator();
115: vmenu.add(createmenuItem(SystemAction
116: .get(PatchAction.class)));
117: popups.add(vmenu);
118: }
119: if (localHistory != null) {
120: JMenu localHistoryMenu = createVersioningSystemPopup(
121: localHistory, nodes);
122: if (localHistoryMenu != null) {
123: popups.add(localHistoryMenu);
124: }
125: }
126: return popups.toArray(new JComponent[popups.size()]);
127: }
128: return new JComponent[0];
129: }
130:
131: private VersioningSystem getLocalHistory(Node[] nodes) {
132: VCSContext ctx = VCSContext.forNodes(nodes);
133: VersioningSystem owner = null;
134: for (File file : ctx.getRootFiles()) {
135: VersioningSystem fileOwner = VersioningManager
136: .getInstance().getLocalHistory(file);
137: if (owner != null) {
138: if (fileOwner != null && fileOwner != owner)
139: return null;
140: } else {
141: owner = fileOwner;
142: }
143: }
144: return owner;
145: }
146:
147: private Set<VersioningSystem> getOwners(Node[] nodes) {
148: VCSContext ctx = VCSContext.forNodes(nodes);
149: Set<VersioningSystem> owners = new HashSet<VersioningSystem>(2);
150: for (File file : ctx.getRootFiles()) {
151: VersioningSystem fileOwner = VersioningManager
152: .getInstance().getOwner(file);
153: owners.add(fileOwner);
154: }
155: return owners;
156: }
157:
158: private JComponent[] createVersioningSystemItems(
159: VersioningSystem owner, Node[] nodes) {
160: VCSAnnotator an = owner.getVCSAnnotator();
161: if (an == null)
162: return null;
163: VCSContext ctx = VCSContext.forNodes(nodes);
164: Action[] actions = an.getActions(ctx,
165: VCSAnnotator.ActionDestination.PopupMenu);
166: JComponent[] items = new JComponent[actions.length];
167: int i = 0;
168: for (Action action : actions) {
169: if (action == null) {
170: items[i++] = Utils.createJSeparator();
171: } else {
172: JMenuItem item = createmenuItem(action);
173: items[i++] = item;
174: }
175: }
176: return items;
177: }
178:
179: private JMenuItem createmenuItem(Action action) {
180: JMenuItem item;
181: if (action instanceof SystemAction) {
182: final SystemAction sa = (SystemAction) action;
183: item = new JMenuItem(new AbstractAction(sa.getName()) {
184: public void actionPerformed(ActionEvent e) {
185: sa.actionPerformed(e);
186: }
187: });
188: } else {
189: item = new JMenuItem(action);
190: }
191: Mnemonics.setLocalizedText(item, (String) action
192: .getValue(Action.NAME));
193: return item;
194: }
195:
196: private JMenu createVersioningSystemPopup(VersioningSystem owner,
197: Node[] nodes) {
198: JComponent[] items = createVersioningSystemItems(owner, nodes);
199: if (items == null)
200: return null;
201: JMenu menu = new JMenu(Utils.getDisplayName(owner));
202: for (JComponent item : items) {
203: menu.add(item);
204: }
205: return menu;
206: }
207:
208: private Node[] getActivatedNodes() {
209: return TopComponent.getRegistry().getActivatedNodes();
210: }
211:
212: private class DynamicDummyItem extends JMenuItem implements
213: DynamicMenuContent {
214: public JComponent[] getMenuPresenters() {
215: return createItems();
216: }
217:
218: public JComponent[] synchMenuPresenters(JComponent[] items) {
219: return createItems();
220: }
221: }
222: }
|