I have built a simple Eclipse plugin where a user may use a TableViewer of database resources to open an editor on any of those resources.
Users may therefore have zero upwards instances of the editor running.
Is there an API available to get a list of those editor instances?
You can get references to all open editors with:
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getEditorReferences();
And then check these to select the ones that reference instances of your editor type.
According to the javadoc for the API a workbench can have several windows, and a window can have several pages, and they do not share editors.
So, in order to get all and every open editor, you should do something along these lines (error checking etc excluded):
List<IEditorReference> editors = new ArrayList<IEditorReference>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editor : page.getEditorReferences()) {
editors.add(editor);
}
}
}
Be aware the such an enumeration will not respect the tab order
Here is an example of an enumeration of editors:
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IWorkbenchPage page = window.getActivePage();
IEditorPart actEditor = page.getActiveEditor();
IEditorReference[] editors = page.getEditorReferences();
for (int i=0; i<editors.length-1; i++) {
if (editors[i].getEditor(true) == actEditor) {
page.activate(editors[i+1].getEditor(true));
return null;
}
}
Related
I'm developing an RCP that has two product versions, a core app and one with some extensions. If a user opens the core app after having opened the extended app in the same workspace, eclipse detects a perspective used only in the extended app and makes a local copy of it, so it shows up in the perspective toolbar as an orphaned extension.
I created an activity to hide the extended app perspective when running the core app. That hides it from the perspective menu and the perspective shortcut menu, but it doesn't remove it from the perspective toolbar.I also tried detecting orphaned perspectives from the active page of the active workbench window (by looking for angle brackets in the label) and removing them with PlatformUI.getWorkbench().getPerspectiveRegistry().deletePerspective(perspective), but this doesn't affect the perspective toolbar either. The perspective I'm removing is not present in the core app.
Is there a way I can access the perspective toolbar programmatically so I can remove any orphaned perspectives? Or any other approach tha would work?
I thought a good solution would be creating a custom perspective switcher, but that path was blocked by an eclipse bug. There is a suggested workaround, but it did not work for me. I created a custom perspective switcher toolbar, but I could find no way to make it update when perspectives are opened or activated. My attempts are documented here.
I removed the orphan perspectives in a workspace shutdown hook, but for some reason an NPE is thrown by the E4 workbench (LazyStackRenderer line 238) when I select a perspective in the switcher that was opened but not selected when I launched the app.
I got it to work as desired by closing all open perspectives on shutdown, after storing their IDs in a preference value, and then opening them again when the app is launched in the WorkbenchWindowAdvisor. It's a bit of a hack, but it's the only way I could find to avoid the E4 workbench NPE, which also prevents setting the perspective from the toolbar until it's closed and re-opened from the Window menu.
Here's my code.
...
IWorkbench workbench = ...
static final String PERPSECTIVE_ID_1 = ...
static fnal String PERSPECTIVE_ID_2 = ...
static final String PREFERENCE_KEY = ...
workbench.addWorkbenchListener( new IWorkbenchListener() {
public boolean preShutdown( IWorkbench workbench, boolean forced ) {
IPerspectiveDescriptor[] openPerspectives = page.getOpenPerspectives();
page.closeAllPerspectives(false, false);
StringBuilder sb = new StringBuilder();
String delim = "";
for (IPerspectiveDescriptor persp : openPerspectives) {
if (!persp.getId().equals(PERSPECTIVE_ID_1) && !persp.getId().equals(PERSPECTIVE_ID_2) {
sb.append(delim + persp.getId());
delim = ";";
}
}
getPreferenceStore().setValue(PREF_KEY, sb.toString());
return true;
}
public void postShutdown( IWorkbench workbench ) {
}
});
class MyWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
static final String PRODUCT_ID_1 = ...
static final String PRODUCT_ID_2 = ...
static final String PREFERENCE_KEY = ...
...
#Override
public void postWindowOpen() {
IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
String savedOpenPerspectiveStr = getPreferenceStore().getString(PREFERENCE_KEY);
if (!"".equals(savedOpenPerspectiveStr)) {
List<IPerspectiveDescriptor> openPerspectives = new ArrayList<IPerspectiveDescriptor>();
String[] perspectiveIds = savedOpenPerspectiveStr.split(";");
if (perspectiveIds.length == 0) {
openPerspectives.add(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(savedOpenPerspectiveStr));
} else {
for (String id : perspectiveIds) {
openPerspectives.add(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(id));
}
}
//successively setting perspectives causes them to appear in the perspective switcher toolbar
for (IPerspectiveDescriptor persp : openPerspectives) {
page.setPerspective(persp);
}
}
//now we set the appropriate perspective
if (Platform.getProduct().getId().equals(PRODUCT_ID_1)) {
page.setPerspective(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(PERSPECTIVE_ID_1));
} else if (Platform.getProduct().getId().equals(PRODUCT_ID_2)) {
page.setPerspective(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(PERSPECTIVE_ID_2));
}
}
...
}
I want to remove the File, edit, Source, Refactor, etc. menus from my RCP application
Can I use hideActionSet() ? or what should I do ?
That's right; in your ApplicationWorkbenchWindowAdvisor, override postWindowOpen().
The tricky bit is usually figuring out the names of the actionsets that you want to remove, but you can use the old standby ALT-SHIFT-F2 (the default keybinding for 'Plugin-in Menu Spy') and click on one of the menu items that you want to remove.
Note that if the menu item is disabled, the spy won't give you any info on it.
public void postWindowOpen() {
runApplicationWorkbenchDelegate();
// remove unwanted UI contributions that eclipse makes by default
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (int i = 0; i < windows.length; ++i) {
IWorkbenchPage page = windows[i].getActivePage();
if (page != null) {
// hide generic 'File' commands
page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
// hide 'Convert Line Delimiters To...'
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo");
// hide 'Search' commands
page.hideActionSet("org.eclipse.search.searchActionSet");
// hide 'Annotation' commands
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.annotationNavigation");
// hide 'Forward/Back' type navigation commands
page.hideActionSet("org.eclipse.ui.edit.text.actionSet.navigation");
}
}
}
Although the question is old:
Lars Vogel's tutorial about Eclipse Activities shows how to hide entire menus in an RCP application rather than removing single menu-entries.
EDIT:
Alternatively you can use the MenuManager attached to the workbench window to show or hide Menus/Contributions.
Try the following code to hide all menus:
WorkbenchWindow workbenchWin = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
MenuManager menuManager = workbenchWin.getMenuManager();
IContributionItem[] items = menuManager.getItems();
for(IContributionItem item : items) {
item.setVisible(false);
}
favorite
I have a UI in which when I select an item (in a tree) and then press a button "add", I get a new editor. With each item I can get an editor. (but all have the same ID)
My purpose is to close only the editor of item1, for example, when I press "save". I'm able to close all the editors with: getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);
But not only the one that I need to close. The following solution helped me:
// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);
// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getEditorReferences();
List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().equals(tmpInput)) {
relevantEditors.add(iEditorReference);
}
}
PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.closeEditors(
(IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
.size()]), true);
….but I still have some problems... As I can open many editors in the same time, and all of them have the same button "save", it happens that I press "save" in editor1 but close editor3... Actually, I save the last editor to be open (thanks to its "item")... this is the problem.. So I'm wondering if there is a way to identify the editor in which the button exists, so that I close it..
Thanks a lot I appreciate any help or hint (Sorry if my questions look easy and not worth being asked, but I'm still a beginner...)
if the Button is rendered in your IEditorPart implementation, you can close the editor directly in your EditorPart.
button.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().closeEditor(this, true);
}
});
Selected editor open or another editor can be close using RCP eclipse.
Multiple Editor open at time selected editor can be open or close using RCP eclipse.
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
EmployeeEditorInput input = new EmployeeEditorInput();
//List out all the editors open
IEditorReference[] editors = page.getEditorReferences();
for (int i=0; i<editors.length; i++) {
//class : EmployeeEditor
//public static final String Id="rcp_demo.Editor.emp";
if (editors[i].getId().equals(EmployeeEditor.Id)) {
page.activate(editors[i].getEditor(true));
//or
//page.closeEditor(page.getActiveEditor(),true);
System.out.println("Employee Editor Exist");
return null;
}
else
{
page.closeEditor(page.getActiveEditor(), true);
System.out.println("Close other Editor");
}
}
I'm developing an Eclipse plug-in where upon pressing a button, the plug-in takes the selected text in the Java editor and puts in a text box which appears.
My code looks like this: I got it from here: http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg02200.html
private ITextSelection getSelection(ITextEditor editor) {
ISelection selection = editor.getSelectionProvider()
.getSelection();
return (ITextSelection) selection;
}
private String getSelectedText(ITextEditor editor) {
return getSelection(editor).getText();
}
The problem is how will I get the ITextEditor of the Java editor being displayed. Coincidentally it's the next question in the thread in the link I posted but it's unanswered :(
You could ask for the ActiveEditor, as in this thread:
IEditorPart part;
part =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().get
ActiveEditor();
if(part instanceof ITextEditor){
ITextEditor editor = (ITextEditor)part;
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
The OP Krt_Malta mentions this blog entry "Programmatically query current text selection", which is similar to this other SO answer (written before the blog entry) "Replace selected code from eclipse editor through plugin command".
I'd like to add one thing to VonCs answer. The technique he describes to get the selection is useful for all kinds of text editors, not only Java editors as this questions is about. But his solution does not work in the case that the workspace part is a MultiPageEditorPart, since that is not a ITextEditor.
But in many cases (for example with the standard XML editor) a MultiPageEditorPart has pages which are ITextEditors. In those cases you can get the active page from a MultiPageEditorPart and get the selection from that.
This can be done with the following code:
ITextEditor editor = null;
if (part instanceof ITextEditor) {
editor = (ITextEditor) part;
} else if (part instanceof MultiPageEditorPart) {
Object page = ((MultiPageEditorPart) part).getSelectedPage();
if (page instanceof ITextEditor) editor = (ITextEditor) page;
}
if (editor != null) {
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
}
Eclipse RCP question
I open file with:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = IDE.openEditor(page, file);
I also get document with:
IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
I need to get to text viewer of that document (for creating LinkedModeUI), is there any way to do this?
The following worked for me:
IEditorPart editorPart = getSite().getPage().getActiveEditor();
if (editorPart != null) {
ITextOperationTarget target =
(ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = (ITextViewer)target;
// ...
}
}
1) One document can be opened with more than one editor. You'll have to iterate all editors to look for your file's editors.
2) Viewer is encapsulated in editor. I think the only way is extend editor class to add getter. Or redefine it, if viewer is inaccessible from inheritors.