KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sendfile > SendFile


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

19 package org.lucane.applications.sendfile;
20
21 import org.lucane.client.*;
22 import org.lucane.client.widgets.*;
23 import org.lucane.common.*;
24 import org.lucane.common.net.ObjectConnection;
25
26 import java.awt.*;
27 import java.io.*;
28
29 import javax.swing.*;
30
31
32 public class SendFile
33 extends Plugin
34 {
35     private ObjectConnection connection;
36     private ConnectInfo friend;
37     private String JavaDoc fileName;
38     
39     private SendFileDialog dialog;
40     
41     public SendFile()
42     {
43     }
44     
45     //-- plugin interface
46

47     public Plugin newInstance(ConnectInfo[] friends)
48     {
49         if(friends.length > 0)
50             return new SendFile(friends[0]);
51         else
52             return new SendFile(null);
53     }
54     
55     public SendFile(ConnectInfo friend)
56     {
57         this.friend = friend;
58     }
59     
60     public void load(ObjectConnection friend, ConnectInfo who, String JavaDoc filename)
61     {
62         this.connection = friend;
63         this.friend = who;
64         this.fileName = filename;
65     }
66     
67     public void start()
68     {
69         
70         // selection check
71
if(this.friend == null)
72         {
73             DialogBox.info(tr("noselect"));
74             exit();
75             return;
76         }
77         
78         dialog = new SendFileDialog(this, getTitle() + "> " + friend.getName(), true);
79         dialog.setExitPluginOnClose(true);
80         selectFile(dialog);
81         dialog.show();
82     }
83     
84     public void follow()
85     {
86         dialog = new SendFileDialog(this, getTitle() + " < " + friend.getName(), false);
87         dialog.setPreferredSize(new Dimension(400, 300));
88         dialog.setFilePath(fileName);
89         
90         try {
91             String JavaDoc comment = connection.readString();
92             dialog.setComment(comment);
93         } catch (Exception JavaDoc e) {
94             dialog.setComment(tr("err.comment"));
95         }
96         
97         dialog.show();
98     }
99     
100     //-- added methods
101

102     public void selectFile(SendFileDialog dialog)
103     {
104         JFileChooser fc = new JFileChooser();
105         int returnVal = fc.showOpenDialog(null);
106         if(returnVal == JFileChooser.APPROVE_OPTION)
107             dialog.setFilePath(fc.getSelectedFile().getPath());
108     }
109     
110     public void askForAccept(String JavaDoc filePath, String JavaDoc comment)
111     {
112         dialog.dispose();
113         
114         filePath = filePath.replace('\\', '/');
115         int index = filePath.lastIndexOf('/');
116         if(index > 0)
117             this.fileName = filePath.substring(index+1);
118         else
119             this.fileName = filePath;
120         
121         try {
122             connection = Communicator.getInstance().sendMessageTo(friend, getName(), fileName);
123             connection.write(comment);
124             
125             Boolean JavaDoc acceptation = Boolean.FALSE;
126             
127             try {
128                 acceptation = (Boolean JavaDoc)connection.read();
129             } catch(Exception JavaDoc e) {
130                 //file refused
131
}
132             
133             if(acceptation.booleanValue())
134                 sendFile(filePath);
135             else
136             {
137                 String JavaDoc reject = tr("msg.rejectFile");
138                 reject = reject.replaceAll("%1", friend.getName());
139                 reject = reject.replaceAll("%2", fileName);
140                 DialogBox.error(reject);
141             }
142         }
143         catch(Exception JavaDoc e)
144         {
145             DialogBox.error(tr("sendError"));
146             e.printStackTrace();
147         }
148         
149         connection.close();
150         exit();
151     }
152     
153     public void saveFile()
154     {
155         JFileChooser fc = new JFileChooser();
156         fc.setSelectedFile(new File(fileName));
157         int returnVal = fc.showSaveDialog(null);
158         if(returnVal == JFileChooser.APPROVE_OPTION)
159         {
160             try {
161                 connection.write(Boolean.TRUE);
162             } catch (IOException e) {
163                 e.printStackTrace();
164             }
165             
166             File file = fc.getSelectedFile();
167             try {
168                 downloadFile(file.getAbsolutePath());
169                 String JavaDoc get = tr("msg.get").replaceAll("%1", fileName);
170                 DialogBox.info(get);
171             } catch (Exception JavaDoc e) {
172                 DialogBox.error(tr("getError"));
173             }
174             exit();
175         }
176         else
177             rejectFile();
178     }
179     
180     public void openFile()
181     {
182         try {
183             String JavaDoc extension = getFileExtension();
184             File file = File.createTempFile(fileName, "." + extension);
185             
186             String JavaDoc command = getCommand();
187             if(command == null)
188                 command = selectCommand();
189             
190             //if command is still null, save the file instead of losing it
191
if(command == null)
192             {
193                 boolean doSave = DialogBox.question(
194                         tr("msg.selectCommandFor").replaceAll("%1", extension),
195                         tr("msg.saveInsteadOfOpen"));
196                 
197                 if(doSave)
198                     saveFile();
199                 else
200                     rejectFile();
201                     
202                 return;
203             }
204             
205             connection.write(Boolean.TRUE);
206             downloadFile(file.getAbsolutePath());
207
208             try {
209                 Runtime.getRuntime().exec(command + " " + file.getAbsolutePath());
210             } catch (IOException ioe) {
211                 String JavaDoc err = tr("err.commandFailed").replaceAll("%1", command);
212                 DialogBox.error(err);
213             }
214         } catch(Exception JavaDoc e) {
215             DialogBox.error(tr("getError"));
216             e.printStackTrace();
217         }
218         exit();
219     }
220     
221     public void rejectFile()
222     {
223         try {
224             connection.write(Boolean.FALSE);
225         } catch (IOException e) {
226             //oops
227
}
228         Logging.getLogger().finer("SendFile::REJECT");
229         connection.close();
230         dialog.dispose();
231         exit();
232     }
233     
234     public void sendFile(String JavaDoc filePath)
235     throws Exception JavaDoc
236     {
237         File file = new File(filePath);
238         connection.write(new Long JavaDoc(file.length()));
239
240         DataInputStream dis = null;
241         try {
242             dis = new DataInputStream(new FileInputStream(filePath));
243             while(dis.available() > 0)
244             {
245                 byte[] buffer = new byte[10240];
246                 int bytes = dis.read(buffer);
247                 if(bytes != buffer.length);
248                 {
249                     byte[] temp = new byte[bytes];
250                     System.arraycopy(buffer, 0, temp, 0, bytes);
251                     buffer = temp;
252                 }
253                 connection.write(buffer);
254             }
255         } finally {
256             if(dis != null)
257                 dis.close();
258             connection.close();
259         }
260         
261         String JavaDoc sent = tr("msg.sent").replaceAll("%1", fileName);
262         DialogBox.info(sent);
263     }
264     
265     public void downloadFile(String JavaDoc destination)
266     throws IOException, ClassNotFoundException JavaDoc
267     {
268         connection.write(Boolean.TRUE);
269         Logging.getLogger().finer("SendFile::ACCEPT");
270         dialog.dispose();
271
272         DataOutputStream dos = new DataOutputStream(new FileOutputStream(destination));
273         long length = ((Long JavaDoc)connection.read()).longValue();
274         long bytes = 0;
275         while(bytes < length)
276         {
277             byte[] buffer = (byte[])connection.read();
278             dos.write(buffer);
279             bytes += buffer.length;
280         }
281
282         connection.close();
283     }
284     
285     public String JavaDoc selectCommand()
286     {
287         String JavaDoc command = null;
288         String JavaDoc oldCommand = getCommand();
289         String JavaDoc extension = getFileExtension();
290         
291         JFileChooser fc = new JFileChooser();
292         fc.setDialogTitle(tr("msg.selectCommandFor").replaceAll("%1", extension));
293         
294         if(oldCommand != null)
295             fc.setSelectedFile(new File(oldCommand));
296         
297         int returnVal = fc.showOpenDialog(null);
298         if(returnVal == JFileChooser.APPROVE_OPTION)
299         {
300             command = fc.getSelectedFile().getAbsolutePath();
301             getLocalConfig().set("cmd." + extension, command);
302         }
303
304         return command;
305     }
306     
307     //-- tools
308

309     public String JavaDoc getFileExtension()
310     {
311         int index = fileName.lastIndexOf('.');
312         if(index < 0)
313             return "";
314         
315         return fileName.substring(index+1).toLowerCase();
316     }
317     
318     public String JavaDoc getCommand()
319     {
320         return getLocalConfig().get("cmd." + getFileExtension());
321     }
322     
323     public String JavaDoc getShortCommand()
324     {
325         String JavaDoc command = getCommand();
326         if(command != null)
327         {
328             command.replace('\\', '/');
329             int index = command.lastIndexOf('/');
330             if(index > 0)
331                 command = command.substring(index+1);
332         }
333         
334         return command;
335     }
336 }
337
Popular Tags