1 19 20 package org.netbeans.modules.javadoc.search; 21 22 import javax.swing.JPopupMenu ; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.net.URL ; 27 import java.util.*; 28 import java.lang.ref.Reference ; 29 import java.lang.ref.WeakReference ; 30 import javax.swing.JComponent ; 31 import javax.swing.JMenu ; 32 import javax.swing.JMenuItem ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import org.openide.ErrorManager; 36 import org.openide.awt.HtmlBrowser; 37 import org.openide.awt.Mnemonics; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileStateInvalidException; 40 import org.openide.filesystems.FileSystem; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.NbBundle; 43 import org.openide.awt.DynamicMenuContent; 44 import org.openide.util.actions.SystemAction; 45 import org.openide.util.actions.Presenter; 46 47 53 public final class IndexOverviewAction extends SystemAction implements Presenter.Menu { 54 55 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.javadoc.search.IndexOverviewAction.IndexMenu"); 57 public IndexOverviewAction() { 58 putValue("noIconInMenu", Boolean.TRUE); } 60 61 public void actionPerformed(ActionEvent ev) { 62 } 64 65 public String getName() { 66 return NbBundle.getMessage(IndexOverviewAction.class, "CTL_INDICES_MenuItem"); 67 } 68 69 protected String iconResource() { 70 return null; } 72 73 public HelpCtx getHelpCtx() { 74 return new HelpCtx("javadoc.search"); } 76 77 public JMenuItem getMenuPresenter() { 78 return new IndexMenu(); 79 } 80 81 86 private final class IndexMenu extends JMenu implements HelpCtx.Provider, DynamicMenuContent { 87 88 private int itemHash = 0; 89 90 public IndexMenu() { 91 Mnemonics.setLocalizedText(this, IndexOverviewAction.this.getName()); 92 getModel().addChangeListener(new ChangeListener () { 95 public void stateChanged(ChangeEvent e) { 96 if (getModel().isSelected()) { 97 getPopupMenu2(); 98 } 99 } 100 }); 101 } 102 103 public HelpCtx getHelpCtx() { 104 return IndexOverviewAction.this.getHelpCtx(); 105 } 106 107 public JComponent [] getMenuPresenters() { 108 return new JComponent [] {this}; 109 } 110 111 public JComponent [] synchMenuPresenters(JComponent [] items) { 112 return items; 113 } 114 115 123 public void getPopupMenu2() { 124 List[] data = IndexBuilder.getDefault().getIndices(); 125 int newHash = computeDataHash(data); 126 if (newHash != itemHash) { 127 if (err.isLoggable(ErrorManager.INFORMATIONAL)) { 128 err.log("recreating popup menu (" + itemHash + " -> " + newHash + ")"); 129 } 130 itemHash = newHash; 131 removeAll(); 133 List names = data[0]; List indices = data[1]; int size = names.size(); 136 if (size != indices.size()) throw new IllegalStateException (); 137 if (size > 0) { 138 for (int i = 0; i < size; i++) { 139 try { 140 add(new IndexMenuItem((String )names.get(i), (FileObject)indices.get(i))); 141 } catch (FileStateInvalidException e) { 142 err.notify(ErrorManager.INFORMATIONAL, e); 143 } 144 } 145 } else { 146 JMenuItem dummy = new JMenuItem (NbBundle.getMessage(IndexOverviewAction.class, "CTL_no_indices_found")); 147 dummy.setEnabled(false); 148 add(dummy); 149 } 150 } 151 } 152 153 private int computeDataHash(List[] data) { 154 int x = data[0].hashCode(); 155 Iterator it = data[1].iterator(); 156 while (it.hasNext()) { 157 FileObject fo = (FileObject)it.next(); 158 try { 161 x += fo.getURL().hashCode(); 162 } catch (FileStateInvalidException e) { 163 err.notify(ErrorManager.INFORMATIONAL, e); 164 } 165 } 166 return x; 167 } 168 169 } 170 171 174 private final class IndexMenuItem extends JMenuItem implements ActionListener , HelpCtx.Provider { 175 176 177 private URL u; 178 179 private final Reference fsRef; 180 181 private String foPath; 182 183 public IndexMenuItem(String display, FileObject index) throws FileStateInvalidException { 184 super(display); 185 fsRef = new WeakReference (index.getFileSystem()); 186 foPath = index.getPath(); 187 addActionListener(this); 188 } 189 190 public void actionPerformed(ActionEvent ev) { 191 URL loc = getURL(); 192 HtmlBrowser.URLDisplayer.getDefault().showURL(loc); 193 } 194 195 private URL getURL() { 196 if (u == null) { 197 FileSystem fs = (FileSystem) fsRef.get(); 198 assert fs != null; 199 FileObject index = fs.findResource(foPath); 200 assert index != null: foPath; 201 u = JavadocURLMapper.findURL(index); 202 } 203 return u; 204 } 205 206 public HelpCtx getHelpCtx() { 207 return IndexOverviewAction.this.getHelpCtx(); 208 } 209 210 } 211 212 } 213 | Popular Tags |