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.policies;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.eclipse.draw2d.ColorConstants;
18  import org.eclipse.draw2d.FigureUtilities;
19  import org.eclipse.draw2d.Graphics;
20  import org.eclipse.draw2d.IFigure;
21  import org.eclipse.draw2d.RectangleFigure;
22  import org.eclipse.draw2d.geometry.Rectangle;
23  import org.eclipse.gef.Request;
24  import org.eclipse.gef.editpolicies.SelectionEditPolicy;
25  
26  import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
27  import sk.uniba.euromath.editor.textEditor.Interval;
28  import sk.uniba.euromath.editor.textEditor.requests.RequestConstants;
29  import sk.uniba.euromath.editor.textEditor.requests.selectionRequest.TextSelectRequest;
30  
31  /***
32   * Creates and cares about figures that represent selection. It cares about
33   * figures of two kinds
34   * <ul>
35   * <li> border of the selection - this figure is painted when the selections
36   * ends
37   * <li> filled selection - this figure is created when the selection starts and
38   * is justified when draging in method updateDraggingFeedback
39   * </ul>
40   * 
41   * Should be installed in EditPolicy.SELECTION_FEEDBACK_ROLE
42   * 
43   * @author Martin Kollar
44   */
45  abstract public class AbstractTextSelectionEditPolicy extends
46          SelectionEditPolicy {
47  
48      /***
49       * 
50       */
51      protected final List<IFigure> selectionFigures = new ArrayList<IFigure>();
52  
53      /***
54       * Constructor.
55       */
56      public AbstractTextSelectionEditPolicy() {
57          super();
58      }
59  
60      /***
61       * Creates Figure that is used when the selection is finished is used to
62       * show what is selected after selection is finished
63       * 
64       * @param start
65       * @param end
66       * 
67       * @return <code>RectangleFigure</code> that is painted
68       */
69      private IFigure createSelectionFigure(int start, int end) {
70          RectangleFigure result = new RectangleFigure();
71  
72          if (!getHostFigure().isShowing()) {
73              getHost().getViewer().reveal(getHost());
74          }
75  
76          Rectangle r = getHostKeeper().getTextLocator()
77                  .getTextBounds(start, end);
78  
79          if (r == null)
80              r = new Rectangle(0, 0, 0, 0);
81          result.setBounds(r);
82          result.setLineStyle(Graphics.LINE_SOLID);
83          result.setForegroundColor(ColorConstants.white);
84  
85          FigureUtilities.makeGhostShape(result);
86          return result;
87      }
88  
89      /***
90       * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#hideSelection()
91       */
92      @Override
93      protected void hideSelection() {
94          for (IFigure figure : this.selectionFigures) {
95              removeFeedback(figure);
96          }
97      }
98  
99      /***
100      * 
101      * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#showSelection()
102      */
103     @Override
104     protected void showSelection() {
105         for (IFigure figure : this.selectionFigures) {
106             addFeedback(figure);
107         }
108     }
109 
110     /***
111      * 
112      */
113     private void updateSelectionFigures() {
114         this.selectionFigures.clear();
115         for (Interval interval : getHostKeeper().getSelectionStatus()
116                 .getSelectionIntervals()) {
117             this.selectionFigures.add(createSelectionFigure(interval.getStart(),
118                     interval.getEnd()));
119         }
120     }
121 
122     @Override
123     public void showSourceFeedback(Request request) {
124         if (!request.getType().equals(RequestConstants.TEXT_SELECT))
125             throw new IllegalArgumentException();
126         hideSelection();
127         // set new selection state according to request
128         updateHostSelectionState((TextSelectRequest) request);
129         updateSelectionFigures();
130         showSelection();
131     }
132 
133     /***
134      * @param request
135      */
136     protected void updateHostSelectionState(TextSelectRequest request) {
137         if (request.getSelectType().equals(TextSelectRequest.NEW_SELECTION)) {
138             // set new selection
139             // TODO SELEKCIE posunut o offset ...
140             getHostKeeper().getSelectionStatus().setSelectionIntervals(
141                     request.getSelection());
142         } else {
143             // xor selections
144             getHostKeeper().getSelectionStatus().xorSelectionIntervals(
145                     request.getSelection());
146         }
147 
148     }
149 
150     /***
151      * Helper method.
152      * 
153      * @return
154      */
155     protected ITextPieceKeeper getHostKeeper() {
156         return (ITextPieceKeeper) getHost();
157     }
158 
159     /***
160      * @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
161      * 
162      * @return <code>true</code> if request is TextSelectionRequest else
163      *         return <code>false</code>
164      */
165     @Override
166     public boolean understandsRequest(Request req) {
167         if (RequestConstants.TEXT_SELECT.equals(req.getType())) {
168             return true;
169         }
170         return false;
171     }
172 
173 }