KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > ui > EJBLocationsWizardPanel


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.j2ee.ejbfreeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
34 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport;
35 import org.openide.ErrorManager;
36 import org.openide.WizardDescriptor;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42
43
44 /**
45  * @author Radko Najman
46  */

47 public class EJBLocationsWizardPanel implements WizardDescriptor.Panel, ChangeListener JavaDoc {
48
49     private EJBLocationsPanel component;
50     private WizardDescriptor wizardDescriptor;
51     private File JavaDoc baseFolder;
52
53     public EJBLocationsWizardPanel() {
54         getComponent().setName(NbBundle.getMessage(NewEJBFreeformProjectWizardIterator.class, "TXT_NewEJBFreeformProjectWizardIterator_EJBSources")); // NOI18N
55
}
56
57     public Component JavaDoc getComponent() {
58         if (component == null) {
59             component = new EJBLocationsPanel(this);
60         }
61         return component;
62     }
63
64     public HelpCtx getHelp() {
65         return new HelpCtx( EJBLocationsWizardPanel.class );
66     }
67
68     public boolean isValid() {
69         getComponent();
70         return component.valid(wizardDescriptor);
71     }
72
73     private final Set JavaDoc/*<ChangeListener>*/ listeners = new HashSet JavaDoc(1);
74     public final void addChangeListener(ChangeListener JavaDoc l) {
75         synchronized (listeners) {
76             listeners.add(l);
77         }
78     }
79     public final void removeChangeListener(ChangeListener JavaDoc l) {
80         synchronized (listeners) {
81             listeners.remove(l);
82         }
83     }
84     protected final void fireChangeEvent() {
85         Iterator JavaDoc it;
86         synchronized (listeners) {
87             it = new HashSet JavaDoc(listeners).iterator();
88         }
89         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
90         while (it.hasNext()) {
91             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
92         }
93     }
94
95     public void readSettings(Object JavaDoc settings) {
96         wizardDescriptor = (WizardDescriptor) settings;
97         wizardDescriptor.putProperty("NewProjectWizard_Title", component.getClientProperty("NewProjectWizard_Title")); // NOI18N
98

99         //guess EJB modules well-known locations and preset them
100
File JavaDoc baseFolder = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_LOCATION);
101         File JavaDoc nbProjectFolder = (File JavaDoc)wizardDescriptor.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER);
102         final String JavaDoc configFiles;
103         final String JavaDoc srcPackages;
104         if(baseFolder.equals(this.baseFolder)) {
105             configFiles = component.getConfigFilesLocation().getAbsolutePath();
106             srcPackages = component.getSrcPackagesLocation().getAbsolutePath();
107         } else {
108             this.baseFolder = baseFolder;
109             FileObject fo = FileUtil.toFileObject(baseFolder);
110             if (fo != null) {
111                 configFiles = guessConfigFiles(fo);
112                 srcPackages = guessJavaRoot(fo);
113             } else {
114                 configFiles = ""; // NOI18N
115
srcPackages = ""; // NOI18N
116
}
117         }
118         component.setFolders(baseFolder, nbProjectFolder);
119         component.setConfigFilesField(configFiles);
120         component.setSrcPackages(srcPackages);
121     }
122
123     public void storeSettings(Object JavaDoc settings) {
124         wizardDescriptor = (WizardDescriptor) settings;
125         wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_EJBMODULES, component.getEJBModules());
126         
127         List JavaDoc l = component.getJavaSrcFolder();
128         wizardDescriptor.putProperty(NewJavaFreeformProjectSupport.PROP_EXTRA_JAVA_SOURCE_FOLDERS, l);
129         
130         wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_SOURCE_FOLDERS, component.getEJBSrcFolder());
131         wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.PROP_EJB_RESOURCE_FOLDERS, component.getResourcesFolder());
132         wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.SERVER_ID, component.getSelectedServerID());
133         wizardDescriptor.putProperty(NewEJBFreeformProjectWizardIterator.J2EE_LEVEL, component.getSelectedJ2eeSpec());
134         wizardDescriptor.putProperty("NewProjectWizard_Title", null); // NOI18N
135
}
136
137     // TODO: ma154696: change this algorithm from web pages to ejb DDs
138
private String JavaDoc guessConfigFiles (FileObject dir) {
139         Enumeration JavaDoc ch = dir.getChildren (true);
140         while (ch.hasMoreElements ()) {
141             FileObject f = (FileObject) ch.nextElement ();
142             if (f.getNameExt().equals ("ejb-jar.xml")) { // NOI18N
143
return FileUtil.toFile(f.getParent()).getAbsolutePath();
144             }
145         }
146         return ""; // NOI18N
147
}
148
149     private String JavaDoc guessJavaRoot (FileObject dir) {
150         Enumeration JavaDoc ch = dir.getChildren (true);
151         try {
152             while (ch.hasMoreElements ()) {
153                 FileObject f = (FileObject) ch.nextElement ();
154                 if (f.getExt ().equals ("java") && !f.isFolder()) { // NOI18N
155
String JavaDoc pckg = guessPackageName (f);
156                     String JavaDoc pkgPath = f.getParent ().getPath ();
157                     if (pckg != null && pkgPath.endsWith (pckg.replace ('.', '/'))) {
158                         String JavaDoc rootName = pkgPath.substring (0, pkgPath.length () - pckg.length ());
159                         return FileUtil.toFile(f.getFileSystem().findResource(rootName)).getAbsolutePath();
160                     }
161                 }
162             }
163         } catch (FileStateInvalidException fsie) {
164             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, fsie);
165         }
166         return ""; // NOI18N
167
}
168
169     private String JavaDoc guessPackageName(FileObject f) {
170         java.io.Reader JavaDoc r = null;
171         try {
172             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(f.getInputStream (), "utf-8")); //NOI18N
173
boolean noPackage = false;
174             for (;;) {
175                 String JavaDoc line = ((BufferedReader JavaDoc) r).readLine();
176                 if (line == null) {
177                     if (noPackage)
178                         return "";
179                     else
180                         break;
181                 }
182                 line = line.trim();
183                 //try to find package
184
if (line.trim().startsWith("package")) { // NOI18N
185
int idx = line.indexOf(";"); // NOI18N
186
if (idx >= 0)
187                         return line.substring("package".length(), idx).trim(); // NOI18N
188
}
189                 //an easy check if it is class
190
if (line.indexOf("class") != -1)
191                     noPackage = true;
192             }
193         } catch (java.io.IOException JavaDoc ioe) {
194             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
195         } finally {
196             try {
197                 if (r != null)
198                     r.close ();
199             } catch (java.io.IOException JavaDoc ioe) {
200                 // ignore this
201
}
202         }
203         
204         return null;
205     }
206
207     public void stateChanged(ChangeEvent JavaDoc e) {
208         fireChangeEvent();
209     }
210     
211 }
212
Popular Tags