001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.plugin;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.jface.viewers.TableViewer;
021: import org.eclipse.jface.window.Window;
022: import org.eclipse.jface.wizard.WizardPage;
023: import org.eclipse.pde.internal.ui.IHelpContextIds;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEPluginImages;
026: import org.eclipse.pde.internal.ui.PDEUIMessages;
027: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
028: import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
029: import org.eclipse.pde.internal.ui.util.FileValidator;
030: import org.eclipse.pde.internal.ui.util.SWTUtil;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.KeyAdapter;
033: import org.eclipse.swt.events.KeyEvent;
034: import org.eclipse.swt.events.SelectionAdapter;
035: import org.eclipse.swt.events.SelectionEvent;
036: import org.eclipse.swt.graphics.Image;
037: import org.eclipse.swt.layout.GridData;
038: import org.eclipse.swt.layout.GridLayout;
039: import org.eclipse.swt.widgets.Button;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.FileDialog;
042: import org.eclipse.swt.widgets.Label;
043: import org.eclipse.ui.PlatformUI;
044: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
045: import org.eclipse.ui.model.WorkbenchContentProvider;
046: import org.eclipse.ui.model.WorkbenchLabelProvider;
047:
048: public class LibraryPluginJarsPage extends WizardPage {
049:
050: protected LibraryPluginFieldData fData;
051:
052: /**
053: * List of IFile and File of workspace and external Jars.
054: */
055: protected ArrayList fJarPaths = new ArrayList();
056:
057: protected Button fRemove;
058:
059: protected TableViewer fTableViewer;
060:
061: public LibraryPluginJarsPage(String pageName,
062: LibraryPluginFieldData data) {
063: super (pageName);
064: fData = data;
065: setTitle(PDEUIMessages.LibraryPluginJarsPage_title);
066: setDescription(PDEUIMessages.LibraryPluginJarsPage_desc);
067: }
068:
069: private void chooseFile() {
070: FileDialog dialog = new FileDialog(getShell(), SWT.OPEN
071: | SWT.MULTI);
072: dialog.setFilterExtensions(new String[] { "*.jar" }); //$NON-NLS-1$
073: String res = dialog.open();
074: if (res != null) {
075: String path = new File(res).getParent();
076: String[] fileNames = dialog.getFileNames();
077: for (int i = 0; i < fileNames.length; i++) {
078: File newJarFile = new File(path, fileNames[i]);
079: removeJar(fileNames[i]);
080: fJarPaths.add(newJarFile);
081: fTableViewer.add(newJarFile);
082: }
083: fRemove.setEnabled(fJarPaths.size() > 0);
084: setPageComplete(fJarPaths.size() > 0);
085: }
086: }
087:
088: private void chooseWorkspaceFile() {
089: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
090: getShell(), new WorkbenchLabelProvider(),
091: new WorkbenchContentProvider());
092:
093: dialog.setValidator(new FileValidator());
094: dialog.setAllowMultiple(true);
095: dialog
096: .setTitle(PDEUIMessages.LibraryPluginJarsPage_SelectionDialog_title);
097: dialog
098: .setMessage(PDEUIMessages.LibraryPluginJarsPage_SelectionDialog_message);
099: dialog.addFilter(new FileExtensionFilter("jar")); //$NON-NLS-1$
100: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
101:
102: if (dialog.open() == Window.OK) {
103: Object[] files = dialog.getResult();
104: for (int i = 0; i < files.length; i++) {
105: IFile newJarFile = (IFile) files[i];
106: removeJar(newJarFile.getName());
107: fJarPaths.add(newJarFile);
108: fTableViewer.add(newJarFile);
109: }
110: fRemove.setEnabled(fJarPaths.size() > 0);
111: setPageComplete(fJarPaths.size() > 0);
112: }
113: }
114:
115: public void createControl(Composite parent) {
116: Composite control = new Composite(parent, SWT.NONE);
117: GridLayout layout = new GridLayout();
118: layout.numColumns = 2;
119: // layout.verticalSpacing = 10;
120: control.setLayout(layout);
121:
122: Label l = new Label(control, SWT.WRAP);
123: l.setText(PDEUIMessages.LibraryPluginJarsPage_label);
124: GridData data = new GridData(GridData.FILL_HORIZONTAL);
125: data.horizontalSpan = 2;
126: l.setLayoutData(data);
127: fTableViewer = new TableViewer(control, SWT.MULTI | SWT.BORDER);
128: fTableViewer.setContentProvider(new DefaultTableProvider() {
129: public Object[] getElements(Object inputElement) {
130: return fJarPaths.toArray();
131: }
132: });
133: fTableViewer.setLabelProvider(new LabelProvider() {
134: public String getText(Object obj) {
135: String name;
136: String location;
137: if (obj instanceof IFile) {
138: IFile jarFile = (IFile) obj;
139: name = jarFile.getName();
140: location = jarFile.getParent().getFullPath()
141: .toString().substring(1);
142: } else {
143: File jarFile = (File) obj;
144: name = jarFile.getName();
145: location = jarFile.getParent();
146: }
147: return name + " - " + location; //$NON-NLS-1$
148:
149: }
150:
151: public Image getImage(Object obj) {
152: if (obj instanceof IFile) {
153: return PDEPlugin.getDefault().getLabelProvider()
154: .get(PDEPluginImages.DESC_JAR_OBJ);
155: }
156: return PDEPlugin.getDefault().getLabelProvider().get(
157: PDEPluginImages.DESC_JAR_LIB_OBJ);
158: }
159: });
160: // should not sort, bug 98401
161: //fTableViewer.setSorter(new ViewerSorter());
162: data = new GridData(GridData.FILL_BOTH);
163: fTableViewer.getControl().setLayoutData(data);
164: fTableViewer.setInput(fJarPaths);
165: fTableViewer.getTable().addKeyListener(new KeyAdapter() {
166: public void keyPressed(KeyEvent event) {
167: if (event.character == SWT.DEL && event.stateMask == 0) {
168: handleRemove();
169: }
170: }
171: });
172:
173: Composite buttons = new Composite(control, SWT.NONE);
174: layout = new GridLayout();
175: layout.verticalSpacing = 5;
176: layout.marginHeight = 0;
177: layout.marginWidth = 0;
178: buttons.setLayout(layout);
179: data = new GridData(GridData.FILL_VERTICAL);
180: data.grabExcessVerticalSpace = true;
181: buttons.setLayoutData(data);
182:
183: Button browseWorkspace = new Button(buttons, SWT.PUSH);
184: browseWorkspace
185: .setText(PDEUIMessages.LibraryPluginJarsPage_add);
186: browseWorkspace.setLayoutData(new GridData(
187: GridData.VERTICAL_ALIGN_BEGINNING
188: | GridData.FILL_HORIZONTAL));
189: SWTUtil.setButtonDimensionHint(browseWorkspace);
190: browseWorkspace.addSelectionListener(new SelectionAdapter() {
191: public void widgetSelected(SelectionEvent e) {
192: chooseWorkspaceFile();
193: }
194: });
195:
196: Button browseFile = new Button(buttons, SWT.PUSH);
197: browseFile
198: .setText(PDEUIMessages.LibraryPluginJarsPage_addExternal);
199: browseFile
200: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201: SWTUtil.setButtonDimensionHint(browseFile);
202: browseFile.addSelectionListener(new SelectionAdapter() {
203: public void widgetSelected(SelectionEvent e) {
204: chooseFile();
205: }
206: });
207:
208: fRemove = new Button(buttons, SWT.PUSH);
209: fRemove.setText(PDEUIMessages.LibraryPluginJarsPage_remove);
210: fRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
211: SWTUtil.setButtonDimensionHint(fRemove);
212: fRemove.setEnabled(fJarPaths.size() > 0);
213: setPageComplete(fJarPaths.size() > 0);
214: fRemove.addSelectionListener(new SelectionAdapter() {
215: public void widgetSelected(SelectionEvent e) {
216: handleRemove();
217: }
218: });
219:
220: Dialog.applyDialogFont(control);
221: PlatformUI.getWorkbench().getHelpSystem().setHelp(control,
222: IHelpContextIds.NEW_LIBRARY_PROJECT_JAR_PAGE);
223: setControl(control);
224:
225: PlatformUI.getWorkbench().getHelpSystem().setHelp(control,
226: IHelpContextIds.LIBRARY_PLUGIN_JARS);
227: }
228:
229: private void handleRemove() {
230: IStructuredSelection selection = (IStructuredSelection) fTableViewer
231: .getSelection();
232: if (!selection.isEmpty()) {
233: for (Iterator it = selection.iterator(); it.hasNext();) {
234: Object file = it.next();
235: fJarPaths.remove(file);
236: fTableViewer.remove(file);
237: }
238: fRemove.setEnabled(fJarPaths.size() > 0);
239: setPageComplete(fJarPaths.size() > 0);
240: }
241: }
242:
243: public boolean isPageComplete() {
244: return fJarPaths.size() > 0;
245: }
246:
247: private void removeJar(String fileName) {
248: for (int i = 0; i < fJarPaths.size(); i++) {
249: String name;
250: if (fJarPaths.get(i) instanceof IFile) {
251: IFile jarFile = (IFile) fJarPaths.get(i);
252: name = jarFile.getName();
253: } else {
254: File jarFile = (File) fJarPaths.get(i);
255: name = jarFile.getName();
256: }
257: if (name.equals(fileName)) {
258: Object jarPath = fJarPaths.get(i);
259: fJarPaths.remove(jarPath);
260: fTableViewer.remove(jarPath);
261: }
262: }
263: }
264:
265: public void updateData() {
266: String[] jarPaths = new String[fJarPaths.size()];
267: for (int i = 0; i < fJarPaths.size(); i++) {
268: if (fJarPaths.get(i) instanceof IFile) {
269: IFile jarFile = (IFile) fJarPaths.get(i);
270: jarPaths[i] = jarFile.getLocation().toString();
271: } else {
272: File jarFile = (File) fJarPaths.get(i);
273: jarPaths[i] = jarFile.toString();
274:
275: }
276: }
277: fData.setLibraryPaths(jarPaths);
278: }
279: }
|