1 19 package org.netbeans.modules.refactoring.spi.impl; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import javax.swing.*; 25 import javax.swing.text.JTextComponent ; 26 import javax.swing.text.TextAction ; 27 import org.openide.awt.Actions; 28 import org.openide.awt.JMenuPlus; 29 import org.openide.awt.Mnemonics; 30 import org.openide.cookies.InstanceCookie; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.Repository; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.DataObject; 36 import org.openide.nodes.Node; 37 import org.openide.util.Lookup; 38 import org.openide.util.LookupListener; 39 import org.openide.util.NbBundle; 40 import org.openide.util.actions.Presenter; 41 42 46 public final class RefactoringSubMenuAction extends TextAction implements Presenter.Menu, Presenter.Popup { 47 private final boolean showIcons; 48 49 public static RefactoringSubMenuAction create(FileObject o) { 50 return new RefactoringSubMenuAction(true); 51 } 52 53 public static JMenu createMenu() { 54 RefactoringSubMenuAction action = new RefactoringSubMenuAction(true); 55 return (JMenu) action.getMenuPresenter(); 56 } 57 58 59 RefactoringSubMenuAction(boolean showIcons) { 60 super(NbBundle.getMessage(RefactoringSubMenuAction.class, "LBL_Action")); 61 this.showIcons = showIcons; 62 } 63 64 public void actionPerformed(java.awt.event.ActionEvent e) { 65 } 66 67 public javax.swing.JMenuItem getMenuPresenter() { 68 return new SubMenu(); 69 } 70 71 public javax.swing.JMenuItem getPopupPresenter() { 72 return getMenuPresenter(); 73 } 74 75 public boolean equals(Object o) { 76 return (o instanceof RefactoringSubMenuAction); 77 } 78 79 public int hashCode() { 80 return 1; 81 } 82 83 private class SubMenu extends JMenuPlus implements LookupListener { 84 private ArrayList actions = null; 85 private Lookup.Result nodes = null; 86 private boolean nodesChanged = true; 87 88 public SubMenu() { 89 super((String ) RefactoringSubMenuAction.this.getValue(Action.NAME)); 90 if (showIcons) 91 setMnemonic(NbBundle.getMessage(RefactoringSubMenuAction.class, "LBL_ActionMnemonic").charAt(0)); 92 } 93 94 95 public JPopupMenu getPopupMenu() { 96 if (actions == null) { 97 createMenuItems(); 98 } 99 return super.getPopupMenu(); 100 } 101 102 103 private void createMenuItems() { 104 actions = new ArrayList (); 105 removeAll(); 106 FileSystem dfs = Repository.getDefault().getDefaultFileSystem(); 107 FileObject fo = dfs.findResource("Menu/Refactoring"); DataFolder df = DataFolder.findFolder(fo); 109 110 if (df != null) { 111 DataObject actionObjects[] = df.getChildren(); 112 for (int i = 0; i < actionObjects.length; i++) { 113 InstanceCookie ic = (InstanceCookie) actionObjects[i].getCookie(InstanceCookie.class); 114 if (ic == null) continue; 115 Object instance; 116 try { 117 instance = ic.instanceCreate(); 118 } catch (IOException e) { 119 e.printStackTrace(); 121 continue; 122 } catch (ClassNotFoundException e) { 123 e.printStackTrace(); 125 continue; 126 } 127 if (instance instanceof RefactoringGlobalAction) { 128 actions.add(instance); 131 JMenuItem mi = new JMenuItem(); 132 Actions.connect(mi, (Action ) instance, true); 133 if (!showIcons) 134 mi.setIcon(null); 135 add(mi); 136 } else if (instance instanceof JSeparator) { 137 add((JSeparator) instance); 138 } else if (instance instanceof Presenter.Popup) { 139 JMenuItem temp = ((Presenter.Popup)instance).getPopupPresenter(); 140 if (!showIcons) 141 temp.setIcon(null); 142 add(temp); 143 } else if (instance instanceof Action ) { 144 JMenuItem mi = new JMenuItem(); 145 Actions.connect(mi, (Action ) instance, true); 146 if (!showIcons) 147 mi.setIcon(null); 148 add(mi); 149 } 150 } 151 } 152 } 153 154 private void setText(JMenuItem t, Action a) { 155 String name = (String ) a.getValue(Action.NAME); 156 int i = Mnemonics.findMnemonicAmpersand(name); 157 158 if (i < 0) 159 t.setText(name); 160 else 161 t.setText(name.substring(0, i) + name.substring(i + 1)); 162 } 163 164 public void resultChanged(org.openide.util.LookupEvent ev) { 165 nodesChanged = true; 166 } 167 168 } 169 } 170 | Popular Tags |