Coverage Report - org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotEditor
66%
12/18
100%
2/2
1
SWTBotEditor$1
0%
0/5
N/A
1
SWTBotEditor$2
100%
4/4
N/A
1
SWTBotEditor$3
100%
5/5
N/A
1
SWTBotEditor$4
0%
0/5
N/A
1
SWTBotEditor$5
100%
3/3
N/A
1
 
 1  13
 /*******************************************************************************
 2  
  * Copyright (c) 2008,2009,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  
  *     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  
  *     Ralf Ebert - (bug 294452) - SWTBotEditor does not pass an IProgressMonitor when saving an editor
 13  
  *     Ketan Padegaonkar - (bug 260088) Support for MultiPageEditorPart
 14  
  *******************************************************************************/
 15  
 package org.eclipse.swtbot.eclipse.finder.widgets;
 16  
 
 17  
 import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec;
 18  
 
 19  
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
 20  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 21  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 22  
 import org.eclipse.swtbot.swt.finder.results.Result;
 23  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 24  
 import org.eclipse.ui.IEditorPart;
 25  
 import org.eclipse.ui.IEditorReference;
 26  
 import org.hamcrest.SelfDescribing;
 27  
 
 28  
 /**
 29  
  * This represents an Eclipse workbench editor part.
 30  
  * 
 31  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 32  
  * @author Ralf Ebert www.ralfebert.de (bug 271630)
 33  
  * @version $Id$
 34  
  */
 35  
 public class SWTBotEditor extends SWTBotWorkbenchPart<IEditorReference> {
 36  
 
 37  
         /**
 38  
          * Constructs an instance for the given editorReference.
 39  
          * 
 40  
          * @param editorReference the editor reference.
 41  
          * @param bot the instance of {@link SWTWorkbenchBot} which will be used to drive operations on behalf of this
 42  
          *            object.
 43  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 44  
          * @since 2.0
 45  
          */
 46  
         public SWTBotEditor(IEditorReference editorReference, SWTWorkbenchBot bot) throws WidgetNotFoundException {
 47  42
                 super(editorReference, bot);
 48  42
         }
 49  
 
 50  
         /**
 51  
          * Constructs an instance for the given editorReference.
 52  
          * 
 53  
          * @param editorReference the part reference.
 54  
          * @param bot the helper bot.
 55  
          * @param description the description of the editor part.
 56  
          */
 57  
         public SWTBotEditor(IEditorReference editorReference, SWTWorkbenchBot bot, SelfDescribing description) {
 58  0
                 super(editorReference, bot, description);
 59  0
         }
 60  
 
 61  
         public boolean isActive() {
 62  5
                 return bot.activeEditor().partReference == partReference;
 63  
         }
 64  
 
 65  
         public void setFocus() {
 66  0
                 syncExec(new VoidResult() {
 67  
                         public void run() {
 68  0
                                 IEditorPart editor = partReference.getEditor(true);
 69  0
                                 editor.setFocus();
 70  0
                         }
 71  
                 });
 72  0
         }
 73  
 
 74  
         /**
 75  
          * Save the editor and close it.
 76  
          */
 77  
         public void saveAndClose() {
 78  13
                 save();
 79  13
                 close();
 80  13
         }
 81  
 
 82  
         public void close() {
 83  13
                 UIThreadRunnable.syncExec(new VoidResult() {
 84  
                         public void run() {
 85  13
                                 partReference.getPage().closeEditor(partReference.getEditor(false), false);
 86  13
                         }
 87  
                 });
 88  13
         }
 89  
 
 90  
         /**
 91  
          * Save the editor.
 92  
          */
 93  
         public void save() {
 94  18
                 UIThreadRunnable.syncExec(new VoidResult() {
 95  
                         public void run() {
 96  18
                                 IEditorPart editor = partReference.getEditor(false);
 97  18
                                 partReference.getPage().saveEditor(editor, false);
 98  18
                         }
 99  
                 });
 100  18
         }
 101  
 
 102  
         /**
 103  
          * Shows the editor if it is visible.
 104  
          */
 105  
         public void show() {
 106  0
                 UIThreadRunnable.syncExec(new VoidResult() {
 107  
                         public void run() {
 108  0
                                 IEditorPart editor = partReference.getEditor(true);
 109  0
                                 partReference.getPage().activate(editor);
 110  0
                         }
 111  
                 });
 112  0
         }
 113  
 
 114  
         /**
 115  
          * Returns true if the editor is dirty.
 116  
          * 
 117  
          * @return dirty state of editor
 118  
          */
 119  
         public boolean isDirty() {
 120  3
                 return UIThreadRunnable.syncExec(new Result<Boolean>() {
 121  
                         public Boolean run() {
 122  3
                                 return partReference.isDirty();
 123  
                         }
 124  
                 });
 125  
         }
 126  
 
 127  
         /**
 128  
          * @return an extended version of the editor bot which provides methods for text editors.
 129  
          * @throws WidgetNotFoundException if this is not a text editor
 130  
          */
 131  
         public SWTBotEclipseEditor toTextEditor() {
 132  3
                 return new SWTBotEclipseEditor(this.partReference, this.bot);
 133  
         }
 134  
 
 135  
 }