1 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 ; 25 import javax.swing.event.ChangeListener ; 26 import java.awt.*; 27 import java.io.File ; 28 import java.io.FilenameFilter ; 29 import java.util.ArrayList ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 42 public class ServerInstallPanel implements WizardDescriptor.Panel { 43 44 48 private ServerInstallVisualPanel component; 49 50 private ConnectionDescriptor descriptor; 51 52 53 public ServerInstallPanel(ConnectionDescriptor descriptor) { 54 this.descriptor = descriptor; 55 } 56 57 public ConnectionDescriptor getDescriptor() { 58 return descriptor; 59 } 60 61 62 public ServerInstallPanel() { 63 } 64 65 public Component getComponent() { 70 if (component == null) { 71 component = new ServerInstallVisualPanel(this); 72 } 73 return component; 74 } 75 76 public HelpCtx getHelp() { 77 return HelpCtx.DEFAULT_HELP; 79 } 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 listeners = new HashSet (1); public final void addChangeListener(ChangeListener l) { 98 synchronized (listeners) { 99 listeners.add(l); 100 } 101 } 102 public final void removeChangeListener(ChangeListener l) { 103 synchronized (listeners) { 104 listeners.remove(l); 105 } 106 } 107 protected final void fireChangeEvent() { 108 Iterator it; 109 synchronized (listeners) { 110 it = new HashSet (listeners).iterator(); 111 } 112 ChangeEvent ev = new ChangeEvent (this); 113 while (it.hasNext()) { 114 ((ChangeListener )it.next()).stateChanged(ev); 115 } 116 } 117 118 119 public void readSettings(Object settings) { 124 125 } 126 127 public void storeSettings(Object settings) { 128 if (component.getServerInstallInfo() == null) 130 return; 131 132 List <File > fileList = new ArrayList (); 133 134 String [] serverFiles = descriptor.getSettings().getConnectionType().getConnectionClasspathEntries(); 135 136 137 File serverInstall = component.getServerInstallInfo().getServerInstallLocation(); 138 139 if (serverFiles != null) { 140 for (int i = 0; i < serverFiles.length; i++) { 141 String serverFile = serverFiles[i]; 142 File [] matchedFiles = null; 143 try { 144 if (serverFile.indexOf('/') > 0) { 145 matchedFiles = findDeepFiles(serverInstall, serverFile); 146 } else { 147 matchedFiles = new File [] { findFile(serverInstall, serverFile)}; 148 } 149 } catch (Exception 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 matchedFile = matchedFiles[j]; 156 157 if (matchedFile != null && !fileList.contains(matchedFile)) 158 fileList.add(matchedFile); 159 } 160 } 161 } 162 } else { 163 File [] foundFiles = serverInstall.listFiles(); 165 for (int i = 0; i < foundFiles.length; i++) { 166 File 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 findFile(File directory, String filename) { 176 File [] children = directory.listFiles(); 177 for (int i = 0; i < children.length; i++) { 178 File child = children[i]; 179 if (child.isDirectory()) { 180 File 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 [] findDeepFiles(File directory, String filename) { 192 int in = filename.indexOf("/"); 193 if (in < 0) { 194 195 if (filename.equals("*")) { 196 return directory.listFiles(); 197 } else { 198 File match = getChild(directory, filename); 199 return new File [] { match }; 200 } 201 202 } else { 203 String dir = filename.substring(0,in); 204 String restOfName = filename.substring(in+1,filename.length()); 205 206 File 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 getChild(File directory, final String childName) { 215 File [] children = directory.listFiles(new FilenameFilter () { 216 public boolean accept(File dir, String name) { 217 return childName.equals(name); 218 } 219 }); 220 if (children.length == 1) 221 return children[0]; 222 else 223 throw new RuntimeException ("File ["+childName+"] not found in directory: " + directory.getAbsolutePath()); 224 } 225 } 226 | Popular Tags |