View Javadoc

1   package sk.uniba.euromath.editor.xmlEditor.tools;
2   
3   import org.eclipse.core.runtime.IStatus;
4   import org.eclipse.gef.Request;
5   import org.eclipse.gef.tools.DragEditPartsTracker;
6   import org.w3c.dom.DOMException;
7   import org.w3c.dom.Node;
8   
9   import sk.baka.ikslibs.interval.DOMInterval;
10  import sk.baka.ikslibs.interval.DOMIntervalSet;
11  import sk.baka.ikslibs.ptr.DomPointerFactory;
12  import sk.uniba.euromath.EuroMath;
13  import sk.uniba.euromath.document.XMLAccess;
14  import sk.uniba.euromath.editor.xmlEditor.XMLEditDomain;
15  import sk.uniba.euromath.editor.xmlEditor.editParts.XMLEditPart;
16  import sk.uniba.euromath.editor.xmlEditor.viewers.IXMLGraphicalViewer;
17  
18  /***
19   * Drag tracker handling start-point end-point drags to select all nodes in
20   * document interval created for nodes of start, end editpart. Preview selection
21   * is showed all time of drag and at end of drag is commited to viewer as final.
22   * 
23   * @author Martin Kollar 23.09.2005
24   */
25  public class XMLStructureDragTracker extends DragEditPartsTracker {
26  
27      /***
28       * Selection which is showed.
29       */
30      private DOMIntervalSet selection;
31  
32      /***
33       * Constructs new XMLDragTracker with the given XMLEditPart
34       * 
35       * @param sourceEditPart
36       *            source editPart where selection starts
37       */
38      public XMLStructureDragTracker(XMLEditPart sourceEditPart) {
39          super(sourceEditPart);
40          // TODO Studva maybe not necessary
41          getCurrentViewer().setFocus(sourceEditPart);
42          this.selection = new DOMIntervalSet();
43      }
44  
45      protected XMLAccess getXMLAccess() {
46          return ((XMLEditDomain) getDomain()).getXMLEditor().getXMLAccess();
47      }
48  
49      protected IXMLGraphicalViewer getXMLGraphicalViewer() {
50          return (IXMLGraphicalViewer) getCurrentViewer();
51      }
52  
53      /***
54       * For selecting is not needed any request, so leave default implementation
55       * to avoid over implementing and errors.
56       */
57      @Override
58      protected Request createTargetRequest() {
59          return super.createTargetRequest();
60      }
61  
62      @Override
63      protected boolean handleDragInProgress() {
64          if (isInState(STATE_DRAG_IN_PROGRESS
65          /* | STATE_ACCESSIBLE_DRAG_IN_PROGRESS */)) {
66              updateTargetUnderMouse();
67          }
68          return true;
69      }
70  
71      @Override
72      protected boolean handleExitingEditPart() {
73          prepareSelection();
74          getXMLGraphicalViewer().showSelection(this.selection);
75          return true;
76      }
77  
78      @Override
79      protected boolean handleEnteredEditPart() {
80          prepareSelection();
81          getXMLGraphicalViewer().showSelection(this.selection);
82          return true;
83      }
84  
85      @Override
86      protected boolean handleButtonDown(int button) {
87          // selecting is controlled by left mouse button drags
88          if (button != 1) {
89              // TODO Studva state invalid - how it is controlled if to handle as
90              // drag in selected tool?
91              setState(STATE_INVALID);
92              return false;
93          }
94  
95          stateTransition(STATE_INITIAL, STATE_DRAG);
96          return true;
97      }
98  
99      /***
100      * Prepares selection object to show or perform. Selection object is made
101      * current for state of this drag tracker after method execution.
102      */
103     protected void prepareSelection() {
104         Node startNode = null;
105         Node endNode = null;
106         try {
107             startNode = getXMLAccess().getIdManager().getNode(
108                     ((XMLEditPart) getSourceEditPart()).getID()).item(0);
109             endNode = getXMLAccess().getIdManager().getNode(
110                     ((XMLEditPart) getTargetEditPart()).getID()).item(0);
111         } catch (DOMException e) {
112             EuroMath.log(IStatus.ERROR, 0, "Invalid state of Euromath.", e);
113         }
114 
115         this.selection = null;// getXMLGraphicalViewer().getSelection();
116 
117         // nothing pressed
118         if (!getCurrentInput().isAnyButtonDown()) {
119             this.selection = new DOMIntervalSet(new DOMInterval(
120                     DomPointerFactory.create(startNode), DomPointerFactory
121                             .create(endNode)));
122         }
123 
124         // ctrl pressed
125         if (getCurrentInput().isControlKeyDown()) {
126             // FIXME Studva
127             this.selection.subtract(new DOMInterval(DomPointerFactory
128                     .create(startNode), DomPointerFactory.create(endNode)));
129         }
130 
131         // shift pressed
132         if (getCurrentInput().isShiftKeyDown()) {
133             // FIXME Studva
134             // TODO
135             this.selection = new DOMIntervalSet(new DOMInterval(
136                     DomPointerFactory.create(startNode), DomPointerFactory
137                             .create(endNode)));
138         }
139 
140         // ctrl + shift pressed
141         if (getCurrentInput().isShiftKeyDown()
142                 && getCurrentInput().isShiftKeyDown()) {
143             // FIXME Studva
144             this.selection.union(new DOMInterval(DomPointerFactory.create(startNode),
145                             DomPointerFactory.create(endNode)));
146 
147         }
148 
149     }
150 
151     @Override
152     protected void performSelection() {
153         if (hasSelectionOccurred())
154             return;
155         setFlag(FLAG_SELECTION_PERFORMED, true);
156         getXMLGraphicalViewer().setSelection(this.selection);
157 
158     }
159 
160 }