~piastucki/bzr-eclipse/history-view-enhancements

« back to all changes in this revision

Viewing changes to src/org/vcs/bazaar/eclipse/views/SampleView.java

  • Committer: Guillermo Gonzalez
  • Date: 2007-03-14 12:15:50 UTC
  • Revision ID: guillo.gonzo@gmail.com-20070314121550-tltk3b6f3zblf0dh
Initial commit with the new code layout.
Now starts the real work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.vcs.bazaar.eclipse.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
 
                makeActions();
90
 
                hookContextMenu();
91
 
                hookDoubleClickAction();
92
 
                contributeToActionBars();
93
 
        }
94
 
 
95
 
        private void hookContextMenu() {
96
 
                MenuManager menuMgr = new MenuManager("#PopupMenu");
97
 
                menuMgr.setRemoveAllWhenShown(true);
98
 
                menuMgr.addMenuListener(new IMenuListener() {
99
 
                        public void menuAboutToShow(IMenuManager manager) {
100
 
                                SampleView.this.fillContextMenu(manager);
101
 
                        }
102
 
                });
103
 
                Menu menu = menuMgr.createContextMenu(viewer.getControl());
104
 
                viewer.getControl().setMenu(menu);
105
 
                getSite().registerContextMenu(menuMgr, viewer);
106
 
        }
107
 
 
108
 
        private void contributeToActionBars() {
109
 
                IActionBars bars = getViewSite().getActionBars();
110
 
                fillLocalPullDown(bars.getMenuManager());
111
 
                fillLocalToolBar(bars.getToolBarManager());
112
 
        }
113
 
 
114
 
        private void fillLocalPullDown(IMenuManager manager) {
115
 
                manager.add(action1);
116
 
                manager.add(new Separator());
117
 
                manager.add(action2);
118
 
        }
119
 
 
120
 
        private void fillContextMenu(IMenuManager manager) {
121
 
                manager.add(action1);
122
 
                manager.add(action2);
123
 
                // Other plug-ins can contribute there actions here
124
 
                manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
125
 
        }
126
 
        
127
 
        private void fillLocalToolBar(IToolBarManager manager) {
128
 
                manager.add(action1);
129
 
                manager.add(action2);
130
 
        }
131
 
 
132
 
        private void makeActions() {
133
 
                action1 = new Action() {
134
 
                        public void run() {
135
 
                                showMessage("Action 1 executed");
136
 
                        }
137
 
                };
138
 
                action1.setText("Action 1");
139
 
                action1.setToolTipText("Action 1 tooltip");
140
 
                action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
141
 
                        getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
142
 
                
143
 
                action2 = new Action() {
144
 
                        public void run() {
145
 
                                showMessage("Action 2 executed");
146
 
                        }
147
 
                };
148
 
                action2.setText("Action 2");
149
 
                action2.setToolTipText("Action 2 tooltip");
150
 
                action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
151
 
                                getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
152
 
                doubleClickAction = new Action() {
153
 
                        public void run() {
154
 
                                ISelection selection = viewer.getSelection();
155
 
                                Object obj = ((IStructuredSelection)selection).getFirstElement();
156
 
                                showMessage("Double-click detected on "+obj.toString());
157
 
                        }
158
 
                };
159
 
        }
160
 
 
161
 
        private void hookDoubleClickAction() {
162
 
                viewer.addDoubleClickListener(new IDoubleClickListener() {
163
 
                        public void doubleClick(DoubleClickEvent event) {
164
 
                                doubleClickAction.run();
165
 
                        }
166
 
                });
167
 
        }
168
 
        private void showMessage(String message) {
169
 
                MessageDialog.openInformation(
170
 
                        viewer.getControl().getShell(),
171
 
                        "Sample View",
172
 
                        message);
173
 
        }
174
 
 
175
 
        /**
176
 
         * Passing the focus request to the viewer's control.
177
 
         */
178
 
        public void setFocus() {
179
 
                viewer.getControl().setFocus();
180
 
        }
181
 
}
 
 
b'\\ No newline at end of file'