001: package net.sourceforge.squirrel_sql.client.plugin;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * Modifications Copyright (c) 2004 Jason Height.
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import javax.swing.JMenu;
024:
025: import net.sourceforge.squirrel_sql.client.session.ISession;
026: import net.sourceforge.squirrel_sql.client.session.event.SessionAdapter;
027: import net.sourceforge.squirrel_sql.client.session.event.SessionEvent;
028: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.INodeExpander;
029: import net.sourceforge.squirrel_sql.client.session.properties.ISessionPropertiesPanel;
030: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
031: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
032: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
033: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
034:
035: public abstract class DefaultSessionPlugin extends DefaultPlugin
036: implements ISessionPlugin {
037: /** Subclasses that create a session JMenu will set this */
038: private JMenu sessionMenu = null;
039:
040: /** Logger for this class. */
041: private static final ILogger s_log = LoggerController
042: .createLogger(DefaultSessionPlugin.class);
043:
044: /**
045: * A new session has been created. At this point the
046: * <TT>SessionPanel</TT> does not exist for the new session.
047: *
048: * @param session The new session.
049: *
050: * @throws IllegalArgumentException
051: * Thrown if a <TT>null</TT> ISession</TT> passed.
052: */
053: public void sessionCreated(ISession session) {
054: // Empty body.
055: }
056:
057: public boolean allowsSessionStartedInBackground() {
058: return false;
059: }
060:
061: /**
062: * Called when a session shutdown.
063: *
064: * @param session The session that is ending.
065: */
066: public void sessionEnding(ISession session) {
067: // Empty body.
068: }
069:
070: /**
071: * Override this to create panels for the Session Properties dialog.
072: *
073: * @param session The session that will be displayed in the properties dialog.
074: *
075: * @return <TT>null</TT> to indicate that this plugin doesn't use session property panels.
076: */
077: public ISessionPropertiesPanel[] getSessionPropertiesPanels(
078: ISession session) {
079: return null;
080: }
081:
082: /**
083: * Let app know what extra types of objects in object tree that
084: * plugin can handle.
085: */
086: public IPluginDatabaseObjectType[] getObjectTypes(ISession session) {
087: return null;
088: }
089:
090: /**
091: * Return a node expander for the object tree for a particular default node type.
092: * A plugin could return non null here if they wish to override the default node
093: * expander bahaviour. Most plugins should return null here.
094: */
095: public INodeExpander getDefaultNodeExpander(ISession session,
096: DatabaseObjectType type) {
097: return null;
098: }
099:
100: /**
101: * This should be overridden by all databases-specific subclasses so that
102: * registerSessionMenu will work correctly. It should return true
103: * when the plugin subclass is db-specific and the specified session is for a
104: * database that the plugin supports; false is returned otherwise.
105: */
106: protected boolean isPluginSession(ISession session) {
107: if (s_log.isDebugEnabled() && sessionMenu != null) {
108: s_log
109: .debug("The default isPluginSession() impl was called for session \""
110: + session.getAlias().getName()
111: + "\", but sessionMenu ("
112: + sessionMenu.getText()
113: + ")is not null - this is probably a bug.");
114: }
115: return true;
116: }
117:
118: /**
119: * Plugin sub-classes call this to register their session JMenu with this
120: * class so that this class can manage it's enabled state, as sessions
121: * become activated.
122: *
123: * @param menu the plugin session menu
124: */
125: protected void registerSessionMenu(JMenu menu) {
126: if (menu == null) {
127: throw new IllegalArgumentException("menu cannot be null");
128: }
129: sessionMenu = menu;
130: _app.getSessionManager().addSessionListener(
131: new SessionListener());
132: }
133:
134: /**
135: * A session listener that is used to determine when sessions are activated
136: * and to trigger the enable menu task if the session is relevant to this
137: * plugin.
138: */
139: private class SessionListener extends SessionAdapter {
140: public void sessionActivated(SessionEvent evt) {
141: final ISession session = evt.getSession();
142: EnableMenuTask task = new EnableMenuTask(session);
143: session.getApplication().getThreadPool().addTask(task);
144: }
145: }
146:
147: /**
148: * A runnable that implements changing the enabled state of sessionMenu.
149: * This class ensures that UI state change occurs in the Swing EDT.
150: */
151: private class EnableMenuTask implements Runnable {
152:
153: private ISession _session = null;
154:
155: public EnableMenuTask(ISession session) {
156: _session = session;
157: }
158:
159: public void run() {
160: final boolean enable = isPluginSession(_session);
161: GUIUtils.processOnSwingEventThread(new Runnable() {
162: public void run() {
163: sessionMenu.setEnabled(enable);
164: }
165: });
166: }
167: }
168: }
|