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.Dimension;
027:import java.awt.Frame;
028:import java.awt.GridBagConstraints;
029:import java.awt.GridBagLayout;
030:import java.awt.Insets;
031:import java.awt.Window;
032:import java.awt.event.ActionEvent;
033:import java.awt.event.ActionListener;
034:import java.util.Vector;
035:import javax.swing.BorderFactory;
036:import javax.swing.Box;
037:import javax.swing.DefaultComboBoxModel;
038:import javax.swing.JButton;
039:import javax.swing.JComboBox;
040:import javax.swing.JDialog;
041:import javax.swing.JLabel;
042:import javax.swing.JOptionPane;
043:import javax.swing.JPanel;
044:import javax.swing.SwingUtilities;
045:import javax.swing.border.TitledBorder;
046:import org.skunk.assert.Assertion;
047:import org.skunk.dav.client.DAVConstants;
048:import org.skunk.trace.Debug;
049:
050:/**
051: * a dialog for opening a connection to a dav server.
052: */
053:public class ConnectionDialog
054:{
055: private JComboBox dropdown;
056: private JButton connectButton, cancelButton;
057: private JDialog dialog;
058: private Explorer ex;
059: private boolean shouldConnect=false;
060: /**
061: * constructs a ConnectionDialog for the given explorer
062: * @param ex the explorer
063: */
064: public ConnectionDialog(Explorer ex)
065: {
066: this .ex=ex;
067: initComponents();
068: }
069:
070: private Frame getFrame()
071: {
072: Window w=SwingUtilities.windowForComponent(ex);
073: return (w instanceof Frame)
074: ? (Frame) w
075: : null;
076: }
077:
078: private void initComponents()
079: {
080: dialog=new JDialog(getFrame(),
081: ResourceManager.getMessage(ResourceManager.CONNECTION_DIALOG_TITLE),
082: true);
083: dropdown=new JComboBox();
084: dropdown.setEditable(true);
085: dropdown.setMinimumSize(new Dimension(660, dropdown.getPreferredSize().height));
086:
087: connectButton=new JButton(ResourceManager.getMessage(ResourceManager.CONNECT_KEY));
088: connectButton.addActionListener(new ActionListener()
089: {
090: public void actionPerformed(ActionEvent ae)
091: {
092: ConnectionDialog.this .shouldConnect=true;
093: dialog.setVisible(false);
094: }
095: });
096:
097: cancelButton=new JButton(ResourceManager.getMessage(ResourceManager.CANCEL));
098: cancelButton.addActionListener(new ActionListener()
099: {
100: public void actionPerformed(ActionEvent ae)
101: {
102: ConnectionDialog.this .shouldConnect=false;
103: dialog.setVisible(false);
104: }
105: });
106:
107: JPanel panel=new JPanel(new GridBagLayout());
108: GridBagConstraints gbc=new GridBagConstraints();
109: gbc.gridx=0;
110: gbc.gridy=0;
111: gbc.gridwidth=1;
112: gbc.gridheight=1;
113: gbc.insets=new Insets(4, 4, 4, 4);
114: gbc.fill=GridBagConstraints.HORIZONTAL;
115: gbc.anchor=GridBagConstraints.CENTER;
116: gbc.ipadx=2;
117: gbc.ipady=2;
118:
119: panel.add(dropdown, gbc);
120: gbc.gridy++;
121:
122: Box buttonBox=Box.createHorizontalBox();
123: buttonBox.add(Box.createHorizontalGlue());
124: buttonBox.add(connectButton);
125: buttonBox.add(Box.createHorizontalStrut(10));
126: buttonBox.add(cancelButton);
127: buttonBox.add(Box.createHorizontalGlue());
128: panel.add(buttonBox, gbc);
129: String borderTitle=ResourceManager.getMessage(ResourceManager.CONNECTION_DIALOG_PROMPT);
130: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
131: borderTitle,
132: TitledBorder.LEFT,
133: TitledBorder.TOP));
134:
135: dialog.setContentPane(panel);
136: dialog.getRootPane().setDefaultButton(connectButton);
137: dialog.pack();
138: }
139:
140: /**
141: * show the dialog.
142: * @return whether or not the user wants to connect
143: */
144: public boolean show()
145: {
146: DefaultComboBoxModel model=new DefaultComboBoxModel((Vector)ServerData.getServers().clone());
147: dropdown.setModel(model);
148: int size=model.getSize();
149: if (size>0)
150: dropdown.setSelectedIndex(size-1);
151: dialog.pack();
152: dialog.setLocationRelativeTo(ex);
153: shouldConnect=false;
154: dialog.show();
155: return shouldConnect;
156: }
157:
158: /**
159: * returns the ServerData chosen
160: * @return the ServerData
161: */
162: public ServerData getServerData()
163: {
164: Object val=dropdown.getSelectedItem();
165: if (val==null)
166: return null;
167: else if (val instanceof ServerData)
168: {
169: Debug.trace(this , Debug.DP5, "we have a ServerData: {0}", val);
170: return (ServerData) val;
171: }
172: else
173: {
174: ServerData retSd=ServerData.getServer(val.toString().trim());
175: if (retSd==null)
176: retSd=parseNewServer(val);
177: if (retSd!=null)
178: ExplorerMenuBar.refreshMenus();
179: return retSd;
180: }
181: }
182:
183: private ServerData parseNewServer(Object value)
184: {
185: Debug.trace(this , Debug.DP5, "in parseNewServer with value {0}", value);
186: Debug.trace(this , Debug.DP5, "value class: {0}", value.getClass());
187: String s=value.toString();
188: boolean https=s.startsWith(DAVConstants.HTTPS);
189: int port=80;
190: String host=null;
191: String initialPath=DAVConstants.DAV_FILE_SEPARATOR;
192: int protocolPadding="://".length();
193: if (https)
194: s=s.substring(DAVConstants.HTTPS.length()+protocolPadding);
195: else if (s.startsWith(DAVConstants.HTTP))
196: s=s.substring(DAVConstants.HTTP.length()+protocolPadding);
197: int firstColon=s.indexOf(":");
198: int firstSlash=s.indexOf(DAVConstants.DAV_FILE_SEPARATOR);
199: /*
200: * possible valid states:
201: * colon and slash: www.webdav.org:80/
202: * a colon, but no slash: www.webdav.org:80
203: * a slash, but no colon: www.webdav.org/
204: * no colon and no slash: www.webdav.org
205: */
206: int stateBit=((firstColon>0) ? 2 : 0)
207: + ((firstSlash>firstColon) ? 1 : 0);
208: switch (stateBit)
209: {
210: case 3: //there is a port and a path.
211: initialPath=s.substring(firstSlash);
212: //FALL THROUGH!
213: case 2: //there is a port, no path
214: host=s.substring(0, firstColon);
215: try
216: {
217: int portEnd=(stateBit==3) ? firstSlash : s.length();
218: port =Integer.parseInt(s.substring(firstColon+1, portEnd));
219: }
220: catch (NumberFormatException nafta)
221: {
222: doParseError();
223: return null;
224: }
225: break;
226: case 1: //there is a path, no port
227: host=s.substring(0, firstSlash);
228: initialPath=s.substring(firstSlash);
229: break;
230: case 0: //there is neither path nor port
231: host=s;
232: break;
233: }
234: if (host==null)
235: {
236: Assertion.assert(false, "host is not null");
237: return null;
238: }
239: else
240: return new ServerData(https,
241: host,
242: port,
243: initialPath,
244: "",
245: "");
246: }
247:
248: private void doParseError()
249: {
250: String title=ResourceManager.getMessage(ResourceManager.ERROR_DIALOG_TITLE);
251: String message=ResourceManager.getMessage(ResourceManager.CONNECTION_PARSE_ERROR_MESSAGE);
252: JOptionPane.showMessageDialog(ex, message, title, JOptionPane.ERROR_MESSAGE);
253: }
254:}
255:
256:/* $Log: ConnectionDialog.java,v $
257:/* Revision 1.3 2001/02/12 05:01:12 smulloni
258:/* made the ConnectionDialog a tad more spacious
259:/*
260:/* Revision 1.2 2001/01/09 19:08:24 smulloni
261:/* adjusted the preferred size of the combo box.
262:/*
263:/* Revision 1.1 2001/01/05 08:01:12 smulloni
264:/* changes to the connection gui for the Explorer; added depth configurability to
265:/* propfind in the jpython test script; added an experimental "allprop" system
266:/* property which affects the propfind query type
267:/* */
|