01: /*
02: * uDig - User Friendly Desktop Internet GIS client
03: * http://udig.refractions.net
04: * (C) 2004, Refractions Research Inc.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package net.refractions.udig.project.ui.internal.actions;
18:
19: import java.io.File;
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: import net.refractions.udig.project.ui.internal.MapFactory;
26: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
27:
28: import org.eclipse.emf.common.ui.action.WorkbenchWindowActionDelegate;
29: import org.eclipse.jface.action.IAction;
30: import org.eclipse.swt.SWT;
31: import org.eclipse.swt.widgets.Display;
32: import org.eclipse.swt.widgets.FileDialog;
33:
34: /**
35: * Performs the open action from the file menu of uDig. It is responseible for creating new maps
36: * from selected resources.
37: *
38: * @author rgould
39: * @since 0.6.0
40: */
41: public class AddLayerFiles extends WorkbenchWindowActionDelegate {
42:
43: public static final String ID = "net.refractions.udig.project.ui.openFilesAction"; //$NON-NLS-1$
44:
45: /*
46: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
47: */
48: public void run(IAction action) {
49: addLayers(false);
50: }
51:
52: protected void addLayers(boolean forceNewMap) {
53: String lastOpenedDirectory = ProjectUIPlugin.getDefault()
54: .getPluginPreferences().getString(
55: ProjectUIPlugin.PREF_OPEN_DIALOG_DIRECTORY);
56: FileDialog fileDialog = new FileDialog(Display.getCurrent()
57: .getActiveShell(), SWT.MULTI);
58: fileDialog.setFilterExtensions(new String[] { "*.shp", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
59: if (lastOpenedDirectory != null) {
60: fileDialog.setFilterPath(lastOpenedDirectory);
61: }
62: String result = fileDialog.open();
63: if (result == null) {
64: return;
65: }
66: String path = fileDialog.getFilterPath();
67: ProjectUIPlugin.getDefault().getPluginPreferences().setValue(
68: ProjectUIPlugin.PREF_OPEN_DIALOG_DIRECTORY, path);
69: ProjectUIPlugin.getDefault().savePluginPreferences();
70: String[] filenames = fileDialog.getFileNames();
71: List<URL> urls = new ArrayList<URL>();
72: for (int i = 0; i < filenames.length; i++) {
73: try {
74: URL url = new File(
75: path
76: + System.getProperty("file.separator") + filenames[i]).toURL(); //$NON-NLS-1$
77: urls.add(url);
78: } catch (MalformedURLException e) {
79: e.printStackTrace();
80: }
81: }
82: MapFactory.instance().processURLs(urls, null, forceNewMap);
83: }
84: }
|