KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > LibraryPluginJarsPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.pde.internal.ui.wizards.plugin;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.pde.internal.ui.IHelpContextIds;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEPluginImages;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
29 import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
30 import org.eclipse.pde.internal.ui.util.FileValidator;
31 import org.eclipse.pde.internal.ui.util.SWTUtil;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.KeyAdapter;
34 import org.eclipse.swt.events.KeyEvent;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.FileDialog;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
46 import org.eclipse.ui.model.WorkbenchContentProvider;
47 import org.eclipse.ui.model.WorkbenchLabelProvider;
48
49 public class LibraryPluginJarsPage extends WizardPage {
50
51     protected LibraryPluginFieldData fData;
52
53     /**
54      * List of IFile and File of workspace and external Jars.
55      */

56     protected ArrayList JavaDoc fJarPaths = new ArrayList JavaDoc();
57
58     protected Button fRemove;
59
60     protected TableViewer fTableViewer;
61
62     public LibraryPluginJarsPage(String JavaDoc pageName, LibraryPluginFieldData data) {
63         super(pageName);
64         fData = data;
65         setTitle(PDEUIMessages.LibraryPluginJarsPage_title);
66         setDescription(PDEUIMessages.LibraryPluginJarsPage_desc);
67     }
68
69     private void chooseFile() {
70         FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.MULTI);
71         dialog.setFilterExtensions(new String JavaDoc[] { "*.jar" }); //$NON-NLS-1$
72
String JavaDoc res = dialog.open();
73         if (res != null) {
74             String JavaDoc path = new File JavaDoc(res).getParent();
75             String JavaDoc[] fileNames = dialog.getFileNames();
76             for (int i = 0; i < fileNames.length; i++) {
77                 File JavaDoc newJarFile = new File JavaDoc(path, fileNames[i]);
78                 removeJar(fileNames[i]);
79                 fJarPaths.add(newJarFile);
80                 fTableViewer.add(newJarFile);
81             }
82             fRemove.setEnabled(fJarPaths.size() > 0);
83             setPageComplete(fJarPaths.size() > 0);
84         }
85     }
86
87     private void chooseWorkspaceFile() {
88         ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
89                 getShell(), new WorkbenchLabelProvider(),
90                 new WorkbenchContentProvider());
91
92         dialog.setValidator(new FileValidator());
93         dialog.setAllowMultiple(true);
94         dialog
95                 .setTitle(PDEUIMessages.LibraryPluginJarsPage_SelectionDialog_title);
96         dialog
97                 .setMessage(PDEUIMessages.LibraryPluginJarsPage_SelectionDialog_message);
98         dialog.addFilter(new FileExtensionFilter("jar")); //$NON-NLS-1$
99
dialog.setInput(PDEPlugin.getWorkspace().getRoot());
100
101         if (dialog.open() == Window.OK) {
102             Object JavaDoc[] files = dialog.getResult();
103             for (int i = 0; i < files.length; i++) {
104                 IFile newJarFile = (IFile) files[i];
105                 removeJar(newJarFile.getName());
106                 fJarPaths.add(newJarFile);
107                 fTableViewer.add(newJarFile);
108             }
109             fRemove.setEnabled(fJarPaths.size() > 0);
110             setPageComplete(fJarPaths.size() > 0);
111         }
112     }
113
114     public void createControl(Composite parent) {
115         Composite control = new Composite(parent, SWT.NONE);
116         GridLayout layout = new GridLayout();
117         layout.numColumns = 2;
118         // layout.verticalSpacing = 10;
119
control.setLayout(layout);
120
121         Label l = new Label(control, SWT.WRAP);
122         l.setText(PDEUIMessages.LibraryPluginJarsPage_label);
123         GridData data = new GridData(GridData.FILL_HORIZONTAL);
124         data.horizontalSpan = 2;
125         l.setLayoutData(data);
126         fTableViewer = new TableViewer(control, SWT.MULTI | SWT.BORDER);
127         fTableViewer.setContentProvider(new DefaultTableProvider() {
128             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
129                 return fJarPaths.toArray();
130             }
131         });
132         fTableViewer.setLabelProvider(new LabelProvider() {
133             public String JavaDoc getText(Object JavaDoc obj) {
134                 String JavaDoc name;
135                 String JavaDoc location;
136                 if (obj instanceof IFile) {
137                     IFile jarFile = (IFile) obj;
138                     name = jarFile.getName();
139                     location = jarFile.getParent().getFullPath().toString()
140                             .substring(1);
141                 } else {
142                     File JavaDoc jarFile = (File JavaDoc) obj;
143                     name = jarFile.getName();
144                     location = jarFile.getParent();
145                 }
146                 return name + " - " + location; //$NON-NLS-1$
147

148             }
149
150             public Image getImage(Object JavaDoc obj) {
151                 if (obj instanceof IFile) {
152                     return PDEPlugin.getDefault().getLabelProvider().get(
153                             PDEPluginImages.DESC_JAR_OBJ);
154                 }
155                 return PDEPlugin.getDefault().getLabelProvider().get(
156                         PDEPluginImages.DESC_JAR_LIB_OBJ);
157             }
158         });
159         // should not sort, bug 98401
160
//fTableViewer.setSorter(new ViewerSorter());
161
data = new GridData(GridData.FILL_BOTH);
162         fTableViewer.getControl().setLayoutData(data);
163         fTableViewer.setInput(fJarPaths);
164         fTableViewer.getTable().addKeyListener(new KeyAdapter() {
165             public void keyPressed(KeyEvent event) {
166                 if (event.character == SWT.DEL && event.stateMask == 0) {
167                     handleRemove();
168                 }
169             }
170         });
171
172         Composite buttons = new Composite(control, SWT.NONE);
173         layout = new GridLayout();
174         layout.verticalSpacing = 5;
175         layout.marginHeight = 0;
176         layout.marginWidth = 0;
177         buttons.setLayout(layout);
178         data = new GridData(GridData.FILL_VERTICAL);
179         data.grabExcessVerticalSpace = true;
180         buttons.setLayoutData(data);
181
182         Button browseWorkspace = new Button(buttons, SWT.PUSH);
183         browseWorkspace.setText(PDEUIMessages.LibraryPluginJarsPage_add);
184         browseWorkspace.setLayoutData(new GridData(
185                 GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL));
186         SWTUtil.setButtonDimensionHint(browseWorkspace);
187         browseWorkspace.addSelectionListener(new SelectionAdapter() {
188             public void widgetSelected(SelectionEvent e) {
189                 chooseWorkspaceFile();
190             }
191         });
192
193         Button browseFile = new Button(buttons, SWT.PUSH);
194         browseFile.setText(PDEUIMessages.LibraryPluginJarsPage_addExternal);
195         browseFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
196         SWTUtil.setButtonDimensionHint(browseFile);
197         browseFile.addSelectionListener(new SelectionAdapter() {
198             public void widgetSelected(SelectionEvent e) {
199                 chooseFile();
200             }
201         });
202
203         fRemove = new Button(buttons, SWT.PUSH);
204         fRemove.setText(PDEUIMessages.LibraryPluginJarsPage_remove);
205         fRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
206         SWTUtil.setButtonDimensionHint(fRemove);
207         fRemove.setEnabled(fJarPaths.size() > 0);
208         setPageComplete(fJarPaths.size() > 0);
209         fRemove.addSelectionListener(new SelectionAdapter() {
210             public void widgetSelected(SelectionEvent e) {
211                 handleRemove();
212             }
213         });
214
215         Dialog.applyDialogFont(control);
216         PlatformUI.getWorkbench().getHelpSystem().setHelp(control,
217                 IHelpContextIds.NEW_LIBRARY_PROJECT_JAR_PAGE);
218         setControl(control);
219         
220         PlatformUI.getWorkbench().getHelpSystem().setHelp(control, IHelpContextIds.LIBRARY_PLUGIN_JARS);
221     }
222
223     private void handleRemove() {
224         IStructuredSelection selection = (IStructuredSelection) fTableViewer
225                 .getSelection();
226         if (!selection.isEmpty()) {
227             for (Iterator JavaDoc it = selection.iterator(); it.hasNext();) {
228                 Object JavaDoc file = it.next();
229                 fJarPaths.remove(file);
230                 fTableViewer.remove(file);
231             }
232             fRemove.setEnabled(fJarPaths.size() > 0);
233             setPageComplete(fJarPaths.size() > 0);
234         }
235     }
236
237     public boolean isPageComplete() {
238         return fJarPaths.size() > 0;
239     }
240
241     private void removeJar(String JavaDoc fileName) {
242         for (int i = 0; i < fJarPaths.size(); i++) {
243             String JavaDoc name;
244             if (fJarPaths.get(i) instanceof IFile) {
245                 IFile jarFile = (IFile) fJarPaths.get(i);
246                 name = jarFile.getName();
247             } else {
248                 File JavaDoc jarFile = (File JavaDoc) fJarPaths.get(i);
249                 name = jarFile.getName();
250             }
251             if (name.equals(fileName)) {
252                 Object JavaDoc jarPath = fJarPaths.get(i);
253                 fJarPaths.remove(jarPath);
254                 fTableViewer.remove(jarPath);
255             }
256         }
257     }
258
259     public void updateData() {
260         String JavaDoc[] jarPaths = new String JavaDoc[fJarPaths.size()];
261         for (int i = 0; i < fJarPaths.size(); i++) {
262             if (fJarPaths.get(i) instanceof IFile) {
263                 IFile jarFile = (IFile) fJarPaths.get(i);
264                 jarPaths[i] = jarFile.getLocation().toString();
265             } else {
266                 File JavaDoc jarFile = (File JavaDoc) fJarPaths.get(i);
267                 jarPaths[i] = jarFile.toString();
268
269             }
270         }
271         fData.setLibraryPaths(jarPaths);
272     }
273 }
274
Popular Tags