Mercurial > projects > dwt-addons
view dwtx/jface/text/TextSelection.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
line wrap: on
line source
/******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * Port to the D programming language: * Frank Benoit <benoit@tionex.de> *******************************************************************************/ module dwtx.jface.text.TextSelection; import dwt.dwthelper.utils; /** * Standard implementation of {@link dwtx.jface.text.ITextSelection}. * <p> * Makes advantage of the weak contract of correctness of its interface. If * generated from a selection provider, it only remembers its offset and length * and computes the remaining information on request.</p> */ public class TextSelection : ITextSelection { /** Internal empty text selection */ private final static ITextSelection NULL= new TextSelection(); /** * Returns a shared instance of an empty text selection. * * @return a shared instance of an empty text selection */ public static ITextSelection emptySelection() { return NULL; } /** Document which delivers the data of the selection */ private IDocument fDocument; /** Offset of the selection */ private int fOffset; /** Length of the selection */ private int fLength; /** * Creates an empty text selection. */ private TextSelection() { this(null, -1, -1); } /** * Creates a text selection for the given range. This * selection object describes generically a text range and * is intended to be an argument for the <code>setSelection</code> * method of selection providers. * * @param offset the offset of the range * @param length the length of the range */ public TextSelection(int offset, int length) { this(null, offset, length); } /** * Creates a text selection for the given range of the given document. * This selection object is created by selection providers in responds * <code>getSelection</code>. * * @param document the document whose text range is selected in a viewer * @param offset the offset of the selected range * @param length the length of the selected range */ public TextSelection(IDocument document, int offset, int length) { fDocument= document; fOffset= offset; fLength= length; } /** * * Returns true if the offset and length are smaller than 0. * A selection of length 0, is a valid text selection as it * describes, e.g., the cursor position in a viewer. * * @return <code>true</code> if this selection is empty * @see dwtx.jface.viewers.ISelection#isEmpty() */ public bool isEmpty() { return fOffset < 0 || fLength < 0; } /* * @see dwtx.jface.text.ITextSelection#getOffset() */ public int getOffset() { return fOffset; } /* * @see dwtx.jface.text.ITextSelection#getLength() */ public int getLength() { return fLength; } /* * @see dwtx.jface.text.ITextSelection#getStartLine() */ public int getStartLine() { try { if (fDocument !is null) return fDocument.getLineOfOffset(fOffset); } catch (BadLocationException x) { } return -1; } /* * @see dwtx.jface.text.ITextSelection#getEndLine() */ public int getEndLine() { try { if (fDocument !is null) { int endOffset= fOffset + fLength; if (fLength !is 0) endOffset--; return fDocument.getLineOfOffset(endOffset); } } catch (BadLocationException x) { } return -1; } /* * @see dwtx.jface.text.ITextSelection#getText() */ public String getText() { try { if (fDocument !is null) return fDocument.get(fOffset, fLength); } catch (BadLocationException x) { } return null; } /* * @see java.lang.Object#equals(Object) */ public bool equals(Object obj) { if (obj is this) return true; if (obj is null || getClass() !is obj.getClass()) return false; TextSelection s= (TextSelection) obj; bool sameRange= (s.fOffset is fOffset && s.fLength is fLength); if (sameRange) { if (s.fDocument is null && fDocument is null) return true; if (s.fDocument is null || fDocument is null) return false; try { String sContent= s.fDocument.get(fOffset, fLength); String content= fDocument.get(fOffset, fLength); return sContent.equals(content); } catch (BadLocationException x) { } } return false; } /* * @see java.lang.Object#hashCode() */ public int hashCode() { int low= fDocument !is null ? fDocument.hashCode() : 0; return (fOffset << 24) | (fLength << 16) | low; } }