1 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 ; 33 import java.awt.event.ActionEvent ; 34 import java.util.*; 35 36 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 e) { 48 } 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 ) 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 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 |