View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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                  // model must correspond with child model from parent editpart
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                  // get direct children
95                  List<IEMFigure> descendants = new ArrayList<IEMFigure>();
96                  descendants.addAll(getFigure().getChildren());
97                  IEMFigure figure;
98                  // Depth Search
99                  while (!(descendants.isEmpty())) {
100                         figure = descendants.get(0);
101                         descendants.remove(0);
102                         // figure needs EditPart, so is added to result
103                         if (figure.needsEditPart()) {
104                                 result.add(figure);
105                                 // figure doesn't need EditPart, so its children
106                                 // should be
107                                 // processed
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          * (non-Javadoc)
157          * @see sk.uniba.euromath.editor.xmlEditor.IXMLEditPart#getID()
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                 // first detach child figure from parent
180                 if (child.getParent() != null)
181                         child.getParent().remove(child);
182                 // specialy handle index -1, what means to add it to end
183                 if (index == -1)
184                         getFigure().add(child);
185                 int children = getContentPane().getChildren().size();
186                 // countContentPaneChildren is equal to number of child
187                 // editparts except
188                 // that which figure is now addeing as child
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 }