KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > views > KnownConfigurationsView


1 /*
2  * Created on 2004-10-12
3  *
4  */

5 package org.hibernate.eclipse.console.views;
6
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.MenuManager;
11 import org.eclipse.jface.action.Separator;
12 import org.eclipse.jface.viewers.DoubleClickEvent;
13 import org.eclipse.jface.viewers.IDoubleClickListener;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.TreeViewer;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.IWorkbenchActionConstants;
23 import org.eclipse.ui.actions.ActionContext;
24 import org.eclipse.ui.actions.ActionGroup;
25 import org.eclipse.ui.part.ViewPart;
26 import org.hibernate.console.ConsoleConfiguration;
27 import org.hibernate.console.KnownConfigurations;
28 import org.hibernate.console.KnownConfigurations.IConsoleConfigurationListener;
29 import org.hibernate.console.node.BaseNode;
30 import org.hibernate.console.node.ConfigurationNode;
31 import org.hibernate.eclipse.console.actions.EditConsoleConfiguration;
32
33
34 /**
35  * @author max
36  *
37  */

38 public class KnownConfigurationsView extends ViewPart {
39
40     TreeViewer viewer;
41     
42     
43     private ActionGroup actionGroup;
44
45
46     private Action doubleAction;
47     
48     /**
49      *
50      */

51     public KnownConfigurationsView() {
52         super();
53     }
54
55     public void createPartControl(Composite parent) {
56         viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
57         
58         viewer.setContentProvider(new ConfigurationsContentProvider());
59         
60         viewer.setLabelProvider(new ConfigurationsLabelProvider());
61         
62         viewer.setInput(KnownConfigurations.getInstance().getRootNode());
63         
64         KnownConfigurations.getInstance().addConsoleConfigurationListener(new IConsoleConfigurationListener() {
65             public void configurationAdded(ConsoleConfiguration root) {
66                 refreshInUI();
67             }
68
69             private void refreshInUI() {
70                 Display.getDefault().syncExec(new Runnable JavaDoc() {
71                     public void run() {
72                         viewer.refresh();
73                     }
74                 });
75             }
76
77             public void configurationRemoved(ConsoleConfiguration root) {
78                 refreshInUI();
79             }
80         });
81         
82         makeActions();
83         createContextMenu();
84         hookDoubleClick();
85         provideSelection();
86     }
87
88     private void hookDoubleClick() {
89         viewer.addDoubleClickListener(new IDoubleClickListener() {
90             public void doubleClick(DoubleClickEvent event) {
91                 doubleAction.run();
92             }
93         });
94     }
95
96     private void provideSelection() {
97         getSite().setSelectionProvider(viewer);
98     }
99
100     private void createContextMenu ()
101     {
102         MenuManager menuMgr = new MenuManager("#PopupMenu");
103         menuMgr.setRemoveAllWhenShown(true);
104         menuMgr.addMenuListener(new IMenuListener() {
105             public void menuAboutToShow(IMenuManager manager) {
106                 fillContextMenu(manager);
107             }
108         });
109
110         Menu menu = menuMgr.createContextMenu(viewer.getControl());
111         
112         viewer.getControl().setMenu(menu);
113         getSite().registerContextMenu(menuMgr, viewer);
114         
115         IActionBars actionBars = getViewSite().getActionBars();
116         IMenuManager dropDownMenu = actionBars.getMenuManager();
117         
118         actionGroup.fillContextMenu(dropDownMenu);
119     }
120     
121     protected void fillContextMenu(IMenuManager menuMgr) {
122         actionGroup.setContext(new ActionContext(viewer.getSelection()));
123         actionGroup.fillContextMenu(menuMgr);
124         menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
125     }
126
127     /**
128      *
129      */

130     private void makeActions() {
131         
132         this.actionGroup = new ConfigurationsViewActionGroup(this, viewer);
133                 
134         this.doubleAction = new Action() {
135             public void run() {
136                 // TODO: make action dependent on having a connected console configuration!
137
ISelection selection = viewer.getSelection();
138                 BaseNode node = (BaseNode) ((IStructuredSelection)selection).getFirstElement();
139                 ConsoleConfiguration consoleConfiguration = node.getConsoleConfiguration();
140                 if(node instanceof ConfigurationNode) {
141                     new EditConsoleConfiguration().run();
142                 } else if(consoleConfiguration.isSessionFactoryCreated()) {
143                         consoleConfiguration.executeHQLQuery(node.getHQL());
144                 }
145             }
146         };
147     }
148     
149
150     public void setFocus() {
151         viewer.getTree().setFocus();
152     }
153
154 }
155
Popular Tags