KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > SimpleTargetChooserPanel


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;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.SourceGroup;
31 import org.netbeans.spi.project.ui.templates.support.Templates;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37
38 /**
39  *
40  * @author Petr Hrebejk
41  */

42 final class SimpleTargetChooserPanel implements WizardDescriptor.Panel, ChangeListener JavaDoc {
43
44     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
45     private SimpleTargetChooserPanelGUI gui;
46
47     private Project project;
48     private SourceGroup[] folders;
49     private WizardDescriptor.Panel bottomPanel;
50     private WizardDescriptor wizard;
51     private boolean isFolder;
52     
53     SimpleTargetChooserPanel( Project project, SourceGroup[] folders, WizardDescriptor.Panel bottomPanel, boolean isFolder ) {
54         this.folders = folders;
55         this.project = project;
56         this.bottomPanel = bottomPanel;
57         if ( bottomPanel != null ) {
58             bottomPanel.addChangeListener( this );
59         }
60         this.isFolder = isFolder;
61         this.gui = null;
62     }
63
64     public Component JavaDoc getComponent() {
65         if (gui == null) {
66             gui = new SimpleTargetChooserPanelGUI( project, folders, bottomPanel == null ? null : bottomPanel.getComponent(), isFolder );
67             gui.addChangeListener(this);
68         }
69         return gui;
70     }
71
72     public HelpCtx getHelp() {
73         if ( bottomPanel != null ) {
74             HelpCtx bottomHelp = bottomPanel.getHelp();
75             if ( bottomHelp != null ) {
76                 return bottomHelp;
77             }
78         }
79         
80         //XXX
81
return null;
82         
83     }
84
85     public boolean isValid() {
86         boolean ok = ( gui != null && gui.getTargetName() != null &&
87                ( bottomPanel == null || bottomPanel.isValid() ) );
88         
89         if (!ok) {
90             return false;
91         }
92         
93         // check if the file name can be created
94
FileObject template = Templates.getTemplate( wizard );
95
96         String JavaDoc errorMessage = ProjectUtilities.canUseFileName (gui.getTargetGroup().getRootFolder(), gui.getTargetFolder(), gui.getTargetName(), template.getExt (), isFolder);
97         wizard.putProperty ("WizardPanel_errorMessage", errorMessage); // NOI18N
98

99         return errorMessage == null;
100     }
101
102     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
103         listeners.add(l);
104     }
105
106     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
107         listeners.remove(l);
108     }
109
110     private void fireChange() {
111         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
112         List JavaDoc<ChangeListener JavaDoc> templist;
113         synchronized (this) {
114             templist = new ArrayList JavaDoc<ChangeListener JavaDoc> (listeners);
115         }
116     for (ChangeListener JavaDoc l: templist) {
117             l.stateChanged(e);
118         }
119     }
120
121     public void readSettings( Object JavaDoc settings ) {
122                 
123         wizard = (WizardDescriptor)settings;
124                 
125         if ( gui == null ) {
126             getComponent();
127         }
128         
129         // Try to preselect a folder
130
FileObject preselectedTarget = Templates.getTargetFolder( wizard );
131         // Try to preserve the already entered target name
132
String JavaDoc targetName = Templates.getTargetName( wizard );
133         // Init values
134
gui.initValues( Templates.getTemplate( wizard ), preselectedTarget, targetName );
135         
136         // XXX hack, TemplateWizard in final setTemplateImpl() forces new wizard's title
137
// this name is used in NewFileWizard to modify the title
138
Object JavaDoc substitute = gui.getClientProperty ("NewFileWizard_Title"); // NOI18N
139
if (substitute != null) {
140             wizard.putProperty ("NewFileWizard_Title", substitute); // NOI18N
141
}
142         
143         wizard.putProperty ("WizardPanel_contentData", new String JavaDoc[] { // NOI18N
144
NbBundle.getBundle (SimpleTargetChooserPanel.class).getString ("LBL_TemplatesPanel_Name"), // NOI18N
145
NbBundle.getBundle (SimpleTargetChooserPanel.class).getString ("LBL_SimpleTargetChooserPanel_Name")}); // NOI18N
146

147         if ( bottomPanel != null ) {
148             bottomPanel.readSettings( settings );
149         }
150     }
151     
152     public void storeSettings(Object JavaDoc settings) {
153         if ( WizardDescriptor.PREVIOUS_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) {
154             return;
155         }
156         if( isValid() ) {
157             if ( bottomPanel != null ) {
158                 bottomPanel.storeSettings( settings );
159             }
160             
161             FileObject template = Templates.getTemplate( wizard );
162             
163             String JavaDoc name = gui.getTargetName ();
164             if (name.indexOf ('/') > 0) { // NOI18N
165
name = name.substring (name.lastIndexOf ('/') + 1);
166             }
167             
168             Templates.setTargetFolder( (WizardDescriptor)settings, getTargetFolderFromGUI () );
169             Templates.setTargetName( (WizardDescriptor)settings, name );
170         }
171         ((WizardDescriptor)settings).putProperty ("NewFileWizard_Title", null); // NOI18N
172
}
173
174     public void stateChanged(ChangeEvent JavaDoc e) {
175         fireChange();
176     }
177     
178     private FileObject getTargetFolderFromGUI () {
179         FileObject rootFolder = gui.getTargetGroup().getRootFolder();
180         String JavaDoc folderName = gui.getTargetFolder();
181         String JavaDoc newObject = gui.getTargetName ();
182         
183         if (newObject.indexOf ('/') > 0) { // NOI18N
184
String JavaDoc path = newObject.substring (0, newObject.lastIndexOf ('/')); // NOI18N
185
folderName = folderName == null || "".equals (folderName) ? path : folderName + '/' + path; // NOI18N
186
}
187
188         FileObject targetFolder;
189         if ( folderName == null ) {
190             targetFolder = rootFolder;
191         }
192         else {
193             targetFolder = rootFolder.getFileObject( folderName );
194         }
195
196         if ( targetFolder == null ) {
197             // XXX add deletion of the file in uninitalize ow the wizard
198
try {
199                 targetFolder = FileUtil.createFolder( rootFolder, folderName );
200             } catch (IOException JavaDoc ioe) {
201                 // XXX
202
// Can't create the folder
203
}
204         }
205         
206         return targetFolder;
207     }
208 }
209
Popular Tags