1 11 package org.eclipse.jdt.internal.debug.ui.jres; 12 13 14 import java.io.File ; 15 import java.net.URL ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.jdt.core.IClasspathEntry; 23 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 24 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 25 import org.eclipse.jdt.internal.debug.ui.SWTFactory; 26 import org.eclipse.jdt.internal.debug.ui.jres.LibraryContentProvider.SubElement; 27 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 28 import org.eclipse.jdt.launching.IVMInstall; 29 import org.eclipse.jdt.launching.IVMInstallType; 30 import org.eclipse.jdt.launching.JavaRuntime; 31 import org.eclipse.jdt.launching.LibraryLocation; 32 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess; 33 import org.eclipse.jface.dialogs.IDialogSettings; 34 import org.eclipse.jface.viewers.DoubleClickEvent; 35 import org.eclipse.jface.viewers.IDoubleClickListener; 36 import org.eclipse.jface.viewers.ISelectionChangedListener; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.TreeViewer; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.events.SelectionListener; 43 import org.eclipse.swt.graphics.Font; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.FileDialog; 49 50 53 public class VMLibraryBlock implements SelectionListener, ISelectionChangedListener { 54 55 59 protected static final String LAST_PATH_SETTING = "LAST_PATH_SETTING"; 61 64 protected static final String DIALOG_SETTINGS_PREFIX = "VMLibraryBlock"; 66 protected boolean fInCallback = false; 67 protected IVMInstall fVmInstall; 68 protected IVMInstallType fVmInstallType; 69 protected File fHome; 70 71 protected LibraryContentProvider fLibraryContentProvider; 73 protected AddVMDialog fDialog = null; 74 protected TreeViewer fLibraryViewer; 75 private Button fUpButton; 76 private Button fDownButton; 77 private Button fRemoveButton; 78 private Button fAddButton; 79 private Button fJavadocButton; 80 private Button fSourceButton; 81 protected Button fDefaultButton; 82 83 86 public VMLibraryBlock(AddVMDialog dialog) { 87 fDialog = dialog; 88 } 89 90 95 public Control createControl(Composite parent) { 96 Font font = parent.getFont(); 97 98 Composite comp = SWTFactory.createComposite(parent, font, 2, 1, GridData.FILL_BOTH, 0, 0); 99 100 fLibraryViewer= new TreeViewer(comp); 101 GridData gd = new GridData(GridData.FILL_BOTH); 102 gd.heightHint = 6; 103 fLibraryViewer.getControl().setLayoutData(gd); 104 fLibraryContentProvider= new LibraryContentProvider(); 105 fLibraryViewer.setContentProvider(fLibraryContentProvider); 106 fLibraryViewer.setLabelProvider(new LibraryLabelProvider()); 107 fLibraryViewer.setInput(this); 108 fLibraryViewer.addSelectionChangedListener(this); 109 110 Composite pathButtonComp = SWTFactory.createComposite(comp, font, 1, 1, GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL, 0, 0); 111 fAddButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_7, JREMessages.VMLibraryBlock_16, null); 112 fAddButton.addSelectionListener(this); 113 fJavadocButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_3, JREMessages.VMLibraryBlock_17, null); 114 fJavadocButton.addSelectionListener(this); 115 fSourceButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_11, JREMessages.VMLibraryBlock_18, null); 116 fSourceButton.addSelectionListener(this); 117 fLibraryViewer.addDoubleClickListener(new IDoubleClickListener() { 118 public void doubleClick(DoubleClickEvent event) { 119 IStructuredSelection sel = (IStructuredSelection)event.getViewer().getSelection(); 120 Object obj = sel.getFirstElement(); 121 if(obj instanceof SubElement) { 122 edit(sel, ((SubElement)obj).getType()); 123 } 124 } 125 }); 126 fRemoveButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_6, JREMessages.VMLibraryBlock_12, null); 127 fRemoveButton.addSelectionListener(this); 128 fUpButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_4, JREMessages.VMLibraryBlock_13, null); 129 fUpButton.addSelectionListener(this); 130 fDownButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_5, JREMessages.VMLibraryBlock_14, null); 131 fDownButton.addSelectionListener(this); 132 fDefaultButton = SWTFactory.createPushButton(pathButtonComp, JREMessages.VMLibraryBlock_9, JREMessages.VMLibraryBlock_15, null); 133 fDefaultButton.addSelectionListener(this); 134 135 return comp; 136 } 137 138 141 public void restoreDefaultLibraries() { 142 LibraryLocation[] libs = null; 143 File installLocation = getHomeDirectory(); 144 if (installLocation == null) { 145 libs = new LibraryLocation[0]; 146 } else { 147 libs = getVMInstallType().getDefaultLibraryLocations(installLocation); 148 } 149 fLibraryContentProvider.setLibraries(libs); 150 update(); 151 } 152 153 160 public void initializeFrom(IVMInstall vm, IVMInstallType type) { 161 fVmInstall = vm; 162 fVmInstallType = type; 163 if (vm != null) { 164 setHomeDirectory(vm.getInstallLocation()); 165 fLibraryContentProvider.setLibraries(JavaRuntime.getLibraryLocations(getVMInstall())); 166 } 167 update(); 168 } 169 170 173 public void setHomeDirectory(File file) { 174 fHome = file; 175 } 176 177 180 protected File getHomeDirectory() { 181 return fHome; 182 } 183 184 187 public void update() { 188 updateButtons(); 189 IStatus status = Status.OK_STATUS; 190 if (fLibraryContentProvider.getLibraries().length == 0) { status = new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, 192 JREMessages.VMLibraryBlock_Libraries_cannot_be_empty__1, null); 193 } 194 LibraryStandin[] standins = fLibraryContentProvider.getStandins(); 195 for (int i = 0; i < standins.length; i++) { 196 IStatus st = standins[i].validate(); 197 if (!st.isOK()) { 198 status = st; 199 break; 200 } 201 } 202 fDialog.setSystemLibraryStatus(status); 203 fDialog.updateStatusLine(); 204 } 205 206 209 public void performApply(IVMInstall vm) { 210 if (isDefaultLocations()) { 211 vm.setLibraryLocations(null); 212 } else { 213 LibraryLocation[] libs = fLibraryContentProvider.getLibraries(); 214 vm.setLibraryLocations(libs); 215 } 216 } 217 218 222 protected boolean isDefaultLocations() { 223 LibraryLocation[] libraryLocations = fLibraryContentProvider.getLibraries(); 224 IVMInstall install = getVMInstall(); 225 226 if (install == null || libraryLocations == null) { 227 return true; 228 } 229 File installLocation = install.getInstallLocation(); 230 if (installLocation != null) { 231 LibraryLocation[] def = getVMInstallType().getDefaultLibraryLocations(installLocation); 232 if (def.length == libraryLocations.length) { 233 for (int i = 0; i < def.length; i++) { 234 if (!def[i].equals(libraryLocations[i])) { 235 return false; 236 } 237 } 238 return true; 239 } 240 } 241 return false; 242 } 243 244 249 protected IVMInstall getVMInstall() { 250 return fVmInstall; 251 } 252 253 258 protected IVMInstallType getVMInstallType() { 259 return fVmInstallType; 260 } 261 262 265 public void widgetSelected(SelectionEvent e) { 266 Object source= e.getSource(); 267 if (source == fUpButton) { 268 fLibraryContentProvider.up((IStructuredSelection) fLibraryViewer.getSelection()); 269 } else if (source == fDownButton) { 270 fLibraryContentProvider.down((IStructuredSelection) fLibraryViewer.getSelection()); 271 } else if (source == fRemoveButton) { 272 fLibraryContentProvider.remove((IStructuredSelection) fLibraryViewer.getSelection()); 273 } else if (source == fAddButton) { 274 add((IStructuredSelection) fLibraryViewer.getSelection()); 275 } 276 else if(source == fJavadocButton) { 277 edit((IStructuredSelection) fLibraryViewer.getSelection(), SubElement.JAVADOC_URL); 278 } 279 else if(source == fSourceButton) { 280 edit((IStructuredSelection) fLibraryViewer.getSelection(), SubElement.SOURCE_PATH); 281 } 282 else if (source == fDefaultButton) { 283 restoreDefaultLibraries(); 284 } 285 update(); 286 } 287 288 291 public void widgetDefaultSelected(SelectionEvent e) {} 292 293 296 private void add(IStructuredSelection selection) { 297 IDialogSettings dialogSettings= JDIDebugUIPlugin.getDefault().getDialogSettings(); 298 String lastUsedPath= dialogSettings.get(LAST_PATH_SETTING); 299 if (lastUsedPath == null) { 300 lastUsedPath= ""; } 302 FileDialog dialog= new FileDialog(fLibraryViewer.getControl().getShell(), SWT.MULTI); 303 dialog.setText(JREMessages.VMLibraryBlock_10); 304 dialog.setFilterExtensions(new String [] {"*.jar;*.zip"}); dialog.setFilterPath(lastUsedPath); 306 String res= dialog.open(); 307 if (res == null) { 308 return; 309 } 310 String [] fileNames= dialog.getFileNames(); 311 int nChosen= fileNames.length; 312 313 IPath filterPath= new Path(dialog.getFilterPath()); 314 LibraryLocation[] libs= new LibraryLocation[nChosen]; 315 for (int i= 0; i < nChosen; i++) { 316 libs[i]= new LibraryLocation(filterPath.append(fileNames[i]).makeAbsolute(), Path.EMPTY, Path.EMPTY); 317 } 318 dialogSettings.put(LAST_PATH_SETTING, filterPath.toOSString()); 319 320 fLibraryContentProvider.add(libs, selection); 321 } 322 323 327 private void edit(IStructuredSelection selection, int type) { 328 Object obj = selection.getFirstElement(); 329 LibraryStandin standin = null; 330 if(obj instanceof LibraryStandin) { 331 standin = (LibraryStandin) obj; 332 } 333 else if(obj instanceof SubElement){ 334 SubElement sub = (SubElement)obj; 335 standin = sub.getParent(); 336 } 337 if(standin != null) { 338 LibraryLocation library = standin.toLibraryLocation(); 339 if (type == SubElement.JAVADOC_URL) { 340 URL [] urls = BuildPathDialogAccess.configureJavadocLocation(fLibraryViewer.getControl().getShell(), library.getSystemLibraryPath().toOSString(), library.getJavadocLocation()); 341 if (urls != null) { 342 fLibraryContentProvider.setJavadoc(urls[0], selection); 343 } 344 } 345 else if(type == SubElement.SOURCE_PATH){ 346 IRuntimeClasspathEntry entry = JavaRuntime.newArchiveRuntimeClasspathEntry(library.getSystemLibraryPath()); 347 entry.setSourceAttachmentPath(library.getSystemLibrarySourcePath()); 348 entry.setSourceAttachmentRootPath(library.getPackageRootPath()); 349 IClasspathEntry classpathEntry = BuildPathDialogAccess.configureSourceAttachment(fLibraryViewer.getControl().getShell(), entry.getClasspathEntry()); 350 if (classpathEntry != null) { 351 fLibraryContentProvider.setSourcePath(classpathEntry.getSourceAttachmentPath(), classpathEntry.getSourceAttachmentRootPath(), selection); 352 } 353 } 354 } 355 } 356 357 360 public void selectionChanged(SelectionChangedEvent event) { 361 updateButtons(); 362 } 363 364 367 private void updateButtons() { 368 IStructuredSelection selection = (IStructuredSelection) fLibraryViewer.getSelection(); 369 fRemoveButton.setEnabled(!selection.isEmpty()); 370 boolean enableUp = true, 371 enableDown = true, 372 allSource = true, 373 allJavadoc = true, 374 allRoots = true; 375 Object [] libraries = fLibraryContentProvider.getElements(null); 376 if (selection.isEmpty() || libraries.length == 0) { 377 enableUp = false; 378 enableDown = false; 379 } else { 380 Object first = libraries[0]; 381 Object last = libraries[libraries.length - 1]; 382 for (Iterator iter= selection.iterator(); iter.hasNext();) { 383 Object element= iter.next(); 384 Object lib; 385 if (element instanceof SubElement) { 386 allRoots = false; 387 SubElement subElement= (SubElement)element; 388 lib = (subElement).getParent().toLibraryLocation(); 389 if (subElement.getType() == SubElement.JAVADOC_URL) { 390 allSource = false; 391 } else { 392 allJavadoc = false; 393 } 394 } else { 395 lib = element; 396 allSource = false; 397 allJavadoc = false; 398 } 399 if (lib == first) { 400 enableUp = false; 401 } 402 if (lib == last) { 403 enableDown = false; 404 } 405 } 406 } 407 fUpButton.setEnabled(enableUp); 408 fDownButton.setEnabled(enableDown); 409 fJavadocButton.setEnabled(!selection.isEmpty() && (allJavadoc || allRoots)); 410 fSourceButton.setEnabled(!selection.isEmpty() && (allSource || allRoots)); 411 } 412 } 413 | Popular Tags |