001: package net.sourceforge.squirrel_sql.plugins.codecompletion;
002:
003: /*
004: * Copyright (C) 2003 Gerd Wagner
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
021: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
022: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
023: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
024: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
025: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
026: import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
027: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
028: import net.sourceforge.squirrel_sql.client.session.ISession;
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.util.log.ILogger;
032: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
033: import net.sourceforge.squirrel_sql.fw.util.Utilities;
034: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
035: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
036: import net.sourceforge.squirrel_sql.plugins.codecompletion.prefs.CodeCompletionPreferences;
037: import net.sourceforge.squirrel_sql.plugins.codecompletion.prefs.CodeCompletionPreferencesController;
038:
039: import javax.swing.*;
040: import java.io.File;
041: import java.util.Iterator;
042:
043: /**
044: * The plugin class.
045: *
046: * @author Gerd Wagner
047: */
048: public class CodeCompletionPlugin extends DefaultSessionPlugin {
049: /** Logger for this class. */
050: @SuppressWarnings("unused")
051: private final static ILogger s_log = LoggerController
052: .createLogger(CodeCompletionPlugin.class);
053:
054: /** Resources for this plugin. */
055: private Resources _resources;
056: private static final String PREFS_FILE_NAME = "codecompletionprefs.xml";
057:
058: private CodeCompletionPreferences _newSessionPrefs;
059: public static final String PLUGIN_OBJECT_PREFS_KEY = "codecompletionprefs";
060:
061: /**
062: * Return the internal name of this plugin.
063: *
064: * @return the internal name of this plugin.
065: */
066: public String getInternalName() {
067: return "codecompletion";
068: }
069:
070: /**
071: * Return the descriptive name of this plugin.
072: *
073: * @return the descriptive name of this plugin.
074: */
075: public String getDescriptiveName() {
076: return "SQL Entry Code Completion";
077: }
078:
079: /**
080: * Returns the current version of this plugin.
081: *
082: * @return the current version of this plugin.
083: */
084: public String getVersion() {
085: return "1.0";
086: }
087:
088: /**
089: * Returns a comma separated list of other contributors.
090: *
091: * @return Contributors names.
092: */
093: public String getContributors() {
094: return "Christian Sell";
095: }
096:
097: /**
098: * Returns the authors name.
099: *
100: * @return the authors name.
101: */
102: public String getAuthor() {
103: return "Gerd Wagner";
104: }
105:
106: /**
107: * Returns the name of the change log for the plugin. This should
108: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
109: * directory.
110: *
111: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
112: * a change log.
113: */
114: public String getChangeLogFileName() {
115: return "changes.txt";
116: }
117:
118: /**
119: * Returns the name of the Help file for the plugin. This should
120: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
121: * directory.
122: *
123: * @return the Help file name or <TT>null</TT> if plugin doesn't have
124: * a help file.
125: */
126: public String getHelpFileName() {
127: return "readme.html";
128: }
129:
130: /**
131: * Returns the name of the Licence file for the plugin. This should
132: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
133: * directory.
134: *
135: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
136: * a licence file.
137: */
138: public String getLicenceFileName() {
139: return "licence.txt";
140: }
141:
142: /**
143: * Called on application startup after application started.
144: */
145: public void initialize() throws PluginException {
146: _resources = new Resources(this );
147: loadPrefs();
148: }
149:
150: public void unload() {
151: savePrefs();
152: }
153:
154: private void savePrefs() {
155: try {
156: File prefsFile = new File(getPluginUserSettingsFolder(),
157: PREFS_FILE_NAME);
158: final XMLBeanWriter wtr = new XMLBeanWriter(
159: _newSessionPrefs);
160: wtr.save(prefsFile);
161: } catch (Exception e) {
162: throw new RuntimeException(e);
163: }
164: }
165:
166: private void loadPrefs() {
167: try {
168: _newSessionPrefs = new CodeCompletionPreferences();
169: File prefsFile = new File(getPluginUserSettingsFolder(),
170: PREFS_FILE_NAME);
171: if (prefsFile.exists()) {
172: XMLBeanReader reader = new XMLBeanReader();
173: reader.load(prefsFile, getClass().getClassLoader());
174:
175: Iterator<?> it = reader.iterator();
176:
177: if (it.hasNext()) {
178: _newSessionPrefs = (CodeCompletionPreferences) it
179: .next();
180: }
181:
182: }
183: } catch (Exception e) {
184: throw new RuntimeException(e);
185: }
186: }
187:
188: /**
189: * Create preferences panel for the New Session Properties dialog.
190: *
191: * @return preferences panel.
192: */
193: public INewSessionPropertiesPanel[] getNewSessionPropertiesPanels() {
194: return new INewSessionPropertiesPanel[] { new CodeCompletionPreferencesController(
195: _newSessionPrefs) };
196: }
197:
198: /**
199: * Create panels for the Session Properties dialog.
200: *
201: * @return Array of panels for the properties dialog.
202: */
203: public ISessionPropertiesPanel[] getSessionPropertiesPanels(
204: ISession session) {
205: CodeCompletionPreferences sessionPrefs = (CodeCompletionPreferences) session
206: .getPluginObject(this , PLUGIN_OBJECT_PREFS_KEY);
207:
208: return new ISessionPropertiesPanel[] { new CodeCompletionPreferencesController(
209: sessionPrefs) };
210: }
211:
212: public void sessionCreated(ISession session) {
213: CodeCompletionPreferences prefs = (CodeCompletionPreferences) Utilities
214: .cloneObject(_newSessionPrefs, getClass()
215: .getClassLoader());
216: session.putPluginObject(this , PLUGIN_OBJECT_PREFS_KEY, prefs);
217: }
218:
219: public boolean allowsSessionStartedInBackground() {
220: return true;
221: }
222:
223: /**
224: * Session has been started.
225: *
226: * @param session Session that has started.
227: */
228: public PluginSessionCallback sessionStarted(final ISession session) {
229: ISQLPanelAPI sqlPaneAPI = session.getSessionSheet()
230: .getSQLPaneAPI();
231:
232: initCodeCompletion(sqlPaneAPI, session);
233:
234: PluginSessionCallback ret = new PluginSessionCallback() {
235: public void sqlInternalFrameOpened(
236: final SQLInternalFrame sqlInternalFrame,
237: final ISession sess) {
238: initCodeCompletion(sqlInternalFrame.getSQLPanelAPI(),
239: sess);
240: }
241:
242: public void objectTreeInternalFrameOpened(
243: ObjectTreeInternalFrame objectTreeInternalFrame,
244: ISession sess) {
245: }
246: };
247:
248: return ret;
249: }
250:
251: private void initCodeCompletion(final ISQLPanelAPI sqlPaneAPI,
252: final ISession session) {
253: GUIUtils.processOnSwingEventThread(new Runnable() {
254: public void run() {
255: CodeCompletionInfoCollection c = new CodeCompletionInfoCollection(
256: session, CodeCompletionPlugin.this );
257:
258: CompleteCodeAction cca = new CompleteCodeAction(session
259: .getApplication(), CodeCompletionPlugin.this ,
260: sqlPaneAPI.getSQLEntryPanel(), session, c);
261:
262: JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cca);
263:
264: _resources.configureMenuItem(cca, item);
265:
266: JComponent comp = sqlPaneAPI.getSQLEntryPanel()
267: .getTextComponent();
268: comp.registerKeyboardAction(cca, _resources
269: .getKeyStroke(cca),
270: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
271:
272: sqlPaneAPI.addToToolsPopUp("completecode", cca);
273: }
274:
275: });
276: }
277:
278: /**
279: * Retrieve plugins resources.
280: *
281: * @return Plugins resources.
282: */
283: public PluginResources getResources() {
284: return _resources;
285: }
286:
287: }
|