001: package net.sourceforge.squirrel_sql.plugins.h2;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import net.sourceforge.squirrel_sql.client.IApplication;
023: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
024: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
025: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
026: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
027: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
028: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.expanders.TableWithChildNodesExpander;
031: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.DatabaseObjectInfoTab;
032: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
033: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
034: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
035: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
036: import net.sourceforge.squirrel_sql.fw.util.StringManager;
037: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
038: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
039: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
040: import net.sourceforge.squirrel_sql.plugins.h2.exp.H2TableIndexExtractorImpl;
041: import net.sourceforge.squirrel_sql.plugins.h2.exp.H2TableTriggerExtractorImpl;
042: import net.sourceforge.squirrel_sql.plugins.h2.exp.SchemaExpander;
043: import net.sourceforge.squirrel_sql.plugins.h2.tab.IndexDetailsTab;
044: import net.sourceforge.squirrel_sql.plugins.h2.tab.IndexSourceTab;
045: import net.sourceforge.squirrel_sql.plugins.h2.tab.SequenceDetailsTab;
046: import net.sourceforge.squirrel_sql.plugins.h2.tab.TriggerDetailsTab;
047: import net.sourceforge.squirrel_sql.plugins.h2.tab.ViewSourceTab;
048:
049: /**
050: * The main controller class for the H2 plugin.
051: *
052: * @author manningr
053: */
054: public class H2Plugin extends DefaultSessionPlugin {
055:
056: private static final StringManager s_stringMgr = StringManagerFactory
057: .getStringManager(H2Plugin.class);
058:
059: /** Logger for this class. */
060: @SuppressWarnings("unused")
061: private final static ILogger s_log = LoggerController
062: .createLogger(H2Plugin.class);
063:
064: /** API for the Obejct Tree. */
065: private IObjectTreeAPI _treeAPI;
066:
067: static interface i18n {
068: //i18n[PostgresPlugin.showIndexSource=Show index source]
069: String SHOW_INDEX_SOURCE = s_stringMgr
070: .getString("PostgresPlugin.showIndexSource");
071:
072: //i18n[PostgresPlugin.showViewSource=Show view source]
073: String SHOW_VIEW_SOURCE = s_stringMgr
074: .getString("PostgresPlugin.showViewSource");
075:
076: //i18n[PostgresPlugin.showProcedureSource=Show procedure source]
077: String SHOW_PROCEDURE_SOURCE = s_stringMgr
078: .getString("PostgresPlugin.showProcedureSource");
079: }
080:
081: /**
082: * Return the internal name of this plugin.
083: *
084: * @return the internal name of this plugin.
085: */
086: public String getInternalName() {
087: return "h2";
088: }
089:
090: /**
091: * Return the descriptive name of this plugin.
092: *
093: * @return the descriptive name of this plugin.
094: */
095: public String getDescriptiveName() {
096: return "H2 Plugin";
097: }
098:
099: /**
100: * Returns the current version of this plugin.
101: *
102: * @return the current version of this plugin.
103: */
104: public String getVersion() {
105: return "0.01";
106: }
107:
108: /**
109: * Returns the authors name.
110: *
111: * @return the authors name.
112: */
113: public String getAuthor() {
114: return "Rob Manning";
115: }
116:
117: /**
118: * Returns a comma separated list of other contributors.
119: *
120: * @return Contributors names.
121: */
122: public String getContributors() {
123: return "";
124: }
125:
126: /**
127: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getChangeLogFileName()
128: */
129: public String getChangeLogFileName() {
130: return "changes.txt";
131: }
132:
133: /**
134: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getHelpFileName()
135: */
136: public String getHelpFileName() {
137: return "readme.html";
138: }
139:
140: /**
141: * @see net.sourceforge.squirrel_sql.client.plugin.IPlugin#getLicenceFileName()
142: */
143: public String getLicenceFileName() {
144: return "licence.txt";
145: }
146:
147: /**
148: * Load this plugin.
149: *
150: * @param app Application API.
151: */
152: public synchronized void load(IApplication app)
153: throws PluginException {
154: super .load(app);
155: }
156:
157: /**
158: * Initialize this plugin.
159: */
160: public synchronized void initialize() throws PluginException {
161: super .initialize();
162: }
163:
164: /**
165: * Application is shutting down so save preferences.
166: */
167: public void unload() {
168: super .unload();
169: }
170:
171: public boolean allowsSessionStartedInBackground() {
172: return true;
173: }
174:
175: /**
176: * Session has been started. Update the tree api in using the event thread
177: *
178: * @param session Session that has started.
179: *
180: * @return <TT>true</TT> if session is Oracle in which case this plugin
181: * is interested in it.
182: */
183: public PluginSessionCallback sessionStarted(final ISession session) {
184: if (!isPluginSession(session)) {
185: return null;
186: }
187:
188: GUIUtils.processOnSwingEventThread(new Runnable() {
189: public void run() {
190: updateTreeApi(session);
191: }
192: });
193: return new PluginSessionCallback() {
194: public void sqlInternalFrameOpened(
195: SQLInternalFrame sqlInternalFrame, ISession sess) {
196: // Supports Session main window only
197: }
198:
199: public void objectTreeInternalFrameOpened(
200: ObjectTreeInternalFrame objectTreeInternalFrame,
201: ISession sess) {
202: // Supports Session main window only
203: }
204: };
205: }
206:
207: @Override
208: protected boolean isPluginSession(ISession session) {
209: return DialectFactory.isH2(session.getMetaData());
210: }
211:
212: private void updateTreeApi(ISession session) {
213: IQueryTokenizer qt = session.getQueryTokenizer();
214: String stmtSep = qt.getSQLStatementSeparator();
215:
216: _treeAPI = session.getSessionInternalFrame().getObjectTreeAPI();
217: // Expanders - trigger and index expanders are added inside the table
218: // expander
219: _treeAPI.addExpander(DatabaseObjectType.SCHEMA,
220: new SchemaExpander());
221:
222: TableWithChildNodesExpander tableExp = new TableWithChildNodesExpander();
223: tableExp
224: .setTableIndexExtractor(new H2TableIndexExtractorImpl());
225: tableExp
226: .setTableTriggerExtractor(new H2TableTriggerExtractorImpl());
227: _treeAPI.addExpander(DatabaseObjectType.TABLE, tableExp);
228:
229: // View Tab
230: _treeAPI.addDetailTab(DatabaseObjectType.VIEW,
231: new ViewSourceTab(i18n.SHOW_VIEW_SOURCE, stmtSep));
232:
233: // Index tab
234: _treeAPI.addDetailTab(DatabaseObjectType.INDEX,
235: new DatabaseObjectInfoTab());
236: _treeAPI.addDetailTab(DatabaseObjectType.INDEX,
237: new IndexDetailsTab());
238: _treeAPI.addDetailTab(DatabaseObjectType.INDEX,
239: new IndexSourceTab(i18n.SHOW_INDEX_SOURCE, stmtSep));
240:
241: // Trigger tabs
242: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER_TYPE_DBO,
243: new DatabaseObjectInfoTab());
244: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
245: new DatabaseObjectInfoTab());
246: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
247: new TriggerDetailsTab());
248:
249: // H2 uses Java classes that implement the "Trigger" interface to operate
250: // on database tables rows when an action triggers them. Therefore, there
251: // is currently no way to access the source for a trigger. Hopefully this
252: // will change at some point in the future.
253: //_treeAPI.addDetailTab(DatabaseObjectType.TRIGGER, new TriggerSourceTab("The source of the trigger"));
254:
255: // Sequence tabs
256: _treeAPI.addDetailTab(DatabaseObjectType.SEQUENCE,
257: new DatabaseObjectInfoTab());
258: _treeAPI.addDetailTab(DatabaseObjectType.SEQUENCE,
259: new SequenceDetailsTab());
260:
261: }
262:
263: }
|