1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.BeanInfo ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.IOException ; 28 import java.text.MessageFormat ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import javax.swing.*; 33 import javax.swing.JPopupMenu.Separator; 34 import javax.swing.event.PopupMenuEvent ; 35 import javax.swing.event.PopupMenuListener ; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.modules.project.ui.NewFileWizard; 38 import org.netbeans.modules.project.ui.NoProjectNew; 39 import org.netbeans.modules.project.ui.OpenProjectList; 40 import org.netbeans.modules.project.ui.ProjectUtilities; 41 import org.netbeans.spi.project.ui.templates.support.Templates; 42 import org.openide.ErrorManager; 43 import org.openide.awt.Mnemonics; 44 import org.openide.filesystems.FileObject; 45 import org.openide.loaders.DataFolder; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.nodes.Node; 49 import org.openide.util.Lookup; 50 import org.openide.util.Mutex; 51 import org.openide.util.NbBundle; 52 import org.openide.util.Utilities; 53 import org.openide.util.WeakListeners; 54 import org.openide.util.actions.Presenter.Popup; 55 56 58 public class NewFile extends ProjectAction implements PropertyChangeListener , Popup, PopupMenuListener { 59 60 private static final Icon ICON = new ImageIcon( Utilities.loadImage( "org/netbeans/modules/project/ui/resources/newFile.gif" ) ); private static final String NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Name" ); private static final String _SHORT_DESCRIPTION = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Tooltip" ); private static final String POPUP_NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_PopupName" ); private static final String FILE_POPUP_NAME = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_File_PopupName" ); private static final String TEMPLATE_NAME_FORMAT = NbBundle.getMessage( NewFile.class, "LBL_NewFileAction_Template_PopupName" ); 67 private JMenu subMenu; 68 69 public NewFile() { 70 this( null ); 71 } 72 73 public NewFile( Lookup context ) { 74 super( (String )null, NAME, ICON, context ); putValue("iconBase","org/netbeans/modules/project/ui/resources/newFile.gif"); putValue(SHORT_DESCRIPTION, _SHORT_DESCRIPTION); 77 OpenProjectList.getDefault().addPropertyChangeListener( WeakListeners.propertyChange( this, OpenProjectList.getDefault() ) ); 78 refresh( getLookup() ); 79 } 80 81 protected void refresh( Lookup context ) { 82 Mutex.EVENT.readAccess(new Runnable () { 84 public void run() { 85 setEnabled(OpenProjectList.getDefault().getOpenProjects().length > 0); 86 setDisplayName(NAME); 87 } 88 }); 89 } 90 91 public JMenuItem getPopupPresenter() { 92 JMenuItem menu = new JMenuItem(this); 93 menu.setIcon(null); 94 Mnemonics.setLocalizedText(menu, (String ) getValue(Action.NAME)); 95 return menu; 97 } 98 99 101 protected void actionPerformed( Lookup context ) { 102 doPerform( context, null, true ); 103 } 104 105 private void doPerform( Lookup context, DataObject template, boolean inProject ) { 106 107 if ( context == null ) { 108 context = getLookup(); 109 } 110 111 if ( !inProject ) { 112 NoProjectNew.showDialog( template, preselectedFolder( context ) ); 114 return; 115 } 116 117 NewFileWizard wd = new NewFileWizard( preselectedProject( context ) ); 118 119 DataFolder preselectedFolder = preselectedFolder( context ); 120 if ( preselectedFolder != null ) { 121 wd.setTargetFolder( preselectedFolder ); 122 } 123 124 try { 125 Set resultSet = template == null ? wd.instantiate () : wd.instantiate( template ); 126 127 if (resultSet == null || resultSet.isEmpty ()) { 128 return ; 130 } 131 132 Iterator it = resultSet.iterator (); 133 134 while (it.hasNext ()) { 135 Object obj = it.next (); 136 DataObject newDO = null; 137 if (obj instanceof DataObject) { 138 newDO = (DataObject) obj; 139 } else if (obj instanceof FileObject) { 140 try { 141 newDO = DataObject.find ((FileObject) obj); 142 } catch (DataObjectNotFoundException x) { 143 assert false : obj; 145 } 146 } else { 147 assert false : obj; 148 } 149 if (newDO != null) { 150 ProjectUtilities.openAndSelectNewObject (newDO); 151 } 152 } 153 } 154 catch ( IOException e ) { 155 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); 156 } 157 158 Project project = Templates.getProject( wd ); 160 FileObject foTemplate = Templates.getTemplate( wd ); 161 OpenProjectList.getDefault().updateTemplatesLRU( foTemplate ); 162 163 } 164 165 167 public Action createContextAwareInstance( Lookup actionContext ) { 168 return new NewFile( actionContext ); 169 } 170 171 173 public JMenuItem getSubmenuPopupPresenter() { 174 if (subMenu == null) { 175 subMenu = new JMenu(POPUP_NAME); 176 subMenu.getPopupMenu().addPopupMenuListener(this); 177 } 178 return subMenu; 179 } 180 181 private void fillSubMenu() { 182 Project projects[] = ActionsUtil.getProjectsFromLookup( getLookup(), null ); 183 if ( projects != null && projects.length > 0 ) { 184 fillSubMenu(subMenu, projects[0]); 185 } 186 else { 187 fillNonProjectSubMenu(subMenu); 189 } 190 } 191 192 194 private Project preselectedProject( Lookup context ) { 195 Project preselectedProject = null; 196 197 199 Project[] projects = ActionsUtil.getProjectsFromLookup( context, null ); 200 if ( projects.length > 0 ) { 201 preselectedProject = projects[0]; 202 } 203 204 205 if ( preselectedProject == null ) { 206 preselectedProject = OpenProjectList.getDefault().getMainProject(); 208 if ( preselectedProject == null ) { 209 preselectedProject = OpenProjectList.getDefault().getOpenProjects()[0]; 211 } 212 } 213 214 if ( preselectedProject == null ) { 215 assert false : "Action should be disabled"; } 217 218 return preselectedProject; 219 } 220 221 private DataFolder preselectedFolder( Lookup context ) { 222 223 DataFolder preselectedFolder = null; 224 225 preselectedFolder = (DataFolder)context.lookup( DataFolder.class ); 227 if ( preselectedFolder == null ) { 228 DataObject dobj = (DataObject)context.lookup( DataObject.class ); 230 if ( dobj != null) { 231 preselectedFolder = dobj.getFolder(); 233 } 234 } 235 236 return preselectedFolder; 237 } 238 239 public void propertyChange(PropertyChangeEvent evt) { 240 refresh( Lookup.EMPTY ); 241 } 242 243 public static String TEMPLATE_PROPERTY = "org.netbeans.modules.project.ui.actions.NewFile.Template"; public static String IN_PROJECT_PROPERTY = "org.netbeans.modules.project.ui.actions.NewFile.InProject"; 246 247 private void fillSubMenu(JMenu menuItem, Project project) { 248 menuItem.removeAll(); 249 250 ActionListener menuListener = new PopupListener(); 251 252 JMenuItem fileItem = new JMenuItem( FILE_POPUP_NAME, (Icon)getValue( Action.SMALL_ICON ) ); 253 fileItem.addActionListener( menuListener ); 254 fileItem.putClientProperty( TEMPLATE_PROPERTY, null ); 255 menuItem.add( fileItem ); 256 257 List lruList = OpenProjectList.getDefault().getTemplatesLRU( project ); 258 boolean first = true; 259 for( Iterator it = lruList.iterator(); it.hasNext(); ) { 260 DataObject template = (DataObject)it.next(); 261 262 Node delegate = template.getNodeDelegate(); 263 JMenuItem item = new JMenuItem( 264 MessageFormat.format( TEMPLATE_NAME_FORMAT, new Object [] { delegate.getDisplayName() } ), 265 new ImageIcon( delegate.getIcon( BeanInfo.ICON_COLOR_16x16 ) ) ); 266 item.addActionListener( menuListener ); 267 item.putClientProperty( TEMPLATE_PROPERTY, template ); 268 if ( first ) { 269 menuItem.add( new Separator() ); 270 first = false; 271 } 272 menuItem.add( item ); 273 } 274 } 275 276 277 private void fillNonProjectSubMenu(JMenu menuItem) { 278 menuItem.removeAll(); 279 280 ActionListener menuListener = new PopupListener(); 281 282 DataFolder preselectedFolder = preselectedFolder( getLookup() ); 283 284 boolean canWrite; 285 if ( preselectedFolder == null ) { 286 canWrite = false; 287 } 288 else { 289 FileObject pf = preselectedFolder.getPrimaryFile(); 290 canWrite = pf != null && pf.canWrite(); 291 } 292 293 DataObject templates[] = NoProjectNew.getTemplates(); 294 for( int i = 0; i < templates.length; i++ ) { 295 Node n = templates[i].getNodeDelegate(); 296 JMenuItem item = new JMenuItem( 297 MessageFormat.format( TEMPLATE_NAME_FORMAT, new Object [] { n.getDisplayName() } ), 298 new ImageIcon( n.getIcon( BeanInfo.ICON_COLOR_16x16 ) ) ); 299 item.addActionListener( menuListener ); 300 item.putClientProperty( TEMPLATE_PROPERTY, templates[i] ); 301 item.putClientProperty( IN_PROJECT_PROPERTY, Boolean.FALSE ); 302 item.setEnabled( canWrite ); 303 menuItem.add( item ); 304 } 305 } 306 307 private class PopupListener implements ActionListener { 308 309 public void actionPerformed( ActionEvent e ) { 310 JMenuItem source = (JMenuItem)e.getSource(); 311 312 Boolean inProject = (Boolean )source.getClientProperty( IN_PROJECT_PROPERTY ); 313 DataObject template = (DataObject)source.getClientProperty( TEMPLATE_PROPERTY ); 314 315 if ( inProject != null && inProject == Boolean.FALSE ) { 316 doPerform( null, template, false ); 317 } 318 else { 319 doPerform( null, template, true ); 320 } 321 } 322 323 } 324 325 327 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 328 fillSubMenu(); 329 } 330 331 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 332 } 333 334 public void popupMenuCanceled(PopupMenuEvent e) { 335 } 336 337 340 public static final class WithSubMenu extends NewFile { 341 342 public WithSubMenu() {} 343 344 private WithSubMenu(Lookup actionContext) { 345 super(actionContext); 346 } 347 348 public JMenuItem getPopupPresenter() { 349 return getSubmenuPopupPresenter(); 350 } 351 352 public Action createContextAwareInstance(Lookup actionContext) { 353 return new WithSubMenu(actionContext); 354 } 355 356 } 357 358 } 359 | Popular Tags |