Logo Search packages:      
Sourcecode: ooeclipseintegration version File versions  Download package

UnoidlDoubleClickStrategy.java

/*************************************************************************
 *
 * $RCSfile: UnoidlDoubleClickStrategy.java,v $
 *
 * $Revision: 1.3 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2006/08/20 11:55:54 $
 *
 * The Contents of this file are made available subject to the terms of
 * either of the GNU Lesser General Public License Version 2.1
 *
 * Sun Microsystems Inc., October, 2000
 *
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 * Copyright: 2002 by Sun Microsystems, Inc.
 *
 * All Rights Reserved.
 *
 * Contributor(s): Cedric Bosdonnat
 *
 *
 ************************************************************************/
package org.openoffice.ide.eclipse.core.editors;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.ITextViewer;

/**
 * This class is responsible for the selection of words using double-clics
 * in the UNO-IDL editor.
 * 
 * @author cbosdonnat
 *
 */
00058 public class UnoidlDoubleClickStrategy implements ITextDoubleClickStrategy {
      
      protected ITextViewer mText;

      /*
       *  (non-Javadoc)
       * @see org.eclipse.jface.text.ITextDoubleClickStrategy#doubleClicked(org.eclipse.jface.text.ITextViewer)
       */
      public void doubleClicked(ITextViewer part) {
            int pos = part.getSelectedRange().x;

            if (pos < 0)
                  return;

            mText = part;

            if (!selectComment(pos)) {
                  selectWord(pos);
            }
      }
      
      /**
       * Test if the caret is positioned in a comment partition
       * 
       * @param caretPos the caret position
       * @return <code>true</code> if the cursor is in a comment, 
       *          <code>false</code> ortherwise
       */
00086       protected boolean selectComment(int caretPos) {
            IDocument doc = mText.getDocument();
            int startPos, endPos;

            try {
                  int pos = caretPos;
                  char c = ' ';

                  while (pos >= 0) {
                        c = doc.getChar(pos);
                        if (c == '\\') {
                              pos -= 2;
                              continue;
                        }
                        if (c == Character.LINE_SEPARATOR || c == '\"')
                              break;
                        --pos;
                  }

                  if (c != '\"')
                        return false;

                  startPos = pos;

                  pos = caretPos;
                  int length = doc.getLength();
                  c = ' ';

                  while (pos < length) {
                        c = doc.getChar(pos);
                        if (c == Character.LINE_SEPARATOR || c == '\"')
                              break;
                        ++pos;
                  }
                  if (c != '\"')
                        return false;

                  endPos = pos;

                  int offset = startPos + 1;
                  int len = endPos - offset;
                  mText.setSelectedRange(offset, len);
                  return true;
            } catch (BadLocationException x) {
            }

            return false;
      }
      
      /**
       * Test if the caret is positioned in a word partition
       * 
       * @param caretPos the caret position
       * @return <code>true</code> if the cursor is in a word, 
       *          <code>false</code> ortherwise
       */
00142       protected boolean selectWord(int caretPos) {

            IDocument doc = mText.getDocument();
            int startPos, endPos;

            try {

                  int pos = caretPos;
                  char c;

                  while (pos >= 0) {
                        c = doc.getChar(pos);
                        if (!Character.isJavaIdentifierPart(c))
                              break;
                        --pos;
                  }

                  startPos = pos;

                  pos = caretPos;
                  int length = doc.getLength();

                  while (pos < length) {
                        c = doc.getChar(pos);
                        if (!Character.isJavaIdentifierPart(c))
                              break;
                        ++pos;
                  }

                  endPos = pos;
                  selectRange(startPos, endPos);
                  return true;

            } catch (BadLocationException x) {
            }

            return false;
      }

      /**
       * Define the text selection using a range.
       * 
       * @param startPos the position of the selection start
       * @param stopPos the position of the selection end
       */
00187       private void selectRange(int startPos, int stopPos) {
            int offset = startPos + 1;
            int length = stopPos - offset;
            mText.setSelectedRange(offset, length);
      }
}

Generated by  Doxygen 1.6.0   Back to index