001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Component;
027: import java.awt.Container;
028: import java.awt.Frame;
029: import java.awt.GridBagConstraints;
030: import java.awt.GridBagLayout;
031: import java.awt.Insets;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import javax.swing.BorderFactory;
037: import javax.swing.Box;
038: import javax.swing.JButton;
039: import javax.swing.JComboBox;
040: import javax.swing.JDialog;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JPasswordField;
044: import javax.swing.JTextField;
045: import javax.swing.SwingConstants;
046: import javax.swing.border.TitledBorder;
047: import javax.swing.text.JTextComponent;
048: import org.skunk.dav.client.DAVConnectionPool;
049: import org.skunk.dav.client.DAVConstants;
050: import org.skunk.swing.text.EntryDocument;
051: import org.skunk.swing.text.PositiveIntegerEntryFilter;
052:
053: public class NewConnectionDialog extends JDialog implements
054: ActionListener {
055: private HashMap textfields = new HashMap();
056: private JButton okButton;
057: private static HashMap defaultData = new HashMap();
058: private static NewConnectionDialog dialog;
059: private ServerData sd = null;
060: private static final String PROTOCOL = "protocol";
061: private static final String HOST = "host";
062: private static final String PORT = "port";
063: private static final String PATH = "path";
064: private static final String USERNAME = "username";
065: private static final String PASSWORD = "password";
066: private static final String OK = "OK";
067: private static final String CANCEL = "CANCEL";
068: private JComboBox protocolChooser;
069:
070: static {
071: defaultData.put(PROTOCOL, DAVConstants.HTTP);
072: defaultData.put(HOST, "");
073: defaultData.put(PORT, "80");
074: defaultData.put(PATH, "/");
075: defaultData.put(USERNAME, "");
076: defaultData.put(PASSWORD, "");
077: }
078:
079: private NewConnectionDialog(Frame owner, String title) {
080: super (owner, title, true);
081: init();
082: }
083:
084: private void init() {
085: Container contentPane = getContentPane();
086: contentPane.setLayout(new BorderLayout());
087: contentPane.add(getForm(), BorderLayout.CENTER);
088: contentPane.add(getButtons(), BorderLayout.SOUTH);
089: getRootPane().setDefaultButton(okButton);
090: pack();
091: }
092:
093: private JPanel getForm() {
094: JPanel formPanel = new JPanel();
095: formPanel.setLayout(new GridBagLayout());
096: GridBagConstraints gbc = new GridBagConstraints();
097: gbc.gridx = 0;
098: gbc.gridy = 0;
099: gbc.gridwidth = 1;
100: gbc.gridheight = 1;
101: gbc.fill = gbc.NONE;
102: gbc.insets = new Insets(2, 2, 2, 2);
103: gbc.ipadx = 1;
104: gbc.ipady = 1;
105: JLabel tmpLabel;
106:
107: gbc.anchor = gbc.EAST;
108: tmpLabel = new JLabel(ResourceManager
109: .getMessage(ResourceManager.PROTOCOL_PROMPT),
110: SwingConstants.RIGHT);
111: formPanel.add(tmpLabel, gbc);
112:
113: gbc.gridx++;
114: gbc.anchor = gbc.WEST;
115: protocolChooser = new JComboBox(getAvailableProtocols());
116: formPanel.add(protocolChooser, gbc);
117: gbc.gridx = 0;
118: gbc.gridy++;
119:
120: JTextField tmpText;
121:
122: gbc.anchor = gbc.EAST;
123: tmpLabel = new JLabel(ResourceManager
124: .getMessage(ResourceManager.HOST_PROMPT),
125: SwingConstants.RIGHT);
126: formPanel.add(tmpLabel, gbc);
127:
128: gbc.gridx++;
129: gbc.anchor = gbc.WEST;
130: tmpText = new JTextField(22);
131: textfields.put(HOST, tmpText);
132: formPanel.add(tmpText, gbc);
133:
134: gbc.gridx = 0;
135: gbc.gridy++;
136: gbc.anchor = gbc.EAST;
137: tmpLabel = new JLabel(ResourceManager
138: .getMessage(ResourceManager.PORT_PROMPT),
139: SwingConstants.RIGHT);
140: formPanel.add(tmpLabel, gbc);
141:
142: gbc.gridx++;
143: gbc.anchor = gbc.WEST;
144: tmpText = new JTextField(5);
145: tmpText.setDocument(new EntryDocument(
146: new PositiveIntegerEntryFilter(0, ((int) Math
147: .pow(2, 16)) - 1)));
148: tmpText.setText(defaultData.get(PORT).toString());
149: textfields.put(PORT, tmpText);
150: formPanel.add(tmpText, gbc);
151:
152: gbc.gridx = 0;
153: gbc.gridy++;
154: gbc.anchor = gbc.EAST;
155: tmpLabel = new JLabel(ResourceManager
156: .getMessage(ResourceManager.PATH_PROMPT),
157: SwingConstants.RIGHT);
158: formPanel.add(tmpLabel, gbc);
159:
160: gbc.gridx++;
161: gbc.anchor = gbc.WEST;
162: tmpText = new JTextField(22);
163: tmpText.setText(defaultData.get(PATH).toString());
164: textfields.put(PATH, tmpText);
165: formPanel.add(tmpText, gbc);
166:
167: gbc.gridx = 0;
168: gbc.gridy++;
169: gbc.anchor = gbc.EAST;
170: tmpLabel = new JLabel(ResourceManager
171: .getMessage(ResourceManager.USERNAME_PROMPT),
172: SwingConstants.RIGHT);
173: formPanel.add(tmpLabel, gbc);
174:
175: gbc.gridx++;
176: gbc.anchor = gbc.WEST;
177: tmpText = new JTextField(22);
178: textfields.put(USERNAME, tmpText);
179: formPanel.add(tmpText, gbc);
180:
181: gbc.gridx = 0;
182: gbc.gridy++;
183: gbc.anchor = gbc.EAST;
184: tmpLabel = new JLabel(ResourceManager
185: .getMessage(ResourceManager.PASSWORD_PROMPT),
186: SwingConstants.RIGHT);
187: formPanel.add(tmpLabel, gbc);
188:
189: gbc.gridx++;
190: gbc.anchor = gbc.WEST;
191: tmpText = new JPasswordField(22);
192: textfields.put(PASSWORD, tmpText);
193: formPanel.add(tmpText, gbc);
194:
195: formPanel
196: .setBorder(BorderFactory
197: .createTitledBorder(
198: BorderFactory.createEtchedBorder(),
199: ResourceManager
200: .getMessage(ResourceManager.ADD_CONNECTION_PROMPT),
201: TitledBorder.LEFT, TitledBorder.TOP));
202: return formPanel;
203: }
204:
205: private Component getButtons() {
206: Box buttonBox = Box.createHorizontalBox();
207: okButton = new JButton(ResourceManager
208: .getMessage(ResourceManager.OK));
209: okButton.setActionCommand(OK);
210: okButton.addActionListener(this );
211: buttonBox.add(Box.createHorizontalGlue());
212: buttonBox.add(okButton);
213: buttonBox.add(Box.createHorizontalGlue());
214: JButton tmpButton = new JButton(ResourceManager
215: .getMessage(ResourceManager.CANCEL));
216: tmpButton.setActionCommand(CANCEL);
217: tmpButton.addActionListener(this );
218: buttonBox.add(tmpButton);
219: buttonBox.add(Box.createHorizontalGlue());
220: return buttonBox;
221: }
222:
223: public void actionPerformed(ActionEvent ae) {
224: if (ae.getActionCommand().equals(CANCEL)) {
225: sd = null;
226: } else {
227: boolean useSSL = protocolChooser.getSelectedItem()
228: .toString().equals(DAVConstants.HTTPS);
229: String host = getValue(HOST);
230: try {
231: int port = Integer.parseInt(getValue(PORT));
232: String path = getValue(PATH);
233: String username = getValue(USERNAME);
234: String password = getValue(PASSWORD);
235:
236: sd = new ServerData(useSSL, host, port, path, username,
237: password);
238: } catch (NumberFormatException nafta) {
239: sd = null;
240: }
241:
242: }
243: protocolChooser.setSelectedIndex(0);
244: resetTextFields();
245: hide();
246: }
247:
248: private Object[] getAvailableProtocols() {
249: if (DAVConnectionPool.canSupportSSL())
250: return new Object[] { DAVConstants.HTTP, DAVConstants.HTTPS };
251: else
252: return new Object[] { DAVConstants.HTTP };
253: }
254:
255: private String getValue(String key) {
256: return ((JTextField) textfields.get(key)).getText().trim();
257: }
258:
259: private void resetTextFields() {
260: for (Iterator it = textfields.keySet().iterator(); it.hasNext();) {
261: String key = it.next().toString();
262: JTextComponent txtfld = (JTextComponent) textfields
263: .get(key);
264: if (key.equals(HOST))
265: txtfld.setText(defaultData.get(HOST).toString());
266: else if (key.equals(PORT))
267: txtfld.setText(defaultData.get(PORT).toString());
268: else if (key.equals(PATH))
269: txtfld.setText(defaultData.get(PATH).toString());
270: else
271: txtfld.setText("");
272: }
273: }
274:
275: public static ServerData getNewServerData(Frame owner) {
276: if (dialog == null)
277: dialog = new NewConnectionDialog(owner, ResourceManager
278: .getMessage(ResourceManager.ADD_CONNECTION_TITLE));
279: dialog.setLocationRelativeTo(owner);
280: dialog.show();
281: if (dialog.sd != null)
282: return (ServerData) dialog.sd.clone();
283: else
284: return null;
285: }
286:
287: }
288:
289: /* $Log: NewConnectionDialog.java,v $
290: /* Revision 1.10 2001/09/01 14:42:59 smulloni
291: /* fix for port entry upper limit being too low on add connection dialog
292: /* (thanks to Will Radcliffe for pointing this out).
293: /*
294: /* Revision 1.9 2001/01/05 08:01:12 smulloni
295: /* changes to the connection gui for the Explorer; added depth configurability to
296: /* propfind in the jpython test script; added an experimental "allprop" system
297: /* property which affects the propfind query type
298: /*
299: /* Revision 1.8 2000/12/19 22:36:05 smulloni
300: /* adjustments to preamble.
301: /*
302: /* Revision 1.7 2000/12/18 23:22:47 smulloni
303: /* added optional SSL capability to the gui application. Separate make targets
304: /* exist for building it will SSL support.
305: /*
306: /* Revision 1.6 2000/12/03 23:53:26 smulloni
307: /* added license and copyright preamble to java files.
308: /*
309: /* Revision 1.5 2000/11/09 23:34:58 smullyan
310: /* log added to every Java file, with the help of python. Lock stealing
311: /* implemented, and treatment of locks made more robust.
312: /* */
|