KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > CustomizeProject


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.project.ui.actions;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.modules.project.ui.ExitDialog;
31 import org.netbeans.spi.project.support.ProjectOperations;
32 import org.netbeans.spi.project.ui.CustomizerProvider;
33 import org.openide.DialogDisplayer;
34 import org.openide.NotifyDescriptor;
35 import org.openide.cookies.SaveCookie;
36 import org.openide.loaders.DataObject;
37 import org.openide.util.Lookup;
38 import org.openide.util.NbBundle;
39 import org.openide.util.actions.Presenter;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42
43 /** Action for invoking project customizer
44  */

45 public class CustomizeProject extends ProjectAction implements Presenter.Popup {
46
47     private static final String JavaDoc namePattern = NbBundle.getMessage( CustomizeProject.class, "LBL_CustomizeProjectAction_Name" ); // NOI18N
48
private static final String JavaDoc namePatternPopup = NbBundle.getMessage( CustomizeProject.class, "LBL_CustomizeProjectAction_Popup_Name" ); // NOI18N
49

50     public CustomizeProject() {
51         this( null );
52     }
53     
54     public CustomizeProject( Lookup context ) {
55         super( (String JavaDoc)null, namePattern, null, context );
56         refresh( getLookup() );
57     }
58             
59     
60    
61     protected void refresh( Lookup context ) {
62      
63         super.refresh( context );
64         
65         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
66                             
67         if ( projects.length != 1 || projects[0].getLookup().lookup( CustomizerProvider.class ) == null ) {
68             setEnabled( false );
69             // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, new Project[0] ) );
70
}
71         else {
72             setEnabled( true );
73             // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, projects ) );
74
}
75         
76         
77     }
78     
79     public void actionPerformed( Lookup context ) {
80     
81         Project[] projects = ActionsUtil.getProjectsFromLookup( context, null );
82         
83         if ( projects.length == 1 ) {
84             CustomizerProvider cp = projects[0].getLookup().lookup( CustomizerProvider.class );
85             if ( cp != null ) {
86                 if (!DataObject.getRegistry().getModifiedSet().isEmpty()) {
87                     // #50992: danger! Project properties dialog may try to write to the same config files.
88

89                     //#92011 - reducing the frequency of the dialog popping up.
90
Set JavaDoc<DataObject> candidates = new HashSet JavaDoc<DataObject>();
91                     List JavaDoc<FileObject> dataFiles = ProjectOperations.getDataFiles(projects[0]);
92                     boolean opSupported = ProjectOperations.isCopyOperationSupported(projects[0]) ||
93                                           ProjectOperations.isMoveOperationSupported(projects[0]) ||
94                                           ProjectOperations.isDeleteOperationSupported(projects[0]);
95                                           
96                     for (DataObject dobj : DataObject.getRegistry().getModifiedSet()) {
97                         // only consider files from our project
98
if (projects[0] == FileOwnerQuery.getOwner(dobj.getPrimaryFile())) {
99                             // now check if it's metadata or data - not 100% bulletproof, but should reduce the probability significantly
100
boolean found = false;
101                             for (FileObject df : dataFiles) {
102                                 if (df.equals(dobj.getPrimaryFile()) ||
103                                         (df.isFolder() && FileUtil.isParentOf(df, dobj.getPrimaryFile()))) {
104                                     found = true;
105                                     break;
106                                 }
107                             }
108                             if (!found || !opSupported) {
109                                 candidates.add(dobj);
110                             }
111                         }
112                     }
113                     if (!candidates.isEmpty()) {
114                         String JavaDoc saveAll = NbBundle.getMessage(CustomizeProject.class, "CustomizeProject.saveAll");
115                         Object JavaDoc ret = DialogDisplayer.getDefault().notify(new NotifyDescriptor(
116                                 NbBundle.getMessage(CustomizeProject.class, "CustomizeProject.save_modified_files"),
117                                 NbBundle.getMessage(CustomizeProject.class, "CustomizeProject.save_modified_title"),
118                                 NotifyDescriptor.OK_CANCEL_OPTION,
119                                 NotifyDescriptor.WARNING_MESSAGE,
120                                 new Object JavaDoc[] {
121                                     saveAll,
122                                     NotifyDescriptor.CANCEL_OPTION
123                                 },
124                                 saveAll));
125                         if (ret != saveAll) {
126                             return;
127                         } else {
128                             for (DataObject dobj : candidates) {
129                                 ExitDialog.doSave(dobj);
130                             }
131                         }
132                     }
133                 }
134                 cp.showCustomizer();
135             }
136         }
137         
138     }
139     
140     public Action JavaDoc createContextAwareInstance( Lookup actionContext ) {
141         return new CustomizeProject( actionContext );
142     }
143     
144     
145     // Implementation of Presenter.Popup ---------------------------------------
146

147     public JMenuItem JavaDoc getPopupPresenter() {
148         JMenuItem JavaDoc popupPresenter = new JMenuItem JavaDoc( this );
149         popupPresenter.setText( namePatternPopup );
150         popupPresenter.setIcon( null );
151
152         return popupPresenter;
153     }
154     
155 }
156
Popular Tags