1 19 24 package org.netbeans.modules.j2ee.sun.ide.j2ee.ui; 25 26 import java.io.File ; 27 import javax.swing.filechooser.FileFilter ; 28 import javax.swing.text.PlainDocument ; 29 import javax.swing.text.AttributeSet ; 30 import javax.swing.text.BadLocationException ; 31 import java.awt.Toolkit ; 32 import javax.swing.JFileChooser ; 33 import javax.swing.SwingUtilities ; 34 import org.netbeans.modules.j2ee.sun.api.Asenv; 35 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 36 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 37 import org.openide.DialogDisplayer; 38 import org.openide.NotifyDescriptor; 39 import org.openide.WizardDescriptor; 40 import org.openide.awt.StatusDisplayer; 41 import org.openide.util.NbBundle; 42 import org.openide.util.RequestProcessor; 43 44 48 public class Util { 49 50 51 public Util() { 52 } 53 54 public static NumericDocument getNumericDocument(){ 56 return new NumericDocument(); 57 } 58 public static class NumericDocument extends PlainDocument { 59 private Toolkit toolkit = Toolkit.getDefaultToolkit(); 60 61 public void insertString(int offs, String str, AttributeSet a) 62 throws BadLocationException { 63 char[] s = str.toCharArray(); 64 char[] r = new char[s.length]; 65 int j = 0; 66 for (int i = 0; i < r.length; i++) { 67 if (Character.isDigit(s[i])) { 68 r[j++] = s[i]; 69 } else { 70 toolkit.beep(); 71 } 72 } 73 super.insertString(offs, new String (r, 0, j), a); 74 } 75 } 77 public static void showInformation(final String msg, final String title){ 78 SwingUtilities.invokeLater(new Runnable () { 79 public void run() { 80 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); 81 d.setTitle(title); 82 DialogDisplayer.getDefault().notify(d); 83 } 84 }); 85 86 } 87 88 public static void showInformationWhenHolding(final String msg, final String title){ 89 RequestProcessor.getDefault().post(new Runnable () { 90 public void run() { 91 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); 92 d.setTitle(title); 93 DialogDisplayer.getDefault().notify(d); 94 } 95 }); 96 97 } 98 99 public static void showInformation(final String msg){ 100 SwingUtilities.invokeLater(new Runnable () { 101 public void run() { 102 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); 103 DialogDisplayer.getDefault().notify(d); 104 } 105 }); 106 107 } 108 109 public static Object showWarning(final String msg){ 111 NotifyDescriptor d = new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.WARNING_MESSAGE); 112 return DialogDisplayer.getDefault().notify(d); 113 114 115 } 116 117 public static Object showWarning(final String msg, final String title){ 118 NotifyDescriptor d = new NotifyDescriptor.Confirmation(msg, title, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.WARNING_MESSAGE); 119 return DialogDisplayer.getDefault().notify(d); 120 121 } 122 123 public static void showError(final String msg){ 124 SwingUtilities.invokeLater(new Runnable () { 125 public void run() { 126 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE); 127 DialogDisplayer.getDefault().notify(d); 128 } 129 }); 130 131 } 132 133 public static void showError(final String msg, final String title){ 134 SwingUtilities.invokeLater(new Runnable () { 135 public void run() { 136 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE); 137 d.setTitle(title); 138 DialogDisplayer.getDefault().notify(d); 139 } 140 }); 141 142 } 143 144 public static void setStatusBar(final String msg){ 145 SwingUtilities.invokeLater(new Runnable () { 146 public void run() { 147 StatusDisplayer.getDefault().setStatusText(msg); 148 } 149 }); 150 151 } 152 153 154 155 static File [] getRegisterableDefaultDomains(File location) { 156 File [] noneRegisterable = new File [0]; 157 String ext = (File.separatorChar == '/' ? "conf" : "bat"); File asenv = new File (location,"config/asenv."+ext); Asenv asenvContent = new Asenv(asenv); 161 String defDomainsDirName = asenvContent.get(Asenv.AS_DEF_DOMAINS_PATH); 162 File domainsDir = new File (defDomainsDirName); if (!domainsDir.exists() && location.getAbsolutePath().startsWith("/opt/SUNWappserver")) { 165 domainsDir = new File ("/var/opt/SUNWappserver/domains"); 166 } 167 if (!domainsDir.exists()) 168 return noneRegisterable; 169 170 File [] possibles = domainsDir.listFiles(new java.io.FileFilter () { 171 public boolean accept(File pathname) { 172 if (pathname.isDirectory() && pathname.canWrite()) 173 return true; 174 return false; 175 } 176 }); 177 if (null == possibles) 178 return noneRegisterable; 179 180 int realCount = 0; 182 for (int i = 0; i < possibles.length; i++) { 183 if (rootOfUsableDomain(possibles[i])) { 184 realCount++; 185 } else { 186 possibles[i] = null; 187 } 188 } 189 File [] retVal = new File [realCount]; 190 int nextSlot = 0; 191 for (int i = 0; i < possibles.length; i++) { 192 if (possibles[i] != null) { 193 retVal[nextSlot] = possibles[i]; 194 nextSlot++; 195 } 196 } 197 return retVal; 198 } 199 200 202 public static boolean rootOfUsableDomain(File f) { 203 File testFile = new File (f,"logs"); 204 if (!testFile.exists() || !testFile.isDirectory() || !testFile.canWrite()) 205 return false; 206 testFile = new File (f,"config"); 207 if (!testFile.exists() || !testFile.isDirectory() || !testFile.canWrite()) 208 return false; 209 testFile = new File (testFile,"domain.xml"); 210 if (!testFile.exists() || !testFile.canWrite()) 211 return false; 212 return true; 213 } 214 215 public static String getHostPort(File domainDir, File platformDir){ 216 File xmlRoot; 217 if(File.pathSeparatorChar == ':') 218 xmlRoot = new File (domainDir.getAbsolutePath() + "/config/domain.xml"); else 220 xmlRoot = new File (domainDir.getAbsolutePath() + "\\config\\domain.xml"); String adminHostPort = null; 222 try{ 223 Class [] argClass = new Class [2]; 224 argClass[0] = File .class; 225 argClass[1] = File .class; 226 Object [] argObject = new Object [2]; 227 argObject[0] = xmlRoot; 228 argObject[1] = platformDir; 229 230 ClassLoader loader = ServerLocationManager.getServerOnlyClassLoader(platformDir); 231 if(loader != null){ 232 Class cc = loader.loadClass("org.netbeans.modules.j2ee.sun.bridge.AppServerBridge"); 233 java.lang.reflect.Method getHostPort = cc.getMethod("getHostPort", argClass); adminHostPort = (String )getHostPort.invoke(null, argObject); 235 } 236 }catch(Exception ex){ 237 } 239 return adminHostPort; 240 } 241 242 static String getDeploymentUri(File domainDir, File platformDir) { 243 return SunURIManager.SUNSERVERSURI+getHostPort(domainDir,platformDir); 244 } 245 246 static File domainFile(File domainDir) { 247 if (File.pathSeparatorChar == ';') 248 return new File (domainDir+"\\config\\domain.xml"); 249 else 250 return new File (domainDir+"/config/domain.xml"); 251 } 252 253 static void fillDescriptorFromDomainXml(final WizardDescriptor wiz, final File domainDir) { 254 String hp = Util.getHostPort(domainDir, 255 (File ) wiz.getProperty(AddDomainWizardIterator.PLATFORM_LOCATION)); 256 if (null != hp) { 257 wiz.putProperty(AddDomainWizardIterator.DOMAIN_FILE, 258 Util.domainFile(domainDir)); 259 int sepDex = hp.indexOf(':'); 260 wiz.putProperty(AddDomainWizardIterator.HOST,hp.substring(0,sepDex)); 261 wiz.putProperty(AddDomainWizardIterator.PORT,hp.substring(sepDex+1)); 262 wiz.putProperty(AddDomainWizardIterator.DOMAIN,domainDir.getName()); 263 wiz.putProperty(AddDomainWizardIterator.INSTALL_LOCATION, 264 domainDir.getParentFile().getAbsolutePath()); 265 wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,null); 266 } else { 267 wiz.putProperty(AddDomainWizardIterator.HOST,""); 268 wiz.putProperty(AddDomainWizardIterator.PORT,""); 269 wiz.putProperty(AddDomainWizardIterator.DOMAIN,""); 270 wiz.putProperty(AddDomainWizardIterator.INSTALL_LOCATION, 271 ""); 272 wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,null); 273 } 274 } 275 276 static JFileChooser getJFileChooser(final FileFilter f){ 277 JFileChooser chooser = new JFileChooser (); 278 decorateChooser(chooser,null,NbBundle.getMessage(Util.class, 279 "LBL_Chooser_Name")); 280 return chooser; 281 } 282 283 static void decorateChooser(JFileChooser chooser,String fname,String title) { 284 chooser.setDialogTitle(title); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); 286 287 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 288 chooser.setApproveButtonMnemonic(NbBundle.getMessage(Util.class, 289 "Choose_Button_Mnemonic").charAt(0)); chooser.setMultiSelectionEnabled(false); 291 chooser.setAcceptAllFileFilterUsed(false); 292 chooser.setApproveButtonToolTipText(NbBundle.getMessage(Util.class, 293 "LBL_Chooser_Name")); 295 chooser.getAccessibleContext(). 296 setAccessibleName(NbBundle.getMessage(Util.class, 297 "LBL_Chooser_Name")); chooser.getAccessibleContext(). 299 setAccessibleDescription(NbBundle.getMessage(Util.class, 300 "LBL_Chooser_Name")); if (null != fname && fname.length() > 0) { 302 File sel = new File (fname); 303 if (sel.isDirectory()) 304 chooser.setCurrentDirectory(sel); 305 else 306 chooser.setSelectedFile(sel); 307 } 308 } 309 310 311 312 } 313 | Popular Tags |