KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > wizard > ServerInstallPanel


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.connection.wizard;
18
19 import org.openide.WizardDescriptor;
20 import org.openide.windows.IOProvider;
21 import org.openide.util.HelpCtx;
22 import org.mc4j.console.connection.install.finder.ServerInstallInfo;
23
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import java.awt.*;
27 import java.io.File JavaDoc;
28 import java.io.FilenameFilter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34
35 /**
36  * This panel supports the visual panel to let you select the server installation
37  * directory.
38  *
39  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
40  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
41  */

42 public class ServerInstallPanel implements WizardDescriptor.Panel /* .FinishPanel */ {
43
44     /** The visual component that displays this panel.
45      * If you need to access the component from this class,
46      * just use getComponent().
47      */

48     private ServerInstallVisualPanel component;
49
50     private ConnectionDescriptor descriptor;
51
52     /** Create the wizard panel descriptor. */
53     public ServerInstallPanel(ConnectionDescriptor descriptor) {
54         this.descriptor = descriptor;
55     }
56
57     public ConnectionDescriptor getDescriptor() {
58         return descriptor;
59     }
60
61     /** Create the wizard panel descriptor. */
62     public ServerInstallPanel() {
63     }
64
65     // Get the visual component for the panel. In this template, the component
66
// is kept separate. This can be more efficient: if the wizard is created
67
// but never displayed, or not all panels are displayed, it is better to
68
// create only those which really need to be visible.
69
public Component getComponent() {
70         if (component == null) {
71             component = new ServerInstallVisualPanel(this);
72         }
73         return component;
74     }
75
76     public HelpCtx getHelp() {
77         // Show no Help button for this panel:
78
return HelpCtx.DEFAULT_HELP;
79         // If you have context help:
80
// return new HelpCtx(ServerInstallPanel.class);
81
}
82
83     public boolean isValid() {
84
85         if (component == null)
86             return false;
87         ServerInstallInfo selectedInfo = component.getServerInstallInfo();
88         if (selectedInfo == null)
89             return false;
90
91         return(
92             getDescriptor().getSettings().getConnectionType().getConnectionType().equals(
93             selectedInfo.getServerConnectionType().getConnectionType()));
94     }
95
96     private final Set JavaDoc listeners = new HashSet JavaDoc(1); // Set<ChangeListener>
97
public final void addChangeListener(ChangeListener JavaDoc l) {
98         synchronized (listeners) {
99             listeners.add(l);
100         }
101     }
102     public final void removeChangeListener(ChangeListener JavaDoc l) {
103         synchronized (listeners) {
104             listeners.remove(l);
105         }
106     }
107     protected final void fireChangeEvent() {
108         Iterator JavaDoc it;
109         synchronized (listeners) {
110             it = new HashSet JavaDoc(listeners).iterator();
111         }
112         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
113         while (it.hasNext()) {
114             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
115         }
116     }
117
118
119     // You can use a settings object to keep track of state.
120
// Normally the settings object will be the WizardDescriptor,
121
// so you can use WizardDescriptor.getProperty & putProperty
122
// to store information entered by the user.
123
public void readSettings(Object JavaDoc settings) {
124
125     }
126
127     public void storeSettings(Object JavaDoc settings) {
128         // to support back button
129
if (component.getServerInstallInfo() == null)
130             return;
131
132         List JavaDoc<File JavaDoc> fileList = new ArrayList JavaDoc();
133
134         String JavaDoc[] serverFiles = descriptor.getSettings().getConnectionType().getConnectionClasspathEntries();
135
136         
137         File JavaDoc serverInstall = component.getServerInstallInfo().getServerInstallLocation();
138
139         if (serverFiles != null) {
140             for (int i = 0; i < serverFiles.length; i++) {
141                 String JavaDoc serverFile = serverFiles[i];
142                 File JavaDoc[] matchedFiles = null;
143                 try {
144                     if (serverFile.indexOf('/') > 0) {
145                         matchedFiles = findDeepFiles(serverInstall, serverFile);
146                     } else {
147                         matchedFiles = new File JavaDoc[] { findFile(serverInstall, serverFile)};
148                     }
149                 } catch (Exception JavaDoc e) {
150                     IOProvider.getDefault().getStdOut().println(e.getMessage());
151                 }
152
153                 if (matchedFiles != null) {
154                     for (int j = 0; j < matchedFiles.length; j++) {
155                         File JavaDoc matchedFile = matchedFiles[j];
156
157                         if (matchedFile != null && !fileList.contains(matchedFile))
158                             fileList.add(matchedFile);
159                     }
160                 }
161             }
162         } else {
163             // Add them all
164
File JavaDoc[] foundFiles = serverInstall.listFiles();
165             for (int i = 0; i < foundFiles.length; i++) {
166                 File JavaDoc foundFile = foundFiles[i];
167                 fileList.add(foundFile);
168             }
169         }
170
171         this.descriptor.getSettings().setClassPathEntries(fileList);
172         System.out.println("Saving " + settings.hashCode());
173     }
174
175      public static File JavaDoc findFile(File JavaDoc directory, String JavaDoc filename) {
176         File JavaDoc[] children = directory.listFiles();
177         for (int i = 0; i < children.length; i++) {
178             File JavaDoc child = children[i];
179             if (child.isDirectory()) {
180                 File JavaDoc result = findFile(child, filename);
181                 if (result != null)
182                     return result;
183             } else {
184                 if (filename.equalsIgnoreCase(child.getName()))
185                     return child;
186             }
187         }
188         return null;
189     }
190
191     public static File JavaDoc[] findDeepFiles(File JavaDoc directory, String JavaDoc filename) {
192         int in = filename.indexOf("/");
193         if (in < 0) {
194
195             if (filename.equals("*")) {
196                 return directory.listFiles();
197             } else {
198                 File JavaDoc match = getChild(directory, filename);
199                 return new File JavaDoc[] { match };
200             }
201
202         } else {
203             String JavaDoc dir = filename.substring(0,in);
204             String JavaDoc restOfName = filename.substring(in+1,filename.length());
205
206             File JavaDoc childDir = getChild(directory, dir);
207             if (childDir == null) {
208                 System.out.println("Could not find " + directory.getAbsolutePath() + " :: " + restOfName);
209             }
210             return findDeepFiles(childDir, restOfName);
211         }
212     }
213
214     public static File JavaDoc getChild(File JavaDoc directory, final String JavaDoc childName) {
215         File JavaDoc[] children = directory.listFiles(new FilenameFilter JavaDoc() {
216             public boolean accept(File JavaDoc dir, String JavaDoc name) {
217                 return childName.equals(name);
218             }
219         });
220         if (children.length == 1)
221             return children[0];
222         else
223             throw new RuntimeException JavaDoc("File ["+childName+"] not found in directory: " + directory.getAbsolutePath());
224     }
225 }
226
Popular Tags