Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotStyledText
75%
44/58
100%
2/2
1.023
SWTBotStyledText$1
100%
4/4
N/A
1.023
SWTBotStyledText$10
100%
3/3
N/A
1.023
SWTBotStyledText$11
0%
0/3
N/A
1.023
SWTBotStyledText$12
100%
3/3
N/A
1.023
SWTBotStyledText$13
100%
7/7
100%
2/2
1.023
SWTBotStyledText$14
100%
3/3
N/A
1.023
SWTBotStyledText$2
100%
5/5
N/A
1.023
SWTBotStyledText$3
100%
8/8
N/A
1.023
SWTBotStyledText$4
100%
4/4
N/A
1.023
SWTBotStyledText$5
100%
3/3
N/A
1.023
SWTBotStyledText$6
100%
5/5
N/A
1.023
SWTBotStyledText$7
100%
3/3
N/A
1.023
SWTBotStyledText$8
100%
3/3
N/A
1.023
SWTBotStyledText$9
100%
3/3
N/A
1.023
 
 1  11
 /*******************************************************************************
 2  
  * Copyright (c) 2008,2010 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import java.text.MessageFormat;
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 
 17  
 import org.eclipse.swt.SWT;
 18  
 import org.eclipse.swt.custom.Bullet;
 19  
 import org.eclipse.swt.custom.StyleRange;
 20  
 import org.eclipse.swt.custom.StyledText;
 21  
 import org.eclipse.swt.custom.StyledTextContent;
 22  
 import org.eclipse.swt.graphics.RGB;
 23  
 import org.eclipse.swt.widgets.Event;
 24  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 25  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 26  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 27  
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
 28  
 import org.eclipse.swtbot.swt.finder.results.IntResult;
 29  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 30  
 import org.eclipse.swtbot.swt.finder.results.Result;
 31  
 import org.eclipse.swtbot.swt.finder.results.StringResult;
 32  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 33  
 import org.eclipse.swtbot.swt.finder.utils.Position;
 34  
 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
 35  
 import org.hamcrest.SelfDescribing;
 36  
 
 37  
 /**
 38  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 39  
  * @version $Id$
 40  
  */
 41  
 @SWTBotWidget(clasz = StyledText.class, preferredName = "styledText", referenceBy = { ReferenceBy.LABEL, ReferenceBy.TEXT })
 42  
 public class SWTBotStyledText extends AbstractSWTBot<StyledText> {
 43  
 
 44  
         /**
 45  
          * Constructs a new instance of this object.
 46  
          *
 47  
          * @param styledText the widget.
 48  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 49  
          * @since 2.0
 50  
          */
 51  
         public SWTBotStyledText(StyledText styledText) throws WidgetNotFoundException {
 52  0
                 this(styledText, null);
 53  0
         }
 54  
 
 55  
         /**
 56  
          * Constructs a new instance of this object.
 57  
          *
 58  
          * @param styledText the widget.
 59  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 60  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 61  
          * @since 2.0
 62  
          */
 63  
         public SWTBotStyledText(StyledText styledText, SelfDescribing description) throws WidgetNotFoundException {
 64  15
                 super(styledText, description);
 65  15
         }
 66  
 
 67  
         /**
 68  
          * Sets the text into the styled text.
 69  
          *
 70  
          * @param text the text to set.
 71  
          */
 72  
         public void setText(final String text) {
 73  11
                 waitForEnabled();
 74  11
                 asyncExec(new VoidResult() {
 75  
                         public void run() {
 76  11
                                 widget.setText(text);
 77  11
                         }
 78  
                 });
 79  11
         }
 80  
 
 81  
         /**
 82  
          * Notifies of the keyboard event.
 83  
          * <p>
 84  
          * FIXME need some work for CTRL|SHIFT + 1 the 1 is to be sent as '!' in this case.
 85  
          * </p>
 86  
          * 
 87  
          * @param modificationKeys the modification keys.
 88  
          * @param c the character.
 89  
          * @see Event#character
 90  
          * @see Event#stateMask
 91  
          * @deprecated use {@link #pressShortcut(int, char)} instead. This api will be removed.
 92  
          */
 93  
         @Deprecated
 94  
         public void notifyKeyboardEvent(int modificationKeys, char c) {
 95  0
                 keyboard().pressShortcut(modificationKeys, c);
 96  0
         }
 97  
 
 98  
         /**
 99  
          * Notifies of keyboard event.
 100  
          *
 101  
          * @param modificationKeys the modification key.
 102  
          * @param c the character.
 103  
          * @param keyCode any special keys (function keys, arrow or navigation keys etc.)
 104  
          * @see Event#keyCode
 105  
          * @see Event#character
 106  
          * @see Event#stateMask
 107  
          * @deprecated use {@link #pressShortcut(int, int, char)} instead. This api will be removed.
 108  
          */
 109  
         @Deprecated
 110  
         public void notifyKeyboardEvent(int modificationKeys, char c, int keyCode) {
 111  0
                 pressShortcut(modificationKeys, keyCode, c);
 112  0
         }
 113  
 
 114  
         /**
 115  
          * Sets the caret at the specified location.
 116  
          *
 117  
          * @param line the line number, 0 based.
 118  
          * @param column the column number, 0 based.
 119  
          */
 120  
         public void navigateTo(final int line, final int column) {
 121  6
                 log.debug(MessageFormat.format("Enquing navigation to location {0}, {1} in {2}", line, column, this)); //$NON-NLS-1$
 122  6
                 waitForEnabled();
 123  6
                 setFocus();
 124  6
                 asyncExec(new VoidResult() {
 125  
                         public void run() {
 126  6
                                 log.debug(MessageFormat.format("Navigating to location {0}, {1} in {2}", line, column, widget)); //$NON-NLS-1$
 127  6
                                 widget.setSelection(offset(line, column));
 128  6
                         }
 129  
                 });
 130  6
         }
 131  
 
 132  
         /**
 133  
          * Sets the caret at the specified location.
 134  
          *
 135  
          * @param position the position of the caret.
 136  
          */
 137  
         public void navigateTo(Position position) {
 138  0
                 navigateTo(position.line, position.column);
 139  0
         }
 140  
 
 141  
         /**
 142  
          * Gets the current position of the cursor. The returned position will contain a 0-based line and column.
 143  
          *
 144  
          * @return the position of the cursor in the styled text.
 145  
          */
 146  
         public Position cursorPosition() {
 147  4
                 return syncExec(new Result<Position>() {
 148  
                         public Position run() {
 149  4
                                 widget.setFocus();
 150  4
                                 int offset = widget.getSelectionRange().x;
 151  4
                                 int line = widget.getContent().getLineAtOffset(offset);
 152  4
                                 int offsetAtLine = widget.getContent().getOffsetAtLine(line);
 153  4
                                 int column = offset - offsetAtLine;
 154  4
                                 return new Position(line, column);
 155  
                         }
 156  
                 });
 157  
         }
 158  
 
 159  
         /**
 160  
          * Types the text at the given location.
 161  
          *
 162  
          * @param line the line number, 0 based.
 163  
          * @param column the column number, 0 based.
 164  
          * @param text the text to be typed at the specified location
 165  
          * @since 1.0
 166  
          */
 167  
         public void typeText(int line, int column, String text) {
 168  2
                 navigateTo(line, column);
 169  2
                 typeText(text);
 170  2
         }
 171  
 
 172  
         /**
 173  
          * Inserts text at the given location.
 174  
          *
 175  
          * @param line the line number, 0 based.
 176  
          * @param column the column number, 0 based.
 177  
          * @param text the text to be inserted at the specified location
 178  
          */
 179  
         public void insertText(int line, int column, String text) {
 180  1
                 navigateTo(line, column);
 181  1
                 insertText(text);
 182  1
         }
 183  
 
 184  
         /**
 185  
          * Inserts text at the end.
 186  
          * <p>
 187  
          * FIXME handle line endings
 188  
          * </p>
 189  
          *
 190  
          * @param text the text to be inserted at the location of the caret.
 191  
          */
 192  
         public void insertText(final String text) {
 193  1
                 waitForEnabled();
 194  1
                 syncExec(new VoidResult() {
 195  
                         public void run() {
 196  1
                                 widget.insert(text);
 197  1
                         }
 198  
                 });
 199  1
         }
 200  
 
 201  
         /**
 202  
          * Types the text.
 203  
          * <p>
 204  
          * FIXME handle line endings
 205  
          * </p>
 206  
          *
 207  
          * @param text the text to be typed at the location of the caret.
 208  
          * @since 1.0
 209  
          */
 210  
         public void typeText(final String text) {
 211  2
                 typeText(text, SWTBotPreferences.TYPE_INTERVAL);
 212  2
         }
 213  
 
 214  
         /**
 215  
          * Types the text.
 216  
          * <p>
 217  
          * FIXME handle line endings
 218  
          * </p>
 219  
          *
 220  
          * @param text the text to be typed at the location of the caret.
 221  
          * @param interval the interval between consecutive key strokes.
 222  
          * @since 1.0
 223  
          */
 224  
         public void typeText(final String text, int interval) {
 225  2
                 log.debug(MessageFormat.format("Inserting text:{0} into styledtext{1}", text, this)); //$NON-NLS-1$
 226  2
                 setFocus();
 227  2
                 keyboard().typeText(text);
 228  2
         }
 229  
 
 230  
         /**
 231  
          * Gets the style for the given line.
 232  
          *
 233  
          * @param line the line number, 0 based.
 234  
          * @param column the column number, 0 based.
 235  
          * @return the {@link StyleRange} at the specified location
 236  
          */
 237  
         public StyleRange getStyle(final int line, final int column) {
 238  1
                 return syncExec(new Result<StyleRange>() {
 239  
                         public StyleRange run() {
 240  1
                                 return widget.getStyleRangeAtOffset(offset(line, column));
 241  
                         }
 242  
                 });
 243  
         }
 244  
 
 245  
         /**
 246  
          * Gets the offset.
 247  
          *
 248  
          * @param line the line number, 0 based.
 249  
          * @param column the column number, 0 based.
 250  
          * @return the character offset at the specified location in the styledtext.
 251  
          * @see StyledTextContent#getOffsetAtLine(int)
 252  
          */
 253  
         protected int offset(final int line, final int column) {
 254  15
                 return widget.getContent().getOffsetAtLine(line) + column;
 255  
         }
 256  
 
 257  
         /**
 258  
          * Selects the range.
 259  
          *
 260  
          * @param line the line number, 0 based.
 261  
          * @param column the column number, 0 based.
 262  
          * @param length the length of the selection.
 263  
          */
 264  
         public void selectRange(final int line, final int column, final int length) {
 265  7
                 waitForEnabled();
 266  7
                 asyncExec(new VoidResult() {
 267  
                         public void run() {
 268  7
                                 int offset = offset(line, column);
 269  7
                                 widget.setSelection(offset, offset + length);
 270  7
                         }
 271  
                 });
 272  7
                 notify(SWT.Selection);
 273  7
         }
 274  
 
 275  
         /**
 276  
          * Gets the current selection text.
 277  
          *
 278  
          * @return the selection in the styled text
 279  
          */
 280  
         public String getSelection() {
 281  1
                 return syncExec(new StringResult() {
 282  
                         public String run() {
 283  1
                                 return widget.getSelectionText();
 284  
                         }
 285  
                 });
 286  
         }
 287  
 
 288  
         /**
 289  
          * Gets the style information.
 290  
          *
 291  
          * @param line the line number, 0 based.
 292  
          * @param column the column number, 0 based.
 293  
          * @param length the length.
 294  
          * @return the styles in the specified range.
 295  
          * @see StyledText#getStyleRanges(int, int)
 296  
          */
 297  
         public StyleRange[] getStyles(final int line, final int column, final int length) {
 298  1
                 return syncExec(new ArrayResult<StyleRange>() {
 299  
                         public StyleRange[] run() {
 300  1
                                 return widget.getStyleRanges(offset(line, column), length);
 301  
                         }
 302  
 
 303  
                 });
 304  
         }
 305  
 
 306  
         /**
 307  
          * Gets the text on the current line.
 308  
          *
 309  
          * @return the text on the current line, without the line delimiters.
 310  
          * @see SWTBotStyledText#getTextOnLine(int)
 311  
          */
 312  
         public String getTextOnCurrentLine() {
 313  1
                 final Position currentPosition = cursorPosition();
 314  1
                 final int line = currentPosition.line;
 315  1
                 return getTextOnLine(line);
 316  
         }
 317  
 
 318  
         /**
 319  
          * Gets the text on the line.
 320  
          * <p>
 321  
          * TODO: throw exception if the line is out of range.
 322  
          * </p>
 323  
          *
 324  
          * @param line the line number, 0 based.
 325  
          * @return the text on the given line number, without the line delimiters.
 326  
          */
 327  
         public String getTextOnLine(final int line) {
 328  1
                 return syncExec(new StringResult() {
 329  
                         public String run() {
 330  1
                                 return widget.getContent().getLine(line);
 331  
                         }
 332  
                 });
 333  
         }
 334  
 
 335  
         /**
 336  
          * Checks if this has a bullet on the current line.
 337  
          *
 338  
          * @return <code>true</code> if the styledText has a bullet on the given line, <code>false</code> otherwise.
 339  
          * @see StyledText#getLineBullet(int)
 340  
          */
 341  
         public boolean hasBulletOnCurrentLine() {
 342  2
                 return hasBulletOnLine(cursorPosition().line);
 343  
         }
 344  
 
 345  
         /**
 346  
          * Gets if this has a bullet on the specific line.
 347  
          *
 348  
          * @param line the line number, 0 based.
 349  
          * @return <code>true</code> if the styledText has a bullet on the given line, <code>false</code> otherwise.
 350  
          * @see StyledText#getLineBullet(int)
 351  
          */
 352  
         public boolean hasBulletOnLine(final int line) {
 353  2
                 return getBulletOnLine(line) != null;
 354  
         }
 355  
 
 356  
         /**
 357  
          * Gets the bullet on the current line.
 358  
          *
 359  
          * @return the bullet on the current line.
 360  
          * @see StyledText#getLineBullet(int)
 361  
          */
 362  
         public Bullet getBulletOnCurrentLine() {
 363  0
                 return getBulletOnLine(cursorPosition().line);
 364  
         }
 365  
 
 366  
         /**
 367  
          * Gets the bullet on the given line.
 368  
          *
 369  
          * @param line the line number, 0 based.
 370  
          * @return the bullet on the given line.
 371  
          * @see StyledText#getLineBullet(int)
 372  
          */
 373  
         public Bullet getBulletOnLine(final int line) {
 374  2
                 return syncExec(new Result<Bullet>() {
 375  
                         public Bullet run() {
 376  2
                                 return widget.getLineBullet(line);
 377  
                         }
 378  
                 });
 379  
         }
 380  
 
 381  
         /**
 382  
          * Selects the text on the specified line.
 383  
          *
 384  
          * @param line the line number, 0 based.
 385  
          * @since 1.1
 386  
          */
 387  
         public void selectLine(int line) {
 388  0
                 selectRange(line, 0, getTextOnLine(line).length());
 389  0
         }
 390  
 
 391  
         /**
 392  
          * Selects the text on the current line.
 393  
          *
 394  
          * @since 1.1
 395  
          */
 396  
         public void selectCurrentLine() {
 397  0
                 selectLine(cursorPosition().line);
 398  0
         }
 399  
 
 400  
         /**
 401  
          * Gets the color of the background on the specified line.
 402  
          *
 403  
          * @param line the line number, 0 based.
 404  
          * @return the RGB of the line background color of the specified line.
 405  
          * @since 1.3
 406  
          */
 407  
         public RGB getLineBackground(final int line) {
 408  0
                 return syncExec(new Result<RGB>() {
 409  
                         public RGB run() {
 410  0
                                 return widget.getLineBackground(line).getRGB();
 411  
                         }
 412  
                 });
 413  
         }
 414  
 
 415  
         /**
 416  
          * Gets the number of lines in the {@link StyledText}.
 417  
          * 
 418  
          * @return the number of lines in the {@link StyledText}.
 419  
          */
 420  
         public int getLineCount(){
 421  2
                 return syncExec(new IntResult() {
 422  
                         public Integer run() {
 423  2
                                 return widget.getLineCount();
 424  
                         }
 425  
                 });
 426  
         }
 427  
 
 428  
         /**
 429  
          * Gets all the lines in the editor.
 430  
          * 
 431  
          * @return the lines in the editor.
 432  
          */
 433  
         public List<String> getLines() {
 434  2
                 return syncExec(new ListResult<String>() {
 435  
                         public List<String> run() {
 436  2
                                 int lineCount = widget.getLineCount();
 437  2
                                 ArrayList<String> lines = new ArrayList<String>(lineCount);
 438  9
                                 for (int i = 0; i < lineCount; i++) {
 439  7
                                         lines.add(widget.getLine(i));
 440  
                                 }
 441  2
                                 return lines;
 442  
                         }
 443  
                 });
 444  
         }
 445  
 
 446  
         /**
 447  
          * Gets the tab width of the {@link StyledText} measured in characters.
 448  
          * 
 449  
          * @return the tab width of the {@link StyledText} measured in characters.
 450  
          */
 451  
         public int getTabs() {
 452  2
                 return syncExec(new IntResult() {
 453  
                         public Integer run() {
 454  2
                                 return widget.getTabs();
 455  
                         }
 456  
                 });
 457  
         }
 458  
 
 459  
 }