KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > gvt > TextSelectionManager


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.swing.gvt;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Cursor JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Shape JavaDoc;
27 import java.awt.geom.AffineTransform JavaDoc;
28
29 import org.apache.batik.gvt.Selectable;
30 import org.apache.batik.gvt.event.AWTEventDispatcher;
31 import org.apache.batik.gvt.event.GraphicsNodeMouseEvent;
32 import org.apache.batik.gvt.event.GraphicsNodeMouseListener;
33 import org.apache.batik.gvt.event.SelectionEvent;
34 import org.apache.batik.gvt.event.SelectionListener;
35 import org.apache.batik.gvt.text.ConcreteTextSelector;
36 import org.apache.batik.gvt.text.Mark;
37
38 /**
39  * This class represents an object which manage GVT text nodes selection.
40  *
41  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
42  * @version $Id: TextSelectionManager.java,v 1.26 2005/03/15 11:24:35 deweese Exp $
43  */

44 public class TextSelectionManager {
45
46     /**
47      * The cursor indicating that a text selection operation is under way.
48      */

49     public final static Cursor JavaDoc TEXT_CURSOR = new Cursor JavaDoc(Cursor.TEXT_CURSOR);
50
51     /**
52      * The text selector.
53      */

54     protected ConcreteTextSelector textSelector;
55
56     /**
57      * The associated JGVTComponent.
58      */

59     protected JGVTComponent component;
60
61     /**
62      * The selection overlay.
63      */

64     protected Overlay selectionOverlay = new SelectionOverlay();
65
66     /**
67      * The mouse listener.
68      */

69     protected MouseListener mouseListener;
70
71     /**
72      * To store the previous cursor.
73      */

74     protected Cursor JavaDoc previousCursor;
75
76     /**
77      * The selection highlight.
78      */

79     protected Shape JavaDoc selectionHighlight;
80
81     /**
82      * The text selection listener.
83      */

84     protected SelectionListener textSelectionListener;
85
86     /**
87      * The color of the selection overlay.
88      */

89     protected Color JavaDoc selectionOverlayColor = new Color JavaDoc(100, 100, 255, 100);
90
91     /**
92      * The color of the outline of the selection overlay.
93      */

94     protected Color JavaDoc selectionOverlayStrokeColor = new Color JavaDoc(255, 255, 255, 255);
95
96     /**
97      * A flag bit that indicates whether or not the selection overlay is
98      * painted in XOR mode.
99      */

100     protected boolean xorMode = false;
101
102     /**
103      * The current selection or null if there is none.
104      */

105     Object JavaDoc selection = null;
106
107     /**
108      * Creates a new TextSelectionManager.
109      */

110     public TextSelectionManager(JGVTComponent comp,
111                                 AWTEventDispatcher ed) {
112         textSelector = new ConcreteTextSelector();
113         textSelectionListener = new TextSelectionListener();
114         textSelector.addSelectionListener(textSelectionListener);
115         mouseListener = new MouseListener();
116
117         component = comp;
118         component.getOverlays().add(selectionOverlay);
119
120         ed.addGraphicsNodeMouseListener(mouseListener);
121     }
122
123     /**
124      * Add a selection listener to be notified when the
125      * text selection changes in the document.
126      */

127     public void addSelectionListener(SelectionListener sl) {
128         textSelector.addSelectionListener(sl);
129     }
130
131     /**
132      * Remove a selection listener to be notified when the
133      * text selection changes in the document.
134      */

135     public void removeSelectionListener(SelectionListener sl) {
136         textSelector.removeSelectionListener(sl);
137     }
138
139     /**
140      * Sets the color of the selection overlay to the specified color.
141      *
142      * @param color the new color of the selection overlay
143      */

144     public void setSelectionOverlayColor(Color JavaDoc color) {
145     this.selectionOverlayColor = color;
146     }
147
148     /**
149      * Returns the color of the selection overlay.
150      */

151     public Color JavaDoc getSelectionOverlayColor() {
152     return selectionOverlayColor;
153     }
154
155     /**
156      * Sets the color of the outline of the selection overlay to the specified
157      * color.
158      *
159      * @param color the new color of the outline of the selection overlay
160      */

161     public void setSelectionOverlayStrokeColor(Color JavaDoc color) {
162     this.selectionOverlayStrokeColor = color;
163     }
164
165     /**
166      * Returns the color of the outline of the selection overlay.
167      */

168     public Color JavaDoc getSelectionOverlayStrokeColor() {
169     return selectionOverlayStrokeColor;
170     }
171
172     /**
173      * Sets whether or not the selection overlay will be painted in XOR mode,
174      * depending on the specified parameter.
175      *
176      * @param state true implies the selection overlay will be in XOR mode
177      */

178     public void setSelectionOverlayXORMode(boolean state) {
179     this.xorMode = state;
180     }
181
182     /**
183      * Returns true if the selection overlay is painted in XOR mode, false
184      * otherwise.
185      */

186     public boolean isSelectionOverlayXORMode() {
187     return xorMode;
188     }
189
190     /**
191      * Returns the selection overlay.
192      */

193     public Overlay getSelectionOverlay() {
194         return selectionOverlay;
195     }
196
197     /**
198      * Returns the current text selection or null if there is none.
199      */

200     public Object JavaDoc getSelection() {
201         return selection;
202     }
203
204     /**
205      * Sets the selected text
206      */

207     public void setSelection(Mark start, Mark end) {
208         textSelector.setSelection(start, end);
209     }
210
211     /**
212      * Clears the selection.
213      */

214     public void clearSelection() {
215     textSelector.clearSelection();
216     }
217
218     /**
219      * To implement a GraphicsNodeMouseListener.
220      */

221     protected class MouseListener implements GraphicsNodeMouseListener {
222         public void mouseClicked(GraphicsNodeMouseEvent evt) {
223             if (evt.getSource() instanceof Selectable) {
224                 textSelector.mouseClicked(evt);
225             }
226         }
227
228         public void mousePressed(GraphicsNodeMouseEvent evt) {
229             if (evt.getSource() instanceof Selectable) {
230                 textSelector.mousePressed(evt);
231             } else if (selectionHighlight != null) {
232                 textSelector.clearSelection();
233             }
234         }
235
236         public void mouseReleased(GraphicsNodeMouseEvent evt) {
237             textSelector.mouseReleased(evt);
238         }
239
240         public void mouseEntered(GraphicsNodeMouseEvent evt) {
241             if (evt.getSource() instanceof Selectable) {
242                 textSelector.mouseEntered(evt);
243                 previousCursor = component.getCursor();
244                 if (previousCursor.getType() == Cursor.DEFAULT_CURSOR) {
245                     component.setCursor(TEXT_CURSOR);
246                 }
247             }
248         }
249
250         public void mouseExited(GraphicsNodeMouseEvent evt) {
251             if (evt.getSource() instanceof Selectable) {
252                 textSelector.mouseExited(evt);
253                 if (component.getCursor() == TEXT_CURSOR) {
254                     component.setCursor(previousCursor);
255                 }
256             }
257         }
258
259         public void mouseDragged(GraphicsNodeMouseEvent evt) {
260             if (evt.getSource() instanceof Selectable) {
261                 textSelector.mouseDragged(evt);
262             }
263         }
264
265         public void mouseMoved(GraphicsNodeMouseEvent evt) { }
266     }
267
268     /**
269      * To implements a selection listener.
270      */

271     protected class TextSelectionListener implements SelectionListener {
272         public void selectionDone(SelectionEvent e) {
273             selectionChanged(e);
274             selection = e.getSelection();
275         }
276         public void selectionCleared(SelectionEvent e) {
277             selectionStarted(e);
278         }
279         public void selectionStarted(SelectionEvent e) {
280             if (selectionHighlight != null) {
281                 Rectangle JavaDoc r = getHighlightBounds();
282                 selectionHighlight = null;
283                 component.repaint(r);
284             }
285             selection = null;
286         }
287         public void selectionChanged(SelectionEvent e) {
288             Rectangle JavaDoc r = null;
289             AffineTransform JavaDoc at = component.getRenderingTransform();
290             if (selectionHighlight != null) {
291                 r = at.createTransformedShape(selectionHighlight).getBounds();
292                 outset(r, 1);
293             }
294
295             selectionHighlight = e.getHighlightShape();
296             if (selectionHighlight != null) {
297                 if (r != null) {
298                     Rectangle JavaDoc r2 = getHighlightBounds();
299                     component.repaint(r.union(r2));
300                 } else {
301                     component.repaint(getHighlightBounds());
302                 }
303             } else if (r != null) {
304                 component.repaint(r);
305             }
306         }
307
308     }
309
310     protected Rectangle JavaDoc outset(Rectangle JavaDoc r, int amount) {
311         r.x -= amount;
312         r.y -= amount;
313         r.width += 2*amount;
314         r.height += 2*amount;
315         return r;
316     }
317
318     /**
319      * The highlight bounds.
320      */

321     protected Rectangle JavaDoc getHighlightBounds() {
322         AffineTransform JavaDoc at = component.getRenderingTransform();
323         Shape JavaDoc s = at.createTransformedShape(selectionHighlight);
324         return outset(s.getBounds(), 1);
325     }
326
327     /**
328      * The selection overlay.
329      */

330     protected class SelectionOverlay implements Overlay {
331
332         /**
333          * Paints this overlay.
334          */

335         public void paint(Graphics JavaDoc g) {
336             if (selectionHighlight != null) {
337                 AffineTransform JavaDoc at = component.getRenderingTransform();
338                 Shape JavaDoc s = at.createTransformedShape(selectionHighlight);
339
340                 Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
341         if (xorMode) {
342             g2d.setColor(Color.black);
343             g2d.setXORMode(Color.white);
344             g2d.fill(s);
345         } else {
346             g2d.setColor(selectionOverlayColor);
347             g2d.fill(s);
348             if (selectionOverlayStrokeColor != null) {
349             g2d.setStroke(new java.awt.BasicStroke JavaDoc(1.0f));
350             g2d.setColor(selectionOverlayStrokeColor);
351             g2d.draw(s);
352             }
353         }
354             }
355         }
356     }
357 }
358
Popular Tags