1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.editParts;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.draw2d.IFigure;
18 import org.eclipse.gef.DragTracker;
19 import org.eclipse.gef.EditPart;
20 import org.eclipse.gef.EditPolicy;
21 import org.eclipse.gef.GraphicalEditPart;
22 import org.eclipse.gef.Request;
23 import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
24
25 import sk.uniba.euromath.editor.figures.IEMFigure;
26 import sk.uniba.euromath.editor.xmlEditor.IXMLEditPart;
27 import sk.uniba.euromath.editor.xmlEditor.XMLEditPartFactory;
28 import sk.uniba.euromath.editor.xmlEditor.policies.FigureSelectionPolicy;
29 import sk.uniba.euromath.editor.xmlEditor.tools.XMLStructureDragTracker;
30
31 /***
32 * The only editpart which uses xmlEditor. One speciality is, that model of this
33 * editpart is IEMFigure, because is created for IEMFigure, which needsEditPart
34 * and as childModels return list of IEMFigures.
35 *
36 * @author Tomáš Studva , Martin Kollar 31.7.2005
37 */
38 public class XMLEditPart extends AbstractGraphicalEditPart implements IXMLEditPart {
39
40 /***
41 * Id of node from source document corresponding to this EditPart.
42 */
43 protected String id;
44
45 /***
46 * Holds model children - figures which needs editparts.
47 */
48 private final List<IEMFigure> modelChildren;
49
50 /***
51 * Selectable flag. True if this editpart can be selected.
52 */
53 private boolean isSelectable;
54
55 /***
56 * Constructor.
57 *
58 * @param figure
59 * not null
60 * @param selectable
61 * if <true> then this EditPart can be selected
62 */
63 public XMLEditPart(IEMFigure figure, Boolean selectable) {
64 super();
65 setFigure(figure);
66
67 setModel(figure);
68 setID(figure.getID());
69 setSelectability(selectable);
70 this.modelChildren = computeModelChildren();
71 }
72
73 /***
74 * @see org.eclipse.gef.EditPart#getTargetEditPart(org.eclipse.gef.Request)
75 */
76 @Override
77 public EditPart getTargetEditPart(Request request) {
78 EditPart result = super.getTargetEditPart(request);
79 if ((result == null) && (getParent() instanceof IXMLEditPart))
80 result = getParent().getTargetEditPart(request);
81
82 return result;
83 }
84
85 /***
86 * @return By depth search returns descendants figures which needs
87 * EditPart.
88 */
89 @SuppressWarnings("unchecked")
90 protected List<IEMFigure> computeModelChildren() {
91 List<IEMFigure> result = new ArrayList<IEMFigure>();
92 if (getFigure() == null)
93 throw new IllegalStateException();
94
95 List<IEMFigure> descendants = new ArrayList<IEMFigure>();
96 descendants.addAll(getFigure().getChildren());
97 IEMFigure figure;
98
99 while (!(descendants.isEmpty())) {
100 figure = descendants.get(0);
101 descendants.remove(0);
102
103 if (figure.needsEditPart()) {
104 result.add(figure);
105
106
107
108 } else {
109 descendants.addAll(0, figure.getChildren());
110 }
111 }
112 return result;
113 }
114
115 /***
116 * Returns XMLEditPartFactory.
117 *
118 * @return XMLEditPartFactory
119 */
120 protected XMLEditPartFactory getXMLEditPartFactory() {
121 return (XMLEditPartFactory) (getViewer().getEditPartFactory());
122 }
123
124 /***
125 * @return Returns precomputed model children - figures which needs
126 * editparts.
127 */
128 @Override
129 protected List getModelChildren() {
130 return this.modelChildren;
131 }
132
133 /***
134 * Due to our architecture, EditParts don't creates figures, they uses
135 * figures from renderer.
136 *
137 * @return null
138 */
139 @Override
140 protected IFigure createFigure() {
141 return null;
142 }
143
144 @Override
145 /***
146 * Every XMLEditPart can by selected, so we instal Policy that cares
147 * about selection border
148 */
149 protected void createEditPolicies() {
150 installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,
151 new FigureSelectionPolicy(false));
152
153 }
154
155
156
157
158
159 public String getID() {
160 return this.id;
161 }
162
163 /***
164 * @param id
165 * sets id
166 */
167 public void setID(String id) {
168 this.id = id;
169 }
170
171 @Override
172 protected void refreshVisuals() {
173 getFigure().repaint();
174 }
175
176 @Override
177 protected void addChildVisual(EditPart childEditPart, int index) {
178 IFigure child = ((GraphicalEditPart) childEditPart).getFigure();
179
180 if (child.getParent() != null)
181 child.getParent().remove(child);
182
183 if (index == -1)
184 getFigure().add(child);
185 int children = getContentPane().getChildren().size();
186
187
188
189 int countContentPaneChildren = getChildren().size() - 1;
190 getContentPane().add(child,
191 index + children - countContentPaneChildren);
192 }
193
194 @Override
195 protected void removeChildVisual(EditPart childEditPart) {
196 IFigure child = ((GraphicalEditPart) childEditPart).getFigure();
197 getContentPane().remove(child);
198 }
199
200 /***
201 * @return XMLDragTracker
202 * @param request
203 * can be anything, the result does not depent on request
204 */
205 @Override
206 public DragTracker getDragTracker(Request request) {
207 return new XMLStructureDragTracker(this);
208 }
209
210 /***
211 * Sets if the editPart can be selected or no.
212 *
213 * @param value
214 * if <code>true<code> then the EditPart can be selected
215 */
216 public void setSelectability(Boolean value) {
217 this.isSelectable = value;
218 }
219
220 /***
221 * @return <code>true<code> if the EditPart can be selected, used when clicking on this editPart
222 * @see org.eclipse.gef.editparts.AbstractEditPart#isSelectable()
223 */
224 @Override
225 public boolean isSelectable() {
226 return this.isSelectable;
227 }
228
229 @Override
230 public String toString() {
231 return "XMLEditPart[id: " + this.id + ", selectable: "
232 + this.isSelectable + "]";
233 }
234
235 }