001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.ui.internal;
016:
017: import net.refractions.udig.internal.ui.UiPlugin;
018: import net.refractions.udig.project.ui.IUDIGDialogPage;
019: import net.refractions.udig.project.ui.IUDIGView;
020: import net.refractions.udig.project.ui.internal.FeatureEditorExtensionProcessor.EditActionContribution;
021: import net.refractions.udig.project.ui.internal.tool.ToolContext;
022:
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IConfigurationElement;
025: import org.eclipse.jface.action.Action;
026: import org.eclipse.jface.action.IAction;
027: import org.eclipse.jface.dialogs.Dialog;
028: import org.eclipse.jface.resource.ImageDescriptor;
029: import org.eclipse.jface.viewers.ISelection;
030: import org.eclipse.jface.viewers.IStructuredSelection;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.widgets.Display;
033: import org.eclipse.swt.widgets.Shell;
034: import org.eclipse.ui.IWorkbenchPage;
035: import org.eclipse.ui.PartInitException;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.plugin.AbstractUIPlugin;
038: import org.geotools.feature.Feature;
039:
040: public class FeatureEditorLoader {
041: /** FeatureEditorLoader processor field */
042: final FeatureEditorExtensionProcessor processor;
043:
044: private String dialogPage;
045:
046: String viewId;
047:
048: private String name;
049:
050: String id;
051:
052: ImageDescriptor icon;
053:
054: IAction contribution;
055:
056: private IConfigurationElement definition;
057:
058: private FeatureTypeMatch featureTypeMatcher;
059:
060: FeatureEditorLoader(FeatureEditorExtensionProcessor processor,
061: IConfigurationElement definition) {
062: this .processor = processor;
063: String iconID = definition.getAttribute("icon"); //$NON-NLS-1$
064: if (iconID != null) {
065: icon = AbstractUIPlugin.imageDescriptorFromPlugin(
066: definition.getNamespaceIdentifier(), iconID);
067: }
068:
069: id = definition.getAttribute("id"); //$NON-NLS-1$
070: name = definition.getAttribute("name"); //$NON-NLS-1$
071: viewId = definition.getAttribute("viewId"); //$NON-NLS-1$
072: dialogPage = definition.getAttribute("dialogPage"); //$NON-NLS-1$
073: this .definition = definition;
074: IConfigurationElement[] featureTypeDef = definition
075: .getChildren("featureType");//$NON-NLS-1$
076: if (featureTypeDef.length == 1)
077: this .featureTypeMatcher = new FeatureTypeMatch(
078: featureTypeDef[0]);
079: else
080: this .featureTypeMatcher = FeatureTypeMatch.ALL;
081: }
082:
083: IAction getAction(final ISelection selection) {
084: if (!(selection instanceof IStructuredSelection))
085: return null;
086:
087: IStructuredSelection structuredSelection = (IStructuredSelection) selection;
088: if (!processor.sameFeatureTypes(structuredSelection))
089: return null;
090:
091: final Feature feature = (Feature) structuredSelection
092: .getFirstElement();
093: if (featureTypeMatcher.matches(feature) == -1)
094: return null;
095: contribution = new Action(name, IAction.AS_RADIO_BUTTON) {
096: public void runWithEvent(org.eclipse.swt.widgets.Event event) {
097:
098: // note: when the gui is used the contribution is checked by the framework.
099: // so the gui must be used in order to test this.
100: if (contribution.isChecked()) {
101: processor.createEditAction(
102: FeatureEditorLoader.this , selection,
103: feature);
104: open(event.display, selection);
105: }
106: }
107: };
108: contribution.setId(id);
109: contribution.setImageDescriptor(icon);
110: EditActionContribution selected = this .processor.selectedEditors
111: .get(feature.getFeatureType());
112: if (selected == null) {
113: selected = processor.createEditAction(processor
114: .getClosestMatch(selection), selection, feature);
115: } else {
116: selected.setSelection(selection);
117: }
118: if (selected != null && selected.getId().equals(id))
119: contribution.setChecked(true);
120: else
121: contribution.setChecked(false);
122:
123: return contribution;
124: }
125:
126: public void open(Display display, ISelection selection) {
127: Feature feature = (Feature) ((IStructuredSelection) selection)
128: .getFirstElement();
129:
130: if (viewId != null) {
131: try {
132: IUDIGView view = (IUDIGView) PlatformUI.getWorkbench()
133: .getActiveWorkbenchWindow().getActivePage()
134: .showView(viewId, null,
135: IWorkbenchPage.VIEW_VISIBLE);
136: try {
137: view.editFeatureChanged(feature);
138: } catch (Throwable e) {
139: UiPlugin.log(view + " threw an exception", e); //$NON-NLS-1$
140: }
141: } catch (PartInitException e) {
142: ProjectUIPlugin.log(null, e);
143: }
144: } else if (dialogPage != null) {
145: try {
146: IUDIGDialogPage page = (IUDIGDialogPage) definition
147: .createExecutableExtension("dialogPage"); //$NON-NLS-1$
148: ToolContext toolContext;
149: synchronized (this .processor.partListener) {
150: toolContext = this .processor.partListener.currentContext;
151: }
152: page.setContext(toolContext);
153: Dialog dialog = new FeatureEditorExtensionProcessor.EditorDialog(
154: new Shell(display.getActiveShell(), SWT.RESIZE
155: | SWT.PRIMARY_MODAL), page);
156: dialog.setBlockOnOpen(false);
157: page.setFeature(feature);
158: dialog.open();
159:
160: PlatformUI
161: .getPreferenceStore()
162: .putValue(
163: FeatureEditorExtensionProcessor.CURRENT_LOADER_ID,
164: id);
165:
166: } catch (CoreException e) {
167: ProjectUIPlugin.log(null, e);
168: }
169:
170: }
171: }
172:
173: /**
174: * Returns an integer indicating the accuracy of the match between a featureType and the one
175: * declared in the FeatureEditor extension declaration.
176: * <p>
177: * -1 indicates no match, 0 a perfect match and each number higher is a match but a poorer
178: * match.
179: * </p>
180: *
181: * @param selection
182: * @return
183: */
184: public int match(ISelection selection) {
185: IStructuredSelection structuredSelection = (IStructuredSelection) selection;
186: if (!processor.sameFeatureTypes(structuredSelection))
187: return -1;
188:
189: final Feature feature = (Feature) structuredSelection
190: .getFirstElement();
191: return featureTypeMatcher.matches(feature);
192: }
193:
194: /**
195: * Returns the dialog page if the feature editor is a dialog, or null otherwise.
196: *
197: * @return the dialog page if the feature editor is a dialog, or null otherwise.
198: */
199: public String getDialogPage() {
200: return dialogPage;
201: }
202:
203: /**
204: * Returns the name of the feature editor
205: *
206: * @returnthe name of the feature editor
207: */
208: public String getName() {
209: return name;
210: }
211:
212: /**
213: * Returns the viewid if the feature editor is a view, or null otherwise.
214: *
215: * @return the dviewid if the feature editor is a view, or null otherwise.
216: */
217: public String getViewId() {
218: return viewId;
219: }
220:
221: public String getId() {
222: return id;
223: }
224: }
|