| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SWTBotEclipseEditor |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$1 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$2 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$3 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$4 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$5 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$6 |
|
| 1.2638888888888888;1.264 | ||||
| SWTBotEclipseEditor$7 |
|
| 1.2638888888888888;1.264 |
| 1 | 2 | /******************************************************************************* |
| 2 | * Copyright (c) 2008-2009 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 | * Ketan Patel - https://bugs.eclipse.org/bugs/show_bug.cgi?id=259837 | |
| 11 | * Ralf Ebert www.ralfebert.de - (bug 271630) SWTBot Improved RCP / Workbench support | |
| 12 | *******************************************************************************/ | |
| 13 | package org.eclipse.swtbot.eclipse.finder.widgets; | |
| 14 | ||
| 15 | import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; | |
| 16 | import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType; | |
| 17 | import static org.hamcrest.Matchers.any; | |
| 18 | ||
| 19 | import java.util.ArrayList; | |
| 20 | import java.util.Arrays; | |
| 21 | import java.util.List; | |
| 22 | ||
| 23 | import org.eclipse.jface.action.IAction; | |
| 24 | import org.eclipse.jface.bindings.keys.KeyStroke; | |
| 25 | import org.eclipse.swt.SWT; | |
| 26 | import org.eclipse.swt.custom.Bullet; | |
| 27 | import org.eclipse.swt.custom.StyleRange; | |
| 28 | import org.eclipse.swt.custom.StyledText; | |
| 29 | import org.eclipse.swt.graphics.Color; | |
| 30 | import org.eclipse.swt.graphics.RGB; | |
| 31 | import org.eclipse.swt.widgets.Control; | |
| 32 | import org.eclipse.swt.widgets.Event; | |
| 33 | import org.eclipse.swt.widgets.Shell; | |
| 34 | import org.eclipse.swt.widgets.Table; | |
| 35 | import org.eclipse.swt.widgets.Widget; | |
| 36 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
| 37 | import org.eclipse.swtbot.eclipse.finder.exceptions.QuickFixNotFoundException; | |
| 38 | import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
| 39 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
| 40 | import org.eclipse.swtbot.swt.finder.keyboard.Keyboard; | |
| 41 | import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes; | |
| 42 | import org.eclipse.swtbot.swt.finder.matchers.AbstractMatcher; | |
| 43 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
| 44 | import org.eclipse.swtbot.swt.finder.results.WidgetResult; | |
| 45 | import org.eclipse.swtbot.swt.finder.utils.MessageFormat; | |
| 46 | import org.eclipse.swtbot.swt.finder.utils.Position; | |
| 47 | import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition; | |
| 48 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; | |
| 49 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; | |
| 50 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText; | |
| 51 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; | |
| 52 | import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; | |
| 53 | import org.eclipse.ui.IEditorReference; | |
| 54 | import org.eclipse.ui.texteditor.ITextEditor; | |
| 55 | import org.hamcrest.Description; | |
| 56 | import org.hamcrest.Matcher; | |
| 57 | import org.hamcrest.SelfDescribing; | |
| 58 | ||
| 59 | /** | |
| 60 | * This represents an eclipse editor item. | |
| 61 | * | |
| 62 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 63 | * @author Ralf Ebert www.ralfebert.de (bug 271630) | |
| 64 | * @version $Id$ | |
| 65 | */ | |
| 66 | public class SWTBotEclipseEditor extends SWTBotEditor { | |
| 67 | ||
| 68 | 2 | private final SWTBotStyledText styledText; |
| 69 | ||
| 70 | /** | |
| 71 | * Constructs an instance of the given object. | |
| 72 | * | |
| 73 | * @param editorReference the editor reference. | |
| 74 | * @param bot the instance of {@link SWTWorkbenchBot} which will be used to drive operations on behalf of this | |
| 75 | * object. | |
| 76 | * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. | |
| 77 | * @since 2.0 | |
| 78 | */ | |
| 79 | public SWTBotEclipseEditor(IEditorReference editorReference, SWTWorkbenchBot bot) throws WidgetNotFoundException { | |
| 80 | 3 | super(editorReference, bot); |
| 81 | 3 | this.styledText = new SWTBotStyledText(widget()); |
| 82 | 3 | } |
| 83 | ||
| 84 | /** | |
| 85 | * Constructs an instance for the given editorReference. | |
| 86 | * | |
| 87 | * @param editorReference the part reference. | |
| 88 | * @param bot the helper bot. | |
| 89 | * @param description the description of the editor part. | |
| 90 | */ | |
| 91 | public SWTBotEclipseEditor(IEditorReference editorReference, SWTWorkbenchBot bot, SelfDescribing description) { | |
| 92 | 0 | super(editorReference, bot, description); |
| 93 | 0 | this.styledText = new SWTBotStyledText(widget()); |
| 94 | 0 | } |
| 95 | ||
| 96 | private StyledText widget() { | |
| 97 | 3 | List<? extends Widget> findWidgets = findWidgets(widgetOfType(StyledText.class)); |
| 98 | 3 | return (StyledText) findWidgets.get(findWidgets.size() - 1); |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * @return the styledText | |
| 103 | */ | |
| 104 | public SWTBotStyledText getStyledText() { | |
| 105 | 0 | return styledText; |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * @return the editor reference for this view. | |
| 110 | * @deprecated use {@link SWTBotWorkbenchPart#getReference()} instead | |
| 111 | */ | |
| 112 | public IEditorReference getEditorReference() { | |
| 113 | 0 | return getReference(); |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Applys a quick fix item at the given index. | |
| 118 | * | |
| 119 | * @param quickFixIndex the index of the quickfix item to apply. | |
| 120 | * @throws WidgetNotFoundException if the quickfix could not be found. | |
| 121 | */ | |
| 122 | public void quickfix(int quickFixIndex) { | |
| 123 | 0 | WaitForObjectCondition<SWTBotTable> quickFixTableCondition = quickFixAppears(any(SWTBotTable.class)); |
| 124 | 0 | waitUntil(quickFixTableCondition); |
| 125 | 0 | SWTBotTable quickFixTable = quickFixTableCondition.get(0); |
| 126 | 0 | selectProposal(quickFixTable, quickFixIndex); |
| 127 | 0 | } |
| 128 | ||
| 129 | /** | |
| 130 | * Applys a quick fix item with the given name. | |
| 131 | * | |
| 132 | * @param quickFixName the name of the quick fix to apply. | |
| 133 | */ | |
| 134 | public void quickfix(String quickFixName) { | |
| 135 | 0 | WaitForObjectCondition<SWTBotTable> quickFixTable = quickFixAppears(tableWithRow(quickFixName)); |
| 136 | 0 | waitUntil(quickFixTable); |
| 137 | 0 | selectProposal(quickFixTable.get(0), quickFixName); |
| 138 | 0 | } |
| 139 | ||
| 140 | /** | |
| 141 | * Finds all the quickfixes in the quickfix list. | |
| 142 | * | |
| 143 | * @return the list of all available quickfixes. | |
| 144 | * @since 1.2 | |
| 145 | */ | |
| 146 | public List<String> getQuickFixes() { | |
| 147 | 0 | WaitForObjectCondition<SWTBotTable> quickFixTableCondition = quickFixAppears(any(SWTBotTable.class)); |
| 148 | 0 | waitUntil(quickFixTableCondition); |
| 149 | 0 | SWTBotTable quickFixTable = quickFixTableCondition.get(0); |
| 150 | 0 | List<String> proposals = getRows(quickFixTable); |
| 151 | 0 | makeProposalsDisappear(); |
| 152 | 0 | return proposals; |
| 153 | } | |
| 154 | ||
| 155 | /** | |
| 156 | * Gets the quick fix item count. | |
| 157 | * | |
| 158 | * @return the number of quickfix items in the quickfix proposals. | |
| 159 | * @since 1.2 | |
| 160 | */ | |
| 161 | public int getQuickfixListItemCount() { | |
| 162 | 0 | WaitForObjectCondition<SWTBotTable> quickFixTableCondition = quickFixAppears(any(SWTBotTable.class)); |
| 163 | 0 | waitUntil(quickFixTableCondition); |
| 164 | 0 | SWTBotTable quickFixTable = quickFixTableCondition.get(0); |
| 165 | 0 | return quickFixTable.rowCount(); |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * Attempst to applys the quick fix. | |
| 170 | * <p> | |
| 171 | * FIXME: this needs a lot of optimization. | |
| 172 | * </p> | |
| 173 | * | |
| 174 | * @param proposalTable the table containing the quickfix. | |
| 175 | * @param proposalText the name of the quickfix to apply. | |
| 176 | */ | |
| 177 | private void selectProposal(SWTBotTable proposalTable, String proposalText) { | |
| 178 | 1 | log.debug(MessageFormat.format("Trying to select proposal {0}", proposalText)); //$NON-NLS-1$ |
| 179 | 1 | if (proposalTable.containsItem(proposalText)) { |
| 180 | 1 | selectProposal(proposalTable, proposalTable.indexOf(proposalText)); |
| 181 | 1 | return; |
| 182 | } | |
| 183 | 0 | throw new QuickFixNotFoundException("Quickfix options not found. Giving up."); //$NON-NLS-1$ |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Applies the specified quickfix. | |
| 188 | * | |
| 189 | * @param proposalTable the table containing the quickfix. | |
| 190 | * @param proposalIndex the index of the quickfix. | |
| 191 | */ | |
| 192 | private void selectProposal(final SWTBotTable proposalTable, final int proposalIndex) { | |
| 193 | 1 | log.debug(MessageFormat.format("Trying to select proposal with index {0}", proposalIndex)); //$NON-NLS-1$ |
| 194 | 1 | UIThreadRunnable.asyncExec(new VoidResult() { |
| 195 | public void run() { | |
| 196 | 1 | Table table = proposalTable.widget; |
| 197 | 2 | log.debug(MessageFormat.format("Selecting row [{0}] {1} in {2}", proposalIndex, table.getItem(proposalIndex).getText(), //$NON-NLS-1$ |
| 198 | 1 | table)); |
| 199 | 1 | table.setSelection(proposalIndex); |
| 200 | 1 | Event event = new Event(); |
| 201 | 1 | event.type = SWT.Selection; |
| 202 | 1 | event.widget = table; |
| 203 | 1 | event.item = table.getItem(proposalIndex); |
| 204 | 1 | table.notifyListeners(SWT.Selection, event); |
| 205 | 1 | table.notifyListeners(SWT.DefaultSelection, event); |
| 206 | 1 | } |
| 207 | }); | |
| 208 | 1 | } |
| 209 | ||
| 210 | /** | |
| 211 | * Gets the quick fix table. | |
| 212 | * | |
| 213 | * @param proposalShell the shell containing the quickfixes. | |
| 214 | * @return the table containing the quickfix. | |
| 215 | */ | |
| 216 | 2 | private SWTBotTable getProposalTable() { |
| 217 | 2 | log.debug("Finding table containing proposals."); |
| 218 | try { | |
| 219 | 2 | Table table = bot.widget(widgetOfType(Table.class), activatePopupShell().widget); |
| 220 | 2 | SWTBotTable swtBotTable = new SWTBotTable(table); |
| 221 | 2 | log.debug(MessageFormat.format("Found table containing proposals -- {0}", getRows(swtBotTable))); |
| 222 | 2 | return swtBotTable; |
| 223 | 0 | } catch (Exception e) { |
| 224 | 0 | throw new QuickFixNotFoundException("Quickfix options not found. Giving up.", e); //$NON-NLS-1$ |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * Gets the auto completion proposal matching the given text.. | |
| 230 | * | |
| 231 | * @param insertText the proposal text to type before auto completing | |
| 232 | * @return the list of proposals | |
| 233 | * @throws TimeoutException if the autocomplete shell did not close in time. | |
| 234 | * @since 1.2 | |
| 235 | */ | |
| 236 | @SuppressWarnings("all") | |
| 237 | public List<String> getAutoCompleteProposals(String insertText) { | |
| 238 | 1 | typeText(insertText); |
| 239 | 1 | WaitForObjectCondition<SWTBotTable> autoCompleteAppears = autoCompleteAppears(tableWithRowIgnoringCase(insertText)); |
| 240 | 1 | waitUntil(autoCompleteAppears); |
| 241 | 1 | final SWTBotTable autoCompleteTable = autoCompleteAppears.get(0); |
| 242 | 1 | List<String> proposals = getRows(autoCompleteTable); |
| 243 | 1 | makeProposalsDisappear(); |
| 244 | 1 | return proposals; |
| 245 | } | |
| 246 | ||
| 247 | /** | |
| 248 | * Auto completes the given proposal. | |
| 249 | * | |
| 250 | * @param insertText the text to be inserted before activating the auto-complete. | |
| 251 | * @param proposalText the auto-completion proposal to select from the list. | |
| 252 | */ | |
| 253 | public void autoCompleteProposal(String insertText, String proposalText) { | |
| 254 | 1 | typeText(insertText); |
| 255 | 1 | WaitForObjectCondition<SWTBotTable> autoCompleteTable = autoCompleteAppears(tableWithRow(proposalText)); |
| 256 | 1 | waitUntil(autoCompleteTable); |
| 257 | 1 | selectProposal(autoCompleteTable.get(0), proposalText); |
| 258 | 1 | } |
| 259 | ||
| 260 | /** | |
| 261 | * Gets the context menu in the editor. | |
| 262 | * | |
| 263 | * @param text the context menu item. | |
| 264 | * @return the menu | |
| 265 | * @throws WidgetNotFoundException if the menu with the specified text could not be found. | |
| 266 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#contextMenu(java.lang.String) | |
| 267 | */ | |
| 268 | public SWTBotMenu contextMenu(String text) throws WidgetNotFoundException { | |
| 269 | 0 | return styledText.contextMenu(text); |
| 270 | } | |
| 271 | ||
| 272 | /** | |
| 273 | * Gets the current position of the cursor. The returned position will contain a 0-based line and column. | |
| 274 | * | |
| 275 | * @return the position of the cursor. | |
| 276 | * @see SWTBotStyledText#cursorPosition() | |
| 277 | */ | |
| 278 | public Position cursorPosition() { | |
| 279 | 0 | return styledText.cursorPosition(); |
| 280 | } | |
| 281 | ||
| 282 | /** | |
| 283 | * Gets if the object's widget is enabled. | |
| 284 | * | |
| 285 | * @return <code>true</code> if the widget is enabled. | |
| 286 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#isEnabled() | |
| 287 | */ | |
| 288 | public boolean isEnabled() { | |
| 289 | 0 | return styledText.isEnabled(); |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * Gets the current selection. | |
| 294 | * | |
| 295 | * @return The selected string. | |
| 296 | */ | |
| 297 | public String getSelection() { | |
| 298 | 0 | return styledText.getSelection(); |
| 299 | } | |
| 300 | ||
| 301 | /** | |
| 302 | * Gets the style text. | |
| 303 | * | |
| 304 | * @param line the line number, 0 based. | |
| 305 | * @param column the column number, 0 based. | |
| 306 | * @return the {@link StyleRange} at the specified location | |
| 307 | * @see SWTBotStyledText#getStyle(int, int) | |
| 308 | */ | |
| 309 | public StyleRange getStyle(int line, int column) { | |
| 310 | 0 | return styledText.getStyle(line, column); |
| 311 | } | |
| 312 | ||
| 313 | /** | |
| 314 | * Gets the text of this object's widget. | |
| 315 | * | |
| 316 | * @return the text on the styledtext. | |
| 317 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#getText() | |
| 318 | */ | |
| 319 | public String getText() { | |
| 320 | 2 | return styledText.getText(); |
| 321 | } | |
| 322 | ||
| 323 | /** | |
| 324 | * @param line the line number, 0 based. | |
| 325 | * @param column the column number, 0 based. | |
| 326 | * @param text the text to be typed at the specified location | |
| 327 | * @see SWTBotStyledText#typeText(int, int, java.lang.String) | |
| 328 | * @since 1.0 | |
| 329 | */ | |
| 330 | public void typeText(int line, int column, String text) { | |
| 331 | 0 | styledText.typeText(line, column, text); |
| 332 | 0 | } |
| 333 | ||
| 334 | /** | |
| 335 | * @param text the text to be typed at the location of the caret. * | |
| 336 | * @see SWTBotStyledText#typeText(java.lang.String) | |
| 337 | * @since 1.0 | |
| 338 | */ | |
| 339 | public void typeText(String text) { | |
| 340 | 2 | styledText.typeText(text); |
| 341 | 2 | } |
| 342 | ||
| 343 | /** | |
| 344 | * @param line the line number, 0 based. | |
| 345 | * @param column the column number, 0 based. | |
| 346 | * @param text the text to be inserted at the specified location | |
| 347 | * @see SWTBotStyledText#insertText(int, int, java.lang.String) | |
| 348 | */ | |
| 349 | public void insertText(int line, int column, String text) { | |
| 350 | 0 | styledText.insertText(line, column, text); |
| 351 | 0 | } |
| 352 | ||
| 353 | /** | |
| 354 | * @param text the text to be inserted at the location of the caret. | |
| 355 | * @see SWTBotStyledText#insertText(java.lang.String) | |
| 356 | */ | |
| 357 | public void insertText(String text) { | |
| 358 | 0 | styledText.insertText(text); |
| 359 | 0 | } |
| 360 | ||
| 361 | /** | |
| 362 | * @param text the text to be typed at the location of the caret. | |
| 363 | * @param interval the interval between consecutive key strokes. | |
| 364 | * @see SWTBotStyledText#typeText(java.lang.String, int) | |
| 365 | * @since 1.0 | |
| 366 | */ | |
| 367 | public void typeText(String text, int interval) { | |
| 368 | 0 | styledText.typeText(text, interval); |
| 369 | 0 | } |
| 370 | ||
| 371 | /** | |
| 372 | * Sets the caret at the specified location. | |
| 373 | * | |
| 374 | * @param position the position of the caret. | |
| 375 | */ | |
| 376 | public void navigateTo(Position position) { | |
| 377 | 0 | styledText.navigateTo(position); |
| 378 | 0 | } |
| 379 | ||
| 380 | /** | |
| 381 | * @param line the line number, 0 based. | |
| 382 | * @param column the column number, 0 based. | |
| 383 | * @see SWTBotStyledText#navigateTo(int, int) | |
| 384 | */ | |
| 385 | public void navigateTo(int line, int column) { | |
| 386 | 2 | styledText.navigateTo(line, column); |
| 387 | 2 | } |
| 388 | ||
| 389 | /** | |
| 390 | * Notifies of the keyboard event. | |
| 391 | * <p> | |
| 392 | * FIXME need some work for CTRL|SHIFT + 1 the 1 is to be sent as '!' in this case. | |
| 393 | * </p> | |
| 394 | * | |
| 395 | * @param modificationKeys the modification keys. | |
| 396 | * @param c the character. | |
| 397 | * @see Event#character | |
| 398 | * @see Event#stateMask | |
| 399 | * @deprecated use {@link #pressShortcut(int, char)} instead. This api will be removed. | |
| 400 | */ | |
| 401 | public void notifyKeyboardEvent(int modificationKeys, char c) { | |
| 402 | 0 | styledText.notifyKeyboardEvent(modificationKeys, c); |
| 403 | 0 | } |
| 404 | ||
| 405 | /** | |
| 406 | * Notifies of keyboard event. | |
| 407 | * | |
| 408 | * @param modificationKeys the modification key. | |
| 409 | * @param c the character. | |
| 410 | * @param keyCode any special keys (function keys, arrow or navigation keys etc.) | |
| 411 | * @see Event#keyCode | |
| 412 | * @see Event#character | |
| 413 | * @see Event#stateMask | |
| 414 | * @deprecated use {@link #pressShortcut(int, int, char)} instead. This api will be removed. | |
| 415 | */ | |
| 416 | public void notifyKeyboardEvent(int modificationKeys, char c, int keyCode) { | |
| 417 | 0 | styledText.notifyKeyboardEvent(modificationKeys, c, keyCode); |
| 418 | 0 | } |
| 419 | ||
| 420 | /** | |
| 421 | * Presses the shortcut specified by the given keys. | |
| 422 | * | |
| 423 | * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} | | |
| 424 | * {@link SWT#COMMAND}. | |
| 425 | * @param c the character. | |
| 426 | * @see Keyboard#pressShortcut(KeyStroke...) | |
| 427 | * @see Keystrokes#toKeys(int, char) | |
| 428 | */ | |
| 429 | public void pressShortcut(int modificationKeys, char c) { | |
| 430 | 0 | styledText.pressShortcut(modificationKeys, c); |
| 431 | 0 | } |
| 432 | ||
| 433 | /** | |
| 434 | * Presses the shortcut specified by the given keys. | |
| 435 | * | |
| 436 | * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} | | |
| 437 | * {@link SWT#COMMAND}. | |
| 438 | * @param keyCode the keyCode, these may be special keys like F1-F12, or navigation keys like HOME, PAGE_UP | |
| 439 | * @param c the character | |
| 440 | * @see Keystrokes#toKeys(int, char) | |
| 441 | */ | |
| 442 | public void pressShortcut(int modificationKeys, int keyCode, char c) { | |
| 443 | 0 | styledText.pressShortcut(modificationKeys, keyCode, c); |
| 444 | 0 | } |
| 445 | ||
| 446 | /** | |
| 447 | * Presses the shortcut specified by the given keys. | |
| 448 | * | |
| 449 | * @param keys the keys to press | |
| 450 | * @see Keyboard#pressShortcut(KeyStroke...) | |
| 451 | * @see Keystrokes | |
| 452 | */ | |
| 453 | public void pressShortcut(KeyStroke... keys) { | |
| 454 | 0 | styledText.pressShortcut(keys); |
| 455 | 0 | } |
| 456 | ||
| 457 | /** | |
| 458 | * @param line the line number, 0 based. | |
| 459 | * @param column the column number, 0 based. | |
| 460 | * @param length the length of the selection. | |
| 461 | * @see SWTBotStyledText#selectRange(int, int, int) | |
| 462 | */ | |
| 463 | public void selectRange(int line, int column, int length) { | |
| 464 | 0 | styledText.selectRange(line, column, length); |
| 465 | 0 | } |
| 466 | ||
| 467 | /** | |
| 468 | * @param line the line number to select, 0 based. | |
| 469 | * @see SWTBotStyledText#selectLine(int) | |
| 470 | * @since 1.1 | |
| 471 | */ | |
| 472 | public void selectLine(int line) { | |
| 473 | 0 | styledText.selectLine(line); |
| 474 | 0 | } |
| 475 | ||
| 476 | /** | |
| 477 | * Selects the text on the current line. | |
| 478 | * | |
| 479 | * @see SWTBotStyledText#selectCurrentLine() | |
| 480 | * @since 1.1 | |
| 481 | */ | |
| 482 | public void selectCurrentLine() { | |
| 483 | 0 | styledText.selectCurrentLine(); |
| 484 | 0 | } |
| 485 | ||
| 486 | /** | |
| 487 | * @see SWTBotStyledText#setFocus() | |
| 488 | */ | |
| 489 | public void setFocus() { | |
| 490 | 1 | styledText.setFocus(); |
| 491 | 1 | } |
| 492 | ||
| 493 | /** | |
| 494 | * @param text the text to set. | |
| 495 | * @see SWTBotStyledText#setText(java.lang.String) | |
| 496 | */ | |
| 497 | public void setText(String text) { | |
| 498 | 0 | styledText.setText(text); |
| 499 | 0 | } |
| 500 | ||
| 501 | /** | |
| 502 | * @return the bullet on the current line. | |
| 503 | * @see SWTBotStyledText#getBulletOnCurrentLine() | |
| 504 | */ | |
| 505 | public Bullet getBulletOnCurrentLine() { | |
| 506 | 0 | return styledText.getBulletOnCurrentLine(); |
| 507 | } | |
| 508 | ||
| 509 | /** | |
| 510 | * @param line the line number, 0 based. | |
| 511 | * @return the bullet on the given line. | |
| 512 | * @see SWTBotStyledText#getBulletOnLine(int) | |
| 513 | */ | |
| 514 | public Bullet getBulletOnLine(int line) { | |
| 515 | 0 | return styledText.getBulletOnLine(line); |
| 516 | } | |
| 517 | ||
| 518 | /** | |
| 519 | * @param line the line number, 0 based. | |
| 520 | * @param column the column number, 0 based. | |
| 521 | * @param length the length. | |
| 522 | * @return the styles in the specified range. | |
| 523 | * @see SWTBotStyledText#getStyles(int, int, int) | |
| 524 | */ | |
| 525 | public StyleRange[] getStyles(int line, int column, int length) { | |
| 526 | 0 | return styledText.getStyles(line, column, length); |
| 527 | } | |
| 528 | ||
| 529 | /** | |
| 530 | * @return the text on the current line, without the line delimiters. | |
| 531 | * @see SWTBotStyledText#getTextOnCurrentLine() | |
| 532 | */ | |
| 533 | public String getTextOnCurrentLine() { | |
| 534 | 0 | return styledText.getTextOnCurrentLine(); |
| 535 | } | |
| 536 | ||
| 537 | /** | |
| 538 | * @param line the line number, 0 based. | |
| 539 | * @return the text on the given line number, without the line delimiters. | |
| 540 | * @see SWTBotStyledText#getTextOnLine(int) | |
| 541 | */ | |
| 542 | public String getTextOnLine(int line) { | |
| 543 | 0 | return styledText.getTextOnLine(line); |
| 544 | } | |
| 545 | ||
| 546 | /** | |
| 547 | * @return <code>true</code> if the styledText has a bullet on the given line, <code>false</code> otherwise. | |
| 548 | * @see SWTBotStyledText#hasBulletOnCurrentLine() | |
| 549 | */ | |
| 550 | public boolean hasBulletOnCurrentLine() { | |
| 551 | 0 | return styledText.hasBulletOnCurrentLine(); |
| 552 | } | |
| 553 | ||
| 554 | /** | |
| 555 | * @param line the line number, 0 based. | |
| 556 | * @return <code>true</code> if the styledText has a bullet on the given line, <code>false</code> otherwise. | |
| 557 | * @see SWTBotStyledText#hasBulletOnLine(int) | |
| 558 | */ | |
| 559 | public boolean hasBulletOnLine(int line) { | |
| 560 | 0 | return styledText.hasBulletOnLine(line); |
| 561 | } | |
| 562 | ||
| 563 | /** | |
| 564 | * Gets the background color of the widget. | |
| 565 | * | |
| 566 | * @return the background color on the widget, or <code>null</code> if the widget is not an instance of | |
| 567 | * {@link Control}. | |
| 568 | * @since 1.3 | |
| 569 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#backgroundColor() | |
| 570 | */ | |
| 571 | public Color backgroundColor() { | |
| 572 | 0 | return styledText.backgroundColor(); |
| 573 | } | |
| 574 | ||
| 575 | /** | |
| 576 | * Gets the foreground color of the widget. | |
| 577 | * | |
| 578 | * @return the foreground color on the widget, or <code>null</code> if the widget is not an instance of | |
| 579 | * {@link Control}. | |
| 580 | * @since 1.3 | |
| 581 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#foregroundColor() | |
| 582 | */ | |
| 583 | public Color foregroundColor() { | |
| 584 | 0 | return styledText.foregroundColor(); |
| 585 | } | |
| 586 | ||
| 587 | /** | |
| 588 | * Gets the color of the background on the specified line. | |
| 589 | * | |
| 590 | * @param line the line number, 0 based. | |
| 591 | * @return the RGB of the line background color of the specified line. | |
| 592 | * @since 1.3 | |
| 593 | * @see SWTBotStyledText#getLineBackground(int) | |
| 594 | */ | |
| 595 | public RGB getLineBackground(int line) { | |
| 596 | 0 | return styledText.getLineBackground(line); |
| 597 | } | |
| 598 | ||
| 599 | /** | |
| 600 | * Gets the number of lines in the {@link StyledText}. | |
| 601 | * | |
| 602 | * @return the number of lines in the {@link StyledText}. | |
| 603 | */ | |
| 604 | public int getLineCount(){ | |
| 605 | 0 | return styledText.getLineCount(); |
| 606 | } | |
| 607 | ||
| 608 | /** | |
| 609 | * Gets all the lines in the editor. | |
| 610 | * | |
| 611 | * @return the lines in the editor. | |
| 612 | */ | |
| 613 | public List<String> getLines() { | |
| 614 | 0 | return styledText.getLines(); |
| 615 | } | |
| 616 | ||
| 617 | /** | |
| 618 | * Gets the tooltip of this object's widget. | |
| 619 | * | |
| 620 | * @return the tooltip on the widget. | |
| 621 | * @see org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot#getToolTipText() | |
| 622 | * @since 1.3 | |
| 623 | */ | |
| 624 | public String getToolTipText() { | |
| 625 | 0 | return styledText.getToolTipText(); |
| 626 | } | |
| 627 | ||
| 628 | 0 | private void makeProposalsDisappear() { |
| 629 | // clear away all content assists for next retry. | |
| 630 | 1 | log.debug("Making proposals disappear."); |
| 631 | 1 | setFocus(); |
| 632 | 1 | } |
| 633 | ||
| 634 | private Matcher<SWTBotTable> tableWithRow(final String itemText) { | |
| 635 | 1 | return new AbstractMatcher<SWTBotTable>() { |
| 636 | ||
| 637 | protected boolean doMatch(Object item) { | |
| 638 | 1 | return ((SWTBotTable) item).containsItem(itemText); |
| 639 | } | |
| 640 | ||
| 641 | public void describeTo(Description description) { | |
| 642 | 0 | description.appendText("table with item (").appendText(itemText).appendText(")"); |
| 643 | 0 | } |
| 644 | }; | |
| 645 | } | |
| 646 | ||
| 647 | private Matcher<SWTBotTable> tableWithRowIgnoringCase(final String itemText) { | |
| 648 | 1 | final String lowerCaseText = itemText.toLowerCase(); |
| 649 | 1 | return new AbstractMatcher<SWTBotTable>() { |
| 650 | ||
| 651 | protected boolean doMatch(Object item) { | |
| 652 | 1 | List<String> rows = getRows((SWTBotTable) item); |
| 653 | 2 | for (String row : rows) { |
| 654 | 1 | if (row.toLowerCase().startsWith(lowerCaseText)) { |
| 655 | 1 | return true; |
| 656 | } | |
| 657 | } | |
| 658 | 0 | return false; |
| 659 | } | |
| 660 | ||
| 661 | public void describeTo(Description description) { | |
| 662 | 0 | description.appendText("table with item (").appendText(itemText).appendText(")"); |
| 663 | 0 | } |
| 664 | }; | |
| 665 | } | |
| 666 | ||
| 667 | private WaitForObjectCondition<SWTBotTable> quickFixAppears(Matcher<SWTBotTable> tableMatcher) { | |
| 668 | 0 | return new WaitForObjectCondition<SWTBotTable>(tableMatcher) { |
| 669 | protected List<SWTBotTable> findMatches() { | |
| 670 | try { | |
| 671 | 0 | activateQuickFixShell(); |
| 672 | 0 | SWTBotTable quickFixTable = getProposalTable(); |
| 673 | 0 | if (matcher.matches(quickFixTable)) |
| 674 | 0 | return Arrays.asList(quickFixTable); |
| 675 | 0 | } catch (Throwable e) { |
| 676 | 0 | makeProposalsDisappear(); |
| 677 | } | |
| 678 | 0 | return null; |
| 679 | } | |
| 680 | ||
| 681 | public String getFailureMessage() { | |
| 682 | 0 | return "Could not find auto complete proposal using matcher " + matcher; |
| 683 | } | |
| 684 | ||
| 685 | }; | |
| 686 | } | |
| 687 | ||
| 688 | /** | |
| 689 | * This activates the popup shell. | |
| 690 | * | |
| 691 | * @return The shell. | |
| 692 | */ | |
| 693 | private SWTBotShell activatePopupShell() { | |
| 694 | 2 | log.debug("Activating quickfix shell."); //$NON-NLS-1$ |
| 695 | try { | |
| 696 | 2 | Shell mainWindow = syncExec(new WidgetResult<Shell>() { |
| 697 | public Shell run() { | |
| 698 | 2 | return styledText.widget.getShell(); |
| 699 | } | |
| 700 | }); | |
| 701 | 2 | SWTBotShell shell = bot.shell("", mainWindow); //$NON-NLS-1$ |
| 702 | 2 | shell.activate(); |
| 703 | 2 | log.debug("Activated quickfix shell."); //$NON-NLS-1$ |
| 704 | 2 | return shell; |
| 705 | 0 | } catch (Exception e) { |
| 706 | 0 | throw new QuickFixNotFoundException("Quickfix popup not found. Giving up.", e); //$NON-NLS-1$ |
| 707 | } | |
| 708 | } | |
| 709 | ||
| 710 | private WaitForObjectCondition<SWTBotTable> autoCompleteAppears(Matcher<SWTBotTable> tableMatcher) { | |
| 711 | 2 | return new WaitForObjectCondition<SWTBotTable>(tableMatcher) { |
| 712 | protected List<SWTBotTable> findMatches() { | |
| 713 | try { | |
| 714 | 2 | activateAutoCompleteShell(); |
| 715 | 2 | SWTBotTable autoCompleteTable = getProposalTable(); |
| 716 | 2 | if (matcher.matches(autoCompleteTable)) { |
| 717 | 2 | SWTBotEclipseEditor.this.log.debug("matched table, returning"); |
| 718 | 2 | return Arrays.asList(autoCompleteTable); |
| 719 | } | |
| 720 | 0 | } catch (Throwable e) { |
| 721 | 0 | makeProposalsDisappear(); |
| 722 | } | |
| 723 | 0 | return null; |
| 724 | } | |
| 725 | ||
| 726 | public String getFailureMessage() { | |
| 727 | 0 | return "Could not find auto complete proposal using matcher " + matcher; |
| 728 | } | |
| 729 | ||
| 730 | }; | |
| 731 | } | |
| 732 | ||
| 733 | 2 | private void activateAutoCompleteShell() { |
| 734 | 2 | invokeAction("ContentAssistProposal"); |
| 735 | 2 | } |
| 736 | ||
| 737 | private void invokeAction(final String actionId) { | |
| 738 | 2 | final IAction action = ((ITextEditor) partReference.getEditor(false)).getAction(actionId); |
| 739 | 2 | syncExec(new VoidResult() { |
| 740 | public void run() { | |
| 741 | 2 | log.debug(MessageFormat.format("Activating action with id {0}", actionId)); |
| 742 | 2 | action.run(); |
| 743 | 2 | } |
| 744 | }); | |
| 745 | 2 | } |
| 746 | ||
| 747 | 1 | private List<String> getRows(SWTBotTable table) { |
| 748 | 4 | int rowCount = table.rowCount(); |
| 749 | 4 | List<String> result = new ArrayList<String>(); |
| 750 | 11 | for (int i = 0; i < rowCount; i++) |
| 751 | 7 | result.add(table.cell(i, 0)); |
| 752 | 4 | return result; |
| 753 | } | |
| 754 | ||
| 755 | 0 | private void activateQuickFixShell() { |
| 756 | 0 | invokeAction("QuickAssist"); |
| 757 | 0 | } |
| 758 | ||
| 759 | private void waitUntil(WaitForObjectCondition<SWTBotTable> table) { | |
| 760 | 2 | bot.waitUntil(table, 10000); |
| 761 | 2 | } |
| 762 | ||
| 763 | } |