001: /*
002: * Copyright (C) 2003 Joseph Mocker
003: * mock-sf@misfit.dhs.org
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package net.sourceforge.squirrel_sql.plugins.sqlbookmark;
021:
022: import java.awt.Frame;
023:
024: import javax.swing.JOptionPane;
025:
026: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
027: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
028: import net.sourceforge.squirrel_sql.client.session.ISQLEntryPanel;
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
031: import net.sourceforge.squirrel_sql.fw.util.ICommand;
032: import net.sourceforge.squirrel_sql.fw.util.StringManager;
033: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036:
037: /**
038: * Initiate the addition of a new SQL Bookmark.
039: *
040: * The SQL for the bookmark is taken from the current SQL Edit buffer.
041: * The user is prompted for a name for the bookmark.
042: *
043: * @author Joseph Mocker
044: **/
045: public class AddBookmarkCommand implements ICommand {
046:
047: private static final StringManager s_stringMgr = StringManagerFactory
048: .getStringManager(AddBookmarkCommand.class);
049:
050: private static ILogger logger = LoggerController
051: .createLogger(AddBookmarkCommand.class);
052:
053: /** Parent frame. */
054: private final Frame frame;
055:
056: /** The session that we are saving a script for. */
057: private final ISession session;
058:
059: /** The current plugin. */
060: private SQLBookmarkPlugin plugin;
061:
062: /**
063: * Ctor.
064: *
065: * @param frame Parent Frame.
066: * @param session The session that we are saving a script for.
067: * @param plugin The current plugin.
068: *
069: * @throws IllegalArgumentException
070: * Thrown if a <TT>null</TT> <TT>ISession</TT> or <TT>IPlugin</TT>
071: * passed.
072: */
073: public AddBookmarkCommand(Frame frame, ISession session,
074: SQLBookmarkPlugin plugin) throws IllegalArgumentException {
075: super ();
076: if (session == null) {
077: throw new IllegalArgumentException("Null ISession passed");
078: }
079: if (plugin == null) {
080: throw new IllegalArgumentException("Null IPlugin passed");
081: }
082: this .frame = frame;
083: this .session = session;
084: this .plugin = plugin;
085: }
086:
087: /**
088: * Execute the addition of the bookmark.
089: */
090: public void execute() {
091: if (session == null) {
092: return;
093: }
094:
095: ISQLEntryPanel sqlEntryPanel;
096:
097: if (session.getActiveSessionWindow() instanceof SessionInternalFrame) {
098: sqlEntryPanel = ((SessionInternalFrame) session
099: .getActiveSessionWindow()).getSQLPanelAPI()
100: .getSQLEntryPanel();
101: } else if (session.getActiveSessionWindow() instanceof SQLInternalFrame) {
102: sqlEntryPanel = ((SQLInternalFrame) session
103: .getActiveSessionWindow()).getSQLPanelAPI()
104: .getSQLEntryPanel();
105: } else {
106: return;
107: }
108:
109: String sql = sqlEntryPanel.getSQLToBeExecuted();
110: if (null == sql || 0 == sql.trim().length()) {
111: // i18n[sqlbookmark.noAdd=No text to be added.]
112: JOptionPane.showMessageDialog(frame, s_stringMgr
113: .getString("sqlbookmark.noAdd"));
114: return;
115: }
116:
117: AddBookmarkDialog abd = new AddBookmarkDialog(frame, plugin);
118: GUIUtils.centerWithinParent(abd);
119: abd.setVisible(true);
120:
121: if (false == abd.isOK())
122: return;
123:
124: Bookmark bookmark = new Bookmark(abd.getBookmarkName(), abd
125: .getDescription(), sql);
126:
127: if (!plugin.getBookmarkManager().add(bookmark)) {
128: plugin.addBookmarkItem(bookmark);
129: }
130:
131: plugin.getBookmarkManager().save();
132: }
133:
134: }
|