| 1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.StringTokenizer ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipException ; 22 import java.util.zip.ZipFile ; 23 24 import org.eclipse.ant.core.IAntClasspathEntry; 25 import org.eclipse.ant.internal.ui.AntUIPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.variables.VariablesPlugin; 30 import org.eclipse.jface.dialogs.IDialogConstants; 31 import org.eclipse.jface.dialogs.StatusDialog; 32 import org.eclipse.jface.viewers.DoubleClickEvent; 33 import org.eclipse.jface.viewers.IDoubleClickListener; 34 import org.eclipse.jface.viewers.ISelectionChangedListener; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 import org.eclipse.jface.viewers.ITreeContentProvider; 37 import org.eclipse.jface.viewers.SelectionChangedEvent; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.custom.BusyIndicator; 40 import org.eclipse.swt.events.FocusAdapter; 41 import org.eclipse.swt.events.FocusEvent; 42 import org.eclipse.swt.events.KeyAdapter; 43 import org.eclipse.swt.events.KeyEvent; 44 import org.eclipse.swt.events.ModifyEvent; 45 import org.eclipse.swt.events.ModifyListener; 46 import org.eclipse.swt.events.SelectionAdapter; 47 import org.eclipse.swt.events.SelectionEvent; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.layout.GridLayout; 50 import org.eclipse.swt.widgets.Combo; 51 import org.eclipse.swt.widgets.Composite; 52 import org.eclipse.swt.widgets.Control; 53 import org.eclipse.swt.widgets.Label; 54 import org.eclipse.swt.widgets.Shell; 55 import org.eclipse.swt.widgets.Text; 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.dialogs.FileSystemElement; 58 import org.eclipse.ui.externaltools.internal.ui.TreeAndListGroup; 59 import org.eclipse.ui.model.WorkbenchContentProvider; 60 import org.eclipse.ui.model.WorkbenchLabelProvider; 61 import org.eclipse.ui.model.WorkbenchViewerComparator; 62 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; 63 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider; 64 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 65 66 public class AddCustomDialog extends StatusDialog { 67 68 private ZipFileStructureProvider providerCache; 69 private IImportStructureProvider currentProvider; 70 71 private boolean entryChanged = false; 73 74 private Combo sourceNameField; 75 private List libraryEntries; 76 private List existingNames; 77 78 private String noNameErrorMsg; 79 private String alreadyExistsErrorMsg; 80 81 private TreeAndListGroup selectionGroup; 82 83 private Text nameField; 84 85 private String name=""; private IAntClasspathEntry library= null; 87 private String className=""; 89 private boolean editing= false; 90 91 private String helpContext; 92 93 96 public AddCustomDialog(Shell parent, List libraryEntries, List existingNames, String helpContext) { 97 super(parent); 98 this.libraryEntries = libraryEntries; 99 this.existingNames= existingNames; 100 this.helpContext= helpContext; 101 setShellStyle(getShellStyle() | SWT.RESIZE); 102 } 103 104 107 protected Control createDialogArea(Composite parent) { 108 Composite topComposite= (Composite) super.createDialogArea(parent); 109 topComposite.setSize(topComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 110 111 Composite topGroup = new Composite(topComposite, SWT.NONE); 112 GridLayout layout = new GridLayout(); 113 layout.numColumns = 2; 114 layout.marginHeight= 0; 115 layout.marginWidth= 0; 116 topGroup.setLayout(layout); 117 topGroup.setFont(topComposite.getFont()); 118 topGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 119 120 createNameGroup(topGroup); 121 createRootDirectoryGroup(topGroup); 122 createFileSelectionGroup(topComposite); 123 124 if (library != null) { 125 setSourceName(library.getLabel()); 126 } 127 return topComposite; 128 } 129 130 private void createNameGroup(Composite topComposite) { 131 Label label = new Label(topComposite, SWT.NONE); 132 label.setFont(topComposite.getFont()); 133 label.setText(AntPreferencesMessages.AddCustomDialog__Name__3); 134 135 nameField = new Text(topComposite, SWT.BORDER); 136 GridData data = new GridData(GridData.FILL_HORIZONTAL); 137 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 138 139 nameField.setLayoutData(data); 140 nameField.setFont(topComposite.getFont()); 141 nameField.setText(name); 142 nameField.addModifyListener(new ModifyListener() { 143 public void modifyText(ModifyEvent e) { 144 updateStatus(); 145 } 146 }); 147 } 148 149 152 protected void configureShell(Shell newShell) { 153 super.configureShell(newShell); 154 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, helpContext); 155 } 156 157 161 private void clearProviderCache() { 162 if (providerCache != null) { 163 closeZipFile(providerCache.getZipFile()); 164 providerCache = null; 165 } 166 } 167 168 171 private boolean closeZipFile(ZipFile file) { 172 try { 173 file.close(); 174 } catch (IOException e) { 175 AntUIPlugin.log(MessageFormat.format(AntPreferencesMessages.AddCustomDialog_Could_not_close_zip_file__0__4, new String []{file.getName()}), e); 176 return false; 177 } 178 179 return true; 180 } 181 182 185 private void createRootDirectoryGroup(Composite parent) { 186 Label groupLabel = new Label(parent, SWT.NONE); 187 groupLabel.setText(AntPreferencesMessages.AddCustomDialog__Location); 188 groupLabel.setFont(parent.getFont()); 189 190 sourceNameField = new Combo(parent, SWT.BORDER | SWT.READ_ONLY); 192 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); 193 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 194 sourceNameField.setLayoutData(data); 195 sourceNameField.setFont(parent.getFont()); 196 197 sourceNameField.addSelectionListener(new SelectionAdapter() { 198 public void widgetSelected(SelectionEvent e) { 199 updateFromSourceField(); 200 } 201 }); 202 203 Iterator libraries= libraryEntries.iterator(); 204 while (libraries.hasNext()) { 205 ClasspathEntry entry = (ClasspathEntry) libraries.next(); 206 sourceNameField.add(entry.getLabel()); 207 } 208 209 sourceNameField.addKeyListener(new KeyAdapter() { 210 213 public void keyPressed(KeyEvent e) { 214 entryChanged = true; 216 } 217 }); 218 219 sourceNameField.addFocusListener(new FocusAdapter() { 220 223 public void focusLost(FocusEvent e) { 224 if (entryChanged) { 226 entryChanged = false; 227 updateFromSourceField(); 228 } 229 } 230 }); 231 } 232 233 236 private void updateFromSourceField(){ 237 setSourceName(sourceNameField.getText()); 238 updateStatus(); 239 } 240 241 242 245 private void updateStatus() { 246 StatusInfo status= new StatusInfo(); 247 String customName= nameField.getText().trim(); 248 if (customName.length() == 0) { 249 status.setError(noNameErrorMsg); 250 } else if (!editing){ 251 Iterator names= existingNames.iterator(); 252 while (names.hasNext()) { 253 String aName = (String ) names.next(); 254 if(aName.equals(customName)) { 255 status.setError(MessageFormat.format(alreadyExistsErrorMsg, new String []{customName})); 256 updateStatus(status); 257 return; 258 } 259 } 260 } 261 if (selectionGroup.getListTableSelection().isEmpty()) { 262 status.setError(AntPreferencesMessages.AddCustomDialog_mustSelect); 263 } 264 updateStatus(status); 265 } 266 267 268 275 private void setSourceName(String path) { 276 277 if (path.length() > 0) { 278 279 String [] currentItems = this.sourceNameField.getItems(); 280 int selectionIndex = -1; 281 for (int i = 0; i < currentItems.length; i++) { 282 if (currentItems[i].equals(path)) { 283 selectionIndex = i; 284 break; 285 } 286 } 287 if (selectionIndex < 0) { 288 int oldLength = currentItems.length; 289 String [] newItems = new String [oldLength + 1]; 290 System.arraycopy(currentItems, 0, newItems, 0, oldLength); 291 newItems[oldLength] = path; 292 this.sourceNameField.setItems(newItems); 293 selectionIndex = oldLength; 294 } 295 this.sourceNameField.select(selectionIndex); 296 297 resetSelection(); 298 } 299 } 300 301 304 private void createFileSelectionGroup(Composite parent) { 305 FileSystemElement dummyRoot= new FileSystemElement("Dummy", null, true); this.selectionGroup = new TreeAndListGroup(parent, dummyRoot, 308 getFolderProvider(), new WorkbenchLabelProvider(), getFileProvider(), 309 new WorkbenchLabelProvider(), SWT.NONE, 400, 150, false); 310 311 ISelectionChangedListener listener = new ISelectionChangedListener() { 312 public void selectionChanged(SelectionChangedEvent event) { 313 updateStatus(); 314 } 315 }; 316 317 WorkbenchViewerComparator comparator = new WorkbenchViewerComparator(); 318 this.selectionGroup.setTreeComparator(comparator); 319 this.selectionGroup.setListSorter(comparator); 320 this.selectionGroup.addSelectionChangedListener(listener); 321 selectionGroup.addDoubleClickListener(new IDoubleClickListener() { 322 public void doubleClick(DoubleClickEvent event) { 323 if (getButton(IDialogConstants.OK_ID).isEnabled()) { 324 buttonPressed(IDialogConstants.OK_ID); 325 } 326 } 327 }); 328 } 329 330 334 protected boolean ensureSourceIsValid() { 335 ZipFile specifiedFile = getSpecifiedSourceFile(); 336 337 if (specifiedFile == null){ 338 return false; 339 } 340 341 return closeZipFile(specifiedFile); 342 } 343 348 private MinimizedFileSystemElement getFileSystemTree() { 349 IImportStructureProvider provider= null; 350 MinimizedFileSystemElement element= null; 351 ZipFile sourceFile = getSpecifiedSourceFile(); 352 if (sourceFile == null) { 353 File file= new File (sourceNameField.getText()); 354 if (file.exists()) { 355 provider = FileSystemStructureProvider.INSTANCE; 356 element= selectFiles(file, provider); 357 } 358 } else { 359 provider = getStructureProvider(sourceFile); 361 element= selectFiles(((ZipFileStructureProvider)provider).getRoot(), provider); 362 } 363 this.currentProvider = provider; 364 return element; 365 } 366 367 372 private MinimizedFileSystemElement selectFiles(final Object rootFileSystemObject, final IImportStructureProvider structureProvider) { 373 374 final MinimizedFileSystemElement[] results = new MinimizedFileSystemElement[1]; 375 376 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 377 public void run() { 378 results[0] = createRootElement(rootFileSystemObject, structureProvider); 380 } 381 }); 382 383 return results[0]; 384 } 385 386 390 private MinimizedFileSystemElement createRootElement(Object fileSystemObject, IImportStructureProvider provider) { 391 boolean isContainer = provider.isFolder(fileSystemObject); 392 String elementLabel = provider.getLabel(fileSystemObject); 393 394 MinimizedFileSystemElement dummyParent = 397 new MinimizedFileSystemElement("", null, true); dummyParent.setPopulated(); 399 MinimizedFileSystemElement result = 400 new MinimizedFileSystemElement(elementLabel, dummyParent, isContainer); 401 result.setFileSystemObject(fileSystemObject); 402 403 result.getFiles(provider); 405 406 return dummyParent; 407 } 408 409 413 private ZipFile getSpecifiedSourceFile() { 414 try { 415 String expanded = sourceNameField.getText(); 416 expanded= VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(expanded); 417 return new ZipFile (expanded); 418 } catch (ZipException e) { 419 StatusInfo status= new StatusInfo(); 420 status.setError(AntPreferencesMessages.AddCustomDialog_Bad_Format); 421 updateStatus(status); 422 } catch (IOException e) { 423 StatusInfo status= new StatusInfo(); 424 status.setError(AntPreferencesMessages.AddCustomDialog_Unreadable); 425 updateStatus(status); 426 } catch (CoreException e) { 427 StatusInfo status= new StatusInfo(); 428 status.setError(AntPreferencesMessages.AddCustomDialog_13); 429 updateStatus(status); 430 } 431 432 sourceNameField.setFocus(); 433 return null; 434 } 435 438 private ZipFileStructureProvider getStructureProvider(ZipFile targetZip) { 439 if (providerCache == null) { 440 providerCache = new ZipFileStructureProvider(targetZip); 441 } else if (!providerCache.getZipFile().getName().equals(targetZip.getName())) { 442 clearProviderCache(); 443 providerCache = new ZipFileStructureProvider(targetZip); 445 } else if (!providerCache.getZipFile().equals(targetZip)) { 446 closeZipFile(targetZip); } 448 449 return providerCache; 450 } 451 452 455 private void resetSelection() { 456 MinimizedFileSystemElement currentRoot = getFileSystemTree(); 457 selectionGroup.setRoot(currentRoot); 458 459 if (className.length() != 0) { 460 StringTokenizer tokenizer= new StringTokenizer (className, "."); selectClass(currentRoot, tokenizer); 462 } 463 } 464 465 private void selectClass(MinimizedFileSystemElement currentParent, StringTokenizer tokenizer) { 466 if (!tokenizer.hasMoreTokens()) { 467 return; 468 } 469 List folders= currentParent.getFolders(currentProvider); 470 if (folders.size() == 1) { 471 MinimizedFileSystemElement element = (MinimizedFileSystemElement)folders.get(0); 472 if (element.getLabel(null).equals("/")) { selectionGroup.selectAndRevealFolder(element); 474 selectClass(element, tokenizer); 475 return; 476 } 477 } 478 String currentName= tokenizer.nextToken(); 479 if (tokenizer.hasMoreTokens()) { 480 Iterator allFolders= folders.iterator(); 481 while (allFolders.hasNext()) { 482 MinimizedFileSystemElement folder = (MinimizedFileSystemElement) allFolders.next(); 483 if (folder.getLabel(null).equals(currentName)) { 484 selectionGroup.selectAndRevealFolder(folder); 485 selectClass(folder, tokenizer); 486 return; 487 } 488 } 489 } else { 490 List files= currentParent.getFiles(currentProvider); 491 Iterator iter= files.iterator(); 492 while (iter.hasNext()) { 493 MinimizedFileSystemElement file = (MinimizedFileSystemElement) iter.next(); 494 if (file.getLabel(null).equals(currentName + ".class")) { selectionGroup.selectAndRevealFile(file); 496 return; 497 } 498 } 499 } 500 } 501 502 506 private ITreeContentProvider getFileProvider() { 507 return new WorkbenchContentProvider() { 508 public Object [] getChildren(Object o) { 509 if (o instanceof MinimizedFileSystemElement) { 510 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 511 return element.getFiles(currentProvider).toArray(); 512 } 513 return new Object [0]; 514 } 515 }; 516 } 517 518 522 private ITreeContentProvider getFolderProvider() { 523 return new WorkbenchContentProvider() { 524 public Object [] getChildren(Object o) { 525 if (o instanceof MinimizedFileSystemElement) { 526 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 527 return element.getFolders(currentProvider).toArray(); 528 } 529 return new Object [0]; 530 } 531 public boolean hasChildren(Object o) { 532 if (o instanceof MinimizedFileSystemElement) { 533 MinimizedFileSystemElement element = (MinimizedFileSystemElement) o; 534 if (element.isPopulated()) { 535 return getChildren(element).length > 0; 536 } 537 538 return true; 540 } 541 return false; 542 } 543 }; 544 } 545 546 549 protected void cancelPressed() { 550 clearProviderCache(); 551 super.cancelPressed(); 552 } 553 554 557 protected void okPressed() { 558 clearProviderCache(); 559 name= nameField.getText().trim(); 560 library= (ClasspathEntry)libraryEntries.get(sourceNameField.getSelectionIndex()); 561 IStructuredSelection selection= this.selectionGroup.getListTableSelection(); 562 MinimizedFileSystemElement element= (MinimizedFileSystemElement)selection.getFirstElement(); 563 if (element == null) { 564 super.okPressed(); 565 return; 566 } 567 Object file= element.getFileSystemObject(); 568 if (file instanceof ZipEntry ) { 569 className= ((ZipEntry )file).getName(); 570 } else { 571 className= ((File )file).getAbsolutePath(); 572 IPath classPath= new Path(className); 573 IPath libraryPath= new Path(library.getEntryURL().getPath()); 574 int matching= classPath.matchingFirstSegments(libraryPath); 575 classPath= classPath.removeFirstSegments(matching); 576 classPath= classPath.setDevice(null); 577 className= classPath.toString(); 578 } 579 int index= className.lastIndexOf('.'); 580 className= className.substring(0, index); 581 className= className.replace('/', '.'); 582 super.okPressed(); 583 } 584 585 protected String getName() { 586 return name; 587 } 588 589 protected void setName(String name) { 590 this.name= name; 591 } 592 593 protected void setLibraryEntry(IAntClasspathEntry library) { 594 this.library= library; 595 editing= true; 596 } 597 598 protected IAntClasspathEntry getLibraryEntry() { 599 return this.library; 600 } 601 602 protected String getClassName() { 603 return className; 604 } 605 606 protected void setClassName(String className) { 607 this.className= className; 608 } 609 610 613 public void create() { 614 super.create(); 615 getButton(IDialogConstants.OK_ID).setEnabled(!(library == null)); 616 } 617 618 protected void setAlreadyExistsErrorMsg(String alreadyExistsErrorMsg) { 619 this.alreadyExistsErrorMsg = alreadyExistsErrorMsg; 620 } 621 622 protected void setNoNameErrorMsg(String noNameErrorMsg) { 623 this.noNameErrorMsg = noNameErrorMsg; 624 } 625 } 626 | Popular Tags |