001: /*
002: * ProfileSelectionDialog.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.profiles;
013:
014: import java.awt.BorderLayout;
015: import java.awt.FlowLayout;
016: import java.awt.Frame;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.awt.event.MouseEvent;
020: import java.awt.event.MouseListener;
021: import java.awt.event.WindowEvent;
022: import java.awt.event.WindowListener;
023:
024: import javax.swing.ActionMap;
025: import javax.swing.InputMap;
026: import javax.swing.JButton;
027: import javax.swing.JComponent;
028: import javax.swing.JDialog;
029: import javax.swing.JPanel;
030: import javax.swing.JRootPane;
031: import javax.swing.event.TreeSelectionEvent;
032: import javax.swing.event.TreeSelectionListener;
033:
034: import workbench.db.ConnectionProfile;
035: import workbench.gui.WbSwingUtilities;
036: import workbench.gui.actions.EscAction;
037: import workbench.gui.components.WbButton;
038: import workbench.resource.ResourceMgr;
039: import workbench.resource.Settings;
040: import workbench.util.StringUtil;
041:
042: /**
043: *
044: * @author support@sql-workbench.net
045: */
046: public class ProfileSelectionDialog extends JDialog implements
047: ActionListener, WindowListener, TreeSelectionListener,
048: MouseListener {
049: private JPanel buttonPanel;
050: private JButton okButton;
051: private JButton cancelButton;
052: private ProfileEditorPanel profiles;
053: private ConnectionProfile selectedProfile;
054: private boolean cancelled = false;
055: private String escActionCommand;
056:
057: public ProfileSelectionDialog(Frame parent, boolean modal) {
058: this (parent, modal, null);
059: }
060:
061: public ProfileSelectionDialog(Frame parent, boolean modal,
062: String lastProfileKey) {
063: super (parent, modal);
064: initComponents(lastProfileKey);
065:
066: JRootPane root = this .getRootPane();
067: root.setDefaultButton(okButton);
068: InputMap im = root
069: .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
070: ActionMap am = root.getActionMap();
071: EscAction esc = new EscAction(this );
072: esc.addToInputMap(im, am);
073: escActionCommand = esc.getActionName();
074: }
075:
076: private void initComponents(String lastProfileKey) {
077: profiles = new ProfileEditorPanel(lastProfileKey);
078:
079: buttonPanel = new JPanel();
080: okButton = new WbButton(ResourceMgr
081: .getString(ResourceMgr.TXT_OK));
082: okButton.setEnabled(profiles.getSelectedProfile() != null);
083:
084: cancelButton = new WbButton(ResourceMgr
085: .getString(ResourceMgr.TXT_CANCEL));
086:
087: addWindowListener(this );
088: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
089:
090: buttonPanel.add(okButton);
091: okButton.addActionListener(this );
092:
093: buttonPanel.add(cancelButton);
094: cancelButton.addActionListener(this );
095:
096: profiles.addListMouseListener(this );
097: profiles.addSelectionListener(this );
098:
099: BorderLayout bl = new BorderLayout();
100: this .getContentPane().setLayout(bl);
101: getContentPane().add(profiles, BorderLayout.CENTER);
102: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
103:
104: setTitle(ResourceMgr.getString("LblSelectProfile"));
105: this .restoreSize();
106: }
107:
108: private void closeDialog() {
109: this .saveSize();
110: this .profiles.saveSettings();
111: this .setVisible(false);
112: dispose();
113: }
114:
115: public ConnectionProfile getSelectedProfile() {
116: return this .selectedProfile;
117: }
118:
119: public void restoreSize() {
120: if (!Settings.getInstance().restoreWindowSize(this )) {
121: this .setSize(700, 550);
122: }
123: }
124:
125: public void saveSize() {
126: Settings s = Settings.getInstance();
127: s.storeWindowSize(this );
128: }
129:
130: public void selectProfile() {
131: if (this .profiles.validateInput()) {
132: this .selectedProfile = this .profiles.getSelectedProfile();
133: if (this .checkPassword()) {
134: this .cancelled = false;
135: this .closeDialog();
136: }
137: }
138: }
139:
140: private boolean checkPassword() {
141: if (this .selectedProfile == null)
142: return false;
143: if (this .selectedProfile.getStorePassword())
144: return true;
145:
146: String pwd = WbSwingUtilities.getUserInput(this , ResourceMgr
147: .getString("MsgInputPwdWindowTitle"), "", true);
148: if (StringUtil.isEmptyString(pwd))
149: return false;
150: this .selectedProfile.setPassword(pwd);
151: return true;
152: }
153:
154: public void profileListClicked(MouseEvent evt) {
155: if (evt.getButton() == MouseEvent.BUTTON1
156: && evt.getClickCount() == 2) {
157: selectProfile();
158: }
159: }
160:
161: public void setInitialFocus() {
162: profiles.setInitialFocus();
163: }
164:
165: public void actionPerformed(ActionEvent e) {
166: if (e.getSource() == this .okButton) {
167: selectProfile();
168: } else if (e.getSource() == this .cancelButton
169: || e.getActionCommand().equals(escActionCommand)) {
170: this .selectedProfile = null;
171: this .closeDialog();
172: }
173: }
174:
175: public boolean isCancelled() {
176: return this .cancelled;
177: }
178:
179: public void windowActivated(WindowEvent e) {
180: }
181:
182: public void setVisible(boolean aFlag) {
183: super .setVisible(aFlag);
184: if (aFlag) {
185: this .setInitialFocus();
186: }
187: }
188:
189: public void windowClosed(WindowEvent e) {
190: this .profiles.done();
191: }
192:
193: public void windowClosing(WindowEvent e) {
194: this .cancelled = true;
195: this .selectedProfile = null;
196: this .closeDialog();
197: }
198:
199: public void windowDeactivated(WindowEvent e) {
200: }
201:
202: public void windowDeiconified(WindowEvent e) {
203: }
204:
205: public void windowIconified(WindowEvent e) {
206: }
207:
208: public void windowOpened(WindowEvent e) {
209: this .cancelled = true;
210: this .selectedProfile = null;
211: this .setInitialFocus();
212: }
213:
214: public void valueChanged(TreeSelectionEvent e) {
215: this .okButton.setEnabled(profiles.getSelectedProfile() != null);
216: }
217:
218: public void mouseClicked(MouseEvent evt) {
219: profileListClicked(evt);
220: }
221:
222: public void mousePressed(MouseEvent e) {
223: }
224:
225: public void mouseReleased(MouseEvent e) {
226: }
227:
228: public void mouseEntered(MouseEvent e) {
229: }
230:
231: public void mouseExited(MouseEvent e) {
232: }
233:
234: }
|