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  
13  package sk.uniba.euromath.editor.xmlEditor.policies;
14  
15  import org.eclipse.draw2d.ColorConstants;
16  import org.eclipse.draw2d.IFigure;
17  import org.eclipse.draw2d.LineBorder;
18  import org.eclipse.draw2d.RectangleFigure;
19  import org.eclipse.draw2d.geometry.Rectangle;
20  import org.eclipse.gef.EditPart;
21  import org.eclipse.gef.Request;
22  import org.eclipse.gef.RequestConstants;
23  import org.eclipse.gef.editpolicies.SelectionEditPolicy;
24  
25  import org.eclipse.swt.graphics.Color;
26  
27  import sk.uniba.euromath.editor.figures.EMImageFigure;
28  
29  /***
30   * Responsible for handling selection border
31   * 
32   * @author Martin Kollar 10.9.2005
33   */
34  public class FigureSelectionPolicy extends SelectionEditPolicy {
35  
36      /*** constant that gives width of the selection rectangle */
37      public static final int SELECION_BORDER_WIDTH = 3;
38  
39      /***
40       * Holds background color of host, needed when returning host's state before
41       * highlight.
42       */
43      private Color revertColor;
44  
45      /***
46       * Figure used to display selection around host.
47       */
48      private IFigure feedback;
49  
50      /***
51       * Highlight flag.
52       */
53      private boolean showHighlight = true;
54  
55      /***
56       * Holds opaque state of host, needed when returning host's state before
57       * highlight.
58       */
59      private boolean wasOpaque;
60  
61      /***
62       * Construcotr. Creates new policy.
63       */
64      public FigureSelectionPolicy() {
65          super();
66      }
67  
68      /***
69       * @param showHighlight
70       *            if <code>true</code> then host is highlighted when cursor of
71       *            mouse is over host
72       */
73      public FigureSelectionPolicy(boolean showHighlight) {
74          this();
75          this.showHighlight = showHighlight;
76      }
77  
78      /***
79       * @return Figure that is selection border
80       */
81      protected IFigure createFeedBack() {
82          RectangleFigure rFig = new RectangleFigure();
83          IFigure orig = getHostFigure();
84  
85          if (orig instanceof EMImageFigure) {
86              Rectangle rectBounds = orig.getBounds().getCopy();
87              // rectBounds.width -= 3;
88              // rectBounds.height -= 3;
89              // rectBounds.expand(3, 3);
90              // rectBounds.translate(3,3);
91              // rectBounds.shrink(3, 3);
92  
93              rFig.setBounds(rectBounds);
94              rFig.setBorder(new LineBorder(ColorConstants.lightBlue,
95                      SELECION_BORDER_WIDTH));
96              rFig.setFill(false);
97          } else {
98              Rectangle rectBounds = orig.getBounds().getCopy();
99              rectBounds.expand(SELECION_BORDER_WIDTH, SELECION_BORDER_WIDTH);
100             // rectBounds.translate(3, 3);
101             // rectBounds.shrink(3, 3);
102 
103             rFig.setBounds(rectBounds);
104             rFig.setBorder(new LineBorder(ColorConstants.darkBlue,
105                     SELECION_BORDER_WIDTH));
106             rFig.setFill(false);
107         }
108 
109         return rFig;
110     }
111 
112     /***
113      * @param request
114      *            not need to be SelectionRequest
115      */
116     @Override
117     public void eraseTargetFeedback(Request request) {
118         if (this.revertColor != null) {
119             getHostFigure().setOpaque(this.wasOpaque);
120             setHostBackground(this.revertColor);
121             this.revertColor = null;
122         }
123     }
124 
125     /***
126      * Returns host's background color.
127      * 
128      * @return host's background color
129      */
130     private Color getHostBackground() {
131         return getHostFigure().getBackgroundColor();
132     }
133 
134     /***
135      * Sets host's background color.
136      * 
137      * @param c
138      *            color to set
139      */
140     private void setHostBackground(Color c) {
141         getHostFigure().setBackgroundColor(c);
142     }
143 
144     /***
145      * @param request
146      * @return host or <code>null</code>
147      */
148     @Override
149     public EditPart getTargetEditPart(Request request) {
150         return request.getType().equals(RequestConstants.REQ_SELECTION_HOVER) ? getHost()
151                 : null;
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#hideSelection()
158      */
159     @Override
160     protected void hideSelection() {
161         if (this.feedback != null) {
162             removeFeedback(this.feedback);
163             this.feedback = null;
164         }
165     }
166 
167     /***
168      * Highlights host - figure associated with editpart where policy is
169      * installed.
170      */
171     protected void showHighlight() {
172         if (!this.showHighlight)
173             return;
174 
175         if (this.revertColor == null) {
176             this.revertColor = getHostBackground();
177             this.wasOpaque = getHostFigure().isOpaque();
178             getHostFigure().setOpaque(true);
179             setHostBackground(ColorConstants.titleInactiveBackground);
180         }
181     }
182 
183     /*
184      * (non-Javadoc)
185      * 
186      * @see org.eclipse.gef.editpolicies.SelectionEditPolicy#showSelection()
187      */
188     @Override
189     protected void showSelection() {
190         if (this.feedback != null)
191             hideSelection();
192         this.feedback = createFeedBack();
193         if (this.feedback != null)
194             addFeedback(this.feedback);
195     }
196 
197     /***
198      * Shows the selection feedback
199      * 
200      * @param request
201      *            should by SelectionRequest
202      */
203     @Override
204     public void showTargetFeedback(Request request) {
205         if (request.getType().equals(RequestConstants.REQ_SELECTION)) {
206             showHighlight();
207             getHost().refresh();
208         }
209     }
210 
211 }