KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > text > ConcreteTextSelector


1 /*
2
3    Copyright 2001-2004 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 package org.apache.batik.gvt.text;
19
20 import java.awt.Shape JavaDoc;
21 import java.awt.Toolkit JavaDoc;
22 import java.awt.datatransfer.Clipboard JavaDoc;
23 import java.awt.datatransfer.StringSelection JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.NoninvertibleTransformException JavaDoc;
26 import java.awt.geom.Point2D JavaDoc;
27 import java.text.CharacterIterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.batik.gvt.GraphicsNode;
32 import org.apache.batik.gvt.Selectable;
33 import org.apache.batik.gvt.Selector;
34 import org.apache.batik.gvt.TextNode;
35 import org.apache.batik.gvt.event.GraphicsNodeChangeEvent;
36 import org.apache.batik.gvt.event.GraphicsNodeEvent;
37 import org.apache.batik.gvt.event.GraphicsNodeKeyEvent;
38 import org.apache.batik.gvt.event.GraphicsNodeMouseEvent;
39 import org.apache.batik.gvt.event.SelectionEvent;
40 import org.apache.batik.gvt.event.SelectionListener;
41
42 /**
43  * ConcreteTextSelector.java:
44  * A simple implementation of GraphicsNodeMouseListener for text selection.
45  *
46  * @author <a HREF="mailto:bill.haneman@ireland.sun.com">Bill Haneman</a>
47  * @version $Id: ConcreteTextSelector.java,v 1.22 2005/03/15 11:24:35 deweese Exp $
48  */

