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.printing.ui.internal.editor;
016:
017: import java.util.concurrent.atomic.AtomicInteger;
018:
019: import net.refractions.udig.printing.model.BoxPrinter;
020: import net.refractions.udig.printing.ui.IBoxEditAction;
021: import net.refractions.udig.printing.ui.internal.PrintingPlugin;
022: import net.refractions.udig.printing.ui.internal.editor.parts.PrintingEditPolicy;
023:
024: import org.eclipse.core.runtime.IConfigurationElement;
025: import org.eclipse.core.runtime.IStatus;
026: import org.eclipse.core.runtime.Status;
027: import org.eclipse.gef.EditPart;
028: import org.eclipse.gef.EditPolicy;
029: import org.eclipse.gef.Request;
030: import org.eclipse.gef.ui.actions.SelectionAction;
031: import org.eclipse.jface.resource.ImageDescriptor;
032: import org.eclipse.swt.widgets.Display;
033: import org.eclipse.ui.IWorkbenchPart;
034:
035: /**
036: * An action that is added to a PageEditor. Each BoxAction corresponds to a editAction in a box
037: * extension point.
038: *
039: * @author Jesse
040: * @since 1.1.0
041: */
042: public class BoxAction extends SelectionAction {
043:
044: private IConfigurationElement editActionElement;
045: Request request;
046: private String acceptable;
047: private Class<? extends BoxPrinter> acceptableClass;
048: private IBoxEditAction editAction;
049: private static AtomicInteger nextID = new AtomicInteger(0);
050:
051: public BoxAction(IWorkbenchPart part,
052: IConfigurationElement editActionElement2, String acceptable) {
053: super (part);
054: this .editActionElement = editActionElement2;
055: final int id = nextID.getAndIncrement();
056: this .acceptable = acceptable;
057:
058: setText(editActionElement2.getAttribute("name")); //$NON-NLS-1$
059: String attribute = editActionElement2.getAttribute("image");//$NON-NLS-1$
060: ImageDescriptor image;
061: if (attribute != null && attribute.trim().length() != 0) {
062: image = PrintingPlugin.imageDescriptorFromPlugin(
063: editActionElement2.getNamespace(), attribute);
064: setImageDescriptor(image);
065: }
066: setId("BOX_EDIT_ACTION_" + getText() + id); //$NON-NLS-1$
067:
068: request = new Request() {
069: String type = getText() + "_REQUEST_" + id; //$NON-NLS-1$
070:
071: @Override
072: public Object getType() {
073: return type;
074: }
075: };
076: }
077:
078: public BoxAction(BoxAction action) {
079: super (action.getWorkbenchPart());
080: setText(action.getText());
081: setToolTipText(action.getToolTipText());
082: setId(action.getId());
083: setActionDefinitionId(action.getActionDefinitionId());
084: this .acceptable = action.acceptable;
085: this .acceptableClass = action.acceptableClass;
086: this .editActionElement = action.editActionElement;
087: this .request = action.request;
088: }
089:
090: @Override
091: protected boolean calculateEnabled() {
092: if (getSelectedObjects().size() == 1
093: && (getSelectedObjects().get(0) instanceof EditPart)) {
094: EditPart part = (EditPart) getSelectedObjects().get(0);
095: return part.understandsRequest(request);
096: }
097: return false;
098: }
099:
100: @Override
101: public void run() {
102: try {
103: EditPart part = (EditPart) getSelectedObjects().get(0);
104: part.performRequest(request);
105: } catch (ClassCastException e) {
106: PrintingPlugin.log("", e); //$NON-NLS-1$
107: } catch (IndexOutOfBoundsException e) {
108: PrintingPlugin.log("", e); //$NON-NLS-1$
109: }
110: }
111:
112: @SuppressWarnings("unchecked")
113: public synchronized Class<? extends BoxPrinter> getAcceptablePrinterClass(
114: Class<? extends BoxPrinter> class1) {
115: if (acceptableClass == null) {
116: try {
117: acceptableClass = (Class<? extends BoxPrinter>) Class
118: .forName(acceptable, true, class1
119: .getClassLoader());
120: } catch (Exception e) {
121: return null;
122: }
123: }
124: return acceptableClass;
125: }
126:
127: public synchronized EditPolicy getEditPolicy() {
128: PrintingEditPolicy editPolicy = new PrintingEditPolicy(
129: new BoxAction(this ));
130: return editPolicy;
131: }
132:
133: public Request getRequest() {
134: return request;
135: }
136:
137: public synchronized IBoxEditAction getBoxEditAction() {
138: if (editAction == null) {
139: try {
140: editAction = (IBoxEditAction) editActionElement
141: .createExecutableExtension("class"); //$NON-NLS-1$
142: } catch (Exception e) {
143: PrintingPlugin.getDefault().getLog().log(
144: new Status(IStatus.ERROR, editActionElement
145: .getNamespace(), IStatus.OK, "", e)); //$NON-NLS-1$
146: }
147: }
148: return editAction;
149: }
150:
151: }
|