DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Perspectives and Views Toolbar Plugin for Eclipse

Perspectives and Views Toolbar Plugin for Eclipse

Sandip Chitale user avatar by
Sandip Chitale
·
Sep. 11, 08 · Interview
Like (0)
Save
Tweet
Share
19.19K Views

Join the DZone community and get the full member experience.

Join For Free
In the Eclipse IDE, I find it very inconvenient to go to the the Window:Open Perspective or Window:Show View submenu, and then select the one I want in dialog that is shown. I was inconvenienced enough that I wrote a simple Eclipse Plug-in called - Perspectives and Views Toolbar. The basic idea is that it adds two drop down menus to the toolbar. Using the drop down you can directly select the Perspective or the View that you want to show. In this article I show how to implement the Perspectives and Views Toolbar plug-in.

Perspectives and Views Toolbar

[img_assist|nid=4962|title=|desc=|link=none|align=undefined|width=601|height=322]

Implementing actions

Extension Point

The following extension point declares the actions for showing the Perspectives and Views drop down tool bar buttons. The visible="true" (1) attribute of the acitionSet element makes the actions always visible. Of source the user can hide the actions using the Window:Customize Perspective. Also note that the actions appear in the reverse order of declarion in the plugin.xml file.
   <extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Perspectives and Views Toolbar"
1 visible="true"
id="PerspectivesViewsToolbar.actionSet">
<action
class="perspectivesviewstoolbar.actions.ShowViewsPulldownMenuAction"
icon="icons/views.gif"
id="PerspectivesViewsToolbar.ViewsPulldownAction"
label="Show Views"
style="pulldown"
toolbarPath="PerspectivesViewsToolbar/additions"
tooltip="Show Views">
</action>
<action
class="perspectivesviewstoolbar.actions.ShowPerspectivesPulldownMenuAction"
icon="icons/perspectives.gif"
id="PerspectivesViewsToolbar.PerspectivesPulldownAction"
label="Show Perspectives"
style="pulldown"
toolbarPath="PerspectivesViewsToolbar/additions"
tooltip="Show Perspectives">
</action>
</actionSet>
</extension>

ShowPerspectivesPulldownMenu Action

The action implements the IWorkbenchWindowPulldownDelegate (1) interface - which is what makes it a drop down menu action. Every time the action is invoked the getMenu() (2) method is called. When the getMenu() method is called the first time the drop down menu is created and is populated with menu items that represent all the Perspectives that are registered 3 with Eclipse.

package perspectivesviewstoolbar.actions;

import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

/**
* This action shows the alphabetically sorted pull-down menu of all registered
* perspectives. Selecting the menu item shows and activates the perspective.
* The menu item for the currently active perspective is shown checked and
IWorkbenchWindowPulldownDelegate * disabled.
*
* @author Sandip V. Chitale
*
*/
public class ShowPerspectivesPulldownMenuAction implements
1 IWorkbenchWindowPulldownDelegate {

private Menu showPerspectivesPulldownMenu;

2 public Menu getMenu(Control parent) {
if (showPerspectivesPulldownMenu == null) {
// Build the menu
showPerspectivesPulldownMenu = createPerspectiveMenu(parent,
showPerspectivesPulldownMenu);
}
// Determine active perspective id
IWorkbench workbench = PlatformUI.getWorkbench();
IPerspectiveDescriptor perspective = workbench
.getActiveWorkbenchWindow().getActivePage().getPerspective();
String id = null;
if (perspective != null) {
id = perspective.getId();
}
MenuItem[] items = showPerspectivesPulldownMenu.getItems();
for (MenuItem item : items) {
if (id == null) {
// No perspective is active
item.setEnabled(true);
} else {
// Check and disable the menuItem for the active perspective
boolean equals = item.getData().equals(id);
item.setEnabled(!equals);
}
}
return showPerspectivesPulldownMenu;
}

private static Menu createPerspectiveMenu(Control parent, Menu menu) {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (menu == null) {
menu = new Menu(parent);
3 IPerspectiveRegistry perspectiveRegistry = PlatformUI
.getWorkbench().getPerspectiveRegistry();
// Get all perspectives
IPerspectiveDescriptor[] perspectiveDescriptors = perspectiveRegistry
.getPerspectives();

// Sort alphabetically by label
Arrays.sort(perspectiveDescriptors,
new Comparator<IPerspectiveDescriptor>() {
public int compare(IPerspectiveDescriptor pd1,
IPerspectiveDescriptor pd2) {
return pd1.getLabel().compareTo(pd2.getLabel());
}
});

// Configure the menu items for each Perspective
for (IPerspectiveDescriptor perspectiveDescriptor : perspectiveDescriptors) {
final MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText(perspectiveDescriptor.getLabel());
menuItem.setImage(perspectiveDescriptor.getImageDescriptor()
.createImage());
menuItem.setData(perspectiveDescriptor.getId());

// Handle selection
menuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
IPerspectiveDescriptor perspectiveDescriptorWithId = workbench
.getPerspectiveRegistry()
.findPerspectiveWithId(
(String) e.widget.getData());
if (perspectiveDescriptorWithId != null) {
workbench.showPerspective(
perspectiveDescriptorWithId.getId(),
workbench.getActiveWorkbenchWindow());
} else {
// may be delete this menuItem ?
}
} catch (WorkbenchException we) {
}
}
});
}
} else {
// Delete children
}

