~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to javadoc/src/org/netbeans/modules/javadoc/search/IndexOverviewAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.javadoc.search;
 
43
 
 
44
import javax.swing.JPopupMenu;
 
45
 
 
46
import java.awt.event.ActionEvent;
 
47
import java.awt.event.ActionListener;
 
48
import java.net.URL;
 
49
import java.util.*;
 
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;
 
68
 
 
69
/**
 
70
 * Action which shows mounted Javadoc filesystems with known indexes as a submenu,
 
71
 * so you can choose a Javadoc set.
 
72
 *
 
73
 * @author Jesse Glick
 
74
 */
 
75
public final class IndexOverviewAction extends SystemAction implements Presenter.Menu {
 
76
    
 
77
    private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.javadoc.search.IndexOverviewAction.IndexMenu"); // NOI18N
 
78
 
 
79
    public IndexOverviewAction() {
 
80
        putValue("noIconInMenu", Boolean.TRUE); // NOI18N
 
81
    }
 
82
    
 
83
    public void actionPerformed(ActionEvent ev) {
 
84
        // do nothing -- should never be called
 
85
    }
 
86
    
 
87
    public String getName() {
 
88
        return NbBundle.getMessage(IndexOverviewAction.class, "CTL_INDICES_MenuItem");
 
89
    }
 
90
    
 
91
    protected String iconResource() {
 
92
        return null;//"org/netbeans/modules/javadoc/resources/JavaDoc.gif"; // NOI18N
 
93
    }
 
94
    
 
95
    public HelpCtx getHelpCtx() {
 
96
        return new HelpCtx("javadoc.search"); // NOI18N
 
97
    }
 
98
    
 
99
    public JMenuItem getMenuPresenter() {
 
100
        return new IndexMenu();
 
101
    }
 
102
    
 
103
    /**
 
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.
 
107
     */
 
108
    private final class IndexMenu extends JMenu implements HelpCtx.Provider, DynamicMenuContent {
 
109
        
 
110
        private int itemHash = 0;
 
111
        
 
112
        public IndexMenu() {
 
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()) {
 
119
                        getPopupMenu2();
 
120
                    }
 
121
                }
 
122
            });
 
123
        }
 
124
        
 
125
        public HelpCtx getHelpCtx() {
 
126
            return IndexOverviewAction.this.getHelpCtx();
 
127
        }
 
128
        
 
129
        public JComponent[] getMenuPresenters() {
 
130
            return new JComponent[] {this};
 
131
        }
 
132
        
 
133
        public JComponent[] synchMenuPresenters(JComponent[] items) {
 
134
            return items;
 
135
        }
 
136
        
 
137
//        public void addNotify() {
 
138
//            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
 
139
//                err.log("addNotify");
 
140
//            }
 
141
//            super.addNotify();
 
142
//            IndexBuilder.getDefault();
 
143
//        }
 
144
        
 
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 + ")");
 
151
                }
 
152
                itemHash = newHash;
 
153
                // Probably need to recreate the menu.
 
154
                removeAll();
 
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();
 
159
                if (size > 0) {
 
160
                    for (int i = 0; i < size; i++) {
 
161
                        try {
 
162
                            add(new IndexMenuItem((String)names.get(i), (FileObject)indices.get(i)));
 
163
                        } catch (FileStateInvalidException e) {
 
164
                            err.notify(ErrorManager.INFORMATIONAL, e);
 
165
                        }
 
166
                    }
 
167
                } else {
 
168
                    JMenuItem dummy = new JMenuItem(NbBundle.getMessage(IndexOverviewAction.class, "CTL_no_indices_found"));
 
169
                    dummy.setEnabled(false);
 
170
                    add(dummy);
 
171
                }
 
172
            }
 
173
        }
 
174
        
 
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...
 
182
                try {
 
183
                    x += fo.getURL().hashCode();
 
184
                } catch (FileStateInvalidException e) {
 
185
                    err.notify(ErrorManager.INFORMATIONAL, e);
 
186
                }
 
187
            }
 
188
            return x;
 
189
        }
 
190
        
 
191
    }
 
192
 
 
193
    /**
 
194
     * Menu item representing one Javadoc index.
 
195
     */
 
196
    private final class IndexMenuItem extends JMenuItem implements ActionListener, HelpCtx.Provider {
 
197
        
 
198
        /** cached url */
 
199
        private URL u;
 
200
        /** a reference to org.openide.filesystems.FileSystem */
 
201
        private final Reference fsRef;
 
202
        /** path to index file */
 
203
        private String foPath;
 
204
        
 
205
        public IndexMenuItem(String display, FileObject index) throws FileStateInvalidException {
 
206
            super(display);
 
207
            fsRef = new WeakReference(index.getFileSystem());
 
208
            foPath = index.getPath();
 
209
            addActionListener(this);
 
210
        }
 
211
        
 
212
        public void actionPerformed(ActionEvent ev) {
 
213
            URL loc = getURL();
 
214
            HtmlBrowser.URLDisplayer.getDefault().showURL(loc);
 
215
        }
 
216
        
 
217
        private URL getURL() {
 
218
            if (u == null) {
 
219
                FileSystem fs = (FileSystem) fsRef.get();
 
220
                assert fs != null;
 
221
                FileObject index = fs.findResource(foPath);
 
222
                assert index != null: foPath;
 
223
                u = JavadocURLMapper.findURL(index);
 
224
            }
 
225
            return u;
 
226
        }
 
227
        
 
228
        public HelpCtx getHelpCtx() {
 
229
            return IndexOverviewAction.this.getHelpCtx();
 
230
        }
 
231
        
 
232
    }
 
233
    
 
234
}