View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.textEditor.tools;
13  
14  import org.eclipse.draw2d.geometry.Point;
15  import org.eclipse.gef.EditPart;
16  import org.eclipse.gef.Request;
17  import org.eclipse.swt.events.KeyEvent;
18  
19  import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
20  import sk.uniba.euromath.editor.textEditor.requests.editTextRequests.ActivateTextEditingRequest;
21  import sk.uniba.euromath.editor.xmlEditor.tools.XMLStructureTool;
22  
23  /***
24   * Extends from XMLStructureTool and adds functionality of switching to
25   * TextTool by pressing CTRL+T
26   * @author Martin Kollar Created on 6.6.2005
27   */
28  
29  public class TextStructureTool extends XMLStructureTool {
30  
31      /***
32       * location where this tool was last time used, this needs to be stored
33       * because of switching into TextTool
34       */
35      private Point lastLocation = null;
36      
37  	/*** EditPart i that was Caret befor switching in this tool **/
38  	private EditPart lastActive;
39  	
40  	/***
41  	 * This constructor is called when this is the first Tool
42  	 * and so no TextEditPart was active before creating thos tool
43  	 */
44      public TextStructureTool() {
45          super();
46      }
47      
48      /***
49       * @param location from what location is this tool created
50       * @param lastActive EditPart in that was Caret
51       */
52      public TextStructureTool(Point location, EditPart lastActive) {
53          this();
54          setLastLocation(location);
55          setLastActive(lastActive);
56      }
57      
58      protected boolean handleKeyDown(KeyEvent event) {
59      	return super.handleKeyDown(event);
60      }
61      
62      protected boolean handleKeyUp(KeyEvent e) {
63          // 116 = CTRL + t
64      	removeKey(e.keyCode);
65      	
66          if (e.keyCode == 116) {
67          	EditPart targetEditPart;
68          	
69          	if ( (getLastActive() != null) && (getLastActive() instanceof ITextPieceKeeper) )
70          		targetEditPart = getLastActive();
71          	else
72                 targetEditPart = getCurrentViewer().findObjectAt(
73                         getLastLocation());
74          	
75              if ( (targetEditPart != null) && (targetEditPart instanceof ITextPieceKeeper) ) {
76              	
77                  Point loc = getLocation().getCopy();
78                  ((ITextPieceKeeper)targetEditPart).getTextLocator().translateToRelative(loc);
79                  int caretIndex = ((ITextPieceKeeper)targetEditPart).getTextLocator().getCharIndexAtPos(loc);
80              	
81                  Request activation = new ActivateTextEditingRequest((ITextPieceKeeper)targetEditPart,caretIndex,
82                  		getCurrentViewer().getEditDomain());
83              	
84                  targetEditPart.performRequest(activation);
85              }
86  
87          }
88          return super.handleKeyUp(e);
89      }
90      
91      /***
92       * Activates text editing. Activates TextTool.
93       * @see org.eclipse.gef.tools.AbstractTool#handleDoubleClick(int)
94       * 
95       * @param button which button was double-clicked
96       */
97      protected boolean handleDoubleClick(int button) {
98          EditPart caretTarget = null;
99          if (button == 1) {
100             caretTarget = getCurrentViewer().findObjectAt(getLocation());
101             if (caretTarget != null) {
102                 Point loc = getLocation().getCopy();
103                 ((ITextPieceKeeper)caretTarget).getTextLocator().translateToRelative(loc);
104                 int caretIndex = ((ITextPieceKeeper)caretTarget).getTextLocator().getCharIndexAtPos(loc);
105             	
106                 Request activation = new ActivateTextEditingRequest((ITextPieceKeeper)caretTarget,caretIndex,
107                 		getCurrentViewer().getEditDomain());
108             	            	
109 				caretTarget.performRequest(activation);
110                 return true;
111             }
112         }
113         return false;
114     }
115     
116     protected boolean handleButtonUp(int button) {
117         setLastLocation( getLocation() );
118         return super.handleButtonUp(button);
119     }
120     
121 
122 	protected EditPart getLastActive() {
123 		return lastActive;
124 	}
125 
126 	protected void setLastActive(EditPart lastActive) {
127 		this.lastActive = lastActive;
128 	}
129 	
130     /*** @return location where tool was last time used */
131     public Point getLastLocation() {
132         return this.lastLocation;
133     }
134     
135     protected void setLastLocation(Point location){
136     	this.lastLocation = location;
137     }
138 
139 }