KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > tasks > AddBookmarks


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16
17 //NOTE TO SELF: use setModal here somewhere?
18
package net.sf.jftp.gui.tasks;
19
20 import net.sf.jftp.*;
21 import net.sf.jftp.config.*;
22 import net.sf.jftp.gui.framework.*;
23 import net.sf.jftp.net.*;
24 import net.sf.jftp.system.StringUtils;
25 import net.sf.jftp.util.*;
26
27 import java.awt.*;
28 import java.awt.event.*;
29
30 import java.io.*;
31
32 import javax.swing.*;
33
34
35 public class AddBookmarks extends HFrame implements ActionListener,
36                                                     WindowListener
37 {
38     private static JFtp jftp;
39     private HButton add = new HButton("Add Bookmark");
40     private HButton addAndConnect = new HButton("Add Bookmark and Connect to Server");
41
42     //public JComboBox protocols = new JComboBox();
43
public HComboBox protocols = new HComboBox("Protocol:");
44     public HTextField host = new HTextField("Hostname:", "localhost");
45     public HTextField user = new HTextField("Username:", "anonymous");
46     public HPasswordField pass = new HPasswordField("Password:",
47                                                     "none@nowhere.no");
48     public HTextField port = new HTextField("Port: ", "21");
49     public HTextField dirOrDom = new HTextField("Directory/Domain: ", "");
50
51     //public JComboBox isLocal = new JComboBox();
52
public HComboBox isLocal = new HComboBox("Local Connection:");
53
54     //private ActionListener protocolListener = new ActionListener();
55
//private FlowLayout fl = new FlowLayout(FlowLayout.RIGHT, 10, 5);
56
public AddBookmarks(ComponentListener l, JFtp jftp)
57     {
58         //listener = l;
59
this.jftp = jftp;
60         init();
61     }
62
63     public AddBookmarks(JFtp jftp)
64     {
65         this.jftp = jftp;
66         init();
67     }
68
69     public void init()
70     {
71         setSize(650, 400);
72         setLocation(50, 150);
73         setTitle("Add Bookmarks...");
74
75         //setBackground(okP.getBackground());
76
getContentPane().setLayout(new GridLayout(8, 1));
77
78         getContentPane().add(protocols);
79         getContentPane().add(host);
80         getContentPane().add(port);
81
82         getContentPane().add(user);
83         getContentPane().add(pass);
84
85         getContentPane().add(dirOrDom);
86
87         getContentPane().add(isLocal);
88
89         //getContentPane().add(new FlowLayout(FlowLayout.RIGHT, 10, 5));
90
//getContentPane().add(fl);
91
//fl.add(add);
92
Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER, 5, 20));
93
94         getContentPane().add(buttonPanel);
95         buttonPanel.add(add);
96         buttonPanel.add(addAndConnect);
97
98         /*
99         getContentPane().add(add);
100         getContentPane().add(addAndConnect);
101         */

