KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > TargetChooserPanel


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

45 final class TargetChooserPanel implements WizardDescriptor.Panel {
46
47     private final List JavaDoc/*<ChangeListener>*/ listeners = new ArrayList JavaDoc();
48     private TargetChooserPanelGUI gui;
49
50     private Project project;
51     private SourceGroup[] folders;
52     private FileType fileType;
53     private TemplateWizard templateWizard;
54     private String JavaDoc j2eeVersion;
55     
56     //TODO how to add [,] to the regular expression?
57
private static final Pattern JavaDoc INVALID_FILENAME_CHARACTERS = Pattern.compile("[`~!@#$%^&*()=+\\|{};:'\",<>/?]"); // NOI18N
58

59     TargetChooserPanel(Project project, SourceGroup[] folders, FileType fileType) {
60         this.folders = folders;
61         this.project = project;
62         this.fileType=fileType;
63         
64         if (FileType.TAG.equals(fileType)) {
65             j2eeVersion = WebModule.J2EE_14_LEVEL;
66             if (folders!=null && folders.length>0) {
67                 WebModule wm = WebModule.getWebModule(folders[0].getRootFolder());
68                 if (wm!=null) j2eeVersion=wm.getJ2eePlatformVersion();
69             }
70         }
71     }
72     
73     TemplateWizard getTemplateWizard() {
74         return templateWizard;
75     }
76
77     public Component JavaDoc getComponent() {
78         if (gui == null) {
79             gui = new TargetChooserPanelGUI(this, project, folders, fileType );
80         }
81         return gui;
82     }
83
84     public HelpCtx getHelp() {
85         return null;
86         //return new HelpCtx( this.getClass().getName() +"."+fileType.toString()); //NOI18N
87
}
88
89     public boolean isValid() {
90         // cannot create tag files in j2ee1.3
91
if (FileType.TAG.equals(fileType) && WebModule.J2EE_13_LEVEL.equals(j2eeVersion)) {
92             templateWizard.putProperty ("WizardPanel_errorMessage", // NOI18N
93
NbBundle.getMessage(TargetChooserPanel.class, "MSG_13notSupported"));
94             return false;
95         }
96         
97         boolean ok = ( gui != null && gui.getTargetName() != null);
98         
99         if (!ok) {
100             templateWizard.putProperty ("WizardPanel_errorMessage", null); // NOI18N
101
return false;
102         }
103         
104         // check if the TLD info is correct
105
if (FileType.TAG.equals(fileType) && gui.isTldCheckBoxSelected()) {
106             String JavaDoc mes=null;
107             FileObject tldFo = gui.getTldFileObject();
108             String JavaDoc tagName = gui.getTagName();
109             if (tldFo==null) {
110                 mes = NbBundle.getMessage(TargetChooserPanel.class,"MSG_noTldSelectedForTagFile");
111             } else if (!gui.isValidTagName(tagName)) {
112                 mes = NbBundle.getMessage(TargetChooserPanel.class,"TXT_wrongTagName",tagName);
113             } else if (gui.tagNameExists(tagName)) {
114                 mes = NbBundle.getMessage(TargetChooserPanel.class,"TXT_tagNameExists",tagName);
115             }
116             if (mes!=null) {
117                 templateWizard.putProperty ("WizardPanel_errorMessage", mes); // NOI18N
118
return false;
119             }
120         }
121         
122         String JavaDoc filename = gui.getTargetName();
123         if (INVALID_FILENAME_CHARACTERS.matcher(filename).find()) {
124             templateWizard.putProperty ("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class, "MSG_invalid_filename", filename)); // NOI18N
125
return false;
126         }
127
128         // check if the TLD info is correct
129
if (FileType.TAGLIBRARY.equals(fileType)) {
130             // XX precisely we should check for 'tokens composed of characters,
131
// digits, ".", ":", "-", and the characters defined by Unicode,
132
// such as "combining" or "extender"' to be sure that TLD will validate
133
String JavaDoc tldName = gui.getTargetName();
134             if (tldName.indexOf(' ') >= 0 ||
135                     tldName.indexOf(',') >= 0) {
136                 templateWizard.putProperty ("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class,"TXT_wrongTagLibName",tldName)); // NOI18N
137
return false;
138             }
139         }
140         // check if the file name can be created
141
String JavaDoc targetName=gui.getTargetName();
142         java.io.File JavaDoc file = gui.getTargetFile();
143         FileObject template = Templates.getTemplate( templateWizard );
144         String JavaDoc ext = template.getExt ();
145         if (FileType.JSP.equals(fileType) || FileType.TAG.equals(fileType)) {
146             if (isSegment()) ext+="f"; //NOI18N
147
else if (isXml()) ext+="x"; //NOI18N
148
}
149         
150         String JavaDoc errorMessage = Utilities.canUseFileName (file, gui.getRelativeTargetFolder(), targetName, ext);
151         if (errorMessage!=null)
152             templateWizard.putProperty ("WizardPanel_errorMessage", errorMessage); // NOI18N
153
else
154             templateWizard.putProperty("WizardPanel_errorMessage", gui.getErrorMessage()); //NOI18N
155

156         boolean valid = gui.isPanelValid() && errorMessage == null;
157
158         if (valid && targetName.indexOf(".")>=0) {
159             // warning when file name contains dots
160
templateWizard.putProperty("WizardPanel_errorMessage", // NOI18N
161
NbBundle.getMessage(TargetChooserPanel.class, "MSG_dotsInName",targetName+"."+ext));
162         }
163         return valid;
164     }
165
166     public void addChangeListener(ChangeListener JavaDoc l) {
167         listeners.add(l);
168     }
169
170     public void removeChangeListener(ChangeListener JavaDoc l) {
171         listeners.remove(l);
172     }
173
174     protected void fireChange() {
175         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
176         Iterator JavaDoc it = listeners.iterator();
177         while (it.hasNext()) {
178             ((ChangeListener JavaDoc)it.next()).stateChanged(e);
179         }
180     }
181
182     public void readSettings( Object JavaDoc settings ) {
183         
184         templateWizard = (TemplateWizard)settings;
185         
186         if ( gui != null ) {
187             
188             Project project = Templates.getProject( templateWizard );
189             
190             // Try to preselect a folder
191
// XXX The test should be rewritten if external project dirs are supported
192

193             FileObject preselectedTarget = Templates.getTargetFolder( templateWizard );
194         
195             // Init values
196
gui.initValues( project, Templates.getTemplate( templateWizard ), preselectedTarget );
197             
198             if (FileType.JSP.equals(fileType))
199                 templateWizard.putProperty ("NewFileWizard_Title", // NOI18N
200
NbBundle.getMessage(TargetChooserPanel.class, "TITLE_JspFile"));
201             else if (FileType.TAG.equals(fileType))
202                 templateWizard.putProperty ("NewFileWizard_Title", // NOI18N
203
NbBundle.getMessage(TargetChooserPanel.class, "TITLE_TagFile"));
204             else if (FileType.TAGLIBRARY.equals(fileType))
205                 templateWizard.putProperty ("NewFileWizard_Title", // NOI18N
206
NbBundle.getMessage(TargetChooserPanel.class, "TITLE_TLD"));
207             else if (FileType.HTML.equals(fileType))
208                 templateWizard.putProperty ("NewFileWizard_Title", // NOI18N
209
NbBundle.getMessage(TargetChooserPanel.class, "TITLE_HTML"));
210             else if (FileType.XHTML.equals(fileType))
211                 templateWizard.putProperty ("NewFileWizard_Title", // NOI18N
212
NbBundle.getMessage(TargetChooserPanel.class, "TITLE_XHTML"));
213         }
214     }
215
216     public void storeSettings(Object JavaDoc settings) {
217         if ( WizardDescriptor.PREVIOUS_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) {
218             return;
219         }
220         if ( WizardDescriptor.CANCEL_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) {
221             return;
222         }
223         if( isValid() ) {
224             File JavaDoc f = new File JavaDoc(gui.getCreatedFilePath());
225             File JavaDoc ff = new File JavaDoc(f.getParentFile().getPath());
226             if ( !ff.exists() ) {
227                 ff.mkdir();
228             }
229             FileObject folder = FileUtil.toFileObject(ff);
230
231             Templates.setTargetFolder( (WizardDescriptor)settings, folder );
232             Templates.setTargetName( (WizardDescriptor)settings, gui.getTargetName() );
233         }
234         ((WizardDescriptor)settings).putProperty ("NewFileWizard_Title", null); // NOI18N
235
}
236     
237     boolean isXml() {
238         return gui.isXml();
239     }
240     
241     boolean isSegment() {
242         return gui.isSegment();
243     }
244     
245     String JavaDoc getUri() {
246         return gui.getUri();
247     }
248     
249     String JavaDoc getPrefix() {
250         return gui.getPrefix();
251     }
252     
253     boolean isTldCheckBoxSelected() {
254         return gui.isTldCheckBoxSelected();
255     }
256     
257     String JavaDoc getTagName() {
258         return gui.getTagName();
259     }
260     
261     FileObject getTldFileObject() {
262         return gui.getTldFileObject();
263     }
264 }
265
Popular Tags