12.2 Creating a View
In the next example,
we're going to use a plug-in to create a new view.
Start by creating a new plug-in project named
org.eclipsebook.ch12.Ch12_02. In the Plug-in Code
Generators pane, which you see in Figure 12-8,
select the "Plug-in with a view"
wizard and click Next.
In the following pane, give Eclipse Book as
the provider's name and click Next again to bring up
the Main View Settings dialog you see in Figure 12-9. In this pane, you set the name of the view
and its category�we'll stick with the
defaults, which will make this a table-based view (i.e., the items in
the view will be displayed in a table control) named Sample View in
the category Sample Category. Click Next again to bring up the final
pane of this wizard.
The last pane,
shown in Figure 12-10, lets you configure the
view's actions, such as responding when the user
double-clicks an item in the view. Leave the defaults selected and
click Finish to create the framework for this plug-in.
Here are the files created and added to
the project's src folder:
src
|_ _org.eclipsebook.ch12.Ch12_02
| |_ _Ch12_02Plugin.java The standard plug-in file
|
|_ _org.eclipsebook.ch12.Ch1202.views
| |_ _SampleView.java The view file
|
|__ _plugin.xml
12.2.1 Adding Items to the View
The new view
is supported in SampleView.java, which
we're going to adapt so that it will display four
clickable items. In SampleView.java, the
SampleView class extends the
ViewPart class, which is the abstract class you
base views on in plug-ins:
public class SampleView extends ViewPart {
private TableViewer viewer;
private Action action1;
private Action action2;
private Action doubleClickAction;
.
.
.
Eclipse gets the content displayed in
the view by calling methods in an object that implements the
IStructuredContentProvider interface.
We're going to adapt the code in the class used to
create that object, the ViewContentProvider class,
to make the view display a set of four items, "Item
1" to "Item 4" in
this example. Here's the code:
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose( ) {
}
public Object[] getElements(Object parent) {
return new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
}
}
The
view is created when Eclipse calls the
createPartControl method. Here's
how the code uses our ViewContentProvider class to
get the content for the view:
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider( ));
viewer.setLabelProvider(new ViewLabelProvider( ));
viewer.setSorter(new NameSorter( ));
viewer.setInput(ResourcesPlugin.getWorkspace( ));
makeActions( );
hookContextMenu( );
hookDoubleClickAction( );
contributeToActionBars( );
}
That sets up our four new items, "Item
1" to "Item 4", in
the view. These items will appear in a table, one on top of the
other. The next step is to actually do something when the user wants
to work with one of these items.
12.2.2 Configuring View Actions
The rest of the code in
SampleView.java supports the
view's actions when you click, double-click, or
right-click items in the view. We'll change the code
in the makeActions method to customize the
view's actions to display an appropriate message
when an item is selected or double-clicked; you get access to the
object corresponding to the item that was selected or double-clicked
this way: Object obj =
((IStructuredSelection)selection).getFirstElement(
). Here's the modified code that will
determine which item the user wants to work with and display a
message that indicates the item by name:
private void makeActions( ) {
action1 = new Action( ) {
public void run( ) {
ISelection selection = viewer.getSelection( );
Object obj =
((IStructuredSelection)selection).getFirstElement( );
showMessage("You selected " + obj.toString( ));
}
};
action1.setText("Action 1");
action1.setToolTipText("Action 1 tooltip");
action1.setImageDescriptor(PlatformUI.getWorkbench( ).getSharedImages( ).
getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
action2 = new Action( ) {
public void run( ) {
showMessage("Action 2 executed");
}
};
action2.setText("Action 2");
action2.setToolTipText("Action 2 tooltip");
action2.setImageDescriptor(PlatformUI.getWorkbench( ).getSharedImages( ).
getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
doubleClickAction = new Action( ) {
public void run( ) {
ISelection selection = viewer.getSelection( );
Object obj =
((IStructuredSelection)selection).getFirstElement( );
showMessage("You double-clicked " + obj.toString( ));
}
};
}
As you can see, the code that the PDE
wizard creates is designed to be relatively easily modified. To see
this new view at work, start the Run-time Workbench and select
Window Show View Other. Select the Sample View
item in the Sample Category folder, as you see in Figure 12-11, and click OK to display the new view.
The new view appears in Figure 12-12, where you can
see the four sample items we created at the bottom of Eclipse. Very
cool�we've added a new, functional view to
Eclipse.
Right-clicking
an item and selecting the Action Item 1 executes the code
we've added to display the message box you see in
Figure 12-13, where the selected item is identified.
Similarly, double-clicking an item
displays the message box you see in Figure 12-14,
where the double-clicked item is identified in a message box.
That's what we wanted to do�create a new view
and handle user actions with the items in the view.
We've been able to create plug-ins with views,
editors, and even wizards. Now that you've gotten
the basics of plug-in creation under your belt, our last topic in
this chapter will be about how to deploy your plug-ins.
|
No comments:
Post a Comment