102         add.addActionListener(this);
103         addAndConnect.addActionListener(this);
104
105         //port.setEnabled(false);
106
protocols.setEditable(false);
107         protocols.addItem("FTP");
108         protocols.addItem("SFTP");
109         protocols.addItem("SMB");
110         protocols.addItem("NFS");
111
112         protocols.addActionListener(this);
113
114         //protocols.addActionListener();
115
isLocal.setEditable(false);
116         isLocal.addItem("No");
117         isLocal.addItem("Yes");
118     }
119
120     public void update()
121     {
122         setVisible(true);
123         toFront();
124     }
125
126     public void windowClosing(WindowEvent e)
127     {
128         //System.exit(0);
129
this.dispose();
130     }
131
132     public void windowClosed(WindowEvent e)
133     {
134     }
135
136     public void windowActivated(WindowEvent e)
137     {
138     }
139
140     public void windowDeactivated(WindowEvent e)
141     {
142     }
143
144     public void windowIconified(WindowEvent e)
145     {
146     }
147
148     public void windowDeiconified(WindowEvent e)
149     {
150     }
151
152     public void windowOpened(WindowEvent e)
153     {
154     }
155
156     public void actionPerformed(ActionEvent e)
157     {
158         if(e.getSource() == add)
159         {
160             getData(false);
161         }
162
163         else if(e.getSource() == addAndConnect)
164         {
165             getData(true);
166         }
167
168         //FOR NOW: only other possible event is update of the protocol ComboBox
169
else
170         {
171             String JavaDoc s = new String JavaDoc("");
172
173             //s = (String) protocols.getSelectedItem()
174
s = protocols.getSelectedItem().toString();
175             System.out.println(s);
176
177             //need to put information on what each protocol accepts
178
//elsewhere to make it universally accessible to all routines
179
//that need this info?
180
if(s.equals("FTP"))
181             {
182                 port.setEnabled(true);
183                 user.setEnabled(true);
184                 pass.setEnabled(true);
185                 dirOrDom.setEnabled(true);
186             }
187             else if(s.equals("SFTP"))
188             {
189                 port.setEnabled(true);
190                 user.setEnabled(true);
191                 pass.setEnabled(true);
192                 dirOrDom.setEnabled(false);
193             }
194
195             else if(s.equals("SMB"))
196             {
197                 port.setEnabled(false);
198                 user.setEnabled(true);
199                 pass.setEnabled(true);
200                 dirOrDom.setEnabled(true);
201             }
202
203             //can assume this is NFS for now
204
else
205             {
206                 port.setEnabled(false);
207                 user.setEnabled(true);
208                 pass.setEnabled(true);
209                 dirOrDom.setEnabled(false);
210             }
211
212             //System.out.println("yes!");
213
}
214
215         //OK event
216
}
217
218     //actionPerformed
219
private void getData(boolean andConnect)
220     {
221         String JavaDoc protocoltmp = new String JavaDoc("");
222
223         //here, get the selected protocol
224
//s = (String) protocols.getSelectedItem()
225
protocoltmp = protocols.getSelectedItem().toString();
226
227         //System.out.println(s);
228
System.out.println("test");
229
230         //what happens next: get data on protocol, and based on the data
231
//for the protocol, get what's needed for each field.
232
//the data extraction from each field can actually be put in a
233
//separate private method which could also be accessed if the
234
//connection is to be initiated here
235
//then, need a routine to open the bookmark file, then append it
236
//to the end (add a blank line, then the data, separated by the
237
//hash/sharp (#) comment) (and should it be here, or should only
238
//one class know about it. Or maybe it can just refer to the config
239
//file
240
//don't need to get all this, it depends on the protocol
241
//***IMPORTANT NOTE!!!!: LAN BROWSING SHOULD BE SPEAPRTE PROTOCOL!
242
//ALSO IMPORTANT: can refer to JFtp.hostinfo.hostname, for current
243
//hostname, JFtp.hostinfo.type for prortocol, etc... refer to
244
//code in HostChooser.java that says this, but might not be the
245
//case for SFTP
246
//FTP: get all of it
247
//SFTP: All but dirOrDom? (SFTP doesn;t seem to support that at this time)
248
//SMB: Don't get port
249
//NFS: Don;t get port or dirOrDom
250
//FOR NOW: just get all data
251

252         /*
253         String htmp = StringUtils.cut(host.getText(), " ");
254         String utmp = StringUtils.cut(user.getText(), " ");
255         String ptmp = StringUtils.cut(pass.getText(), " ");
256         String potmp = StringUtils.cut(port.getText(), " ");
257
258         String dirOrDomtmp = StringUtils.cut(dirOrDom.getText(), " ");
259         */

260         String JavaDoc htmp = checkIfEmpty(StringUtils.cut(host.getText(), " "));
261         String JavaDoc utmp = checkIfEmpty(StringUtils.cut(user.getText(), " "));
262         String JavaDoc ptmp = checkIfEmpty(StringUtils.cut(pass.getText(), " "));
263         String JavaDoc potmp = checkIfEmpty(StringUtils.cut(port.getText(), " "));
264
265         String JavaDoc dirOrDomtmp = checkIfEmpty(StringUtils.cut(dirOrDom.getText(),
266                                                           " "));
267
268         String JavaDoc local = new String JavaDoc("");
269
270         Integer JavaDoc potmpInt = new Integer JavaDoc(potmp);
271         int potmpint = potmpInt.intValue();
272
273         local = isLocal.getSelectedItem().toString();
274
275         String JavaDoc localString = new String JavaDoc("");
276
277         boolean localtmp = false;
278
279         if(local.equals("true"))
280         {
281             localtmp = true;
282             localString = "true";
283         }
284         else
285         {
286             localtmp = false;
287             localString = "false";
288         }
289
290         //first, build the string which is to be appended to end
291
//of bookmark file
292
// protocol#host#user#password#port#dir/domain#local
293
String JavaDoc addedString = new String JavaDoc("");
294
295         addedString += (protocoltmp + "#");
296         addedString += (htmp + "#");
297         addedString += (utmp + "#");
298
299         addedString += (ptmp + "#");
300         addedString += (potmp + "#");
301         addedString += (dirOrDomtmp + "#");
302         addedString += localString;
303
304         //here, start the routine for appending the line to the bookmark
305
//file (SHOULD IT BE IN A SEPARATE ROUTINE?)
306
try
307         {
308             FileOutputStream fos;
309             PrintStream out;
310
311             fos = new FileOutputStream(Settings.bookmarks, true);
312             out = new PrintStream(fos);
313
314             //commented out, just for now
315
out.println(addedString);
316             out.println("");
317
318             //if it worked, update the menu
319
jftp.menuBar.loadBookmarks();
320         }
321         catch(Exception JavaDoc e)
322         {
323             e.printStackTrace();
324         }
325
326         if(andConnect)
327         {
328             if(protocoltmp.equals("FTP"))
329             {
330                 StartConnection.startFtpCon(htmp, utmp, ptmp, potmpint,
331                                             dirOrDomtmp, localtmp);
332             }
333             else
334             {
335                 StartConnection.startCon(protocoltmp, htmp, utmp, ptmp,
336                                          potmpint, dirOrDomtmp, localtmp);
337             }
338         }
339
340         //else {
341
//}
342
this.dispose();
343
344         //this.setVisible(false);
345
JFtp.mainFrame.setVisible(true);
346         JFtp.mainFrame.toFront();
347     }
348
349     //getData
350
private String JavaDoc checkIfEmpty(String JavaDoc value)
351     {
352         String JavaDoc retVal = new String JavaDoc("0");
353
354         if(value.equals(""))
355         {
356             return retVal;
357         }
358         else
359         {
360             return value;
361         }
362     }
363
364     //checkIfEmpty
365
}
366
Popular Tags