1 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 ; 32 import javax.swing.event.MenuEvent ; 33 import java.awt.event.ActionEvent ; 34 import java.util.*; 35 36 41 public class VersioningMainMenu extends AbstractAction implements DynamicMenuContent { 42 43 public void actionPerformed(ActionEvent e) { 44 } 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 () { 108 public void menuSelected(MenuEvent e) { 109 if (menu.getItemCount() != 0) return; 110 constructMenu(menu, system, ctx); 111 } 112 113 public void menuDeselected(MenuEvent e) { 114 } 115 116 public void menuCanceled(MenuEvent 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 |