KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 08-Dec-2004
3  *
4  */

5 package org.hibernate.eclipse.console.actions;
6
7 import java.util.Iterator JavaDoc;
8
9 import org.eclipse.jface.viewers.IStructuredSelection;
10 import org.eclipse.ui.actions.SelectionListenerAction;
11 import org.hibernate.HibernateException;
12 import org.hibernate.console.ConsoleConfiguration;
13 import org.hibernate.console.node.ConfigurationNode;
14 import org.hibernate.eclipse.console.HibernateConsolePlugin;
15
16 /**
17  * @author max
18  *
19  */

20 public abstract class ConsoleConfigurationBasedAction extends SelectionListenerAction {
21
22     boolean enabledWhenNoSessionFactory = false;
23     boolean supportMultiple = true;
24
25     protected void setEnabledWhenNoSessionFactory(
26             boolean enabledWhenNoSessionFactory) {
27         this.enabledWhenNoSessionFactory = enabledWhenNoSessionFactory;
28     }
29     
30     /**
31      * @param supportMultiple The supportMultiple to set.
32      */

33     public void setSupportMultiple(boolean supportMultiple) {
34         this.supportMultiple = supportMultiple;
35     }
36     
37     /**
38      * @param text
39      */

40     protected ConsoleConfigurationBasedAction(String JavaDoc text) {
41         super(text);
42     }
43
44     final public void run() {
45         
46         try {
47             doRun();
48         } catch(HibernateException he) {
49             HibernateConsolePlugin.getDefault().showError(null, "Problem while executing " + getText() + "(" + he + ")", he);
50         }
51     }
52     
53     abstract protected void doRun();
54
55     final protected boolean updateSelection(IStructuredSelection selection) {
56            boolean enabled = false;
57            if(!supportMultiple && selection.size()>1) return false;
58             for (Iterator JavaDoc i = selection.iterator();
59                 i.hasNext();
60                 ) {
61                 Object JavaDoc object = i.next();
62                 if (object instanceof ConfigurationNode) {
63                     ConfigurationNode node = (ConfigurationNode) object;
64                     ConsoleConfiguration consoleConfiguration = node.getConsoleConfiguration();
65                     enabled |= updateState(consoleConfiguration);
66                     
67                 } else {
68                     enabled = false;
69                 }
70             }
71             return enabled;
72     }
73
74     /**
75      * @param consoleConfiguration
76      */

77     protected boolean updateState(ConsoleConfiguration consoleConfiguration) {
78         if(enabledWhenNoSessionFactory) {
79             return !consoleConfiguration.isSessionFactoryCreated();
80         } else {
81             return consoleConfiguration.isSessionFactoryCreated();
82         }
83     }
84
85 }
86
Popular Tags