001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.printing.ui.internal.editor.parts;
018:
019: import java.util.List;
020:
021: import net.refractions.udig.printing.model.Box;
022: import net.refractions.udig.printing.model.BoxPrinter;
023: import net.refractions.udig.printing.model.Connection;
024: import net.refractions.udig.printing.model.PropertyListener;
025: import net.refractions.udig.printing.ui.IBoxEditAction;
026: import net.refractions.udig.printing.ui.internal.PrintingPlugin;
027: import net.refractions.udig.printing.ui.internal.editor.BoxAction;
028: import net.refractions.udig.printing.ui.internal.editor.commands.ConnectionCreateCommand;
029: import net.refractions.udig.printing.ui.internal.editor.commands.ConnectionReconnectCommand;
030: import net.refractions.udig.printing.ui.internal.editor.figures.BoxFigure;
031: import net.refractions.udig.printing.ui.internal.editor.policies.PageElementEditPolicy;
032:
033: import org.eclipse.core.runtime.IAdaptable;
034: import org.eclipse.draw2d.ChopboxAnchor;
035: import org.eclipse.draw2d.ConnectionAnchor;
036: import org.eclipse.draw2d.IFigure;
037: import org.eclipse.draw2d.geometry.Dimension;
038: import org.eclipse.draw2d.geometry.Point;
039: import org.eclipse.draw2d.geometry.Rectangle;
040: import org.eclipse.gef.ConnectionEditPart;
041: import org.eclipse.gef.EditPolicy;
042: import org.eclipse.gef.GraphicalEditPart;
043: import org.eclipse.gef.NodeEditPart;
044: import org.eclipse.gef.Request;
045: import org.eclipse.gef.commands.Command;
046: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
047: import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
048: import org.eclipse.gef.requests.CreateConnectionRequest;
049: import org.eclipse.gef.requests.ReconnectRequest;
050: import org.eclipse.jface.util.IPropertyChangeListener;
051: import org.eclipse.jface.util.PropertyChangeEvent;
052: import org.eclipse.swt.widgets.Display;
053:
054: /**
055: * A part for all boxes.
056: *
057: * @author Richard Gould
058: * @author Jesse
059: * @since 0.3
060: */
061: public class BoxPart extends AbstractGraphicalEditPart implements
062: NodeEditPart, IAdaptable {
063:
064: protected InternalPropertyListener listener = new InternalPropertyListener();
065:
066: @SuppressWarnings("unchecked")
067: public void activate() {
068: if (isActive()) {
069: return;
070: }
071:
072: super .activate();
073: Box box = ((Box) getModel());
074: box.eAdapters().add(this .listener);
075: box.addPropertyChangeListener(listener);
076: }
077:
078: public void deactivate() {
079: if (!isActive()) {
080: return;
081: }
082: super .deactivate();
083: Box box = ((Box) getModel());
084: box.eAdapters().remove(this .listener);
085: box.removePropertyChangeListener(listener);
086: }
087:
088: @Override
089: public Object getAdapter(Class key) {
090: Box box = ((Box) getModel());
091: if (box instanceof IAdaptable) {
092: Object obj = ((IAdaptable) box).getAdapter(key);
093: if (obj != null) {
094: return obj;
095: }
096: }
097: return super .getAdapter(key);
098: }
099:
100: protected void refreshVisuals() {
101: Box labelBox = (Box) this .getModel();
102: Point loc = labelBox.getLocation();
103: Dimension size = labelBox.getSize();
104: Rectangle rectangle = new Rectangle(loc, size);
105:
106: ((BoxFigure) this .getFigure()).setBox((Box) this .getModel());
107:
108: ((GraphicalEditPart) getParent()).setLayoutConstraint(this ,
109: getFigure(), rectangle);
110: }
111:
112: public BoxPrinter getBoxPrinter() {
113: return ((Box) getModel()).getBoxPrinter();
114: }
115:
116: public void performRequest(Request request) {
117: EditPolicy policy = getEditPolicy(request.getType());
118: if (policy instanceof PrintingEditPolicy) {
119: PrintingEditPolicy editPolicy = (PrintingEditPolicy) policy;
120: IBoxEditAction action = editPolicy.getAction()
121: .getBoxEditAction();
122: action.init(this );
123: action.perform();
124: Display display = getViewer().getControl().getDisplay();
125: while (!action.isDone()) {
126: if (!display.readAndDispatch()) {
127: try {
128: Thread.sleep(200);
129: } catch (InterruptedException e) {
130: break;
131: }
132: }
133: }
134: if (!action.isDone())
135: return;
136:
137: Command command = action.getCommand();
138:
139: if (command != null && command.canExecute()) {
140: getViewer().getEditDomain().getCommandStack().execute(
141: command);
142: }
143: }
144: }
145:
146: protected List getModelSourceConnections() {
147: return ((Box) getModel()).getSourceConnections();
148: }
149:
150: protected List getModelTargetConnections() {
151: return ((Box) getModel()).getTargetConnections();
152: }
153:
154: protected void createEditPolicies() {
155: installEditPolicy(EditPolicy.COMPONENT_ROLE,
156: new PageElementEditPolicy());
157: installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
158: new GraphicalNodeEditPolicy() {
159:
160: protected Command getConnectionCompleteCommand(
161: CreateConnectionRequest request) {
162: ConnectionCreateCommand cmd = (ConnectionCreateCommand) request
163: .getStartCommand();
164: cmd.setTarget((Box) getHost().getModel());
165: return cmd;
166: }
167:
168: protected Command getConnectionCreateCommand(
169: CreateConnectionRequest request) {
170: Box source = (Box) getHost().getModel();
171: ConnectionCreateCommand cmd = new ConnectionCreateCommand(
172: source);
173: request.setStartCommand(cmd);
174: return cmd;
175: }
176:
177: protected Command getReconnectTargetCommand(
178: ReconnectRequest request) {
179: Connection conn = (Connection) request
180: .getConnectionEditPart().getModel();
181: Box newTarget = (Box) getHost().getModel();
182: ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(
183: conn);
184: cmd.setNewTarget(newTarget);
185: return cmd;
186: }
187:
188: protected Command getReconnectSourceCommand(
189: ReconnectRequest request) {
190: Connection conn = (Connection) request
191: .getConnectionEditPart().getModel();
192: Box newSource = (Box) getHost().getModel();
193: ConnectionReconnectCommand cmd = new ConnectionReconnectCommand(
194: conn);
195: cmd.setNewSource(newSource);
196: return cmd;
197: }
198:
199: });
200:
201: List<BoxAction> list = PrintingPlugin
202: .getBoxExtensionActions(null);
203: for (BoxAction element : list) {
204: try {
205: Class<? extends BoxPrinter> class1 = ((Box) getModel())
206: .getBoxPrinter().getClass();
207: Class<? extends BoxPrinter> c = element
208: .getAcceptablePrinterClass(class1);
209: if (c != null && c.isAssignableFrom(class1)) {
210:
211: EditPolicy policyObj = element.getEditPolicy();
212: installEditPolicy(element.getRequest().getType(),
213: policyObj);
214:
215: }
216: } catch (Throwable e) {
217: continue;
218: }
219: }
220:
221: }
222:
223: protected IFigure createFigure() {
224: return new BoxFigure();
225: }
226:
227: public ConnectionAnchor getSourceConnectionAnchor(
228: ConnectionEditPart connection) {
229: return new ChopboxAnchor(getFigure());
230: }
231:
232: public ConnectionAnchor getSourceConnectionAnchor(Request request) {
233: return new ChopboxAnchor(getFigure());
234: }
235:
236: public ConnectionAnchor getTargetConnectionAnchor(
237: ConnectionEditPart connection) {
238: return new ChopboxAnchor(getFigure());
239: }
240:
241: public ConnectionAnchor getTargetConnectionAnchor(Request request) {
242: return new ChopboxAnchor(getFigure());
243: }
244:
245: protected class InternalPropertyListener extends PropertyListener
246: implements IPropertyChangeListener {
247:
248: protected void textChanged() {
249: refreshVisuals();
250: }
251:
252: protected void locationChanged() {
253: refreshVisuals();
254: }
255:
256: protected void sizeChanged() {
257: refreshVisuals();
258: }
259:
260: public void propertyChange(PropertyChangeEvent event) {
261: refreshVisuals();
262: }
263: }
264: }
|