Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/draw2d/imageviewer/views/ImageIndexView.java


1   package com.port80.draw2d.imageviewer.views;
2   
3   import java.util.ArrayList;
4   
5   import org.eclipse.core.resources.ResourcesPlugin;
6   import org.eclipse.core.runtime.IAdaptable;
7   import org.eclipse.jface.action.Action;
8   import org.eclipse.jface.action.IMenuListener;
9   import org.eclipse.jface.action.IMenuManager;
10  import org.eclipse.jface.action.IToolBarManager;
11  import org.eclipse.jface.action.MenuManager;
12  import org.eclipse.jface.action.Separator;
13  import org.eclipse.jface.dialogs.MessageDialog;
14  import org.eclipse.jface.viewers.DoubleClickEvent;
15  import org.eclipse.jface.viewers.IDoubleClickListener;
16  import org.eclipse.jface.viewers.ISelection;
17  import org.eclipse.jface.viewers.IStructuredContentProvider;
18  import org.eclipse.jface.viewers.IStructuredSelection;
19  import org.eclipse.jface.viewers.ITreeContentProvider;
20  import org.eclipse.jface.viewers.LabelProvider;
21  import org.eclipse.jface.viewers.TreeViewer;
22  import org.eclipse.jface.viewers.Viewer;
23  import org.eclipse.jface.viewers.ViewerSorter;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.graphics.Image;
26  import org.eclipse.swt.widgets.Composite;
27  import org.eclipse.swt.widgets.Menu;
28  import org.eclipse.ui.IActionBars;
29  import org.eclipse.ui.ISharedImages;
30  import org.eclipse.ui.PlatformUI;
31  import org.eclipse.ui.part.DrillDownAdapter;
32  import org.eclipse.ui.part.ViewPart;
33  
34  /**
35   * This sample class demonstrates how to plug-in a new
36   * workbench view. The view shows data obtained from the
37   * model. The sample creates a dummy model on the fly,
38   * but a real implementation would connect to the model
39   * available either in this or another plug-in (e.g. the workspace).
40   * The view is connected to the model using a content provider.
41   * <p>
42   * The view uses a label provider to define how model
43   * objects should be presented in the view. Each
44   * view can present the same model objects using
45   * different labels and icons, if needed. Alternatively,
46   * a single label provider can be shared between views
47   * in order to ensure that objects of the same type are
48   * presented in the same way everywhere.
49   * <p>
50   */
51  
52  public class ImageIndexView extends ViewPart {
53    private DrillDownAdapter drillDownAdapter;
54    private Action action1;
55    private Action action2;
56    TreeViewer viewer;
57    Action doubleClickAction;
58  
59    /*
60     * The content provider class is responsible for
61     * providing objects to the view. It can wrap
62     * existing objects in adapters or simply return
63     * objects as-is. These objects may be sensitive
64     * to the current input of the view, or ignore
65     * it and always show the same content 
66     * (like Task List, for example).
67     */
68     
69    class TreeObject implements IAdaptable {
70      private String name;
71      private TreeParent parent;
72      
73      public TreeObject(String name) {
74        this.name = name;
75      }
76      public String getName() {
77        return name;
78      }
79      public void setParent(TreeParent parent) {
80        this.parent = parent;
81      }
82      public TreeParent getParent() {
83        return parent;
84      }
85      public String toString() {
86        return getName();
87      }
88      public Object getAdapter(Class key) {
89        return null;
90      }
91    }
92    
93    class TreeParent extends TreeObject {
94      private ArrayList children;
95      public TreeParent(String name) {
96        super(name);
97        children = new ArrayList();
98      }
99      public void addChild(TreeObject child) {
100       children.add(child);
101       child.setParent(this);
102     }
103     public void removeChild(TreeObject child) {
104       children.remove(child);
105       child.setParent(null);
106     }
107     public TreeObject [] getChildren() {
108       return (TreeObject [])children.toArray(new TreeObject[children.size()]);
109     }
110     public boolean hasChildren() {
111       return children.size()>0;
112     }
113   }
114 
115   class ViewContentProvider implements IStructuredContentProvider, 
116                        ITreeContentProvider {
117     private TreeParent invisibleRoot;
118 
119     public void inputChanged(Viewer v, Object oldInput, Object newInput) {
120     }
121     public void dispose() {
122     }
123     public Object[] getElements(Object parent) {
124       if (parent.equals(ResourcesPlugin.getWorkspace())) {
125         if (invisibleRoot==null) initialize();
126         return getChildren(invisibleRoot);
127       }
128       return getChildren(parent);
129     }
130     public Object getParent(Object child) {
131       if (child instanceof TreeObject) {
132         return ((TreeObject)child).getParent();
133       }
134       return null;
135     }
136     public Object [] getChildren(Object parent) {
137       if (parent instanceof TreeParent) {
138         return ((TreeParent)parent).getChildren();
139       }
140       return new Object[0];
141     }
142     public boolean hasChildren(Object parent) {
143       if (parent instanceof TreeParent)
144         return ((TreeParent)parent).hasChildren();
145       return false;
146     }
147 /*
148  * We will set up a dummy model to initialize tree heararchy.
149  * In a real code, you will connect to a real model and
150  * expose its hierarchy.
151  */
152     private void initialize() {
153       TreeObject to1 = new TreeObject("Leaf 1");
154       TreeObject to2 = new TreeObject("Leaf 2");
155       TreeObject to3 = new TreeObject("Leaf 3");
156       TreeParent p1 = new TreeParent("Parent 1");
157       p1.addChild(to1);
158       p1.addChild(to2);
159       p1.addChild(to3);
160       
161       TreeObject to4 = new TreeObject("Leaf 4");
162       TreeParent p2 = new TreeParent("Parent 2");
163       p2.addChild(to4);
164       
165       TreeParent root = new TreeParent("Root");
166       root.addChild(p1);
167       root.addChild(p2);
168       
169       invisibleRoot = new TreeParent("");
170       invisibleRoot.addChild(root);
171     }
172   }
173   class ViewLabelProvider extends LabelProvider {
174 
175     public String getText(Object obj) {
176       return obj.toString();
177     }
178     public Image getImage(Object obj) {
179       String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
180       if (obj instanceof TreeParent)
181          imageKey = ISharedImages.IMG_OBJ_FOLDER;
182       return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
183     }
184   }
185   class NameSorter extends ViewerSorter {
186   }
187 
188   /**
189    * The constructor.
190    */
191   public ImageIndexView() {
192   }
193 
194   /**
195    * This is a callback that will allow us
196    * to create the viewer and initialize it.
197    */
198   public void createPartControl(Composite parent) {
199     viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
200     drillDownAdapter = new DrillDownAdapter(viewer);
201     viewer.setContentProvider(new ViewContentProvider());
202     viewer.setLabelProvider(new ViewLabelProvider());
203     viewer.setSorter(new NameSorter());
204     viewer.setInput(ResourcesPlugin.getWorkspace());
205     makeActions();
206     hookContextMenu();
207     hookDoubleClickAction();
208     contributeToActionBars();
209   }
210 
211   private void hookContextMenu() {
212     MenuManager menuMgr = new MenuManager("#PopupMenu");
213     menuMgr.setRemoveAllWhenShown(true);
214     menuMgr.addMenuListener(new IMenuListener() {
215       public void menuAboutToShow(IMenuManager manager) {
216         ImageIndexView.this.fillContextMenu(manager);
217       }
218     });
219     Menu menu = menuMgr.createContextMenu(viewer.getControl());
220     viewer.getControl().setMenu(menu);
221     getSite().registerContextMenu(menuMgr, viewer);
222   }
223   
224   private void contributeToActionBars() {
225     IActionBars bars = getViewSite().getActionBars();
226     fillLocalPullDown(bars.getMenuManager());
227     fillLocalToolBar(bars.getToolBarManager());
228   }
229 
230   private void fillLocalPullDown(IMenuManager manager) {
231     manager.add(action1);
232     manager.add(new Separator());
233     manager.add(action2);
234   }
235 
236   void fillContextMenu(IMenuManager manager) {
237     manager.add(action1);
238     manager.add(action2);
239     manager.add(new Separator());
240     drillDownAdapter.addNavigationActions(manager);
241     // Other plug-ins can contribute there actions here
242     manager.add(new Separator("Additions"));
243   }
244   
245   private void fillLocalToolBar(IToolBarManager manager) {
246     manager.add(action1);
247     manager.add(action2);
248     manager.add(new Separator());
249     drillDownAdapter.addNavigationActions(manager);
250   }
251 
252   private void makeActions() {
253     action1 = new Action() {
254       public void run() {
255         showMessage("Action 1 executed");
256       }
257     };
258     action1.setText("Action 1");
259     action1.setToolTipText("Action 1 tooltip");
260     action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
261       getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
262     
263     action2 = new Action() {
264       public void run() {
265         showMessage("Action 2 executed");
266       }
267     };
268     action2.setText("Action 2");
269     action2.setToolTipText("Action 2 tooltip");
270     action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
271       getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
272     doubleClickAction = new Action() {
273       public void run() {
274         ISelection selection = viewer.getSelection();
275         Object obj = ((IStructuredSelection)selection).getFirstElement();
276         showMessage("Double-click detected on "+obj.toString());
277       }
278     };
279   }
280 
281   private void hookDoubleClickAction() {
282     viewer.addDoubleClickListener(new IDoubleClickListener() {
283       public void doubleClick(DoubleClickEvent event) {
284         doubleClickAction.run();
285       }
286     });
287   }
288   void showMessage(String message) {
289     MessageDialog.openInformation(
290       viewer.getControl().getShell(),
291       "Image View",
292       message);
293   }
294 
295   /**
296    * Passing the focus request to the viewer's control.
297    */
298   public void setFocus() {
299     viewer.getControl().setFocus();
300   }
301 }