KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > hostchooser > SmbHostChooser


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 package net.sf.jftp.gui.hostchooser;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.config.*;
20 import net.sf.jftp.gui.framework.*;
21 import net.sf.jftp.net.*;
22 import net.sf.jftp.system.logging.Log;
23 import net.sf.jftp.util.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27
28 import java.io.*;
29
30 import java.net.*;
31
32 import java.util.*;
33
34 import javax.swing.*;
35
36
37 public class SmbHostChooser extends HFrame implements ActionListener,
38                                                       WindowListener
39 {
40     public static HTextField host = new HTextField("URL:", "smb://localhost/");
41     public static HTextField user = new HTextField("Username:", "guest");
42
43     //public static HTextField pass = new HTextField("Password:","none@nowhere.no");
44
public static HPasswordField pass = new HPasswordField("Password:",
45                                                            "nopasswd");
46     JCheckBox lan = new JCheckBox("Browse LAN", true);
47     public HTextField domain = new HTextField("Domain: ", "WORKGROUP");
48     public HTextField broadcast = new HTextField("Broadcast IP: ", "AUTO");
49     public HTextField wins = new HTextField("WINS Server IP: ", "NONE");
50
51     //public HTextField ip = new HTextField("Local IP: ","<default>");
52
public JComboBox ip = new JComboBox();
53     private HPanel okP = new HPanel();
54     private HButton ok = new HButton("Connect");
55     private ComponentListener listener = null;
56     private boolean useLocal = false;
57
58     public SmbHostChooser(ComponentListener l, boolean local)
59     {
60         listener = l;
61         useLocal = local;
62         init();
63     }
64
65     public SmbHostChooser(ComponentListener l)
66     {
67         listener = l;
68         init();
69     }
70
71     public SmbHostChooser()
72     {
73         init();
74     }
75
76     public void init()
77     {
78         //setSize(500, 320);
79
setLocation(100, 150);
80         setTitle("Smb Connection...");
81         setBackground(okP.getBackground());
82         getContentPane().setLayout(new GridLayout(5, 2, 5, 3));
83
84         //*** MY CHANGES
85
try
86         {
87             File f = new File(Settings.appHomeDir);
88             f.mkdir();
89
90             File f1 = new File(Settings.login);
91             f1.createNewFile();
92
93             File f2 = new File(Settings.login_def_smb);
94             f2.createNewFile();
95         }
96         catch(IOException ex)
97         {
98             ex.printStackTrace();
99         }
100
101         String JavaDoc[] login = LoadSet.loadSet(Settings.login_def_smb);
102
103         if((login[0] != null) && (login.length > 1))
104         {
105             host.setText(login[0]);
106             user.setText(login[1]);
107
108             //if (login[3] != null)
109
//lan.setText(login[3]);
110
//if (login[4] != null)
111
//ip.setText(login[4]);
112
//if (login[5] != null)
113
domain.setText(login[5]);
114         }
115
116         /*
117         else {
118                 host.setText("smb://localhost/");
119                 user.setText("guest");
120                 domain.setText("WORKGROUP");
121         }
122         */

123         if(Settings.getStorePasswords())
124         {
125             if((login != null) && (login.length > 2) && (login[2] != null))
126             {
127                 pass.setText(login[2]);
128             }
129         }
130         else
131         {
132             pass.setText("");
133         }
134
135         //***end of my changes (for this section)
136
ip.setEditable(true);
137
138         getContentPane().add(host);
139         getContentPane().add(lan);
140         getContentPane().add(user);
141         getContentPane().add(pass);
142
143         getContentPane().add(ip);
144         getContentPane().add(domain);
145
146         getContentPane().add(broadcast);
147         getContentPane().add(okP);
148
149         getContentPane().add(wins);
150
151         JTextArea t = new JTextArea();
152         t.setLineWrap(true);
153         t.setText("Note: URL is in form \"smb://host/\"\n" +
154                   "and most people do not need WINS.");
155
156         getContentPane().add(t);
157
158         okP.add(ok);
159         ok.addActionListener(this);
160
161         host.setEnabled(!lan.isSelected());
162         setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
163
164         lan.addActionListener(this);
165         pass.text.addActionListener(this);
166
167         setModal(false);
168         setVisible(false);
169         addWindowListener(this);
170
171         ip.addItem("<default>");
172
173         try
174         {
175             Enumeration e = NetworkInterface.getNetworkInterfaces();
176
177             while(e.hasMoreElements())
178             {
179                 Enumeration f = ((NetworkInterface) e.nextElement()).getInetAddresses();
180
181                 while(f.hasMoreElements())
182                 {
183                     ;
184                     ip.addItem(((InetAddress) f.nextElement()).getHostAddress());
185                 }
186             }
187         }
188         catch(Exception JavaDoc ex)
189         {
190             Log.debug("Error determining default network interface: " + ex);
191
192             //ex.printStackTrace();
193
}
194
195         //setBCast();
196
domain.setEnabled(false);
197         broadcast.setEnabled(false);
198         wins.setEnabled(false);
199
200         ip.addActionListener(this);
201
202         pack();
203         setVisible(true);
204     }
205
206     private void setBCast()
207     {
208         try
209         {
210             String JavaDoc tmp = ((String JavaDoc) ip.getSelectedItem()).trim();
211             String JavaDoc x = tmp.substring(0, tmp.lastIndexOf(".") + 1) + "255";
212             broadcast.setText(x);
213         }
214         catch(Exception JavaDoc ex)
215         {
216             Log.out("Error (SMBHostChooser): " + ex);
217         }
218     }
219
220     public void update()
221     {
222     fixLocation();
223         setVisible(true);
224         toFront();
225         host.requestFocus();
226     }
227
228     public void actionPerformed(ActionEvent e)
229     {
230         if(e.getSource() == lan)
231         {
232             host.setEnabled(!lan.isSelected());
233         }
234         else if(e.getSource() == ip)
235         {
236             if(ip.getSelectedItem().equals("<default>"))
237             {
238                 domain.setEnabled(false);
239                 broadcast.setEnabled(false);
240                 wins.setEnabled(false);
241             }
242             else
243             {
244                 domain.setEnabled(true);
245                 broadcast.setEnabled(true);
246                 wins.setEnabled(true);
247                 setBCast();
248             }
249         }
250         else if((e.getSource() == ok) || (e.getSource() == pass.text))
251         {
252             // Switch windows
253
//this.setVisible(false);
254
setCursor(new Cursor(Cursor.WAIT_CURSOR));
255
256             SmbConnection con = null;
257
258             //System.out.println(jcifs.Config.getProperty("jcifs.smb.client.laddr"));
259
String JavaDoc tmp = ((String JavaDoc) ip.getSelectedItem()).trim();
260
261             if(!tmp.equals("") && !tmp.equals("<default>"))
262             {
263                 String JavaDoc x = tmp.trim().substring(0, tmp.lastIndexOf(".") + 1) +
264                            "255";
265                 String JavaDoc bcast = broadcast.getText().trim();
266
267                 if(!bcast.equals("AUTO"))
268                 {
269                     x = bcast;
270                 }
271
272                 Log.debug("Setting LAN interface to: " + tmp + "/" + x);
273                 jcifs.Config.setProperty("jcifs.netbios.laddr", tmp);
274                 jcifs.Config.setProperty("jcifs.smb.client.laddr", tmp);
275                 jcifs.Config.setProperty("jcifs.netbios.baddr", x);
276
277                 String JavaDoc y = wins.getText().trim();
278
279                 if(!y.equals("NONE"))
280                 {
281                     Log.debug("Setting WINS server IP to: " + y);
282                     jcifs.Config.setProperty("jcifs.netbios.wins", y);
283                 }
284             }
285
286             //System.out.println(jcifs.Config.getProperty("jcifs.smb.client.laddr"));
287
//JFtp.setHost(host.getText());
288
String JavaDoc htmp = host.getText().trim();
289             String JavaDoc utmp = user.getText().trim();
290             String JavaDoc ptmp = pass.getText();
291             String JavaDoc dtmp = domain.getText().trim();
292
293             //***
294
//if(dtmp.equals("")) dtmp = null;
295
if(dtmp.equals(""))
296             {
297                 dtmp = "NONE";
298             }
299
300             //if(lan.isSelected()) htmp = null;
301
if(lan.isSelected())
302             {
303                 htmp = "(LAN)";
304             }
305
306             //***save the set of selected data
307
SaveSet s = new SaveSet(Settings.login_def_smb, htmp, utmp, ptmp,
308                                     "", "", dtmp);
309
310             //*** Now make the function call to the methos for starting
311
//connections
312
boolean status;
313             int potmp = 0; //*** port number: unlikely to be needed in the future
314

315             status = StartConnection.startCon("SMB", htmp, utmp, ptmp, potmp,
316                                               dtmp, useLocal);
317
318             /*
319             try
320             {
321              con = new SmbConnection(htmp,dtmp,utmp,ptmp, ((ConnectionListener)JFtp.remoteDir));
322
323              //JFtp.statusP.jftp.addConnection(htmp, con);
324              if(useLocal)
325              {
326                      JFtp.statusP.jftp.addLocalConnection(htmp, con);
327                 JFtp.localDir.fresh();
328              }
329              else
330              {
331                       JFtp.statusP.jftp.addConnection(htmp, con);
332                 JFtp.remoteDir.fresh();
333              }
334
335             //JFtp.remoteDir.setCon(con);
336             //con.setLocalPath(JFtp.localDir.getCon().getPWD());
337             //con.addConnectionListener((ConnectionListener) JFtp.localDir);
338             //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
339             //JFtp.remoteDir.fresh();
340             }
341             catch(Exception ex)
342             {
343                     Log.debug("Could not create SMBConnection, does this distribution come with jcifs?");
344             } */

345             setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
346             this.dispose();
347             JFtp.mainFrame.setVisible(true);
348             JFtp.mainFrame.toFront();
349
350             if(listener != null)
351             {
352                 listener.componentResized(new ComponentEvent(this, 0));
353             }
354         }
355     }
356
357     public void windowClosing(WindowEvent e)
358     {
359         //System.exit(0);
360
this.dispose();
361     }
362
363     public void windowClosed(WindowEvent e)
364     {
365     }
366
367     public void windowActivated(WindowEvent e)
368     {
369     }
370
371     public void windowDeactivated(WindowEvent e)
372     {
373     }
374
375     public void windowIconified(WindowEvent e)
376     {
377     }
378
379     public void windowDeiconified(WindowEvent e)
380     {
381     }
382
383     public void windowOpened(WindowEvent e)
384     {
385     }
386
387     public Insets getInsets()
388     {
389         Insets std = super.getInsets();
390
391         return new Insets(std.top + 10, std.left + 10, std.bottom + 10,
392                           std.right + 10);
393     }
394
395     public void pause(int time)
396     {
397         try
398         {
399             Thread.sleep(time);
400         }
401         catch(Exception JavaDoc ex)
402         {
403         }
404     }
405 }
406
Popular Tags