1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.OutputStream ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.OutputKeys ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerException ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.dom.DOMSource ; 36 import javax.xml.transform.stream.StreamResult ; 37 38 import org.eclipse.core.runtime.CoreException; 39 import org.eclipse.core.runtime.IPath; 40 import org.eclipse.core.runtime.IProgressMonitor; 41 import org.eclipse.core.runtime.IStatus; 42 import org.eclipse.core.runtime.MultiStatus; 43 import org.eclipse.core.runtime.NullProgressMonitor; 44 import org.eclipse.core.runtime.Path; 45 46 import org.eclipse.core.resources.IFile; 47 import org.eclipse.core.resources.IProject; 48 import org.eclipse.core.resources.IResource; 49 import org.eclipse.core.resources.IWorkspaceRoot; 50 import org.eclipse.core.resources.ResourcesPlugin; 51 52 import org.eclipse.swt.SWT; 53 import org.eclipse.swt.events.KeyEvent; 54 import org.eclipse.swt.graphics.Point; 55 import org.eclipse.swt.layout.GridData; 56 import org.eclipse.swt.widgets.Composite; 57 import org.eclipse.swt.widgets.Control; 58 import org.eclipse.swt.widgets.FileDialog; 59 import org.eclipse.swt.widgets.Shell; 60 61 import org.eclipse.jface.dialogs.Dialog; 62 import org.eclipse.jface.dialogs.IDialogSettings; 63 import org.eclipse.jface.dialogs.MessageDialog; 64 import org.eclipse.jface.dialogs.StatusDialog; 65 import org.eclipse.jface.operation.IRunnableContext; 66 import org.eclipse.jface.operation.IRunnableWithProgress; 67 import org.eclipse.jface.preference.PreferencePage; 68 import org.eclipse.jface.resource.StringConverter; 69 import org.eclipse.jface.viewers.AbstractTreeViewer; 70 import org.eclipse.jface.viewers.StructuredSelection; 71 import org.eclipse.jface.window.Window; 72 73 import org.eclipse.ui.IWorkbench; 74 import org.eclipse.ui.IWorkbenchPreferencePage; 75 import org.eclipse.ui.PlatformUI; 76 77 import org.eclipse.jdt.core.ClasspathContainerInitializer; 78 import org.eclipse.jdt.core.IAccessRule; 79 import org.eclipse.jdt.core.IClasspathAttribute; 80 import org.eclipse.jdt.core.IClasspathContainer; 81 import org.eclipse.jdt.core.IClasspathEntry; 82 import org.eclipse.jdt.core.IJavaProject; 83 import org.eclipse.jdt.core.JavaCore; 84 import org.eclipse.jdt.core.JavaModelException; 85 86 import org.eclipse.jdt.internal.corext.util.Messages; 87 88 import org.eclipse.jdt.ui.JavaUI; 89 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess; 90 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration; 91 92 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 93 import org.eclipse.jdt.internal.ui.IUIConstants; 94 import org.eclipse.jdt.internal.ui.JavaPlugin; 95 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 96 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 97 import org.eclipse.jdt.internal.ui.wizards.buildpaths.AccessRulesDialog; 98 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport; 99 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 100 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute; 101 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementSorter; 102 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListLabelProvider; 103 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPUserLibraryElement; 104 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathAttributeConfigurationDescriptors; 105 import org.eclipse.jdt.internal.ui.wizards.dialogfields.CheckedListDialogField; 106 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 107 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 108 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 109 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 110 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter; 111 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 112 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 113 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 114 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 115 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 116 import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField; 117 118 import org.w3c.dom.Document ; 119 import org.w3c.dom.Element ; 120 import org.w3c.dom.Node ; 121 import org.w3c.dom.NodeList ; 122 import org.xml.sax.InputSource ; 123 import org.xml.sax.SAXException ; 124 125 public class UserLibraryPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 126 127 public static final String ID= "org.eclipse.jdt.ui.preferences.UserLibraryPreferencePage"; public static final String DATA_DO_CREATE= "do_create"; public static final String DATA_LIBRARY_TO_SELECT= "select_library"; 131 public static class LibraryNameDialog extends StatusDialog implements IDialogFieldListener { 132 133 private StringDialogField fNameField; 134 private SelectionButtonDialogField fIsSystemField; 135 136 private CPUserLibraryElement fElementToEdit; 137 private List fExistingLibraries; 138 139 public LibraryNameDialog(Shell parent, CPUserLibraryElement elementToEdit, List existingLibraries) { 140 super(parent); 141 if (elementToEdit == null) { 142 setTitle(PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_new_title); 143 } else { 144 setTitle(PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_edit_title); 145 } 146 147 fElementToEdit= elementToEdit; 148 fExistingLibraries= existingLibraries; 149 150 fNameField= new StringDialogField(); 151 fNameField.setDialogFieldListener(this); 152 fNameField.setLabelText(PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_name_label); 153 154 fIsSystemField= new SelectionButtonDialogField(SWT.CHECK); 155 fIsSystemField.setLabelText(PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_issystem_label); 156 157 if (elementToEdit != null) { 158 fNameField.setText(elementToEdit.getName()); 159 fIsSystemField.setSelection(elementToEdit.isSystemLibrary()); 160 } else { 161 fNameField.setText(""); fIsSystemField.setSelection(false); 163 } 164 } 165 166 169 protected Control createDialogArea(Composite parent) { 170 Composite composite= (Composite) super.createDialogArea(parent); 171 LayoutUtil.doDefaultLayout(composite, new DialogField[] { fNameField, fIsSystemField }, true, SWT.DEFAULT, SWT.DEFAULT); 172 fNameField.postSetFocusOnDialogField(parent.getDisplay()); 173 174 Dialog.applyDialogFont(composite); 175 176 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.CP_EDIT_USER_LIBRARY); 177 178 return composite; 179 } 180 181 184 public void dialogFieldChanged(DialogField field) { 185 if (field == fNameField) { 186 updateStatus(validateSettings()); 187 } 188 } 189 190 private IStatus validateSettings() { 191 String name= fNameField.getText(); 192 if (name.length() == 0) { 193 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_name_error_entername); 194 } 195 for (int i= 0; i < fExistingLibraries.size(); i++) { 196 CPUserLibraryElement curr= (CPUserLibraryElement) fExistingLibraries.get(i); 197 if (curr != fElementToEdit && name.equals(curr.getName())) { 198 return new StatusInfo(IStatus.ERROR, Messages.format(PreferencesMessages.UserLibraryPreferencePage_LibraryNameDialog_name_error_exists, name)); 199 } 200 } 201 IStatus status= ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE); 202 if (status.matches(IStatus.ERROR)) { 203 return new StatusInfo(IStatus.ERROR, "Name contains invalid characters."); } 205 return StatusInfo.OK_STATUS; 206 } 207 208 public CPUserLibraryElement getNewLibrary() { 209 CPListElement[] entries= null; 210 if (fElementToEdit != null) { 211 entries= fElementToEdit.getChildren(); 212 } 213 return new CPUserLibraryElement(fNameField.getText(), fIsSystemField.isSelected(), entries); 214 } 215 216 } 217 218 public static class LoadSaveDialog extends StatusDialog implements IStringButtonAdapter, IDialogFieldListener, IListAdapter { 219 220 221 private static final String VERSION1= "1"; private static final String CURRENT_VERSION= "2"; 224 private static final String TAG_ROOT= "eclipse-userlibraries"; private static final String TAG_VERSION= "version"; private static final String TAG_LIBRARY= "library"; private static final String TAG_SOURCEATTACHMENT= "source"; private static final String TAG_ARCHIVE_PATH= "path"; private static final String TAG_ARCHIVE= "archive"; private static final String TAG_SYSTEMLIBRARY= "systemlibrary"; private static final String TAG_NAME= "name"; private static final String TAG_JAVADOC= "javadoc"; private static final String TAG_NATIVELIB_PATHS= "nativelibpaths"; private static final String TAG_ACCESSRULES= "accessrules"; private static final String TAG_ACCESSRULE= "accessrule"; private static final String TAG_RULE_KIND= "kind"; private static final String TAG_RULE_PATTERN= "pattern"; 239 private static final String PREF_LASTPATH= JavaUI.ID_PLUGIN + ".lastuserlibrary"; private static final String PREF_USER_LIBRARY_LOADSAVE_SIZE= "UserLibraryLoadSaveDialog.size"; 242 private List fExistingLibraries; 243 private IDialogSettings fSettings; 244 245 private File fLastFile; 246 247 private StringButtonDialogField fLocationField; 248 private CheckedListDialogField fExportImportList; 249 private Point fInitialSize; 250 private final boolean fIsSave; 251 252 public LoadSaveDialog(Shell shell, boolean isSave, List existingLibraries, IDialogSettings dialogSettings) { 253 super(shell); 254 initializeDialogUnits(shell); 255 256 setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE); 257 258 fExistingLibraries= existingLibraries; 259 fSettings= dialogSettings; 260 fLastFile= null; 261 fIsSave= isSave; 262 263 int defaultWidth= convertWidthInCharsToPixels(80); 264 int defaultHeigth= convertHeightInCharsToPixels(34); 265 String lastSize= fSettings.get(PREF_USER_LIBRARY_LOADSAVE_SIZE); 266 if (lastSize != null) { 267 fInitialSize= StringConverter.asPoint(lastSize, new Point(defaultWidth, defaultHeigth)); 268 } else { 269 fInitialSize= new Point(defaultWidth, defaultHeigth); 270 } 271 272 if (isSave()) { 273 setTitle(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_save_title); 274 } else { 275 setTitle(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_title); 276 } 277 278 fLocationField= new StringButtonDialogField(this); 279 fLocationField.setLabelText(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_label); 280 fLocationField.setButtonLabel(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_button); 281 fLocationField.setDialogFieldListener(this); 282 283 String [] buttonNames= new String [] { 284 PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_selectall_button, 285 PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_deselectall_button 286 }; 287 fExportImportList= new CheckedListDialogField(this, buttonNames, new CPListLabelProvider()); 288 fExportImportList.setCheckAllButtonIndex(0); 289 fExportImportList.setUncheckAllButtonIndex(1); 290 fExportImportList.setViewerComparator(new CPListElementSorter()); 291 fExportImportList.setDialogFieldListener(this); 292 if (isSave()) { 293 fExportImportList.setLabelText(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_save_label); 294 fExportImportList.setElements(fExistingLibraries); 295 fExportImportList.checkAll(true); 296 } else { 297 fExportImportList.setLabelText(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_load_label); 298 } 299 String lastPath= fSettings.get(PREF_LASTPATH); 300 if (lastPath != null) { 301 fLocationField.setText(lastPath); 302 } else { 303 fLocationField.setText(""); } 305 } 306 307 protected Point getInitialSize() { 308 return fInitialSize; 309 } 310 311 private boolean isSave() { 312 return fIsSave; 313 } 314 315 318 protected Control createDialogArea(Composite parent) { 319 Composite composite= (Composite) super.createDialogArea(parent); 320 DialogField[] fields; 321 if (isSave()) { 322 fields= new DialogField[] { fExportImportList, fLocationField }; 323 } else { 324 fields= new DialogField[] { fLocationField, fExportImportList }; 325 } 326 LayoutUtil.doDefaultLayout(composite, fields, true, SWT.DEFAULT, SWT.DEFAULT); 327 fExportImportList.getListControl(null).setLayoutData(new GridData(GridData.FILL_BOTH)); 328 329 fLocationField.postSetFocusOnDialogField(parent.getDisplay()); 330 331 Dialog.applyDialogFont(composite); 332 333 if (isSave()) { 334 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.CP_EXPORT_USER_LIBRARY); 335 } else { 336 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.CP_IMPORT_USER_LIBRARY); 337 } 338 339 return composite; 340 } 341 342 343 346 public void changeControlPressed(DialogField field) { 347 String label= isSave() ? PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_filedialog_save_title : PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_filedialog_load_title; 348 FileDialog dialog= new FileDialog(getShell(), isSave() ? SWT.SAVE : SWT.OPEN); 349 dialog.setText(label); 350 dialog.setFilterExtensions(new String [] {"*.userlibraries", "*.*"}); String lastPath= fLocationField.getText(); 352 if (lastPath.length() == 0 || !new File (lastPath).exists()) { 353 lastPath= fSettings.get(PREF_LASTPATH); 354 } 355 if (lastPath != null) { 356 dialog.setFileName(lastPath); 357 } 358 String fileName= dialog.open(); 359 if (fileName != null) { 360 fSettings.put(PREF_LASTPATH, fileName); 361 fLocationField.setText(fileName); 362 } 363 } 364 365 private IStatus updateShownLibraries(IStatus status) { 366 if (!status.isOK()) { 367 fExportImportList.removeAllElements(); 368 fExportImportList.setEnabled(false); 369 fLastFile= null; 370 } else { 371 File file= new File (fLocationField.getText()); 372 if (!file.equals(fLastFile)) { 373 fLastFile= file; 374 try { 375 List elements= loadLibraries(file); 376 fExportImportList.setElements(elements); 377 fExportImportList.checkAll(true); 378 fExportImportList.setEnabled(true); 379 if (elements.isEmpty()) { 380 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_error_empty); 381 } 382 } catch (IOException e) { 383 fExportImportList.removeAllElements(); 384 fExportImportList.setEnabled(false); 385 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_error_invalidfile); 386 } 387 } 388 } 389 return status; 390 } 391 392 395 public void dialogFieldChanged(DialogField field) { 396 if (field == fLocationField) { 397 IStatus status= validateSettings(); 398 if (!isSave()) { 399 status= updateShownLibraries(status); 400 } 401 updateStatus(status); 402 } else if (field == fExportImportList) { 403 updateStatus(validateSettings()); 404 } 405 } 406 407 410 public void customButtonPressed(ListDialogField field, int index) { 411 } 412 413 416 public void selectionChanged(ListDialogField field) { 417 } 418 419 422 public void doubleClicked(ListDialogField field) { 423 List selectedElements= fExportImportList.getSelectedElements(); 424 if (selectedElements.size() == 1) { 425 Object elem= selectedElements.get(0); 426 fExportImportList.setChecked(elem, !fExportImportList.isChecked(elem)); 427 } 428 } 429 430 433 protected void okPressed() { 434 if (isSave()) { 435 final File file= new File (fLocationField.getText()); 436 if (file.exists()) { 437 String title= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_overwrite_title; 438 String message= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_overwrite_message; 439 if (!MessageDialog.openQuestion(getShell(), title, message)) { 440 return; 441 } 442 } 443 try { 444 String encoding= "UTF-8"; IPath filePath= Path.fromOSString(file.getCanonicalPath()); 446 final IPath workspacePath= ResourcesPlugin.getWorkspace().getRoot().getLocation(); 447 if (filePath.matchingFirstSegments(workspacePath) == workspacePath.segmentCount()) { 448 IPath path= filePath.removeFirstSegments(workspacePath.segmentCount()); 449 path= path.makeRelative(); 450 final IFile result= ResourcesPlugin.getWorkspace().getRoot().getFile(path); 451 try { 452 encoding= result.getCharset(true); 453 } catch (CoreException exception) { 454 JavaPlugin.log(exception); 455 } 456 } 457 final List elements= fExportImportList.getCheckedElements(); 458 final String charset= encoding; 459 IRunnableContext context= PlatformUI.getWorkbench().getProgressService(); 460 try { 461 context.run(true, true, new IRunnableWithProgress() { 462 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 463 try { 464 saveLibraries(elements, file, charset, monitor); 465 } catch (IOException e) { 466 throw new InvocationTargetException (e); 467 } 468 } 469 }); 470 fSettings.put(PREF_LASTPATH, file.getPath()); 471 } catch (InvocationTargetException e) { 472 String errorTitle= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_save_errordialog_title; 473 String errorMessage= Messages.format(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_save_errordialog_message, e.getMessage()); 474 ExceptionHandler.handle(e, getShell(), errorTitle, errorMessage); 475 return; 476 } catch (InterruptedException e) { 477 return; 479 } 480 String savedTitle= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_save_ok_title; 481 String savedMessage= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_save_ok_message; 482 MessageDialog.openInformation(getShell(), savedTitle, savedMessage); 483 } catch (IOException exception) { 484 JavaPlugin.log(exception); 485 } 486 } else { 487 HashSet map= new HashSet (fExistingLibraries.size()); 488 for (int k= 0; k < fExistingLibraries.size(); k++) { 489 CPUserLibraryElement elem= (CPUserLibraryElement) fExistingLibraries.get(k); 490 map.add(elem.getName()); 491 } 492 int nReplaced= 0; 493 List elements= getLoadedLibraries(); 494 for (int i= 0; i < elements.size(); i++) { 495 CPUserLibraryElement curr= (CPUserLibraryElement) elements.get(i); 496 if (map.contains(curr.getName())) { 497 nReplaced++; 498 } 499 } 500 if (nReplaced > 0) { 501 String replaceTitle= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_replace_title; 502 String replaceMessage; 503 if (nReplaced == 1) { 504 replaceMessage= PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_replace_message; 505 } else { 506 replaceMessage= Messages.format(PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_replace_multiple_message, String.valueOf(nReplaced)); 507 } 508 if (!MessageDialog.openConfirm(getShell(), replaceTitle, replaceMessage)) { 509 return; 510 } 511 } 512 } 513 super.okPressed(); 514 } 515 516 519 public boolean close() { 520 Point point= getShell().getSize(); 521 fSettings.put(PREF_USER_LIBRARY_LOADSAVE_SIZE, StringConverter.asString(point)); 522 return super.close(); 523 } 524 525 private IStatus validateSettings() { 526 String name= fLocationField.getText(); 527 fLastFile= null; 528 if (isSave()) { 529 if (name.length() == 0) { 530 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_error_save_enterlocation); 531 } 532 File file= new File (name); 533 if (file.isDirectory()) { 534 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_error_save_invalid); 535 } 536 if (fExportImportList.getCheckedSize() == 0) { 537 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_error_save_nothingselected); 538 } 539 fLastFile= file; 540 } else { 541 if (name.length() == 0) { 542 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_error_load_enterlocation); 543 } 544 if (!new File (name).isFile()) { 545 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_location_error_load_invalid); 546 } 547 if (fExportImportList.getSize() > 0 && fExportImportList.getCheckedSize() == 0) { 548 return new StatusInfo(IStatus.ERROR, PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_list_error_load_nothingselected); 549 } 550 } 551 return new StatusInfo(); 552 } 553 554 protected static void saveLibraries(List libraries, File file, String encoding, IProgressMonitor monitor) throws IOException { 555 OutputStream stream= new FileOutputStream (file); 556 try { 557 DocumentBuilder docBuilder= null; 558 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 559 factory.setValidating(false); 560 docBuilder= factory.newDocumentBuilder(); 561 Document document= docBuilder.newDocument(); 562 563 Element rootElement= document.createElement(TAG_ROOT); 565 document.appendChild(rootElement); 566 567 rootElement.setAttribute(TAG_VERSION, CURRENT_VERSION); 568 569 for (int i= 0; i < libraries.size(); i++) { 570 Element libraryElement= document.createElement(TAG_LIBRARY); 571 rootElement.appendChild(libraryElement); 572 573 CPUserLibraryElement curr= (CPUserLibraryElement) libraries.get(i); 574 libraryElement.setAttribute(TAG_NAME, curr.getName()); 575 libraryElement.setAttribute(TAG_SYSTEMLIBRARY, String.valueOf(curr.isSystemLibrary())); 576 577 CPListElement[] children= curr.getChildren(); 578 for (int k= 0; k < children.length; k++) { 579 CPListElement child= children[k]; 580 581 Element childElement= document.createElement(TAG_ARCHIVE); 582 libraryElement.appendChild(childElement); 583 584 childElement.setAttribute(TAG_ARCHIVE_PATH, child.getPath().toPortableString()); 585 IPath sourceAttachment= (IPath) child.getAttribute(CPListElement.SOURCEATTACHMENT); 586 if (sourceAttachment != null) { 587 childElement.setAttribute(TAG_SOURCEATTACHMENT, sourceAttachment.toPortableString()); 588 589 } 590 String javadocLocation= (String ) child.getAttribute(CPListElement.JAVADOC); 591 if (javadocLocation != null) { 592 childElement.setAttribute(TAG_JAVADOC, javadocLocation); 593 } 594 String nativeLibPath= (String ) child.getAttribute(CPListElement.NATIVE_LIB_PATH); 595 if (nativeLibPath != null) { 596 childElement.setAttribute(TAG_NATIVELIB_PATHS, nativeLibPath); 597 } 598 IAccessRule[] accessRules= (IAccessRule[]) child.getAttribute(CPListElement.ACCESSRULES); 599 if (accessRules != null && accessRules.length > 0) { 600 Element rulesElement= document.createElement(TAG_ACCESSRULES); 601 childElement.appendChild(rulesElement); 602 for (int n= 0; n < accessRules.length; n++) { 603 IAccessRule rule= accessRules[n]; 604 Element ruleElement= document.createElement(TAG_ACCESSRULE); 605 rulesElement.appendChild(ruleElement); 606 ruleElement.setAttribute(TAG_RULE_KIND, String.valueOf(rule.getKind())); 607 ruleElement.setAttribute(TAG_RULE_PATTERN, rule.getPattern().toPortableString()); 608 } 609 } 610 } 611 } 612 613 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 615 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); 617 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 620 DOMSource source = new DOMSource (document); 621 StreamResult result = new StreamResult (stream); 622 transformer.transform(source, result); 623 } catch (ParserConfigurationException e) { 624 throw new IOException (e.getMessage()); 625 } catch (TransformerException e) { 626 throw new IOException (e.getMessage()); 627 } finally { 628 try { 629 stream.close(); 630 } catch (IOException e) { 631 } 633 if (monitor != null) { 634 monitor.done(); 635 } 636 } 637 } 638 639 private static List loadLibraries(File file) throws IOException { 640 InputStream stream= new FileInputStream (file); 641 Element cpElement; 642 try { 643 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 644 cpElement = parser.parse(new InputSource (stream)).getDocumentElement(); 645 } catch (SAXException e) { 646 throw new IOException (PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_badformat); 647 } catch (ParserConfigurationException e) { 648 throw new IOException (PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_badformat); 649 } finally { 650 stream.close(); 651 } 652 653 if (!cpElement.getNodeName().equalsIgnoreCase(TAG_ROOT)) { 654 throw new IOException (PreferencesMessages.UserLibraryPreferencePage_LoadSaveDialog_load_badformat); 655 } 656 657 String version= cpElement.getAttribute(TAG_VERSION); 658 659 NodeList libList= cpElement.getElementsByTagName(TAG_LIBRARY); 660 int length = libList.getLength(); 661 662 ArrayList result= new ArrayList (length); 663 for (int i= 0; i < length; i++) { 664 Node lib= libList.item(i); 665 if (!(lib instanceof Element)) { 666 continue; 667 } 668 Element libElement= (Element) lib; 669 String name= libElement.getAttribute(TAG_NAME); 670 boolean isSystem= Boolean.valueOf(libElement.getAttribute(TAG_SYSTEMLIBRARY)).booleanValue(); 671 672 CPUserLibraryElement newLibrary= new CPUserLibraryElement(name, isSystem, null); 673 result.add(newLibrary); 674 675 NodeList archiveList= libElement.getElementsByTagName(TAG_ARCHIVE); 676 for (int k= 0; k < archiveList.getLength(); k++) { 677 Node archiveNode= archiveList.item(k); 678 if (!(archiveNode instanceof Element)) { 679 continue; 680 } 681 Element archiveElement= (Element) archiveNode; 682 683 String pathString= archiveElement.getAttribute(TAG_ARCHIVE_PATH); 684 IPath path= version.equals(VERSION1) ? Path.fromOSString(pathString) : Path.fromPortableString(pathString); 685 CPListElement newArchive= new CPListElement(newLibrary, null, IClasspathEntry.CPE_LIBRARY, path, null); 686 newLibrary.add(newArchive); 687 688 if (archiveElement.hasAttribute(TAG_SOURCEATTACHMENT)) { 689 String sourceAttachString= archiveElement.getAttribute(TAG_SOURCEATTACHMENT); 690 IPath sourceAttach= version.equals(VERSION1) ? Path.fromOSString(sourceAttachString) : Path.fromPortableString(sourceAttachString); 691 newArchive.setAttribute(CPListElement.SOURCEATTACHMENT, sourceAttach); 692 } 693 if (archiveElement.hasAttribute(TAG_JAVADOC)) { 694 String javadoc= archiveElement.getAttribute(TAG_JAVADOC); 695 newArchive.setAttribute(CPListElement.JAVADOC, javadoc); 696 } 697 if (archiveElement.hasAttribute(TAG_NATIVELIB_PATHS)) { 698 String nativeLibPath= archiveElement.getAttribute(TAG_NATIVELIB_PATHS); 699 newArchive.setAttribute(CPListElement.NATIVE_LIB_PATH, nativeLibPath); 700 } 701 NodeList rulesParentNodes= archiveElement.getElementsByTagName(TAG_ACCESSRULES); 702 if (rulesParentNodes.getLength() > 0 && rulesParentNodes.item(0) instanceof Element) { 703 Element ruleParentElement= (Element) rulesParentNodes.item(0); NodeList ruleElements= ruleParentElement.getElementsByTagName(TAG_ACCESSRULE); 705 int nRuleElements= ruleElements.getLength(); 706 if (nRuleElements > 0) { 707 ArrayList resultingRules= new ArrayList (nRuleElements); 708 for (int n= 0; n < nRuleElements; n++) { 709 Node node= ruleElements.item(n); 710 if (node instanceof Element) { 711 Element ruleElement= (Element) node; 712 try { 713 int kind= Integer.parseInt(ruleElement.getAttribute(TAG_RULE_KIND)); 714 IPath pattern= Path.fromPortableString(ruleElement.getAttribute(TAG_RULE_PATTERN)); 715 resultingRules.add(JavaCore.newAccessRule(pattern, kind)); 716 } catch (NumberFormatException e) { 717 } 719 } 720 } 721 newArchive.setAttribute(CPListElement.ACCESSRULES, resultingRules.toArray(new IAccessRule[resultingRules.size()])); 722 } 723 } 724 } 725 } 726 return result; 727 } 728 729 public List getLoadedLibraries() { 730 return fExportImportList.getCheckedElements(); 731 } 732 } 733 734 private IDialogSettings fDialogSettings; 735 private TreeListDialogField fLibraryList; 736 private IJavaProject fDummyProject; 737 private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors; 738 739 private static final int IDX_NEW= 0; 740 private static final int IDX_EDIT= 1; 741 private static final int IDX_ADD= 2; 742 private static final int IDX_REMOVE= 3; 743 private static final int IDX_UP= 5; 744 private static final int IDX_DOWN= 6; 745 private static final int IDX_LOAD= 8; 746 private static final int IDX_SAVE= 9; 747 748 751 public UserLibraryPreferencePage() { 752 setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore()); 753 fDummyProject= createPlaceholderProject(); 754 755 fAttributeDescriptors= JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors(); 756 757 setTitle(PreferencesMessages.UserLibraryPreferencePage_title); 759 setDescription(PreferencesMessages.UserLibraryPreferencePage_description); 760 noDefaultAndApplyButton(); 761 762 fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); 763 764 UserLibraryAdapter adapter= new UserLibraryAdapter(); 765 String [] buttonLabels= new String [] { 766 PreferencesMessages.UserLibraryPreferencePage_libraries_new_button, 767 PreferencesMessages.UserLibraryPreferencePage_libraries_edit_button, 768 PreferencesMessages.UserLibraryPreferencePage_libraries_addjar_button, 769 PreferencesMessages.UserLibraryPreferencePage_libraries_remove_button, 770 null, 771 PreferencesMessages.UserLibraryPreferencePage_UserLibraryPreferencePage_libraries_up_button, 772 PreferencesMessages.UserLibraryPreferencePage_UserLibraryPreferencePage_libraries_down_button, 773 null, 774 775 PreferencesMessages.UserLibraryPreferencePage_libraries_load_button, 776 PreferencesMessages.UserLibraryPreferencePage_libraries_save_button 777 }; 778 779 fLibraryList= new TreeListDialogField(adapter, buttonLabels, new CPListLabelProvider()); 780 fLibraryList.setLabelText(PreferencesMessages.UserLibraryPreferencePage_libraries_label); 781 782 String [] names= JavaCore.getUserLibraryNames(); 783 ArrayList elements= new ArrayList (); 784 785 for (int i= 0; i < names.length; i++) { 786 IPath path= new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(names[i]); 787 try { 788 IClasspathContainer container= JavaCore.getClasspathContainer(path, fDummyProject); 789 elements.add(new CPUserLibraryElement(names[i], container, fDummyProject)); 790 } catch (JavaModelException e) { 791 JavaPlugin.log(e); 792 } 794 } 795 fLibraryList.setElements(elements); 796 fLibraryList.setViewerComparator(new CPListElementSorter()); 797 798 doSelectionChanged(fLibraryList); } 800 801 private static IJavaProject createPlaceholderProject() { 802 String name= "####internal"; IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 804 while (true) { 805 IProject project= root.getProject(name); 806 if (!project.exists()) { 807 return JavaCore.create(project); 808 } 809 name += '1'; 810 } 811 } 812 813 814 817 public void applyData(Object data) { 818 if (data instanceof Map ) { 819 Map map= (Map ) data; 820 Object selectedLibrary= map.get(DATA_LIBRARY_TO_SELECT); 821 boolean createIfNotExists= Boolean.TRUE.equals(map.get(DATA_DO_CREATE)); 822 if (selectedLibrary instanceof String ) { 823 int nElements= fLibraryList.getSize(); 824 for (int i= 0; i < nElements; i++) { 825 CPUserLibraryElement curr= (CPUserLibraryElement) fLibraryList.getElement(i); 826 if (curr.getName().equals(selectedLibrary)) { 827 fLibraryList.selectElements(new StructuredSelection(curr)); 828 fLibraryList.expandElement(curr, 1); 829 break; 830 } 831 } 832 if (createIfNotExists) { 833 CPUserLibraryElement elem= new CPUserLibraryElement((String ) selectedLibrary, null, createPlaceholderProject()); 834 fLibraryList.addElement(elem); 835 fLibraryList.selectElements(new StructuredSelection(elem)); 836 } 837 } 838 } 839 } 840 841 842 846 public void createControl(Composite parent) { 847 super.createControl(parent); 848 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.CP_USERLIBRARIES_PREFERENCE_PAGE); 849 } 850 851 854 protected Control createContents(Composite parent) { 855 Composite composite= new Composite(parent, SWT.NONE); 856 composite.setFont(parent.getFont()); 857 LayoutUtil.doDefaultLayout(composite, new DialogField[] { fLibraryList }, true); 858 LayoutUtil.setHorizontalGrabbing(fLibraryList.getTreeControl(null)); 859 Dialog.applyDialogFont(composite); 860 return composite; 861 } 862 863 866 public void init(IWorkbench workbench) { 867 } 868 869 872 protected void performDefaults() { 873 super.performDefaults(); 874 } 875 876 879 public boolean performOk() { 880 try { 881 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { 882 public void run(IProgressMonitor monitor) throws InvocationTargetException { 883 try { 884 if (monitor != null) { 885 monitor= new NullProgressMonitor(); 886 } 887 888 updateUserLibararies(monitor); 889 } catch (CoreException e) { 890 throw new InvocationTargetException (e); 891 } finally { 892 monitor.done(); 893 } 894 } 895 }); 896 } catch (InterruptedException e) { 897 } catch (InvocationTargetException e) { 899 String title= PreferencesMessages.UserLibraryPreferencePage_config_error_title; 900 String message= PreferencesMessages.UserLibraryPreferencePage_config_error_message; 901 ExceptionHandler.handle(e, getShell(), title, message); 902 } 903 return true; 904 } 905 906 907 private void updateUserLibararies(IProgressMonitor monitor) throws CoreException { 908 List list= fLibraryList.getElements(); 909 HashSet oldNames= new HashSet (Arrays.asList(JavaCore.getUserLibraryNames())); 910 int nExisting= list.size(); 911 912 HashSet newEntries= new HashSet (list.size()); 913 for (int i= 0; i < nExisting; i++) { 914 CPUserLibraryElement element= (CPUserLibraryElement) list.get(i); 915 boolean contained= oldNames.remove(element.getName()); 916 if (!contained) { 917 newEntries.add(element); 918 } 919 } 920 921 int len= nExisting + oldNames.size(); 922 monitor.beginTask(PreferencesMessages.UserLibraryPreferencePage_operation, len); 923 MultiStatus multiStatus= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, PreferencesMessages.UserLibraryPreferencePage_operation_error, null); 924 925 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(JavaCore.USER_LIBRARY_CONTAINER_ID); 926 IJavaProject jproject= fDummyProject; 927 928 for (int i= 0; i < nExisting; i++) { 929 CPUserLibraryElement element= (CPUserLibraryElement) list.get(i); 930 IPath path= element.getPath(); 931 if (newEntries.contains(element) || element.hasChanges(JavaCore.getClasspathContainer(path, jproject))) { 932 IClasspathContainer updatedContainer= element.getUpdatedContainer(); 933 try { 934 initializer.requestClasspathContainerUpdate(path, jproject, updatedContainer); 935 } catch (CoreException e) { 936 multiStatus.add(e.getStatus()); 937 } 938 } 939 monitor.worked(1); 940 } 941 942 Iterator iter= oldNames.iterator(); 943 while (iter.hasNext()) { 944 String name= (String ) iter.next(); 945 946 IPath path= new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(name); 947 try { 948 initializer.requestClasspathContainerUpdate(path, jproject, null); 949 } catch (CoreException e) { 950 multiStatus.add(e.getStatus()); 951 } 952 monitor.worked(1); 953 } 954 955 if (!multiStatus.isOK()) { 956 throw new CoreException(multiStatus); 957 } 958 } 959 960 private CPUserLibraryElement getSingleSelectedLibrary(List selected) { 961 if (selected.size() == 1 && selected.get(0) instanceof CPUserLibraryElement) { 962 return (CPUserLibraryElement) selected.get(0); 963 } 964 return null; 965 } 966 967 private void editAttributeEntry(CPListElementAttribute elem) { 968 String key= elem.getKey(); 969 CPListElement selElement= elem.getParent(); 970 if (key.equals(CPListElement.SOURCEATTACHMENT)) { 971 IClasspathEntry result= BuildPathDialogAccess.configureSourceAttachment(getShell(), selElement.getClasspathEntry()); 972 if (result != null) { 973 selElement.setAttribute(CPListElement.SOURCEATTACHMENT, result.getSourceAttachmentPath()); 974 fLibraryList.refresh(elem); 975 fLibraryList.update(selElement); 976 } 977 } else if (key.equals(CPListElement.ACCESSRULES)) { 978 AccessRulesDialog dialog= new AccessRulesDialog(getShell(), selElement, null, false); 979 if (dialog.open() == Window.OK) { 980 selElement.setAttribute(CPListElement.ACCESSRULES, dialog.getAccessRules()); 981 fLibraryList.refresh(elem); 982 fLibraryList.expandElement(elem, 2); 983 } 984 } else if (!elem.isBuiltIn()) { 985 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key); 986 if (config != null) { 987 IClasspathAttribute result= config.performEdit(getShell(), elem.getClasspathAttributeAccess()); 988 if (result != null) { 989 elem.setValue(result.getValue()); 990 fLibraryList.refresh(elem); 991 } 992 } 993 } 994 } 995 996 protected void doSelectionChanged(TreeListDialogField field) { 997 List list= field.getSelectedElements(); 998 field.enableButton(IDX_REMOVE, canRemove(list)); 999 field.enableButton(IDX_EDIT, canEdit(list)); 1000 field.enableButton(IDX_ADD, canAdd(list)); 1001 field.enableButton(IDX_UP, canMoveUp(list)); 1002 field.enableButton(IDX_DOWN, canMoveDown(list)); 1003 field.enableButton(IDX_SAVE, field.getSize() > 0); 1004 } 1005 1006 protected void doCustomButtonPressed(TreeListDialogField field, int index) { 1007 if (index == IDX_NEW) { 1008 editUserLibraryElement(null); 1009 } else if (index == IDX_ADD) { 1010 doAdd(field.getSelectedElements()); 1011 } else if (index == IDX_REMOVE) { 1012 doRemove(field.getSelectedElements()); 1013 } else if (index == IDX_EDIT) { 1014 doEdit(field.getSelectedElements()); 1015 } else if (index == IDX_SAVE) { 1016 doSave(); 1017 } else if (index == IDX_LOAD) { 1018 doLoad(); 1019 } else if (index == IDX_UP) { 1020 doMoveUp(field.getSelectedElements()); 1021 } else if (index == IDX_DOWN) { 1022 doMoveDown(field.getSelectedElements()); 1023 } 1024 } 1025 1026 protected void doDoubleClicked(TreeListDialogField field) { 1027 List selected= field.getSelectedElements(); 1028 if (canEdit(selected)) { 1029 doEdit(field.getSelectedElements()); 1030 } 1031 } 1032 1033 protected void doKeyPressed(TreeListDialogField field, KeyEvent event) { 1034 if (event.character == SWT.DEL && event.stateMask == 0) { 1035 List selection= field.getSelectedElements(); 1036 if (canRemove(selection)) { 1037 doRemove(selection); 1038 } 1039 } 1040 } 1041 1042 private void doEdit(List selected) { 1043 if (selected.size() == 1) { 1044 Object curr= selected.get(0); 1045 if (curr instanceof CPListElementAttribute) { 1046 editAttributeEntry((CPListElementAttribute) curr); 1047 } else if (curr instanceof CPUserLibraryElement) { 1048 editUserLibraryElement((CPUserLibraryElement) curr); 1049 } else if (curr instanceof CPListElement) { 1050 CPListElement elem= (CPListElement) curr; 1051 editArchiveElement(elem, (CPUserLibraryElement) elem.getParentContainer()); 1052 } 1053 doSelectionChanged(fLibraryList); 1054 } 1055 } 1056 1057 private void editUserLibraryElement(CPUserLibraryElement element) { 1058 LibraryNameDialog dialog= new LibraryNameDialog(getShell(), element, fLibraryList.getElements()); 1059 if (dialog.open() == Window.OK) { 1060 CPUserLibraryElement newLibrary= dialog.getNewLibrary(); 1061 if (element != null) { 1062 fLibraryList.replaceElement(element, newLibrary); 1063 } else { 1064 fLibraryList.addElement(newLibrary); 1065 } 1066 fLibraryList.expandElement(newLibrary, AbstractTreeViewer.ALL_LEVELS); 1067 fLibraryList.selectElements(new StructuredSelection(newLibrary)); 1068 } 1069 } 1070 1071 private void editArchiveElement(CPListElement existingElement, CPUserLibraryElement parent) { 1072 CPListElement[] elements= openExtJarFileDialog(existingElement, parent); 1073 if (elements != null) { 1074 for (int i= 0; i < elements.length; i++) { 1075 if (existingElement != null) { 1076 parent.replace(existingElement, elements[i]); 1077 } else { 1078 parent.add(elements[i]); 1079 } 1080 } 1081 fLibraryList.refresh(parent); 1082 fLibraryList.selectElements(new StructuredSelection(Arrays.asList(elements))); 1083 fLibraryList.expandElement(parent, 2); 1084 } 1085 } 1086 1087 1088 private void doRemove(List selected) { 1089 Object selectionAfter= null; 1090 for (int i= 0; i < selected.size(); i++) { 1091 Object curr= selected.get(i); 1092 if (curr instanceof CPUserLibraryElement) { 1093 fLibraryList.removeElement(curr); 1094 } else if (curr instanceof CPListElement) { 1095 Object parent= ((CPListElement) curr).getParentContainer(); 1096 if (parent instanceof CPUserLibraryElement) { 1097 CPUserLibraryElement elem= (CPUserLibraryElement) parent; 1098 elem.remove((CPListElement) curr); 1099 fLibraryList.refresh(elem); 1100 selectionAfter= parent; 1101 } 1102 } else if (curr instanceof CPListElementAttribute) { 1103 CPListElementAttribute attrib= (CPListElementAttribute) curr; 1104 if (attrib.isBuiltIn()) { 1105 Object value= null; 1106 String key= attrib.getKey(); 1107 if (key.equals(CPListElement.ACCESSRULES)) { 1108 value= new IAccessRule[0]; 1109 } 1110 attrib.getParent().setAttribute(key, value); 1111 fLibraryList.refresh(attrib); 1112 } else { 1113 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(attrib.getKey()); 1114 if (config != null) { 1115 IClasspathAttribute result= config.performRemove(attrib.getClasspathAttributeAccess()); 1116 if (result != null) { 1117 attrib.setValue(result.getValue()); 1118 fLibraryList.refresh(attrib); 1119 } 1120 } 1121 } 1122 } 1123 } 1124 if (fLibraryList.getSelectedElements().isEmpty()) { 1125 if (selectionAfter != null) { 1126 fLibraryList.selectElements(new StructuredSelection(selectionAfter)); 1127 } else { 1128 fLibraryList.selectFirstElement(); 1129 } 1130 } else { 1131 doSelectionChanged(fLibraryList); 1132 } 1133 } 1134 1135 private void doAdd(List list) { 1136 if (canAdd(list)) { 1137 CPUserLibraryElement element= getSingleSelectedLibrary(list); 1138 editArchiveElement(null, element); 1139 } 1140 } 1141 1142 private void doLoad() { 1143 List existing= fLibraryList.getElements(); 1144 LoadSaveDialog dialog= new LoadSaveDialog(getShell(), false, existing, fDialogSettings); 1145 if (dialog.open() == Window.OK) { 1146 HashMap map= new HashMap (existing.size()); 1147 for (int k= 0; k < existing.size(); k++) { 1148 CPUserLibraryElement elem= (CPUserLibraryElement) existing.get(k); 1149 map.put(elem.getName(), elem); 1150 } 1151 1152 List list= dialog.getLoadedLibraries(); 1153 for (int i= 0; i < list.size(); i++) { 1154 CPUserLibraryElement elem= (CPUserLibraryElement) list.get(i); 1155 CPUserLibraryElement found= (CPUserLibraryElement) map.get(elem.getName()); 1156 if (found == null) { 1157 existing.add(elem); 1158 map.put(elem.getName(), elem); 1159 } else { 1160 existing.set(existing.indexOf(found), elem); } 1162 } 1163 fLibraryList.setElements(existing); 1164 fLibraryList.selectElements(new StructuredSelection(list)); 1165 } 1166 } 1167 1168 private void doSave() { 1169 LoadSaveDialog dialog= new LoadSaveDialog(getShell(), true, fLibraryList.getElements(), fDialogSettings); 1170 dialog.open(); 1171 } 1172 1173 private boolean canAdd(List list) { 1174 return getSingleSelectedLibrary(list) != null; 1175 } 1176 1177 private boolean canEdit(List list) { 1178 if (list.size() != 1) 1179 return false; 1180 1181 Object firstElement= list.get(0); 1182 if (firstElement instanceof IAccessRule) 1183 return false; 1184 if (firstElement instanceof CPListElementAttribute) { 1185 CPListElementAttribute attrib= (CPListElementAttribute) firstElement; 1186 if (!attrib.isBuiltIn()) { 1187 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(attrib.getKey()); 1188 return config != null && config.canEdit(attrib.getClasspathAttributeAccess()); 1189 } 1190 } 1191 return true; 1192 } 1193 1194 private boolean canRemove(List list) { 1195 if (list.size() == 0) { 1196 return false; 1197 } 1198 for (int i= 0; i < list.size(); i++) { 1199 Object elem= list.get(i); 1200 if (elem instanceof CPListElementAttribute) { 1201 CPListElementAttribute attrib= (CPListElementAttribute) elem; 1202 if (attrib.isNonModifiable()) { 1203 return false; 1204 } 1205 if (attrib.isBuiltIn()) { 1206 if (attrib.getKey().equals(CPListElement.ACCESSRULES)) { 1207 return ((IAccessRule[]) attrib.getValue()).length > 0; 1208 } 1209 if (attrib.getValue() == null) { 1210 return false; 1211 } 1212 } else { 1213 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(attrib.getKey()); 1214 if (config == null || !config.canRemove(attrib.getClasspathAttributeAccess()) ) { 1215 return false; 1216 } 1217 } 1218 } else if (elem instanceof CPListElement) { 1219 } else if (elem instanceof CPUserLibraryElement) { 1221 } else { return false; 1224 } 1225 } 1226 return true; 1227 } 1228 1229 private CPUserLibraryElement getCommonParent(List list) { 1230 CPUserLibraryElement parent= null; 1231 for (int i= 0, len= list.size(); i < len; i++) { 1232 Object curr= list.get(i); 1233 if (curr instanceof CPListElement) { 1234 Object elemParent= ((CPListElement) curr).getParentContainer(); 1235 if (parent == null) { 1236 if (elemParent instanceof CPUserLibraryElement) { 1237 parent= (CPUserLibraryElement) elemParent; 1238 } else { 1239 return null; 1240 } 1241 } else if (parent != elemParent) { 1242 return null; 1243 } 1244 } else { 1245 return null; 1246 } 1247 } 1248 return parent; 1249 } 1250 1251 private void doMoveUp(List list) { 1252 CPUserLibraryElement parent= getCommonParent(list); 1253 if (parent != null) { 1254 parent.moveUp(list); 1255 fLibraryList.refresh(parent); 1256 doSelectionChanged(fLibraryList); 1257 } 1258 } 1259 1260 private void doMoveDown(List list) { 1261 CPUserLibraryElement parent= getCommonParent(list); 1262 if (parent != null) { 1263 parent.moveDown(list); 1264 fLibraryList.refresh(parent); 1265 doSelectionChanged(fLibraryList); 1266 } 1267 } 1268 1269 1270 private boolean canMoveUp(List list) { 1271 CPUserLibraryElement parent= getCommonParent(list); 1272 if (parent != null) { 1273 CPListElement[] children= parent.getChildren(); 1274 for (int i= 0, len= Math.min(list.size(), children.length); i < len ; i++) { 1275 if (!list.contains(children[i])) { 1276 return true; 1277 } 1278 } 1279 } 1280 return false; 1281 } 1282 1283 private boolean canMoveDown(List list) { 1284 CPUserLibraryElement parent= getCommonParent(list); 1285 if (parent != null) { 1286 CPListElement[] children= parent.getChildren(); 1287 for (int i= children.length - 1, end= Math.max(0, children.length - list.size()); i >= end; i--) { 1288 if (!list.contains(children[i])) { 1289 return true; 1290 } 1291 } 1292 } 1293 return false; 1294 } 1295 1296 1297 private CPListElement[] openExtJarFileDialog(CPListElement existing, Object parent) { 1298 String lastUsedPath; 1299 if (existing != null) { 1300 lastUsedPath= existing.getPath().removeLastSegments(1).toOSString(); 1301 } else { 1302 lastUsedPath= fDialogSettings.get(IUIConstants.DIALOGSTORE_LASTEXTJAR); 1303 if (lastUsedPath == null) { 1304 lastUsedPath= ""; } 1306 } 1307 String title= (existing == null) ? PreferencesMessages.UserLibraryPreferencePage_browsejar_new_title : PreferencesMessages.UserLibraryPreferencePage_browsejar_edit_title; 1308 1309 FileDialog dialog= new FileDialog(getShell(), existing == null ? SWT.MULTI : SWT.SINGLE); 1310 dialog.setText(title); 1311 dialog.setFilterExtensions(new String [] {"*.jar;*.zip"}); dialog.setFilterPath(lastUsedPath); 1313 if (existing != null) { 1314 dialog.setFileName(existing.getPath().lastSegment()); 1315 } 1316 1317 String res= dialog.open(); 1318 if (res == null) { 1319 return null; 1320 } 1321 String [] fileNames= dialog.getFileNames(); 1322 int nChosen= fileNames.length; 1323 1324 IPath filterPath= Path.fromOSString(dialog.getFilterPath()); 1325 CPListElement[] elems= new CPListElement[nChosen]; 1326 for (int i= 0; i < nChosen; i++) { 1327 IPath path= filterPath.append(fileNames[i]).makeAbsolute(); 1328 CPListElement curr= new CPListElement(parent, null, IClasspathEntry.CPE_LIBRARY, path, null); 1329 curr.setAttribute(CPListElement.SOURCEATTACHMENT, BuildPathSupport.guessSourceAttachment(curr)); 1330 curr.setAttribute(CPListElement.JAVADOC, BuildPathSupport.guessJavadocLocation(curr)); 1331 elems[i]= curr; 1332 } 1333 fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath()); 1334 1335 return elems; 1336 } 1337 1338 1339 private class UserLibraryAdapter implements ITreeListAdapter { 1340 1341 private final Object [] EMPTY= new Object [0]; 1342 1343 public void customButtonPressed(TreeListDialogField field, int index) { 1344 doCustomButtonPressed(field, index); 1345 } 1346 1347 public void selectionChanged(TreeListDialogField field) { 1348 doSelectionChanged(field); 1349 } 1350 1351 public void doubleClicked(TreeListDialogField field) { 1352 doDoubleClicked(field); 1353 } 1354 1355 public void keyPressed(TreeListDialogField field, KeyEvent event) { 1356 doKeyPressed(field, event); 1357 } 1358 1359 public Object [] getChildren(TreeListDialogField field, Object element) { 1360 if (element instanceof CPUserLibraryElement) { 1361 CPUserLibraryElement elem= (CPUserLibraryElement) element; 1362 return elem.getChildren(); 1363 } else if (element instanceof CPListElement) { 1364 return ((CPListElement)element).getChildren(false); 1365 } else if (element instanceof CPListElementAttribute) { 1366 CPListElementAttribute attribute= (CPListElementAttribute) element; 1367 if (CPListElement.ACCESSRULES.equals(attribute.getKey())) { 1368 return (IAccessRule[]) attribute.getValue(); 1369 } 1370 } 1371 return EMPTY; 1372 } 1373 1374 public Object getParent(TreeListDialogField field, Object element) { 1375 if (element instanceof CPListElementAttribute) { 1376 return ((CPListElementAttribute) element).getParent(); 1377 } else if (element instanceof CPListElement) { 1378 return ((CPListElement) element).getParentContainer(); 1379 } 1380 return null; 1381 } 1382 1383 public boolean hasChildren(TreeListDialogField field, Object element) { 1384 return getChildren(field, element).length > 0; 1385 } 1386 1387 } 1388 1389 1390} 1391 | Popular Tags |