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