KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > VMLibraryBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.jres;
12
13
14 import java.io.File JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Iterator JavaDoc;
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 /**
51  * Control used to edit the libraries associated with a VM install
52  */

53 public class VMLibraryBlock implements SelectionListener, ISelectionChangedListener {
54     
55     /**
56      * Attribute name for the last path used to open a file/directory chooser
57      * dialog.
58      */

59     protected static final String JavaDoc LAST_PATH_SETTING = "LAST_PATH_SETTING"; //$NON-NLS-1$
60

61     /**
62      * the prefix for dialog setting pertaining to this block
63      */

64     protected static final String JavaDoc DIALOG_SETTINGS_PREFIX = "VMLibraryBlock"; //$NON-NLS-1$
65

66     protected boolean fInCallback = false;
67     protected IVMInstall fVmInstall;
68     protected IVMInstallType fVmInstallType;
69     protected File JavaDoc fHome;
70     
71     //widgets
72
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     /**
84      * Constructor for VMLibraryBlock.
85      */

86     public VMLibraryBlock(AddVMDialog dialog) {
87         fDialog = dialog;
88     }
89
90     /**
91      * Creates and returns the source lookup control.
92      *
93      * @param parent the parent widget of this control
94      */

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 JavaDoc 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     /**
139      * The "default" button has been toggled
140      */

141     public void restoreDefaultLibraries() {
142         LibraryLocation[] libs = null;
143         File JavaDoc 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     /**
154      * Initializes this control based on the settings in the given
155      * vm install and type.
156      *
157      * @param vm vm or <code>null</code> if none
158      * @param type type of vm install
159      */

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     /**
171      * Sets the home directory of the VM Install the user has chosen
172      */

173     public void setHomeDirectory(File JavaDoc file) {
174         fHome = file;
175     }
176     
177     /**
178      * Returns the home directory
179      */

180     protected File JavaDoc getHomeDirectory() {
181         return fHome;
182     }
183     
184     /**
185      * Updates buttons and status based on current libraries
186      */

187     public void update() {
188         updateButtons();
189         IStatus status = Status.OK_STATUS;
190         if (fLibraryContentProvider.getLibraries().length == 0) { // && !isDefaultSystemLibrary()) {
191
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     /**
207      * Saves settings in the given working copy
208      */

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     /**
219      * Determines if the present setup is the default location s for this JRE
220      * @return true if the current set of locations are the defaults, false otherwise
221      */

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 JavaDoc 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     /**
245      * Returns the vm install associated with this library block.
246      *
247      * @return vm install
248      */

249     protected IVMInstall getVMInstall() {
250         return fVmInstall;
251     }
252     
253     /**
254      * Returns the vm install type associated with this library block.
255      *
256      * @return vm install
257      */

258     protected IVMInstallType getVMInstallType() {
259         return fVmInstallType;
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
264      */

265     public void widgetSelected(SelectionEvent e) {
266         Object JavaDoc 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     /* (non-Javadoc)
289      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
290      */

291     public void widgetDefaultSelected(SelectionEvent e) {}
292
293     /**
294      * Open the file selection dialog, and add the return jars as libraries.
295      */

296     private void add(IStructuredSelection selection) {
297         IDialogSettings dialogSettings= JDIDebugUIPlugin.getDefault().getDialogSettings();
298         String JavaDoc lastUsedPath= dialogSettings.get(LAST_PATH_SETTING);
299         if (lastUsedPath == null) {
300             lastUsedPath= ""; //$NON-NLS-1$
301
}
302         FileDialog dialog= new FileDialog(fLibraryViewer.getControl().getShell(), SWT.MULTI);
303         dialog.setText(JREMessages.VMLibraryBlock_10);
304         dialog.setFilterExtensions(new String JavaDoc[] {"*.jar;*.zip"}); //$NON-NLS-1$
305
dialog.setFilterPath(lastUsedPath);
306         String JavaDoc res= dialog.open();
307         if (res == null) {
308             return;
309         }
310         String JavaDoc[] 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     /**
324      * Open the javadoc location dialog or the source location dialog, and set the result
325      * to the selected libraries.
326      */

327     private void edit(IStructuredSelection selection, int type) {
328         Object JavaDoc 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 JavaDoc[] 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     /* (non-Javadoc)
358      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
359      */

360     public void selectionChanged(SelectionChangedEvent event) {
361         updateButtons();
362     }
363
364     /**
365      * Refresh the enable/disable state for the buttons.
366      */

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 JavaDoc[] libraries = fLibraryContentProvider.getElements(null);
376         if (selection.isEmpty() || libraries.length == 0) {
377             enableUp = false;
378             enableDown = false;
379         } else {
380             Object JavaDoc first = libraries[0];
381             Object JavaDoc last = libraries[libraries.length - 1];
382             for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
383                 Object JavaDoc element= iter.next();
384                 Object JavaDoc 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