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.editpolicies.SelectionEditPolicy;
24  
25  import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
26  import sk.uniba.euromath.editor.textEditor.Interval;
27  
28  /***
29   * Creates and cares about figures that represent selection. It cares about
30   * figures of two kinds
31   * <ul>
32   * <li> border of the selection - this figure is painted when the selections
33   * ends
34   * <li> filled selection - this figure is created when the selection starts and
35   * is justified when draging in method updateDraggingFeedback
36   * </ul>
37   * 
38   * Should be installed in EditPolicy.SELECTION_FEEDBACK_ROLE
39   * 
40   * @author Martin Kollar
41   */
42  public class TextSelectionEditPolicy extends
43                  SelectionEditPolicy {
44  
45          /***
46           * List of displayed(shown) selection figures.
47           */
48          protected final List<IFigure> selectionFigures = new ArrayList<IFigure>();
49  
50          /***
51           * Constructor.
52           */
53          public TextSelectionEditPolicy() {
54                  super();
55          }
56  
57          /***
58           * Creates Figure sourrounding keepers text of interval.
59           * 
60           * @param start
61           *                index of first character bounded by figure
62           * @param end
63           *                index of last character bounded by figure
64           * 
65           * @return <code>RectangleFigure</code> that is painted
66           */
67          private IFigure createSelectionFigure(int start, int end) {
68                  RectangleFigure result = new RectangleFigure();
69  
70                  if (!getHostFigure().isShowing()) {
71                          getHost().getViewer().reveal(getHost());
72                  }
73  
74                  Rectangle r = getHostKeeper().getTextLocator().getTextBounds(
75                                  start, end);
76  
77                  if (r == null)
78                          r = new Rectangle(0, 0, 0, 0);
79                  result.setBounds(r);
80                  result.setLineStyle(Graphics.LINE_SOLID);
81                  result.setForegroundColor(ColorConstants.white);
82  
83                  FigureUtilities.makeGhostShape(result);
84                  return result;
85          }
86  
87          /***
88           * Hides selection.
89           */
90          @Override
91          protected void hideSelection() {
92                  for (IFigure figure : this.selectionFigures) {
93                          removeFeedback(figure);
94                  }
95          }
96  
97          /***
98           * Shows selection.
99           */
100         @Override
101         protected void showSelection() {
102                 for (IFigure figure : this.selectionFigures) {
103                         addFeedback(figure);
104                 }
105         }
106 
107         /***
108          * Updates selection figures according to keeper's selection status.
109          */
110         private void updateSelectionFigures() {
111                 this.selectionFigures.clear();
112                 for (Interval interval : getHostKeeper().getSelectionStatus()
113                                 .getSelectionIntervals()) {
114                         this.selectionFigures
115                                         .add(createSelectionFigure(interval
116                                                         .getStart(), interval
117                                                         .getEnd()));
118                 }
119         }
120 
121         // TODO Studva temporary solution
122         @Override
123         protected void setSelectedState(int type) {
124                 hideSelection();
125                 updateSelectionFigures();
126                 showSelection();
127         }
128 
129         /***
130          * Helper method.
131          * 
132          * @return
133          */
134         protected ITextPieceKeeper getHostKeeper() {
135                 return (ITextPieceKeeper) getHost();
136         }
137 }