1 5 package org.hibernate.eclipse.console.actions; 6 7 import java.util.Iterator ; 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 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 33 public void setSupportMultiple(boolean supportMultiple) { 34 this.supportMultiple = supportMultiple; 35 } 36 37 40 protected ConsoleConfigurationBasedAction(String 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 i = selection.iterator(); 59 i.hasNext(); 60 ) { 61 Object 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 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 |