49
50 public class ConcreteTextSelector implements Selector {
51
52     private ArrayList JavaDoc listeners = null;
53     private GraphicsNode selectionNode = null;
54     private GraphicsNode currentNode = null;
55     private int firstHit;
56     private int lastHit;
57
58     public ConcreteTextSelector() {
59     }
60
61     public void mouseClicked(GraphicsNodeMouseEvent evt) {
62         checkSelectGesture(evt);
63     }
64
65     public void mouseDragged(GraphicsNodeMouseEvent evt) {
66         checkSelectGesture(evt);
67     }
68
69     public void mouseEntered(GraphicsNodeMouseEvent evt) {
70         currentNode = evt.getGraphicsNode();
71         checkSelectGesture(evt);
72     }
73
74     public void mouseExited(GraphicsNodeMouseEvent evt) {
75         checkSelectGesture(evt);
76         currentNode = null;
77     }
78
79     public void mouseMoved(GraphicsNodeMouseEvent evt) {
80     }
81
82     public void mousePressed(GraphicsNodeMouseEvent evt) {
83         checkSelectGesture(evt);
84     }
85
86     public void mouseReleased(GraphicsNodeMouseEvent evt) {
87         checkSelectGesture(evt);
88     }
89
90     public void keyPressed(GraphicsNodeKeyEvent evt) {
91         report(evt, "keyPressed");
92     }
93
94     public void keyReleased(GraphicsNodeKeyEvent evt) {
95         report(evt, "keyReleased");
96     }
97
98     public void keyTyped(GraphicsNodeKeyEvent evt) {
99         report(evt, "keyTyped");
100     }
101
102     public void changeStarted (GraphicsNodeChangeEvent gnce) {
103     }
104     public void changeCompleted (GraphicsNodeChangeEvent gnce) {
105         if (selectionNode == null) return;
106         Shape JavaDoc newShape =
107             ((Selectable)selectionNode).getHighlightShape();
108         dispatchSelectionEvent
109             (new SelectionEvent(getSelection(),
110                                 SelectionEvent.SELECTION_CHANGED,
111                                 newShape));
112     }
113
114     public void setSelection(Mark begin, Mark end) {
115         TextNode node = begin.getTextNode();
116         if (node != end.getTextNode())
117             throw new Error JavaDoc("Markers not from same TextNode");
118         node.setSelection(begin, end);
119         selectionNode = node;
120         Object JavaDoc selection = getSelection();
121         Shape JavaDoc shape = node.getHighlightShape();
122         dispatchSelectionEvent(new SelectionEvent
123             (selection, SelectionEvent.SELECTION_DONE, shape));
124     }
125
126     public void clearSelection() {
127         if (selectionNode == null)
128             return;
129         dispatchSelectionEvent(new SelectionEvent
130             (null, SelectionEvent.SELECTION_CLEARED, null));
131         selectionNode = null;
132     }
133
134     /*
135      * Checks the event to see if it is a selection gesture and processes it
136      * accordingly.
137      * @param evt the GraphicsNodeEvent, which may be a "select gesture"
138      * Param evt is a GraphicsNodeEvent rather than a GraphicsNodeMouseEvent
139      * for future extension, so we can use Shift-arrow, etc.
140      */

141     protected void checkSelectGesture(GraphicsNodeEvent evt) {
142
143         GraphicsNodeMouseEvent mevt = null;
144         if (evt instanceof GraphicsNodeMouseEvent) {
145             mevt = (GraphicsNodeMouseEvent) evt;
146         }
147
148         GraphicsNode source = evt.getGraphicsNode();
149         if (isDeselectGesture(evt)) {
150             if (selectionNode != null)
151                 selectionNode.getRoot()
152                     .removeTreeGraphicsNodeChangeListener(this);
153
154             clearSelection();
155         } else if (mevt != null) {
156
157             Point2D JavaDoc p = new Point2D.Double JavaDoc(mevt.getX(), mevt.getY());
158             AffineTransform JavaDoc t = source.getGlobalTransform();
159             if (t == null) {
160                 t = new AffineTransform JavaDoc();
161             }
162             else {
163                  try {
164                      t = t.createInverse();
165                  } catch (NoninvertibleTransformException JavaDoc ni) {
166                  }
167             }
168             p = t.transform(p, null);
169
170             if ((source instanceof Selectable) &&
171                 (isSelectStartGesture(evt))) {
172                 if (selectionNode != source) {
173                     if (selectionNode != null)
174                         selectionNode.getRoot()
175                             .removeTreeGraphicsNodeChangeListener(this);
176                     if (source != null)
177                         source.getRoot()
178                             .addTreeGraphicsNodeChangeListener(this);
179                 }
180
181                 selectionNode = source;
182                 ((Selectable) source).selectAt(p.getX(), p.getY());
183                 dispatchSelectionEvent(
184                         new SelectionEvent(null,
185                                 SelectionEvent.SELECTION_STARTED,
186                                 null));
187
188             } else if (isSelectEndGesture(evt)) {
189                 if (selectionNode == source)
190                     ((Selectable) source).selectTo(p.getX(), p.getY());
191                 Object JavaDoc oldSelection = getSelection();
192                 if (selectionNode != null) {
193                     Shape JavaDoc newShape;
194                     newShape = ((Selectable)selectionNode).getHighlightShape();
195                     dispatchSelectionEvent
196                         (new SelectionEvent(oldSelection,
197                                             SelectionEvent.SELECTION_DONE,
198                                             newShape));
199                 }
200             } else if (isSelectContinueGesture(evt)) {
201
202                 if (selectionNode == source) {
203                     boolean result = ((Selectable) source).selectTo(p.getX(),
204                                                                     p.getY());
205                     if (result) {
206                         Shape JavaDoc newShape =
207                             ((Selectable) selectionNode).getHighlightShape();
208
209                         dispatchSelectionEvent(
210                             new SelectionEvent(null,
211                                 SelectionEvent.SELECTION_CHANGED,
212                                 newShape));
213                     }
214                 }
215             } else if ((source instanceof Selectable) &&
216                        (isSelectAllGesture(evt))) {
217                 if (selectionNode != source) {
218                     if (selectionNode != null)
219                         selectionNode.getRoot()
220                             .removeTreeGraphicsNodeChangeListener(this);
221                     if (source != null)
222                         source.getRoot()
223                             .addTreeGraphicsNodeChangeListener(this);
224                 }
225                 selectionNode = source;
226                 ((Selectable) source).selectAll(p.getX(), p.getY());
227                 Object JavaDoc oldSelection = getSelection();
228                 Shape JavaDoc newShape =
229                     ((Selectable) source).getHighlightShape();
230                 dispatchSelectionEvent(
231                         new SelectionEvent(oldSelection,
232                                 SelectionEvent.SELECTION_DONE,
233                                 newShape));
234             }
235         }
236     }
237
238     private boolean isDeselectGesture(GraphicsNodeEvent evt) {
239         return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_CLICKED)
240             && (((GraphicsNodeMouseEvent) evt).getClickCount() == 1));
241     }
242
243     private boolean isSelectStartGesture(GraphicsNodeEvent evt) {
244         return (evt.getID() == GraphicsNodeMouseEvent.MOUSE_PRESSED);
245     }
246
247     private boolean isSelectEndGesture(GraphicsNodeEvent evt) {
248         return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_RELEASED));
249     }
250
251     private boolean isSelectContinueGesture(GraphicsNodeEvent evt) {
252         return (evt.getID() == GraphicsNodeMouseEvent.MOUSE_DRAGGED);
253     }
254
255     private boolean isSelectAllGesture(GraphicsNodeEvent evt) {
256         return ((evt.getID() == GraphicsNodeMouseEvent.MOUSE_CLICKED)
257             && (((GraphicsNodeMouseEvent) evt).getClickCount() == 2));
258     }
259
260     /*
261      * Get the contents of the current selection.
262      */