return menu;
}

public void dispose() {
if (showPerspectivesPulldownMenu != null) {
showPerspectivesPulldownMenu.dispose();
}
}

public void init(IWorkbenchWindow window) {
}

public void run(IAction action) {
}

public void selectionChanged(IAction action, ISelection selection) {
}

}

ShowViewsPulldownMenu Action

The action implements the IWorkbenchWindowPulldownDelegate (1) interface - which is what makes it a drop down menu action. Every time the action is invoked the getMenu() (2) method is called. When the getMenu() method is called the first time the drop down menu is created and is populated with menu items that represent all the Views that are registered 3 with Eclipse.
package perspectivesviewstoolbar.actions;

import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.IViewDescriptor;
import org.eclipse.ui.views.IViewRegistry;

/**
* This action shows the alphabetically sorted pull-down menu of all registered
* views. Selecting the menu item shows and activates the view. The menu item
* for the currently active view is shown checked and disabled.
*
* @author Sandip V. Chitale
*
*/
public class ShowViewsPulldownMenuAction implements
1 IWorkbenchWindowPulldownDelegate {

private Menu showViewsPulldownMenu;

2 public Menu getMenu(Control parent) {
if (showViewsPulldownMenu == null) {
// Build the menu
showViewsPulldownMenu = createViewsMenu(parent,
showViewsPulldownMenu);
}
// Determine the active view and use it to enable and check
// the menu items
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchPartReference activePartReference = workbench
.getActiveWorkbenchWindow().getActivePage()
.getActivePartReference();
String id = null;
if (activePartReference instanceof IViewReference) {
id = activePartReference.getId();
}
MenuItem[] items = showViewsPulldownMenu.getItems();
for (MenuItem item : items) {
if (id == null) {
// No view is active
item.setEnabled(true);
} else {
// Check and disable the menuItem for the active view
boolean equals = id.equals(item.getData());
item.setEnabled(!equals);
}
}
return showViewsPulldownMenu;
}

private static Menu createViewsMenu(Control parent, Menu menu) {
if (menu == null) {
menu = new Menu(parent);
3 IViewRegistry viewsRegistry = PlatformUI.getWorkbench()
.getViewRegistry();
// Get all views
IViewDescriptor[] viewDescriptors = viewsRegistry.getViews();

// Sort alphabetically by label
Arrays.sort(viewDescriptors, new Comparator<IViewDescriptor>() {
public int compare(IViewDescriptor vd1, IViewDescriptor vd2) {
return vd1.getLabel().compareTo(vd2.getLabel());
}
});

// Configure the menu items for each View
for (IViewDescriptor viewDescriptor : viewDescriptors) {
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText(viewDescriptor.getLabel());
menuItem.setImage(viewDescriptor.getImageDescriptor()
.createImage());
menuItem.setData(viewDescriptor.getId());
// Handle selection
menuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IWorkbench workbench = PlatformUI.getWorkbench();
try {
IViewDescriptor viewWithId = workbench
.getViewRegistry().find(
(String) e.widget.getData());
if (viewWithId != null) {
IWorkbenchPage activePage = workbench
.getActiveWorkbenchWindow()
.getActivePage();
IViewPart view = activePage.showView(viewWithId
.getId(), null,
IWorkbenchPage.VIEW_CREATE);
activePage.activate(view);
} else {
// may be delete this menuItem ?
}
} catch (PartInitException pie) {
}
}
});
}
} else {
// Delete children
}
return menu;
}

public void dispose() {
if (showViewsPulldownMenu != null) {
showViewsPulldownMenu.dispose();
}
}

public void init(IWorkbenchWindow window) {
}

public void run(IAction action) {
}

public void selectionChanged(IAction action, ISelection selection) {
}

}

MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Perspectives and Views Toolbar Plug-in
Bundle-SymbolicName: PerspectivesViewsToolbar;singleton:=true
Bundle-Version: 1.0.1
Bundle-Activator: perspectivesviewstoolbar.Activator
Bundle-Vendor: Sandip V. Chitale
Require-Bundle: org.eclipse.ui,org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy

Installation

The resources section has the link to the plugin jar which can be dropped into your Eclipse installation's plugins directory. Once installed you will be able to use the Perspectives and Views tool bar buttons.

Source Code

The source code is bundled in the plug-in's jar file. In fact you can import the source plug-in project into  PDE  and hack it further. The source code is also available here.

Conclusion

In this tutorial we built a simple yet useful Eclipse plug-in called - Perspectives and Views Toolbar. It makes use of the actionSet extension points to add useful functionality to Eclipse. It also how to make the actions permanently visible.

Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.

Resources

Notes on the Eclipse Plug-in Architecture
PDE Does Plug-ins
Contributing Actions to the Eclipse Workbench
Download
Perspectives and Views Toolbar Plug-in
Sources
Eclipse

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices for Setting up Monitoring Operations for Your AI Team
  • Demystifying Multi-Cloud Integration
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Introduction to NoSQL Database

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: