KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > url > URLPresenter


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.url;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.beans.BeanInfo JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import javax.swing.AbstractButton JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JMenuItem JavaDoc;
33 import org.openide.awt.Mnemonics;
34 import org.openide.cookies.OpenCookie;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileStateInvalidException;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.loaders.DataObject;
39 import org.openide.nodes.Node;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.Utilities;
42 import org.openide.util.WeakListeners;
43 import org.openide.util.actions.Presenter;
44
45 /**
46  * Presenter which creates actual components on demand.
47  *
48  * @author Ian Formanek
49  * @author Marian Petras
50  */

51 final class URLPresenter implements Presenter.Menu,
52                                     Presenter.Toolbar,
53                                     Presenter.Popup,
54                                     ActionListener JavaDoc {
55
56     /** <code>URLDataObject</code> this presenter presents */
57     private final URLDataObject dataObject;
58     
59     /**
60      * Creates a new presenter for a specified <code>URLDataObject</code>.
61      *
62      * @param dataObject <code>URLDataObject</code> to represent
63      */

64     URLPresenter(URLDataObject dataObject) {
65         this.dataObject = dataObject;
66     }
67
68     /* implements interface Presenter.Menu */
69     public JMenuItem JavaDoc getMenuPresenter() {
70         JMenuItem JavaDoc menuItem = new JMenuItem JavaDoc();
71         initialize(menuItem, false);
72         return menuItem;
73     }
74
75     /* implements interface Presenter.Popup */
76     public JMenuItem JavaDoc getPopupPresenter() {
77         JMenuItem JavaDoc menuItem = new JMenuItem JavaDoc();
78         initialize(menuItem, false);
79         return menuItem;
80     }
81
82     /* implements interface Presenter.Toolbar */
83     public Component JavaDoc getToolbarPresenter() {
84         JButton JavaDoc toolbarButton = new JButton JavaDoc();
85         initialize(toolbarButton, true);
86         return toolbarButton;
87     }
88
89     /**
90      * Initializes a specified presenter.
91      *
92      * @param presenter presenter to initialize
93      */

94     private void initialize(AbstractButton JavaDoc presenter, boolean useIcons) {
95
96         if (useIcons) {
97             // set the presenter's icon:
98
Image JavaDoc icon = Utilities.loadImage(
99                     "org/netbeans/modules/url/urlObject.png"); //NOI18N
100
try {
101                 FileObject file = dataObject.getPrimaryFile();
102                 FileSystem.Status fsStatus = file.getFileSystem().getStatus();
103                 icon = fsStatus.annotateIcon(icon,
104                                              BeanInfo.ICON_COLOR_16x16,
105                                              dataObject.files());
106             } catch (FileStateInvalidException fsie) {
107                 // OK, so we use the default icon
108
}
109             presenter.setIcon(new ImageIcon JavaDoc(icon));
110         }
111
112         /* set the presenter's text and ensure it is maintained up-to-date: */
113         NameChangeListener listener = new NameChangeListener(presenter);
114         presenter.addPropertyChangeListener(
115                 WeakListeners.propertyChange(listener, dataObject));
116         updateName(presenter);
117         /*
118          * The above code works with the assumption that it is called
119          * from the AWT event dispatching thread (it manipulates
120          * the presenter's display name). The same applies to
121          * NameChangeListener's method propertyChange(...).
122          *
123          * At least, both mentioned parts of code should be called from
124          * the same thread since method updateText(...) is not thread-safe.
125          */

126
127         presenter.addActionListener(this);
128         HelpCtx.setHelpIDString(presenter,
129                                 dataObject.getHelpCtx().getHelpID());
130     }
131
132     /**
133      * Updates display text and tooltip of a specified presenter.
134      *
135      * @param presenter presenter whose name is to be updated
136      */

137     private void updateName(AbstractButton JavaDoc presenter) {
138         String JavaDoc name = dataObject.getName();
139
140         try {
141             FileObject file = dataObject.getPrimaryFile();
142             FileSystem.Status fsStatus = file.getFileSystem().getStatus();
143             name = fsStatus.annotateName(name, dataObject.files());
144         } catch (FileStateInvalidException fsie) {
145             /* OK, so we use the default name */
146         }
147
148         Mnemonics.setLocalizedText(presenter, name);
149     }
150
151     /* implements interface ActionListener */
152     /**
153      * Performs operation <em>open</em> of the <code>DataObject</code>.
154      */

155     public void actionPerformed(ActionEvent JavaDoc evt) {
156         Node.Cookie open = dataObject.getCookie(OpenCookie.class);
157         if (open != null) {
158             ((OpenCookie) open).open();
159         }
160     }
161
162     /**
163      */

164     private class NameChangeListener implements PropertyChangeListener JavaDoc {
165
166         /** */
167         private final AbstractButton JavaDoc presenter;
168
169         /**
170          */

171         NameChangeListener(AbstractButton JavaDoc presenter) {
172             this.presenter = presenter;
173         }
174
175         /* Implements interface PropertyChangeListener. */
176         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
177             if (DataObject.PROP_NAME.equals(evt.getPropertyName())) {
178                 URLPresenter.this.updateName(presenter);
179             }
180         }
181
182     }
183
184 }
Popular Tags