001: package net.sourceforge.squirrel_sql.plugins.sqlbookmark;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006:
007: public class AddBookmarkDialog extends JDialog {
008: private static final String BM_TITLE = "dialog.add.title";
009: private static final String BM_NAME = "dialog.add.name";
010: private static final String BM_DESCRIPTION = "dialog.add.description";
011: private static final String BM_ENTER_NAME = "dialog.add.entername";
012: private static final String BM_ENTER_DESCRIPTION = "dialog.add.enterdescription";
013: public static final String BM_ACCESS_HINT = "dialog.add.accesshint";
014:
015: private JTextField txtName = new JTextField();
016: private JTextField txtDescription = new JTextField();
017: private JButton btnOK;
018: private JButton btnCancel;
019: private static final String BM_CANCEL = "dialog.add.cancel";
020: private static final String BM_OK = "dialog.add.ok";
021: private SQLBookmarkPlugin plugin;
022: private boolean ok;
023:
024: public AddBookmarkDialog(Frame frame, SQLBookmarkPlugin plugin) {
025: super (frame, plugin.getResourceString(BM_TITLE), true);
026: this .plugin = plugin;
027:
028: createUI();
029:
030: btnOK.addActionListener(new ActionListener() {
031: public void actionPerformed(ActionEvent e) {
032: onOK();
033: }
034: });
035:
036: btnCancel.addActionListener(new ActionListener() {
037: public void actionPerformed(ActionEvent e) {
038: onCancel();
039: }
040: });
041: }
042:
043: private void onCancel() {
044: closeDialog();
045: }
046:
047: private void onOK() {
048: String name = txtName.getText();
049: if (null == name || 0 == txtName.getText().trim().length()
050: || containsWhiteSpaces(name)) {
051: JOptionPane.showMessageDialog(this , plugin
052: .getResourceString(BM_ENTER_NAME));
053: return;
054: }
055:
056: String description = txtDescription.getText();
057: if (null == description
058: || 0 == txtDescription.getText().trim().length()) {
059: JOptionPane.showMessageDialog(this , plugin
060: .getResourceString(BM_ENTER_DESCRIPTION));
061: return;
062: }
063:
064: ok = true;
065:
066: closeDialog();
067: }
068:
069: private boolean containsWhiteSpaces(String name) {
070: for (int i = 0; i < name.length(); ++i) {
071: if (Character.isWhitespace(name.charAt(i))) {
072: return true;
073: }
074: }
075: return false;
076:
077: }
078:
079: private void closeDialog() {
080: setVisible(false);
081: dispose();
082: }
083:
084: private void createUI() {
085: getContentPane().setLayout(new GridBagLayout());
086:
087: GridBagConstraints gbc;
088:
089: gbc = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
090: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
091: new Insets(5, 5, 0, 0), 0, 0);
092: getContentPane().add(
093: new JLabel(plugin.getResourceString(BM_NAME)), gbc);
094:
095: gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
096: GridBagConstraints.NORTHWEST,
097: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5),
098: 0, 0);
099: getContentPane().add(txtName, gbc);
100:
101: gbc = new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
102: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
103: new Insets(5, 5, 0, 0), 0, 0);
104: getContentPane().add(
105: new JLabel(plugin.getResourceString(BM_DESCRIPTION)),
106: gbc);
107:
108: gbc = new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0,
109: GridBagConstraints.NORTHWEST,
110: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5),
111: 0, 0);
112: getContentPane().add(txtDescription, gbc);
113:
114: gbc = new GridBagConstraints(0, 2, 3, 1, 1.0, 0.0,
115: GridBagConstraints.NORTHWEST,
116: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5),
117: 0, 0);
118: JLabel lblAccesshint = new JLabel(plugin
119: .getResourceString(BM_ACCESS_HINT));
120: lblAccesshint.setForeground(Color.red);
121: getContentPane().add(lblAccesshint, gbc);
122:
123: JPanel pnlButtons = new JPanel(new GridBagLayout());
124:
125: btnOK = new JButton(plugin.getResourceString(BM_OK));
126: gbc = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
127: GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
128: new Insets(0, 0, 0, 5), 0, 0);
129: pnlButtons.add(btnOK, gbc);
130:
131: btnCancel = new JButton(plugin.getResourceString(BM_CANCEL));
132: gbc = new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
133: GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
134: new Insets(0, 0, 0, 0), 0, 0);
135: pnlButtons.add(btnCancel, gbc);
136:
137: gbc = new GridBagConstraints(2, 3, 1, 1, 1.0, 0.0,
138: GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
139: new Insets(5, 5, 5, 5), 0, 0);
140: getContentPane().add(pnlButtons, gbc);
141:
142: getRootPane().setDefaultButton(btnOK);
143:
144: txtName.requestFocus();
145:
146: AbstractAction closeAction = new AbstractAction() {
147: public void actionPerformed(ActionEvent actionEvent) {
148: closeDialog();
149: }
150: };
151: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
152: KeyEvent.VK_ESCAPE, 0);
153: getRootPane().getInputMap(
154: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
155: escapeStroke, "CloseAction");
156: getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
157: .put(escapeStroke, "CloseAction");
158: getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
159: escapeStroke, "CloseAction");
160: getRootPane().getActionMap().put("CloseAction", closeAction);
161:
162: setSize(430, 130);
163: }
164:
165: public boolean isOK() {
166: return ok;
167: }
168:
169: public String getDescription() {
170: return txtDescription.getText().trim();
171: }
172:
173: public String getBookmarkName() {
174: return txtName.getText().trim();
175: }
176: }
|