1 19 20 package org.netbeans.modules.websvc.wsdl.wizard; 21 22 import java.awt.Component ; 23 import java.io.*; 24 import java.util.ArrayList ; 25 import javax.swing.JComponent ; 26 import javax.swing.JFileChooser ; 27 import javax.swing.JLabel ; 28 import javax.swing.filechooser.FileFilter ; 29 import javax.swing.text.JTextComponent ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.modules.j2ee.common.Util; 32 import org.openide.DialogDescriptor; 33 import org.openide.WizardDescriptor; 34 import org.openide.DialogDisplayer; 35 import org.openide.NotifyDescriptor; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.loaders.DataObject; 39 import org.openide.nodes.Node; 40 import org.openide.util.NbBundle; 41 45 public class Utilities { 46 47 public static String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 48 int diff = 0; 51 if (before == null) { 52 before = new String [0]; 53 } else if (before.length > 0) { 54 diff = ("...".equals(before[before.length - 1])) ? 1 : 0; } 56 String [] res = new String [ (before.length - diff) + panels.length]; 57 for (int i = 0; i < res.length; i++) { 58 if (i < (before.length - diff)) { 59 res[i] = before[i]; 60 } else { 61 res[i] = panels[i - before.length + diff].getComponent().getName(); 62 } 63 } 64 return res; 65 } 66 67 public static void replaceInDocument(javax.swing.text.Document document, String replaceFrom, String replaceTo) { 68 javax.swing.text.AbstractDocument doc = (javax.swing.text.AbstractDocument )document; 69 int len = replaceFrom.length(); 70 try { 71 String content = doc.getText(0,doc.getLength()); 72 int index = content.lastIndexOf(replaceFrom); 73 while (index>=0) { 74 doc.replace(index,len,replaceTo,null); 75 content=content.substring(0,index); 76 index = content.lastIndexOf(replaceFrom); 77 } 78 } catch (javax.swing.text.BadLocationException ex){} 79 } 80 81 private static File lastDirectory; 83 84 91 public static File[] selectFiles(final String extensions, String dialogTitle, final String maskTitle, 92 Project project){ 93 ArrayList <File> fileList = new ArrayList <File>(); 94 SelectSchemaPanel explorerPanel = new SelectSchemaPanel(project); 95 DialogDescriptor descriptor = new DialogDescriptor(explorerPanel, 96 NbBundle.getMessage(Utilities.class,"TTL_SelectSchemas")); 145 if(DialogDisplayer.getDefault().notify(descriptor).equals(NotifyDescriptor.OK_OPTION)) { 146 boolean accepted = true; 147 String errMsg = null; 148 Node[] selectedNodes = explorerPanel.getSelectedNodes(); 149 for(int i = 0; i < selectedNodes.length; i++){ 150 Node node = selectedNodes[i]; 151 DataObject dobj = (DataObject)node.getCookie(DataObject.class); 152 if(dobj != null){ 153 FileObject fo = dobj.getPrimaryFile(); 154 if(fo != null){ 155 File file = FileUtil.toFile(fo); 156 if (file != null && file.isFile()) { 157 java.util.StringTokenizer token = new java.util.StringTokenizer (extensions, " "); boolean validFile = false; 159 while (token.hasMoreElements()) { 160 if (file.getName().endsWith(token.nextToken())){ 161 fileList.add(file); 162 validFile = true; 163 } 164 } 165 if(!validFile){ 166 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 167 NbBundle.getMessage(Utilities.class,"MSG_inValidFile", file.getName()), NotifyDescriptor.WARNING_MESSAGE)); 168 } 169 } 170 } 171 } 172 } 173 if (!accepted) { 174 NotifyDescriptor.Message notifyDescr = 175 new NotifyDescriptor.Message(errMsg, 176 NotifyDescriptor.ERROR_MESSAGE ); 177 DialogDisplayer.getDefault().notify(notifyDescr); 178 descriptor.setClosingOptions(closingOptionsWithoutOK); 179 } else { 180 descriptor.setClosingOptions(closingOptionsWithOK); 182 } 183 } 184 File[] selectedFiles = new File[fileList.size()]; 185 return fileList.toArray(selectedFiles); 186 } 187 188 private static Object [] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION, 189 DialogDescriptor.CLOSED_OPTION}; 190 private static Object [] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION, 191 DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION}; 192 193 200 public static File selectFile(final String extensions, String dialogTitle, final String maskTitle) { 201 JFileChooser chooser = new JFileChooser (); 202 203 chooser.setFileFilter(new FileFilter () { 204 public boolean accept(File f) { 205 if (f.isDirectory()) return true; 206 java.util.StringTokenizer token = new java.util.StringTokenizer (extensions, " "); while (token.hasMoreElements()) { 208 if (f.getName().endsWith(token.nextToken())) return true; 209 } 210 return false; 211 } 212 public String getDescription() { 213 return maskTitle; } 215 }); 216 217 if (lastDirectory != null) { 218 chooser.setCurrentDirectory(lastDirectory); 219 } 220 221 chooser.setDialogTitle(dialogTitle); 222 while (chooser.showDialog(org.openide.windows.WindowManager.getDefault().getMainWindow(), 223 NbBundle.getMessage(Utilities.class,"PROP_select_button")) 224 == JFileChooser.APPROVE_OPTION) { 225 File f = chooser.getSelectedFile(); 226 lastDirectory = chooser.getCurrentDirectory(); 227 if (f != null && f.isFile()) { 228 java.util.StringTokenizer token = new java.util.StringTokenizer (extensions, " "); while (token.hasMoreElements()) { 230 if (f.getName().endsWith(token.nextToken())) return f; 231 } 232 } 233 234 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 235 NbBundle.getMessage(Utilities.class,"MSG_inValidFile"), NotifyDescriptor.WARNING_MESSAGE)); 236 } 237 return null; 238 } 239 240 243 public static JTextComponent findTextFieldForLabel(JComponent component, String text) { 244 JLabel label = Util.findLabel(component, text); 245 if(label != null) { 246 Component comp = label.getLabelFor(); 247 if (comp!=null && (comp instanceof JTextComponent )) return (JTextComponent )comp; 248 } 249 return null; 250 } 251 } 252 | Popular Tags |