1 19 20 package org.netbeans.modules.url; 21 22 import java.awt.Component ; 23 import java.awt.Image ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.beans.BeanInfo ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import javax.swing.AbstractButton ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.JButton ; 32 import javax.swing.JMenuItem ; 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 51 final class URLPresenter implements Presenter.Menu, 52 Presenter.Toolbar, 53 Presenter.Popup, 54 ActionListener { 55 56 57 private final URLDataObject dataObject; 58 59 64 URLPresenter(URLDataObject dataObject) { 65 this.dataObject = dataObject; 66 } 67 68 69 public JMenuItem getMenuPresenter() { 70 JMenuItem menuItem = new JMenuItem (); 71 initialize(menuItem, false); 72 return menuItem; 73 } 74 75 76 public JMenuItem getPopupPresenter() { 77 JMenuItem menuItem = new JMenuItem (); 78 initialize(menuItem, false); 79 return menuItem; 80 } 81 82 83 public Component getToolbarPresenter() { 84 JButton toolbarButton = new JButton (); 85 initialize(toolbarButton, true); 86 return toolbarButton; 87 } 88 89 94 private void initialize(AbstractButton presenter, boolean useIcons) { 95 96 if (useIcons) { 97 Image icon = Utilities.loadImage( 99 "org/netbeans/modules/url/urlObject.png"); 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 } 109 presenter.setIcon(new ImageIcon (icon)); 110 } 111 112 113 NameChangeListener listener = new NameChangeListener(presenter); 114 presenter.addPropertyChangeListener( 115 WeakListeners.propertyChange(listener, dataObject)); 116 updateName(presenter); 117 126 127 presenter.addActionListener(this); 128 HelpCtx.setHelpIDString(presenter, 129 dataObject.getHelpCtx().getHelpID()); 130 } 131 132 137 private void updateName(AbstractButton presenter) { 138 String 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 146 } 147 148 Mnemonics.setLocalizedText(presenter, name); 149 } 150 151 152 155 public void actionPerformed(ActionEvent evt) { 156 Node.Cookie open = dataObject.getCookie(OpenCookie.class); 157 if (open != null) { 158 ((OpenCookie) open).open(); 159 } 160 } 161 162 164 private class NameChangeListener implements PropertyChangeListener { 165 166 167 private final AbstractButton presenter; 168 169 171 NameChangeListener(AbstractButton presenter) { 172 this.presenter = presenter; 173 } 174 175 176 public void propertyChange(PropertyChangeEvent evt) { 177 if (DataObject.PROP_NAME.equals(evt.getPropertyName())) { 178 URLPresenter.this.updateName(presenter); 179 } 180 } 181 182 } 183 184 } | Popular Tags |