|
1 |
package org.txm.groovy.ide.rcp.views;
|
|
2 |
|
|
3 |
|
|
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;
|
|
13 |
|
|
14 |
|
|
15 |
/**
|
|
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.
|
|
22 |
* <p>
|
|
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.
|
|
30 |
* <p>
|
|
31 |
*/
|
|
32 |
|
|
33 |
public class SampleView extends ViewPart {
|
|
34 |
|
|
35 |
/**
|
|
36 |
* The ID of the view as specified by the extension.
|
|
37 |
*/
|
|
38 |
public static final String ID = "org.txm.groovy.rcp.views.SampleView";
|
|
39 |
|
|
40 |
private TableViewer viewer;
|
|
41 |
|
|
42 |
private Action action1;
|
|
43 |
|
|
44 |
private Action action2;
|
|
45 |
|
|
46 |
private Action doubleClickAction;
|
|
47 |
|
|
48 |
/*
|
|
49 |
* The content provider class is responsible for
|
|
50 |
* providing objects to the view. It can wrap
|
|
51 |
* existing objects in adapters or simply return
|
|
52 |
* objects as-is. These objects may be sensitive
|
|
53 |
* to the current input of the view, or ignore
|
|
54 |
* it and always show the same content
|
|
55 |
* (like Task List, for example).
|
|
56 |
*/
|
|
57 |
|
|
58 |
class ViewContentProvider implements IStructuredContentProvider {
|
|
59 |
|
|
60 |
public void inputChanged(Viewer v, Object oldInput, Object newInput) {}
|
|
61 |
|
|
62 |
public void dispose() {}
|
|
63 |
|
|
64 |
public Object[] getElements(Object parent) {
|
|
65 |
return new String[] { "One", "Two", "Three" };
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
|
|
70 |
|
|
71 |
public String getColumnText(Object obj, int index) {
|
|
72 |
return getText(obj);
|
|
73 |
}
|
|
74 |
|
|
75 |
public Image getColumnImage(Object obj, int index) {
|
|
76 |
return getImage(obj);
|
|
77 |
}
|
|
78 |
|
|
79 |
public Image getImage(Object obj) {
|
|
80 |
return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
class NameSorter extends ViewerSorter {
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* The constructor.
|
|
89 |
*/
|
|
90 |
public SampleView() {}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* This is a callback that will allow us
|
|
94 |
* to create the viewer and initialize it.
|
|
95 |
*/
|
|
96 |
public void createPartControl(Composite parent) {
|
|
97 |
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
|
|
98 |
viewer.setContentProvider(new ViewContentProvider());
|
|
99 |
viewer.setLabelProvider(new ViewLabelProvider());
|
|
100 |
viewer.setSorter(new NameSorter());
|
|
101 |
viewer.setInput(getViewSite());
|
|
102 |
|
|
103 |
// Create the help context id for the viewer's control
|
|
104 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "org.txm.groovy.rcp.viewer");
|
|
105 |
makeActions();
|
|
106 |
hookContextMenu();
|
|
107 |
hookDoubleClickAction();
|
|
108 |
contributeToActionBars();
|
|
109 |
}
|
|
110 |
|
|
111 |
private void hookContextMenu() {
|
|
112 |
MenuManager menuMgr = new MenuManager("#PopupMenu");
|
|
113 |
menuMgr.setRemoveAllWhenShown(true);
|
|
114 |
menuMgr.addMenuListener(new IMenuListener() {
|
|
115 |
|
|
116 |
public void menuAboutToShow(IMenuManager manager) {
|
|
117 |
SampleView.this.fillContextMenu(manager);
|
|
118 |
}
|
|
119 |
});
|
|
120 |
Menu menu = menuMgr.createContextMenu(viewer.getControl());
|
|
121 |
viewer.getControl().setMenu(menu);
|
|
122 |
getSite().registerContextMenu(menuMgr, viewer);
|
|
123 |
}
|
|
124 |
|
|
125 |
private void contributeToActionBars() {
|
|
126 |
IActionBars bars = getViewSite().getActionBars();
|
|
127 |
fillLocalPullDown(bars.getMenuManager());
|
|
128 |
fillLocalToolBar(bars.getToolBarManager());
|
|
129 |
}
|
|
130 |
|
|
131 |
private void fillLocalPullDown(IMenuManager manager) {
|
|
132 |
manager.add(action1);
|
|
133 |
manager.add(new Separator());
|
|
134 |
manager.add(action2);
|
|
135 |
}
|
|
136 |
|
|
137 |
private void fillContextMenu(IMenuManager manager) {
|
|
138 |
manager.add(action1);
|
|
139 |
manager.add(action2);
|
|
140 |
// Other plug-ins can contribute there actions here
|
|
141 |
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
|
|
142 |
}
|
|
143 |
|
|
144 |
private void fillLocalToolBar(IToolBarManager manager) {
|
|
145 |
manager.add(action1);
|
|
146 |
manager.add(action2);
|
|
147 |
}
|
|
148 |
|
|
149 |
private void makeActions() {
|
|
150 |
action1 = new Action() {
|
|
151 |
|
|
152 |
public void run() {
|
|
153 |
showMessage("Action 1 executed");
|
|
154 |
}
|
|
155 |
};
|
|
156 |
action1.setText("Action 1");
|
|
157 |
action1.setToolTipText("Action 1 tooltip");
|
|
158 |
action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
|
|
159 |
|
|
160 |
action2 = new Action() {
|
|
161 |
|
|
162 |
public void run() {
|
|
163 |
showMessage("Action 2 executed");
|
|
164 |
}
|
|
165 |
};
|
|
166 |
action2.setText("Action 2");
|
|
167 |
action2.setToolTipText("Action 2 tooltip");
|
|
168 |
action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
|
|
169 |
doubleClickAction = new Action() {
|
|
170 |
|
|
171 |
public void run() {
|
|
172 |
ISelection selection = viewer.getSelection();
|
|
173 |
Object obj = ((IStructuredSelection) selection).getFirstElement();
|
|
174 |
showMessage("Double-click detected on " + obj.toString());
|
|
175 |
}
|
|
176 |
};
|
|
177 |
}
|
|
178 |
|
|
179 |
private void hookDoubleClickAction() {
|
|
180 |
viewer.addDoubleClickListener(new IDoubleClickListener() {
|
|
181 |
|
|
182 |
public void doubleClick(DoubleClickEvent event) {
|
|
183 |
doubleClickAction.run();
|
|
184 |
}
|
|
185 |
});
|
|
186 |
}
|
|
187 |
|
|
188 |
private void showMessage(String message) {
|
|
189 |
MessageDialog.openInformation(
|
|
190 |
viewer.getControl().getShell(),
|
|
191 |
"Sample View",
|
|
192 |
message);
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Passing the focus request to the viewer's control.
|
|
197 |
*/
|
|
198 |
public void setFocus() {
|
|
199 |
viewer.getControl().setFocus();
|
|
200 |
}
|
|
201 |
}
|
0 |
202 |
|