1 package argeo
.slc
.testplugin
.views
;
4 import org
.eclipse
.swt
.widgets
.Composite
;
5 import org
.eclipse
.ui
.part
.*;
6 import org
.eclipse
.jface
.viewers
.*;
7 import org
.eclipse
.swt
.graphics
.Image
;
8 import org
.eclipse
.jface
.action
.*;
9 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
10 import org
.eclipse
.ui
.*;
11 import org
.eclipse
.swt
.widgets
.Menu
;
12 import org
.eclipse
.swt
.SWT
;
16 * This sample class demonstrates how to plug-in a new
17 * workbench view. The view shows data obtained from the
18 * model. The sample creates a dummy model on the fly,
19 * but a real implementation would connect to the model
20 * available either in this or another plug-in (e.g. the workspace).
21 * The view is connected to the model using a content provider.
23 * The view uses a label provider to define how model
24 * objects should be presented in the view. Each
25 * view can present the same model objects using
26 * different labels and icons, if needed. Alternatively,
27 * a single label provider can be shared between views
28 * in order to ensure that objects of the same type are
29 * presented in the same way everywhere.
33 public class SampleView
extends ViewPart
{
34 private TableViewer viewer
;
35 private Action action1
;
36 private Action action2
;
37 private Action doubleClickAction
;
40 * The content provider class is responsible for
41 * providing objects to the view. It can wrap
42 * existing objects in adapters or simply return
43 * objects as-is. These objects may be sensitive
44 * to the current input of the view, or ignore
45 * it and always show the same content
46 * (like Task List, for example).
49 class ViewContentProvider
implements IStructuredContentProvider
{
50 public void inputChanged(Viewer v
, Object oldInput
, Object newInput
) {
52 public void dispose() {
54 public Object
[] getElements(Object parent
) {
55 return new String
[] { "One", "Two", "Three" };
58 class ViewLabelProvider
extends LabelProvider
implements ITableLabelProvider
{
59 public String
getColumnText(Object obj
, int index
) {
62 public Image
getColumnImage(Object obj
, int index
) {
65 public Image
getImage(Object obj
) {
66 return PlatformUI
.getWorkbench().
67 getSharedImages().getImage(ISharedImages
.IMG_OBJ_ELEMENT
);
70 class NameSorter
extends ViewerSorter
{
80 * This is a callback that will allow us
81 * to create the viewer and initialize it.
83 public void createPartControl(Composite parent
) {
84 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
85 viewer
.setContentProvider(new ViewContentProvider());
86 viewer
.setLabelProvider(new ViewLabelProvider());
87 viewer
.setSorter(new NameSorter());
88 viewer
.setInput(getViewSite());
90 // Create the help context id for the viewer's control
91 PlatformUI
.getWorkbench().getHelpSystem().setHelp(viewer
.getControl(), "argeo.slc.testplugin.viewer");
94 hookDoubleClickAction();
95 contributeToActionBars();
98 private void hookContextMenu() {
99 MenuManager menuMgr
= new MenuManager("#PopupMenu");
100 menuMgr
.setRemoveAllWhenShown(true);
101 menuMgr
.addMenuListener(new IMenuListener() {
102 public void menuAboutToShow(IMenuManager manager
) {
103 SampleView
.this.fillContextMenu(manager
);
106 Menu menu
= menuMgr
.createContextMenu(viewer
.getControl());
107 viewer
.getControl().setMenu(menu
);
108 getSite().registerContextMenu(menuMgr
, viewer
);
111 private void contributeToActionBars() {
112 IActionBars bars
= getViewSite().getActionBars();
113 fillLocalPullDown(bars
.getMenuManager());
114 fillLocalToolBar(bars
.getToolBarManager());
117 private void fillLocalPullDown(IMenuManager manager
) {
118 manager
.add(action1
);
119 manager
.add(new Separator());
120 manager
.add(action2
);
123 private void fillContextMenu(IMenuManager manager
) {
124 manager
.add(action1
);
125 manager
.add(action2
);
126 // Other plug-ins can contribute there actions here
127 manager
.add(new Separator(IWorkbenchActionConstants
.MB_ADDITIONS
));
130 private void fillLocalToolBar(IToolBarManager manager
) {
131 manager
.add(action1
);
132 manager
.add(action2
);
135 private void makeActions() {
136 action1
= new Action() {
138 showMessage("Action 1 executed");
141 action1
.setText("Action 1");
142 action1
.setToolTipText("Action 1 tooltip");
143 action1
.setImageDescriptor(PlatformUI
.getWorkbench().getSharedImages().
144 getImageDescriptor(ISharedImages
.IMG_OBJS_INFO_TSK
));
146 action2
= new Action() {
148 showMessage("Action 2 executed");
151 action2
.setText("Action 2");
152 action2
.setToolTipText("Action 2 tooltip");
153 action2
.setImageDescriptor(PlatformUI
.getWorkbench().getSharedImages().
154 getImageDescriptor(ISharedImages
.IMG_OBJS_INFO_TSK
));
155 doubleClickAction
= new Action() {
157 ISelection selection
= viewer
.getSelection();
158 Object obj
= ((IStructuredSelection
)selection
).getFirstElement();
159 showMessage("Double-click detected on "+obj
.toString());
164 private void hookDoubleClickAction() {
165 viewer
.addDoubleClickListener(new IDoubleClickListener() {
166 public void doubleClick(DoubleClickEvent event
) {
167 doubleClickAction
.run();
171 private void showMessage(String message
) {
172 MessageDialog
.openInformation(
173 viewer
.getControl().getShell(),
179 * Passing the focus request to the viewer's control.
181 public void setFocus() {
182 viewer
.getControl().setFocus();