KickJava   Java API By Example, From Geeks To Geeks.

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


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.tasks;
17
18 import net.sf.jftp.*;
19 import net.sf.jftp.config.*;
20 import net.sf.jftp.gui.framework.*;
21 import net.sf.jftp.system.logging.Log;
22 import net.sf.jftp.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import java.io.*;
28
29 import javax.swing.*;
30
31
32 public class BookmarkManager extends JInternalFrame implements ActionListener
33 {
34     private JTextArea info = new JTextArea(25, 50);
35     private JButton save = new JButton("Save and close");
36     private JButton close = new JButton("Close");
37
38     public BookmarkManager()
39     {
40         super("Manage Bookmarks", true, true, true, true);
41         setLocation(50, 50);
42         setSize(600, 540);
43         getContentPane().setLayout(new BorderLayout());
44
45         load(Settings.bookmarks);
46
47         JScrollPane jsp = new JScrollPane(info);
48         getContentPane().add("Center", jsp);
49
50         HPanel closeP = new HPanel();
51         closeP.setLayout(new FlowLayout(FlowLayout.CENTER));
52
53         //closeP.add(close);
54
closeP.add(save);
55
56         close.addActionListener(this);
57         save.addActionListener(this);
58
59         getContentPane().add("South", closeP);
60
61         info.setCaretPosition(0);
62         pack();
63         setVisible(true);
64     }
65
66     public void actionPerformed(ActionEvent e)
67     {
68         if(e.getSource() == close)
69         {
70             this.dispose();
71         }
72         else
73         {
74             save(Settings.bookmarks);
75             JFtp.menuBar.loadBookmarks();
76             this.dispose();
77         }
78     }
79
80     private void setDefaultText()
81     {
82         info.setText("");
83         info.append("# JFtp Bookmark Configuration file\n");
84         info.append("#\n");
85         info.append("# Syntax: protocol#host#user#password#port#dir/domain#local\n");
86         info.append("#\n");
87         info.append("# Note: not all values are used by every connection, but all fields must contain at least\n");
88         info.append("# one character.\n");
89         info.append("Use \"<%hidden%>\" for password fields you don't want to fill out.");
90         info.append("#\n");
91         info.append("# protocol: FTP, SFTP, SMB or NFS (uppercase)\n");
92         info.append("# host: hostname or ip for ftp + sftp, valid url for smb + nfs (\"(LAN)\" for smb lan browsing)\n");
93         info.append("# user, password: the login data\n");
94         info.append("# port: this must be a number (even if it is not used for smb+nfs, set it in the url for nfs)\n");
95         info.append("# dir/domain: inital directory for the connection, domainname for smb\n");
96         info.append("# local: \"true\" if connection should be opened in local tab, \"false\" otherwise\n");
97         info.append("# directories must be included in <dir></dir> tags and can be ended" +
98                     " using a single\n# <enddir> tag");
99         info.append("#\n");
100         info.append("#\n");
101         info.append("\n<dir>JFtp</dir>\n");
102         info.append("FTP#upload.sourceforge.net#anonymous#j-ftp@sf.net#21#/incoming#false\n");
103         info.append("<enddir>\n");
104         info.append("\n");
105         info.append("FTP#ftp.kernel.org#anonymous#j-ftp@sf.net#21#/pub/linux/kernel/v2.6#false\n");
106         info.append("\n");
107         info.append("SMB#(LAN)#guest#guest#-1#-#false\n\n");
108     }
109
110     private void load(String JavaDoc file)
111     {
112         String JavaDoc data = "";
113         String JavaDoc now = "";
114
115         try
116         {
117             DataInput in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
118
119             while((data = in.readLine()) != null)
120             {
121                 now = now + data + "\n";
122             }
123         }
124         catch(IOException e)
125         {
126             Log.debug("No bookmarks.txt found, using defaults.");
127
128             setDefaultText();
129
130             return;
131         }
132
133         info.setText(now);
134     }
135
136     private void save(String JavaDoc file)
137     {
138         try
139         {
140             PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
141
142             out.println(info.getText());
143             out.flush();
144             out.close();
145         }
146         catch(IOException e)
147         {
148             Log.debug(e + " @BookmarkManager.save()");
149         }
150     }
151
152     public Insets getInsets()
153     {
154         Insets std = super.getInsets();
155
156         return new Insets(std.top + 5, std.left + 5, std.bottom + 5,
157                           std.right + 5);
158     }
159 }
160
Popular Tags