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.util.Iterator;
023:
024: import java.awt.*;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027: import javax.swing.*;
028: import javax.swing.event.TreeSelectionListener;
029: import javax.swing.event.TreeSelectionEvent;
030: import javax.swing.tree.*;
031:
032: import net.sourceforge.squirrel_sql.fw.util.StringManager;
033: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
034: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
035: import net.sourceforge.squirrel_sql.client.IApplication;
036: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
037:
038: /**
039: * Manage the bookmarks.
040: * <p/>
041: * The interface to allow a user to manages his/her bookmarks is through
042: * this class. The user can add, edit, remove and shift the order of the
043: * bookmarks with this user interface. The interface shows up in as
044: * a new tab in the Global Preferences dialog.
045: *
046: * @author Joseph Mocker
047: */
048: public class SQLBookmarkPreferencesController implements
049: IGlobalPreferencesPanel {
050:
051: private static final StringManager s_stringMgr = StringManagerFactory
052: .getStringManager(SQLBookmarkPreferencesController.class);
053:
054: /**
055: * The main panel for preference administration
056: */
057: protected SQLBookmarkPreferencesPanel _pnlPrefs;
058:
059: /**
060: * Handle to the main application
061: */
062: protected IApplication _app;
063:
064: /**
065: * Handle to the plugin
066: */
067: protected SQLBookmarkPlugin _plugin;
068: private DefaultMutableTreeNode _nodeSquirrelMarks;
069: private DefaultMutableTreeNode _nodeUserMarks;
070:
071: /**
072: * Create the preferences
073: */
074: public SQLBookmarkPreferencesController(SQLBookmarkPlugin plugin) {
075: this ._plugin = plugin;
076: }
077:
078: /**
079: * Initialize the user interface
080: *
081: * @param app Handle to the main application.
082: */
083: public void initialize(IApplication app) {
084: this ._app = app;
085:
086: // i18n[sqlbookmark.btnTextEdit=Edit]
087: _pnlPrefs.btnEdit.setText(s_stringMgr
088: .getString("sqlbookmark.btnTextEdit"));
089:
090: DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
091:
092: // i18n[sqlbookmark.nodeSquirrelMarks=SQuirreL bookmarks]
093: _nodeSquirrelMarks = new DefaultMutableTreeNode(s_stringMgr
094: .getString("sqlbookmark.nodeSquirrelMarks"));
095: // i18n[sqlbookmark.nodeUserMarks=User bookmarks]
096: _nodeUserMarks = new DefaultMutableTreeNode(s_stringMgr
097: .getString("sqlbookmark.nodeUserMarks"));
098:
099: root.add(_nodeUserMarks);
100: root.add(_nodeSquirrelMarks);
101: DefaultTreeModel dtm = new DefaultTreeModel(root);
102: _pnlPrefs.treBookmarks.setModel(dtm);
103: _pnlPrefs.treBookmarks.setRootVisible(false);
104:
105: _pnlPrefs.treBookmarks.getSelectionModel().setSelectionMode(
106: TreeSelectionModel.SINGLE_TREE_SELECTION);
107:
108: Bookmark[] defaultBookmarks = DefaultBookmarksFactory
109: .getDefaultBookmarks();
110: for (int i = 0; i < defaultBookmarks.length; i++) {
111: _nodeSquirrelMarks.add(new DefaultMutableTreeNode(
112: defaultBookmarks[i]));
113: }
114:
115: for (Iterator<Bookmark> i = _plugin.getBookmarkManager()
116: .iterator(); i.hasNext();) {
117: Bookmark mark = i.next();
118: _nodeUserMarks.add(new DefaultMutableTreeNode(mark));
119: }
120:
121: _pnlPrefs.treBookmarks.expandPath(new TreePath(dtm
122: .getPathToRoot(_nodeUserMarks)));
123:
124: String propDefaultMarksInPopup = _plugin
125: .getBookmarkProperties()
126: .getProperty(
127: SQLBookmarkPlugin.BOOKMARK_PROP_DEFAULT_MARKS_IN_POPUP,
128: "" + false);
129:
130: _pnlPrefs.chkSquirrelMarksInPopup.setSelected(Boolean.valueOf(
131: propDefaultMarksInPopup).booleanValue());
132:
133: _pnlPrefs.treBookmarks.getSelectionModel()
134: .addTreeSelectionListener(new TreeSelectionListener() {
135: public void valueChanged(TreeSelectionEvent e) {
136: onTreeSelectionChanged(e);
137: }
138: });
139:
140: _pnlPrefs.btnRun.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: onRun();
143: }
144: });
145:
146: _pnlPrefs.btnAdd.addActionListener(new ActionListener() {
147: public void actionPerformed(ActionEvent e) {
148: onAdd();
149: }
150: });
151: _pnlPrefs.btnEdit.addActionListener(new ActionListener() {
152: public void actionPerformed(ActionEvent e) {
153: onEdit();
154: }
155: });
156: _pnlPrefs.btnDel.addActionListener(new ActionListener() {
157: public void actionPerformed(ActionEvent e) {
158: onDelete();
159: }
160: });
161:
162: _pnlPrefs.btnUp.addActionListener(new ActionListener() {
163: public void actionPerformed(ActionEvent e) {
164: onUp();
165: }
166: });
167:
168: _pnlPrefs.btnDown.addActionListener(new ActionListener() {
169: public void actionPerformed(ActionEvent e) {
170: onDown();
171: }
172: });
173:
174: }
175:
176: public void uninitialize(IApplication app) {
177: _plugin.removeALLSQLPanelsAPIListeningForBookmarks();
178: }
179:
180: /**
181: * Return the title for the tab name
182: *
183: * @return The tab title.
184: */
185: public String getTitle() {
186: return _plugin.getResourceString("prefs.title");
187: }
188:
189: /**
190: * Return the tool tip for the tab
191: *
192: * @return The tab hint
193: */
194: public String getHint() {
195: return _plugin.getResourceString("prefs.hint");
196: }
197:
198: /**
199: * Make the changes active to the rest of the application
200: */
201: public void applyChanges() {
202: // create a new bookmark manager
203: BookmarkManager bookmarks = _plugin.getBookmarkManager();
204:
205: bookmarks.removeAll();
206:
207: for (int i = 0; i < _nodeUserMarks.getChildCount(); ++i) {
208: Bookmark bookmark = (Bookmark) ((DefaultMutableTreeNode) _nodeUserMarks
209: .getChildAt(i)).getUserObject();
210: bookmarks.add(bookmark);
211: }
212:
213: // rebuild the bookmark menu.
214: _plugin.rebuildMenu();
215: bookmarks.save();
216:
217: _plugin.getBookmarkProperties().put(
218: SQLBookmarkPlugin.BOOKMARK_PROP_DEFAULT_MARKS_IN_POPUP,
219: "" + _pnlPrefs.chkSquirrelMarksInPopup.isSelected());
220: _plugin.saveBookmarkProperties();
221: }
222:
223: /**
224: * Return the panel that will contain the prefernces ui.
225: *
226: * @return Panel containing preferences.
227: */
228: public Component getPanelComponent() {
229: // this gets called before initialize()
230: _pnlPrefs = new SQLBookmarkPreferencesPanel(_plugin);
231:
232: return _pnlPrefs;
233: }
234:
235: private void onTreeSelectionChanged(TreeSelectionEvent e) {
236: if (null == e.getPath()) {
237: return;
238: }
239:
240: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) e
241: .getPath().getLastPathComponent();
242:
243: if (false == dmtn.getUserObject() instanceof Bookmark) {
244: return;
245: }
246:
247: if (dmtn.getParent() == _nodeSquirrelMarks) {
248: _pnlPrefs.btnUp.setEnabled(false);
249: _pnlPrefs.btnDown.setEnabled(false);
250: _pnlPrefs.btnDel.setEnabled(false);
251: // i18n[sqlbookmark.btnTextView=View]
252: _pnlPrefs.btnEdit.setText(s_stringMgr
253: .getString("sqlbookmark.btnTextView"));
254: } else {
255: _pnlPrefs.btnUp.setEnabled(true);
256: _pnlPrefs.btnDown.setEnabled(true);
257: _pnlPrefs.btnDel.setEnabled(true);
258: // i18n[sqlbookmark.btnTextEdit=Edit]
259: _pnlPrefs.btnEdit.setText(s_stringMgr
260: .getString("sqlbookmark.btnTextEdit"));
261: }
262: }
263:
264: private void onRun() {
265:
266: DefaultMutableTreeNode selNode = null;
267: if (null != _pnlPrefs.treBookmarks.getSelectionPath()) {
268: selNode = (DefaultMutableTreeNode) _pnlPrefs.treBookmarks
269: .getSelectionPath().getLastPathComponent();
270: }
271:
272: if (null == selNode
273: || false == selNode.getUserObject() instanceof Bookmark) {
274: // i18n[sqlbookmark.noRunSelection=Please select the bookmark to run]
275: _app
276: .getMessageHandler()
277: .showErrorMessage(
278: s_stringMgr
279: .getString("sqlbookmark.noRunSelection"));
280: return;
281: }
282:
283: ISQLPanelAPI[] apis = _plugin
284: .getSQLPanelAPIsListeningForBookmarks();
285:
286: if (0 == apis.length) {
287: // i18n[sqlbookmark.noSQLPanel=To run a bookmark you must open this window\nusing the "Edit Bookmarks" toolbar button of a Session window.\nThe bookmars SQL Code will then be written to the Session's SQL editor.]
288: JOptionPane.showMessageDialog(_app.getMainFrame(),
289: s_stringMgr.getString("sqlbookmark.noSQLPanel"));
290: return;
291: }
292:
293: Bookmark bm = (Bookmark) selNode.getUserObject();
294:
295: for (int i = 0; i < apis.length; i++) {
296: ISQLPanelAPI api = apis[i];
297: new RunBookmarkCommand(_app.getMainFrame(), api
298: .getSession(), bm, _plugin, api.getSQLEntryPanel())
299: .execute();
300: }
301:
302: }
303:
304: public void onAdd() {
305: BookmarEditController ctrlr = new BookmarEditController(_app
306: .getMainFrame(), null, true);
307:
308: if (ctrlr.isCancelled()) {
309: return;
310: }
311:
312: DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(
313: ctrlr.getBookmark());
314: _nodeUserMarks.add(newChild);
315:
316: ((DefaultTreeModel) _pnlPrefs.treBookmarks.getModel())
317: .nodeStructureChanged(_nodeUserMarks);
318:
319: selectNode(newChild);
320: }
321:
322: private void selectNode(DefaultMutableTreeNode toSel) {
323: TreeNode[] pathToRoot = ((DefaultTreeModel) _pnlPrefs.treBookmarks
324: .getModel()).getPathToRoot(toSel);
325: _pnlPrefs.treBookmarks
326: .setSelectionPath(new TreePath(pathToRoot));
327: }
328:
329: /**
330: * Internal action class for the main preferences tab. Called when
331: * user clicks the "Edit" button.
332: *
333: * @author Joseph Mocker
334: */
335: public void onEdit() {
336: DefaultMutableTreeNode selNode = null;
337: if (null != _pnlPrefs.treBookmarks.getSelectionPath()) {
338: selNode = (DefaultMutableTreeNode) _pnlPrefs.treBookmarks
339: .getSelectionPath().getLastPathComponent();
340: }
341:
342: if (null == selNode
343: || false == selNode.getUserObject() instanceof Bookmark) {
344: // i18n[sqlbookmark.noEditSelection=Please select the bookmark to edit]
345: _app.getMessageHandler().showErrorMessage(
346: s_stringMgr
347: .getString("sqlbookmark.noEditSelection"));
348: return;
349: }
350:
351: boolean editable = selNode.getParent() == _nodeUserMarks;
352:
353: BookmarEditController ctrlr = new BookmarEditController(_app
354: .getMainFrame(), (Bookmark) selNode.getUserObject(),
355: editable);
356:
357: if (ctrlr.isCancelled()) {
358: return;
359: }
360:
361: selNode.setUserObject(ctrlr.getBookmark());
362: }
363:
364: public void onDelete() {
365:
366: DefaultMutableTreeNode selNode = null;
367: if (null != _pnlPrefs.treBookmarks.getSelectionPath()) {
368: selNode = (DefaultMutableTreeNode) _pnlPrefs.treBookmarks
369: .getSelectionPath().getLastPathComponent();
370: }
371:
372: if (null == selNode
373: || false == selNode.getUserObject() instanceof Bookmark) {
374: // i18n[sqlbookmark.noDeleteSelection=Please select the bookmark to delete]
375: _app
376: .getMessageHandler()
377: .showErrorMessage(
378: s_stringMgr
379: .getString("sqlbookmark.noDeleteSelection"));
380: return;
381: }
382:
383: // i18n[sqlbookmark.deleteConfirm=Do you really wish to delete the selected bookmark?]
384: int ret = JOptionPane.showConfirmDialog(_app.getMainFrame(),
385: s_stringMgr.getString("sqlbookmark.deleteConfirm"));
386: if (JOptionPane.YES_OPTION != ret) {
387: return;
388: }
389:
390: DefaultMutableTreeNode nextSel = selNode.getNextSibling();
391: if (null == nextSel) {
392: nextSel = selNode.getPreviousSibling();
393: }
394:
395: _nodeUserMarks.remove(selNode);
396:
397: ((DefaultTreeModel) _pnlPrefs.treBookmarks.getModel())
398: .nodeStructureChanged(_nodeUserMarks);
399:
400: selectNode(nextSel);
401:
402: }
403:
404: private void onUp() {
405: DefaultMutableTreeNode selNode = null;
406: if (null != _pnlPrefs.treBookmarks.getSelectionPath()) {
407: selNode = (DefaultMutableTreeNode) _pnlPrefs.treBookmarks
408: .getSelectionPath().getLastPathComponent();
409: }
410:
411: if (null == selNode
412: || false == selNode.getUserObject() instanceof Bookmark
413: || 0 == _nodeUserMarks.getIndex(selNode)) {
414: return;
415: }
416:
417: int selIx = _nodeUserMarks.getIndex(selNode);
418:
419: _nodeUserMarks.insert(selNode, selIx - 1);
420:
421: ((DefaultTreeModel) _pnlPrefs.treBookmarks.getModel())
422: .nodeStructureChanged(_nodeUserMarks);
423:
424: selectNode(selNode);
425:
426: }
427:
428: private void onDown() {
429: DefaultMutableTreeNode selNode = null;
430: if (null != _pnlPrefs.treBookmarks.getSelectionPath()) {
431: selNode = (DefaultMutableTreeNode) _pnlPrefs.treBookmarks
432: .getSelectionPath().getLastPathComponent();
433: }
434:
435: if (null == selNode
436: || false == selNode.getUserObject() instanceof Bookmark
437: || _nodeUserMarks.getChildCount() - 1 == _nodeUserMarks
438: .getIndex(selNode)) {
439: return;
440: }
441:
442: int selIx = _nodeUserMarks.getIndex(selNode);
443: _nodeUserMarks.insert(selNode, selIx + 1);
444:
445: ((DefaultTreeModel) _pnlPrefs.treBookmarks.getModel())
446: .nodeStructureChanged(_nodeUserMarks);
447:
448: selectNode(selNode);
449:
450: }
451:
452: }
|