2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.javadoc.search;
44
import javax.swing.JPopupMenu;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
50
import java.lang.ref.Reference;
51
import java.lang.ref.WeakReference;
52
import javax.swing.JComponent;
53
import javax.swing.JMenu;
54
import javax.swing.JMenuItem;
55
import javax.swing.event.ChangeEvent;
56
import javax.swing.event.ChangeListener;
57
import org.openide.ErrorManager;
58
import org.openide.awt.HtmlBrowser;
59
import org.openide.awt.Mnemonics;
60
import org.openide.filesystems.FileObject;
61
import org.openide.filesystems.FileStateInvalidException;
62
import org.openide.filesystems.FileSystem;
63
import org.openide.util.HelpCtx;
64
import org.openide.util.NbBundle;
65
import org.openide.awt.DynamicMenuContent;
66
import org.openide.util.actions.SystemAction;
67
import org.openide.util.actions.Presenter;
70
* Action which shows mounted Javadoc filesystems with known indexes as a submenu,
71
* so you can choose a Javadoc set.
75
public final class IndexOverviewAction extends SystemAction implements Presenter.Menu {
77
private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.javadoc.search.IndexOverviewAction.IndexMenu"); // NOI18N
79
public IndexOverviewAction() {
80
putValue("noIconInMenu", Boolean.TRUE); // NOI18N
83
public void actionPerformed(ActionEvent ev) {
84
// do nothing -- should never be called
87
public String getName() {
88
return NbBundle.getMessage(IndexOverviewAction.class, "CTL_INDICES_MenuItem");
91
protected String iconResource() {
92
return null;//"org/netbeans/modules/javadoc/resources/JavaDoc.gif"; // NOI18N
95
public HelpCtx getHelpCtx() {
96
return new HelpCtx("javadoc.search"); // NOI18N
99
public JMenuItem getMenuPresenter() {
100
return new IndexMenu();
104
* Lazy menu which when added to its parent menu, will begin creating the
105
* list of filesystems and finding their titles. When the popup for it
106
* is created, it will create submenuitems for each available index.
108
private final class IndexMenu extends JMenu implements HelpCtx.Provider, DynamicMenuContent {
110
private int itemHash = 0;
113
Mnemonics.setLocalizedText(this, IndexOverviewAction.this.getName());
114
//setIcon(IndexOverviewAction.this.getIcon());
115
// model listening is the only lazy menu procedure that works on macosx
116
getModel().addChangeListener(new ChangeListener() {
117
public void stateChanged(ChangeEvent e) {
118
if (getModel().isSelected()) {
125
public HelpCtx getHelpCtx() {
126
return IndexOverviewAction.this.getHelpCtx();
129
public JComponent[] getMenuPresenters() {
130
return new JComponent[] {this};
133
public JComponent[] synchMenuPresenters(JComponent[] items) {
137
// public void addNotify() {
138
// if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
139
// err.log("addNotify");
141
// super.addNotify();
142
// IndexBuilder.getDefault();
145
public void getPopupMenu2() {
146
List[] data = IndexBuilder.getDefault().getIndices();
147
int newHash = computeDataHash(data);
148
if (newHash != itemHash) {
149
if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
150
err.log("recreating popup menu (" + itemHash + " -> " + newHash + ")");
153
// Probably need to recreate the menu.
155
List names = data[0]; // List<String>
156
List indices = data[1]; // List<FileObject>
157
int size = names.size();
158
if (size != indices.size()) throw new IllegalStateException();
160
for (int i = 0; i < size; i++) {
162
add(new IndexMenuItem((String)names.get(i), (FileObject)indices.get(i)));
163
} catch (FileStateInvalidException e) {
164
err.notify(ErrorManager.INFORMATIONAL, e);
168
JMenuItem dummy = new JMenuItem(NbBundle.getMessage(IndexOverviewAction.class, "CTL_no_indices_found"));
169
dummy.setEnabled(false);
175
private int computeDataHash(List[] data) {
176
int x = data[0].hashCode();
177
Iterator it = data[1].iterator();
178
while (it.hasNext()) {
179
FileObject fo = (FileObject)it.next();
180
// Just using fo.hashCode() does not work because sometimes the FileObject
181
// is collected and recreated randomly, and now has a new hash code...
183
x += fo.getURL().hashCode();
184
} catch (FileStateInvalidException e) {
185
err.notify(ErrorManager.INFORMATIONAL, e);
194
* Menu item representing one Javadoc index.
196
private final class IndexMenuItem extends JMenuItem implements ActionListener, HelpCtx.Provider {
200
/** a reference to org.openide.filesystems.FileSystem */
201
private final Reference fsRef;
202
/** path to index file */
203
private String foPath;
205
public IndexMenuItem(String display, FileObject index) throws FileStateInvalidException {
207
fsRef = new WeakReference(index.getFileSystem());
208
foPath = index.getPath();
209
addActionListener(this);
212
public void actionPerformed(ActionEvent ev) {
214
HtmlBrowser.URLDisplayer.getDefault().showURL(loc);
217
private URL getURL() {
219
FileSystem fs = (FileSystem) fsRef.get();
221
FileObject index = fs.findResource(foPath);
222
assert index != null: foPath;
223
u = JavadocURLMapper.findURL(index);
228
public HelpCtx getHelpCtx() {
229
return IndexOverviewAction.this.getHelpCtx();