You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by de...@apache.org on 2005/03/05 20:31:09 UTC

cvs commit: xml-batik/sources/org/apache/batik/swing/gvt JGVTComponent.java TextSelectionManager.java

deweese     2005/03/05 11:31:09

  Modified:    sources/org/apache/batik/gvt/renderer
                        StrokingTextPainter.java
               sources/org/apache/batik/gvt/text ConcreteTextSelector.java
               sources/org/apache/batik/swing/gvt JGVTComponent.java
                        TextSelectionManager.java
  Log:
  1) Rationalized Text Selection code.  Copy to clipboard is now done
     by JGVTComponent only if 'unix style' text selection is requested
     (default).
  
  Revision  Changes    Path
  1.55      +4 -4      xml-batik/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java
  
  Index: StrokingTextPainter.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- StrokingTextPainter.java	15 Dec 2004 10:50:30 -0000	1.54
  +++ StrokingTextPainter.java	5 Mar 2005 19:31:09 -0000	1.55
  @@ -1446,7 +1446,7 @@
       public Mark selectLast(TextNode node) {
           AttributedCharacterIterator aci;
           aci = node.getAttributedCharacterIterator();
  -        TextHit textHit = new TextHit(aci.getEndIndex(), false);
  +        TextHit textHit = new TextHit(aci.getEndIndex()-1, false);
           return  new BasicTextPainter.BasicMark(node, textHit);
       }
   
  @@ -1469,8 +1469,8 @@
               start = (BasicTextPainter.BasicMark) startMark;
               finish = (BasicTextPainter.BasicMark) finishMark;
           } catch (ClassCastException cce) {
  -            throw new
  -            Error("This Mark was not instantiated by this TextPainter class!");
  +            throw new Error
  +                ("This Mark was not instantiated by this TextPainter class!");
           }
   
           TextNode textNode = start.getTextNode();
  
  
  
  1.21      +1 -44     xml-batik/sources/org/apache/batik/gvt/text/ConcreteTextSelector.java
  
  Index: ConcreteTextSelector.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/gvt/text/ConcreteTextSelector.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- ConcreteTextSelector.java	18 Aug 2004 07:14:40 -0000	1.20
  +++ ConcreteTextSelector.java	5 Mar 2005 19:31:09 -0000	1.21
  @@ -121,7 +121,6 @@
           Shape  shape     = node.getHighlightShape();
           dispatchSelectionEvent(new SelectionEvent
               (selection, SelectionEvent.SELECTION_DONE, shape));
  -        copyToClipboard(selection);
       }
   
       public void clearSelection() {
  @@ -129,7 +128,6 @@
               return;
           dispatchSelectionEvent(new SelectionEvent
               (null, SelectionEvent.SELECTION_CLEARED, null));
  -        // copyToClipboard(null);
           selectionNode = null;
       }
   
  @@ -207,7 +205,6 @@
                           new SelectionEvent(oldSelection,
                                   SelectionEvent.SELECTION_DONE,
                                   newShape));
  -                copyToClipboard(oldSelection);
               } else
   
               if (isSelectContinueGesture(evt)) {
  @@ -235,7 +232,6 @@
                               .addTreeGraphicsNodeChangeListener(this);
                   }
                   selectionNode = source;
  -                
                   ((Selectable) source).selectAll(p.getX(), p.getY());
                   Object oldSelection = getSelection();
                   Shape newShape =
  @@ -244,7 +240,6 @@
                           new SelectionEvent(oldSelection,
                                   SelectionEvent.SELECTION_DONE,
                                   newShape));
  -                copyToClipboard(oldSelection);
               }
           }
       }
  @@ -341,44 +336,6 @@
           }
       }
   
  -    private void copyToClipboard(final Object o) {
  -	//
  -	// HACK: getSystemClipboard sometimes deadlocks on linux when called
  -	// from the AWT Thread. The Thread creation prevents that.
  -	//
  -	new Thread() {
  -	    public void run() {
  -		// first see if we can access the clipboard
  -		SecurityManager securityManager = System.getSecurityManager();
  -		boolean canAccessClipboard = true;
  -		if (securityManager != null) {
  -		    try {
  -			securityManager.checkSystemClipboardAccess();
  -		    } catch (SecurityException e) {
  -			canAccessClipboard = false;
  -		    }
  -		}
  -		if (canAccessClipboard) {
  -		    String label = "";
  -		    if (o instanceof CharacterIterator) {
  -			CharacterIterator iter = (CharacterIterator) o;
  -			char[] cbuff = new char[iter.getEndIndex()-iter.getBeginIndex()];
  -			if (cbuff.length > 0) {
  -			    cbuff[0] = iter.first();
  -			}
  -			for (int i=1; i<cbuff.length;++i) {
  -			    cbuff[i] = iter.next();
  -			}
  -			label = new String(cbuff);
  -		    }
  -		    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  -		    StringSelection selection = new StringSelection(label);
  -		    clipboard.setContents(selection, selection);
  -		}
  -	    }
  -	}.start();
  -    }
  -
       private void report(GraphicsNodeEvent evt, String message) {
           GraphicsNode source = evt.getGraphicsNode();
           String label = "(non-text node)";
  
  
  
  1.50      +95 -2     xml-batik/sources/org/apache/batik/swing/gvt/JGVTComponent.java
  
  Index: JGVTComponent.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/gvt/JGVTComponent.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- JGVTComponent.java	20 Feb 2005 21:59:10 -0000	1.49
  +++ JGVTComponent.java	5 Mar 2005 19:31:09 -0000	1.50
  @@ -25,7 +25,10 @@
   import java.awt.Graphics2D;
   import java.awt.Rectangle;
   import java.awt.RenderingHints;
  +import java.awt.Toolkit;
   import java.awt.Shape;
  +import java.awt.datatransfer.Clipboard;
  +import java.awt.datatransfer.StringSelection;
   import java.awt.event.ComponentAdapter;
   import java.awt.event.ComponentEvent;
   import java.awt.event.InputEvent;
  @@ -37,6 +40,7 @@
   import java.awt.geom.AffineTransform;
   import java.awt.geom.NoninvertibleTransformException;
   import java.awt.image.BufferedImage;
  +import java.text.CharacterIterator;
   import java.util.Collections;
   import java.util.Iterator;
   import java.util.LinkedList;
  @@ -46,6 +50,8 @@
   
   import org.apache.batik.gvt.GraphicsNode;
   import org.apache.batik.gvt.event.AWTEventDispatcher;
  +import org.apache.batik.gvt.event.SelectionAdapter;
  +import org.apache.batik.gvt.event.SelectionEvent;
   import org.apache.batik.gvt.renderer.ConcreteImageRendererFactory;
   import org.apache.batik.gvt.renderer.ImageRenderer;
   import org.apache.batik.gvt.renderer.ImageRendererFactory;
  @@ -174,6 +180,14 @@
       protected boolean selectableText;
   
       /**
  +     * Whether the JGVTComponent should adhere to 'Unix' text
  +     * selection semantics where as soon as text is selected it
  +     * is copied to the clipboard.  If users want Mac/Windows
  +     * behaviour they need to handle selections them selves.
  +     */
  +    protected boolean useUnixTextSelection = true;
  +
  +    /**
        * Whether to suspend interactions.
        */
       protected boolean suspendInteractions;
  @@ -221,15 +235,42 @@
   
       }
   
  +    /**
  +     * Turn off all 'interactor' objects (pan, zoom, etc) if
  +     * 'b' is true, turn them on if 'b' is false.
  +     */
       public void setDisableInteractions(boolean b) {
           disableInteractions = b;
       }
   
  +    /**
  +     * Returns true if all 'interactor' objects 
  +     * (pan, zoom, etc) are disabled.
  +     */
       public boolean getDisableInteractions() {
           return disableInteractions;
       }
   
       /**
  +     * If 'b' is true text selections will copied to
  +     * the clipboard immediately.  If 'b' is false
  +     * then nothing will be done when selections are
  +     * made (the application is responsable for copying
  +     * the selection in response to user actions).
  +     */
  +    public void setUseUnixTextSelection(boolean b) {
  +        useUnixTextSelection = b;
  +    }
  +
  +    /**
  +     * Returns true if the canvas will copy selections
  +     * to the clipboard when they are completed.
  +     */
  +    public void getUseUnixTextSelection(boolean b) {
  +        useUnixTextSelection = b;
  +    }
  +
  +    /**
        * Returns the interactor list.
        */
       public List getInteractors() {
  @@ -318,6 +359,8 @@
               eventDispatcher = new AWTEventDispatcher();
               if (selectableText) {
                   textSelectionManager = createTextSelectionManager();
  +                textSelectionManager.addSelectionListener
  +                    (new UnixTextSelectionListener());
               }
           }
       }
  @@ -336,7 +379,9 @@
       ////////////////////////////////////////////////////////////////////////
   
       /**
  -     *  Returns the current Text selection manager for the Component.
  +     * Returns the current Text selection manager for the Component.
  +     * Users can register with this to be notifed of changes in
  +     * the text selection.
        */
       public TextSelectionManager getTextSelectionManager() {
           return textSelectionManager;
  @@ -1160,4 +1205,52 @@
               }
           }
       }
  +
  +    protected class UnixTextSelectionListener 
  +        extends SelectionAdapter {
  +        
  +        public void selectionDone(SelectionEvent evt) {
  +            if (!useUnixTextSelection) return;
  +
  +            Object o = evt.getSelection();
  +            if (!(o instanceof CharacterIterator))
  +                return;
  +            CharacterIterator iter = (CharacterIterator) o;
  +
  +            // first see if we can access the clipboard
  +            SecurityManager securityManager;
  +            securityManager = System.getSecurityManager();
  +            if (securityManager != null) {
  +                try {
  +                    securityManager.checkSystemClipboardAccess();
  +                } catch (SecurityException e) {
  +                    return; // Can't access clipboard.
  +                }
  +            }
  +
  +            int sz = iter.getEndIndex()-iter.getBeginIndex();
  +            if (sz == 0) return;
  +
  +            char[] cbuff = new char[sz];
  +            cbuff[0] = iter.first();
  +            for (int i=1; i<cbuff.length;++i) {
  +                cbuff[i] = iter.next();
  +            }
  +            final String strSel = new String(cbuff);
  +            // HACK: getSystemClipboard sometimes deadlocks on
  +            // linux when called from the AWT Thread. The Thread
  +            // creation prevents that.
  +            new Thread() {
  +                public void run() {
  +                    Clipboard cb;
  +                    cb = Toolkit.getDefaultToolkit().getSystemClipboard();
  +                    StringSelection sel;
  +                    sel = new StringSelection(strSel);
  +                    cb.setContents(sel, sel);
  +                }
  +            }.start();
  +        }
  +    }
  +
  +
   }
  
  
  
  1.25      +15 -1     xml-batik/sources/org/apache/batik/swing/gvt/TextSelectionManager.java
  
  Index: TextSelectionManager.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/gvt/TextSelectionManager.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- TextSelectionManager.java	22 Feb 2005 09:13:03 -0000	1.24
  +++ TextSelectionManager.java	5 Mar 2005 19:31:09 -0000	1.25
  @@ -100,6 +100,11 @@
       protected boolean xorMode = false;
   
       /**
  +     * The current selection or null if there is none.
  +     */
  +    Object selection = null;
  +
  +    /**
        * Creates a new TextSelectionManager.
        */
       public TextSelectionManager(JGVTComponent comp,
  @@ -190,6 +195,13 @@
       }
   
       /**
  +     * Returns the current text selection or null if there is none.
  +     */
  +    public Object getSelection() {
  +        return selection;
  +    }
  +
  +    /**
        * Sets the selected text
        */
       public void setSelection(Mark start, Mark end) {
  @@ -265,6 +277,7 @@
       protected class TextSelectionListener implements SelectionListener {
           public void selectionDone(SelectionEvent e) {
               selectionChanged(e);
  +            selection = e.getSelection();
           }
           public void selectionCleared(SelectionEvent e) {
               selectionStarted(e);
  @@ -275,6 +288,7 @@
                   selectionHighlight = null;
                   component.repaint(r);
               }
  +            selection = null;
           }
           public void selectionChanged(SelectionEvent e) {
               Rectangle r = null;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org