01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.project.ui.internal.actions;
16:
17: import java.util.Collections;
18:
19: import net.refractions.udig.catalog.CatalogPlugin;
20: import net.refractions.udig.catalog.IGeoResource;
21: import net.refractions.udig.project.IMap;
22: import net.refractions.udig.project.ui.ApplicationGIS;
23: import net.refractions.udig.project.ui.internal.Messages;
24: import net.refractions.udig.ui.FeatureTypeEditorDialog;
25: import net.refractions.udig.ui.FeatureTypeEditorDialog.ValidateFeatureTypeBuilder;
26:
27: import org.eclipse.jface.action.IAction;
28: import org.eclipse.jface.window.Window;
29: import org.eclipse.swt.widgets.Event;
30: import org.eclipse.ui.IWorkbenchWindow;
31: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
32: import org.eclipse.ui.actions.ActionDelegate;
33: import org.geotools.feature.FeatureTypeBuilder;
34:
35: /**
36: * Allows a FeatureType to be created and adds the feature type to the
37: *
38: * @author Jesse
39: * @since 1.0.0
40: */
41: public class NewLayerAction extends ActionDelegate implements
42: IWorkbenchWindowActionDelegate {
43: private IGeoResource resource = null;
44:
45: private final ValidateFeatureTypeBuilder performOK = new ValidateFeatureTypeBuilder() {
46:
47: public boolean validate(FeatureTypeBuilder featureBuilder) {
48: try {
49: resource = CatalogPlugin.getDefault().getLocalCatalog()
50: .createTemporaryResource(
51: featureBuilder.getFeatureType());
52: return true;
53: } catch (Exception e) {
54: resource = null;
55: featureBuilder
56: .setName(Messages.NewLayerAction_duplicate_type_name);
57: return false;
58: }
59: }
60:
61: };
62:
63: @Override
64: public void runWithEvent(IAction action, Event event) {
65: // Open a dialog for user to create featuretype
66: FeatureTypeEditorDialog dialog = new FeatureTypeEditorDialog(
67: event.display.getActiveShell(), performOK);
68: dialog.setBlockOnOpen(true);
69: int code = dialog.open();
70:
71: if (code == Window.CANCEL)
72: return;
73:
74: if (resource != null) {
75: IMap map = ApplicationGIS.getActiveMap();
76: int index = 0;
77: if (map != null) {
78: index = map.getMapLayers().size();
79: }
80: ApplicationGIS.addLayersToMap(map, Collections
81: .singletonList(resource), index);
82: }
83:
84: }
85:
86: public void init(IWorkbenchWindow window) {
87: }
88:
89: }
|