263     public Object JavaDoc getSelection() {
264         Object JavaDoc value = null;
265         if (selectionNode instanceof Selectable) {
266             value = ((Selectable) selectionNode).getSelection();
267         }
268         return value;
269     }
270
271     /**
272      * Reports whether the current selection contains any objects.
273      */

274     public boolean isEmpty() {
275         return (getSelection() == null);
276     }
277
278     /**
279      * Reports whether the current selection contains any objects.
280      */

281     public void dispatchSelectionEvent(SelectionEvent e) {
282         if (listeners != null) {
283             Iterator JavaDoc iter = listeners.iterator();
284             switch(e.getID()) {
285             case SelectionEvent.SELECTION_DONE:
286                 while (iter.hasNext()) {
287                     ((SelectionListener)iter.next()).selectionDone(e);
288                 }
289                 break;
290             case SelectionEvent.SELECTION_CHANGED:
291                 while (iter.hasNext()) {
292                     ((SelectionListener)iter.next()).selectionChanged(e);
293                 }
294                 break;
295             case SelectionEvent.SELECTION_CLEARED:
296                 while (iter.hasNext()) {
297                     ((SelectionListener)iter.next()).selectionCleared(e);
298                 }
299                 break;
300             case SelectionEvent.SELECTION_STARTED:
301                 while (iter.hasNext()) {
302                     ((SelectionListener)iter.next()).selectionStarted(e);
303                 }
304                 break;
305             }
306         }
307     }
308
309     /**
310      * Add a SelectionListener to this Selector's notification list.
311      * @param l the SelectionListener to add.
312      */

313     public void addSelectionListener(SelectionListener l) {
314         if (listeners == null) {
315             listeners = new ArrayList JavaDoc();
316         }
317         listeners.add(l);
318     }
319
320     /**
321      * Remove a SelectionListener from this Selector's notification list.
322      * @param l the SelectionListener to be removed.
323      */

324     public void removeSelectionListener(SelectionListener l) {
325         if (listeners != null) {
326             listeners.remove(l);
327         }
328     }
329
330     private void report(GraphicsNodeEvent evt, String JavaDoc message) {
331         GraphicsNode source = evt.getGraphicsNode();
332         String JavaDoc label = "(non-text node)";
333         if (source instanceof TextNode) {
334             char[] cbuff;
335             java.text.CharacterIterator JavaDoc iter =
336                 ((TextNode) source).getAttributedCharacterIterator();
337             cbuff = new char[iter.getEndIndex()];
338             if (cbuff.length > 0) cbuff[0] = iter.first();
339             for (int i=1; i<cbuff.length;++i) {
340                 cbuff[i] = iter.next();
341             }
342             label = new String JavaDoc(cbuff);
343         }
344         System.out.println("Mouse "+message+" in "+label);
345     }
346 }
347
348
349
Popular Tags