]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/maven-argeo-pde-plugin/src/test/eclipse/plugins/argeo.slc.testplugin/src/argeo/slc/testplugin/views/SampleView.java
2c306d9fc514276fa557bec6aa68661249825760
[gpl/argeo-slc.git] / plugins / maven-argeo-pde-plugin / src / test / eclipse / plugins / argeo.slc.testplugin / src / argeo / slc / testplugin / views / SampleView.java
1 package argeo.slc.testplugin.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 private TableViewer viewer;
35 private Action action1;
36 private Action action2;
37 private Action doubleClickAction;
38
39 /*
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).
47 */
48
49 class ViewContentProvider implements IStructuredContentProvider {
50 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
51 }
52 public void dispose() {
53 }
54 public Object[] getElements(Object parent) {
55 return new String[] { "One", "Two", "Three" };
56 }
57 }
58 class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
59 public String getColumnText(Object obj, int index) {
60 return getText(obj);
61 }
62 public Image getColumnImage(Object obj, int index) {
63 return getImage(obj);
64 }
65 public Image getImage(Object obj) {
66 return PlatformUI.getWorkbench().
67 getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
68 }
69 }
70 class NameSorter extends ViewerSorter {
71 }
72
73 /**
74 * The constructor.
75 */
76 public SampleView() {
77 }
78
79 /**
80 * This is a callback that will allow us
81 * to create the viewer and initialize it.
82 */
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());
89
90 // Create the help context id for the viewer's control
91 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "argeo.slc.testplugin.viewer");
92 makeActions();
93 hookContextMenu();
94 hookDoubleClickAction();
95 contributeToActionBars();
96 }
97
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);
104 }
105 });
106 Menu menu = menuMgr.createContextMenu(viewer.getControl());
107 viewer.getControl().setMenu(menu);
108 getSite().registerContextMenu(menuMgr, viewer);
109 }
110
111 private void contributeToActionBars() {
112 IActionBars bars = getViewSite().getActionBars();
113 fillLocalPullDown(bars.getMenuManager());
114 fillLocalToolBar(bars.getToolBarManager());
115 }
116
117 private void fillLocalPullDown(IMenuManager manager) {
118 manager.add(action1);
119 manager.add(new Separator());
120 manager.add(action2);
121 }
122
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));
128 }
129
130 private void fillLocalToolBar(IToolBarManager manager) {
131 manager.add(action1);
132 manager.add(action2);
133 }
134
135 private void makeActions() {
136 action1 = new Action() {
137 public void run() {
138 showMessage("Action 1 executed");
139 }
140 };
141 action1.setText("Action 1");
142 action1.setToolTipText("Action 1 tooltip");
143 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
144 getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
145
146 action2 = new Action() {
147 public void run() {
148 showMessage("Action 2 executed");
149 }
150 };
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() {
156 public void run() {
157 ISelection selection = viewer.getSelection();
158 Object obj = ((IStructuredSelection)selection).getFirstElement();
159 showMessage("Double-click detected on "+obj.toString());
160 }
161 };
162 }
163
164 private void hookDoubleClickAction() {
165 viewer.addDoubleClickListener(new IDoubleClickListener() {
166 public void doubleClick(DoubleClickEvent event) {
167 doubleClickAction.run();
168 }
169 });
170 }
171 private void showMessage(String message) {
172 MessageDialog.openInformation(
173 viewer.getControl().getShell(),
174 "Sample View",
175 message);
176 }
177
178 /**
179 * Passing the focus request to the viewer's control.
180 */
181 public void setFocus() {
182 viewer.getControl().setFocus();
183 }
184 }