KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > AddExternalJarAction


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.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
20 import org.eclipse.jdt.launching.JavaRuntime;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.FileDialog;
24
25 import com.ibm.icu.text.MessageFormat;
26
27 /**
28  * Adds an external jar to the runtime class path.
29  */

30 public class AddExternalJarAction extends OpenDialogAction {
31
32     public AddExternalJarAction(IClasspathViewer viewer, String JavaDoc dialogSettingsPrefix) {
33         super(ActionMessages.AddExternalJar_Add_E_xternal_JARs_1, viewer, dialogSettingsPrefix);
34     }
35
36     /**
37      * Prompts for a project to add.
38      *
39      * @see IAction#run()
40      */

41     public void run() {
42                             
43         String JavaDoc lastUsedPath = getDialogSetting(LAST_PATH_SETTING);
44         if (lastUsedPath == null) {
45             lastUsedPath = ""; //$NON-NLS-1$
46
}
47         FileDialog dialog = new FileDialog(getShell(), SWT.MULTI);
48         dialog.setText(ActionMessages.AddExternalJar_Jar_Selection_3);
49         dialog.setFilterExtensions(new String JavaDoc[] {"*.jar;*.zip"}); //$NON-NLS-1$
50
dialog.setFilterPath(lastUsedPath);
51         String JavaDoc res = dialog.open();
52         if (res == null) {
53             return;
54         }
55         String JavaDoc[] fileNames = dialog.getFileNames();
56         int nChosen = fileNames.length;
57             
58         IPath filterPath = new Path(dialog.getFilterPath());
59         ArrayList JavaDoc list = new ArrayList JavaDoc();
60         IPath path = null;
61         for (int i= 0; i < nChosen; i++) {
62             path = filterPath.append(fileNames[i]).makeAbsolute();
63             if(path.toFile().exists()) {
64                 list.add(JavaRuntime.newArchiveRuntimeClasspathEntry(path));
65             }
66             else {
67                 MessageDialog.openError(getShell(), ActionMessages.AddExternalJarAction_error_box_title, MessageFormat.format(ActionMessages.AddExternalJarAction_error_box_message, new String JavaDoc[] {path.makeAbsolute().toOSString()}));
68             }
69         }
70         if(list.size() > 0) {
71             setDialogSetting(LAST_PATH_SETTING, filterPath.toOSString());
72             getViewer().addEntries((IRuntimeClasspathEntry[]) list.toArray(new IRuntimeClasspathEntry[list.size()]));
73         }
74     }
75 }
76
Popular Tags