blob: 79f00627320b4ee2a1dcf63191e3166c6e320fdf [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.adt.design.editparts;
kchong38cbf172006-03-29 03:38:21 +000012
13import org.eclipse.draw2d.IFigure;
14import org.eclipse.draw2d.geometry.Point;
15import org.eclipse.draw2d.geometry.Rectangle;
16import org.eclipse.gef.LayerConstants;
17import org.eclipse.gef.commands.Command;
18import org.eclipse.gef.requests.DirectEditRequest;
19import org.eclipse.swt.widgets.Display;
kchong2be71b32006-04-11 16:32:03 +000020import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.model.IFeedbackHandler;
21import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.IADTUpdateCommand;
22import org.eclipse.wst.xsd.ui.internal.adt.facade.IType;
kchong38cbf172006-03-29 03:38:21 +000023
24/**
25 * This class provides some base function to enable drawing connections to a referenced type
26 *
27 */
28public abstract class BaseTypeConnectingEditPart extends BaseEditPart implements IFeedbackHandler
29{
30 private TypeReferenceConnection connectionFigure;
31
32 public void activate()
33 {
34 super.activate();
35 // TODO (cs) revisit this, perhaps has Randy for his advice
36 // it seems that the edit parts required for the target end of the connections
37 // are not always layed available when active is called. So we need to delay
38 // the activateConnection() until a short time later. For now the only way I can think
39 // of doing this is via an asyncExec.
40 //
41 Display.getCurrent().asyncExec(new Runnable()
42 {
43 public void run()
44 {
45 if (isActive())
46 {
47 activateConnection();
48 }
49 }
50 });
51 }
52
53 public void deactivate()
54 {
55 deactivateConnection();
56 super.deactivate();
57 }
58
59 protected void activateConnection()
60 {
61 // If appropriate, create our connectionFigure and add it to the appropriate layer
62 if (connectionFigure == null && shouldDrawConnection())
63 {
64 connectionFigure = createConnectionFigure();
65 if (connectionFigure != null)
66 {
67 // Add our editpolicy as a listener on the connection, so it can stay in synch
68 //connectionFigure.addPropertyChangeListener((AttributeSelectionFeedbackPolicy) getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE));
69 //connectionFigure.addMouseListener(this);
70 getLayer(LayerConstants.CONNECTION_LAYER).add(connectionFigure);
71 }
72 }
73 }
74
75 protected void deactivateConnection()
76 {
77 // if we have a connection, remove it
78 if (connectionFigure != null)
79 {
80 getLayer(LayerConstants.CONNECTION_LAYER).remove(connectionFigure);
81 // Remove our editpolicy listener(s)
82 //connectionFigure.removePropertyChangeListener((AttributeSelectionFeedbackPolicy) getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE));
83 //connectionFigure.removeMouseListener(this);
84 connectionFigure = null;
85 }
86 }
87
88 protected boolean shouldDrawConnection()
89 {
90 return true;
91 }
92
93 public abstract TypeReferenceConnection createConnectionFigure();
94
95 public void addFeedback()
96 {
97 if (connectionFigure != null)
98 {
99 connectionFigure.setHighlight(true);
100 }
101 }
102
103 public void removeFeedback()
104 {
105 if (connectionFigure != null)
106 {
107 connectionFigure.setHighlight(false);
108 }
109 }
110
111 protected class NameUpdateCommandWrapper extends Command implements IADTUpdateCommand
112 {
113 Command command;
114 protected DirectEditRequest request;
115
116 public NameUpdateCommandWrapper()
117 {
118 super("Update Name");
119 }
120
121 public void setRequest(DirectEditRequest request)
122 {
123 this.request = request;
124 }
125
126 public void execute()
127 {
128 IType iType = (IType)getModel();
129 Object newValue = request.getCellEditor().getValue();
130 if (newValue instanceof String)
131 {
132 command = iType.getUpdateNameCommand((String)newValue);
133 }
134 if (command != null)
135 command.execute();
136 }
137 }
138
139 public boolean hitTest(IFigure target, Point location)
140 {
141 Rectangle b = target.getBounds().getCopy();
142 target.translateToAbsolute(b);
143 return b.contains(location);
144 }
145}