KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > actions > ExecuteHQLAction


1 /*
2  * Created on 2004-10-29 by max
3  *
4  */

5 package org.hibernate.eclipse.console.actions;
6
7 import org.eclipse.jface.action.Action;
8 import org.eclipse.jface.action.ActionContributionItem;
9 import org.eclipse.jface.action.IMenuCreator;
10 import org.eclipse.jface.action.Separator;
11 import org.eclipse.swt.events.MenuAdapter;
12 import org.eclipse.swt.events.MenuEvent;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Menu;
15 import org.eclipse.swt.widgets.MenuItem;
16 import org.hibernate.console.ConsoleConfiguration;
17 import org.hibernate.console.ImageConstants;
18 import org.hibernate.console.KnownConfigurations;
19 import org.hibernate.eclipse.console.HibernateConsolePlugin;
20 import org.hibernate.eclipse.console.utils.EclipseImages;
21 import org.hibernate.eclipse.console.views.HQLEditorView;
22
23
24
25 /**
26  * @author max
27  *
28  */

29 public class ExecuteHQLAction extends Action implements IMenuCreator {
30
31     private static final String JavaDoc LAST_USED_CONFIGURATION_PREFERENCE = "lastusedconfig";
32     private final HQLEditorView view;
33
34     public ExecuteHQLAction(HQLEditorView view) {
35         
36         this.view = view;
37         setMenuCreator(this);
38         setImageDescriptor(EclipseImages.getImageDescriptor(ImageConstants.EXECUTE));
39         initTextAndToolTip();
40     }
41
42     public void dispose() {
43     }
44
45     public Menu getMenu(Control parent) {
46         Menu menu = new Menu(parent);
47         /**
48          * Add listener to repopulate the menu each time do keep uptodate with configurationis
49          */

50         menu.addMenuListener(new MenuAdapter() {
51             public void menuShown(MenuEvent e) {
52                 Menu menu = (Menu)e.widget;
53                 MenuItem[] items = menu.getItems();
54                 for (int i=0; i < items.length; i++) {
55                     items[i].dispose();
56                 }
57                 fillMenu(menu);
58             }
59         });
60         return menu;
61     }
62
63     protected void fillMenu(Menu menu) {
64         ConsoleConfiguration lastUsed = getLastUsedConfiguration();
65         
66         if (lastUsed != null) {
67             createSubAction(menu, lastUsed);
68             Separator separator = new Separator();
69             separator.fill(menu, -1);
70         }
71         
72         ConsoleConfiguration[] configs = KnownConfigurations.getInstance().getConfigurations();
73         //Arrays.sort(bookmarks, new DisplayableComparator());
74
for (int i = 0, length = configs == null ? 0 : configs.length; i < length; i++) {
75             final ConsoleConfiguration bookmark = configs[i];
76             createSubAction(menu, bookmark);
77         }
78     }
79
80     private void createSubAction(Menu menu, final ConsoleConfiguration lastUsed) {
81         Action action = new Action() {
82             public void run() {
83                 ExecuteHQLAction.this.execute(lastUsed);
84             }
85         };
86         //action.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.BOOKMARK));
87

88         action.setText(lastUsed.getName());
89         ActionContributionItem item = new ActionContributionItem(action);
90         item.fill(menu, -1);
91     }
92
93     public void run() {
94         execute(getLastUsedConfiguration());
95     }
96     protected void execute(ConsoleConfiguration lastUsed) {
97         try {
98             if(lastUsed!=null && lastUsed.isSessionFactoryCreated()) {
99                 lastUsed.executeHQLQuery(view.getQuery());
100             }
101         }
102         finally {
103             if(lastUsed!=null) {
104             HibernateConsolePlugin.getDefault().getPreferenceStore().setValue(
105                     LAST_USED_CONFIGURATION_PREFERENCE, lastUsed.getName());
106             } else {
107                 HibernateConsolePlugin.getDefault().getPreferenceStore().setValue(
108                         LAST_USED_CONFIGURATION_PREFERENCE, "");
109             }
110             initTextAndToolTip();
111         }
112         
113     }
114
115     private void initTextAndToolTip() {
116         ConsoleConfiguration lastUsed = getLastUsedConfiguration();
117         if (lastUsed == null) {
118             setText("");
119             setToolTipText("");
120         } else {
121             setText(lastUsed.getName());
122             setToolTipText(lastUsed.getName());
123         }
124         
125     }
126
127     private ConsoleConfiguration getLastUsedConfiguration() {
128         String JavaDoc lastUsedName = HibernateConsolePlugin.getDefault().getPreferenceStore().getString(
129                 LAST_USED_CONFIGURATION_PREFERENCE);
130         ConsoleConfiguration lastUsed = (lastUsedName == null || lastUsedName.trim().length()==0)
131                 ? null
132                 : KnownConfigurations.getInstance().find(lastUsedName);
133         
134         if(lastUsed==null && KnownConfigurations.getInstance().getConfigurations().length==1) {
135             lastUsed = KnownConfigurations.getInstance().getConfigurations()[0];
136         }
137         
138         return lastUsed;
139     }
140
141     public Menu getMenu(Menu parent) {
142         throw new IllegalStateException JavaDoc("should not be called!");
143         //return null;
144
}
145
146 }
147
Popular Tags