001: package net.sourceforge.squirrel_sql.plugins.sqlbookmark;
002:
003: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006:
007: import javax.swing.*;
008: import java.awt.*;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.KeyEvent;
012:
013: public class BookmarEditController {
014: private static final StringManager s_stringMgr = StringManagerFactory
015: .getStringManager(BookmarEditController.class);
016:
017: private boolean _cancelled;
018: private BookmarkEditDialog _dlg;
019: private Bookmark _mark;
020:
021: public BookmarEditController(Frame owner, Bookmark mark,
022: boolean editable) {
023: _mark = mark;
024: _dlg = new BookmarkEditDialog(owner);
025:
026: _dlg.btnOk.setEnabled(editable);
027:
028: if (null != _mark) {
029: _dlg.txtName.setText(_mark.getName());
030: _dlg.txtDescription.setText(_mark.getDescription());
031: _dlg.txtSql.setText(_mark.getSql());
032: }
033:
034: _dlg.btnOk.addActionListener(new ActionListener() {
035: public void actionPerformed(ActionEvent e) {
036: onOK();
037: }
038: });
039: _dlg.btnCancel.addActionListener(new ActionListener() {
040: public void actionPerformed(ActionEvent e) {
041: onCancel();
042: }
043: });
044:
045: AbstractAction closeAction = new AbstractAction() {
046: public void actionPerformed(ActionEvent actionEvent) {
047: onCancel();
048: }
049: };
050: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
051: KeyEvent.VK_ESCAPE, 0);
052: _dlg.getRootPane().getInputMap(
053: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
054: escapeStroke, "CloseAction");
055: _dlg.getRootPane().getInputMap(
056: JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke,
057: "CloseAction");
058: _dlg.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
059: escapeStroke, "CloseAction");
060: _dlg.getRootPane().getActionMap().put("CloseAction",
061: closeAction);
062:
063: GUIUtils.centerWithinParent(_dlg);
064: _dlg.setVisible(true);
065:
066: }
067:
068: private void onOK() {
069: String name = _dlg.txtName.getText();
070: if (null == name || 0 == name.trim().length()
071: || containsWhiteSpaces(name)) {
072: // i18n[sqlbookmark.enterName=Please enter a bookmark name with no blanks]
073: JOptionPane.showMessageDialog(_dlg, s_stringMgr
074: .getString("sqlbookmark.enterName"));
075: return;
076: }
077:
078: String description = _dlg.txtDescription.getText();
079: if (null == description || 0 == description.trim().length()) {
080: // i18n[sqlbookmark.enterDescription=Please enter a bookmark description]
081: JOptionPane.showMessageDialog(_dlg, s_stringMgr
082: .getString("sqlbookmark.enterDescription"));
083: return;
084: }
085:
086: String sql = _dlg.txtSql.getText();
087: if (null == sql || 0 == sql.trim().length()) {
088: // i18n[sqlbookmark.enterSql=Please enter a bookmark sql]
089: JOptionPane.showMessageDialog(_dlg, s_stringMgr
090: .getString("sqlbookmark.enterSql"));
091: return;
092: }
093:
094: if (null == _mark) {
095: _mark = new Bookmark(name.trim(), description.trim(), sql
096: .trim());
097: } else {
098: _mark.setName(name.trim());
099: _mark.setDescription(description.trim());
100: _mark.setSql(sql.trim());
101: }
102:
103: _dlg.setVisible(false);
104: _cancelled = false;
105: }
106:
107: private boolean containsWhiteSpaces(String name) {
108: for (int i = 0; i < name.length(); ++i) {
109: if (Character.isWhitespace(name.charAt(i))) {
110: return true;
111: }
112: }
113: return false;
114:
115: }
116:
117: private void onCancel() {
118: _dlg.setVisible(false);
119: _cancelled = true;
120: }
121:
122: /**
123: * Report whether user cancelled the operation or not.
124: *
125: * @return if true, user cancelled operation.
126: */
127: public boolean isCancelled() {
128: return _cancelled;
129: }
130:
131: /**
132: * Set the cancelled status. Called by the button actions.
133: *
134: * @param status The cancelled status.
135: */
136: public void setCancelled(boolean status) {
137: _cancelled = status;
138: }
139:
140: public Bookmark getBookmark() {
141: return _mark;
142: }
143: }
|