KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > search > IndexOverviewAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javadoc.search;
21
22 import javax.swing.JPopupMenu JavaDoc;
23
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.*;
28 import java.lang.ref.Reference JavaDoc;
29 import java.lang.ref.WeakReference JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JMenu JavaDoc;
32 import javax.swing.JMenuItem JavaDoc;
33 import javax.swing.event.ChangeEvent JavaDoc;
34 import javax.swing.event.ChangeListener JavaDoc;
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 /**
48  * Action which shows mounted Javadoc filesystems with known indexes as a submenu,
49  * so you can choose a Javadoc set.
50  *
51  * @author Jesse Glick
52  */

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"); // NOI18N
56

57     public IndexOverviewAction() {
58         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
59
}
60     
61     public void actionPerformed(ActionEvent JavaDoc ev) {
62         // do nothing -- should never be called
63
}
64     
65     public String JavaDoc getName() {
66         return NbBundle.getMessage(IndexOverviewAction.class, "CTL_INDICES_MenuItem");
67     }
68     
69     protected String JavaDoc iconResource() {
70         return null;//"org/netbeans/modules/javadoc/resources/JavaDoc.gif"; // NOI18N
71
}
72     
73     public HelpCtx getHelpCtx() {
74         return new HelpCtx("javadoc.search"); // NOI18N
75
}
76     
77     public JMenuItem JavaDoc getMenuPresenter() {
78         return new IndexMenu();
79     }
80     
81     /**
82      * Lazy menu which when added to its parent menu, will begin creating the
83      * list of filesystems and finding their titles. When the popup for it
84      * is created, it will create submenuitems for each available index.
85      */

86     private final class IndexMenu extends JMenu JavaDoc implements HelpCtx.Provider, DynamicMenuContent {
87         
88         private int itemHash = 0;
89         
90         public IndexMenu() {
91             Mnemonics.setLocalizedText(this, IndexOverviewAction.this.getName());
92             //setIcon(IndexOverviewAction.this.getIcon());
93
// model listening is the only lazy menu procedure that works on macosx
94
getModel().addChangeListener(new ChangeListener JavaDoc() {
95                 public void stateChanged(ChangeEvent JavaDoc 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 JavaDoc[] getMenuPresenters() {
108             return new JComponent JavaDoc[] {this};
109         }
110         
111         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
112             return items;
113         }
114         
115 // public void addNotify() {
116
// if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
117
// err.log("addNotify");
118
// }
119
// super.addNotify();
120
// IndexBuilder.getDefault();
121
// }
122

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                 // Probably need to recreate the menu.
132
removeAll();
133                 List names = data[0]; // List<String>
134
List indices = data[1]; // List<FileObject>
135
int size = names.size();
136                 if (size != indices.size()) throw new IllegalStateException JavaDoc();
137                 if (size > 0) {
138                     for (int i = 0; i < size; i++) {
139                         try {
140                             add(new IndexMenuItem((String JavaDoc)names.get(i), (FileObject)indices.get(i)));
141                         } catch (FileStateInvalidException e) {
142                             err.notify(ErrorManager.INFORMATIONAL, e);
143                         }
144                     }
145                 } else {
146                     JMenuItem JavaDoc dummy = new JMenuItem JavaDoc(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                 // Just using fo.hashCode() does not work because sometimes the FileObject
159
// is collected and recreated randomly, and now has a new hash code...
160
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     /**
172      * Menu item representing one Javadoc index.
173      */

174     private final class IndexMenuItem extends JMenuItem JavaDoc implements ActionListener JavaDoc, HelpCtx.Provider {
175         
176         /** cached url */
177         private URL JavaDoc u;
178         /** a reference to org.openide.filesystems.FileSystem */
179         private final Reference JavaDoc fsRef;
180         /** path to index file */
181         private String JavaDoc foPath;
182         
183         public IndexMenuItem(String JavaDoc display, FileObject index) throws FileStateInvalidException {
184             super(display);
185             fsRef = new WeakReference JavaDoc(index.getFileSystem());
186             foPath = index.getPath();
187             addActionListener(this);
188         }
189         
190         public void actionPerformed(ActionEvent JavaDoc ev) {
191             URL JavaDoc loc = getURL();
192             HtmlBrowser.URLDisplayer.getDefault().showURL(loc);
193         }
194         
195         private URL JavaDoc 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