加强版的带进度条的传文件和加表情的聊天室 -3

  1package networking;
  2
  3import java.io.*;
  4
  5/**
  6 * @author 
  7 * @version 1.0
  8 */

  9public class FileFrame implements Serializable
 10{
 11    /**
 12     * Attributes
 13     */

 14    private static final long serialVersionUID = 1L;
 15    private double arrayLength;
 16    private double validBytes;
 17    private double sectionNumber;
 18    private byte[] bytes;
 19    private long senderKey;
 20    private long receiverKey;
 21    private String sender;
 22    private String receiver;
 23    
 24    /**
 25     * Constructor
 26     */

 27    public FileFrame(double arrayLength, double validBytes,
 28            double sectionNumber, byte[] bytes, long senderKey,
 29            long receiverKey, String sender, String receiver)
 30    {
 31        super();
 32        this.arrayLength = arrayLength;
 33        this.validBytes = validBytes;
 34        this.sectionNumber = sectionNumber;
 35        this.bytes = bytes;
 36        this.senderKey = senderKey;
 37        this.receiverKey = receiverKey;
 38        this.sender = sender;
 39        this.receiver = receiver;
 40    }

 41    
 42    /**
 43     * Class attribute getter
 44     * 
 45     * @return the arrayLength
 46     */

 47    public double getArrayLength()
 48    {
 49        return this.arrayLength;
 50    }

 51    
 52    /**
 53     * Class attribute setter
 54     * 
 55     * @param arrayLength the arrayLength to set
 56     */

 57    public void setArrayLength(double arrayLength)
 58    {
 59        this.arrayLength = arrayLength;
 60    }

 61    
 62    /**
 63     * Class attribute getter
 64     * 
 65     * @return the bytes
 66     */

 67    public byte[] getBytes()
 68    {
 69        return this.bytes;
 70    }

 71    
 72    /**
 73     * Class attribute setter
 74     * 
 75     * @param bytes the bytes to set
 76     */

 77    public void setBytes(byte[] bytes)
 78    {
 79        this.bytes = bytes;
 80    }

 81    
 82    /**
 83     * Class attribute getter
 84     * 
 85     * @return the receiver
 86     */

 87    public String getReceiver()
 88    {
 89        return this.receiver;
 90    }

 91    
 92    /**
 93     * Class attribute setter
 94     * 
 95     * @param receiver the receiver to set
 96     */

 97    public void setReceiver(String receiver)
 98    {
 99        this.receiver = receiver;
100    }

101    
102    /**
103     * Class attribute getter
104     * 
105     * @return the receiverKey
106     */

107    public long getReceiverKey()
108    {
109        return this.receiverKey;
110    }

111    
112    /**
113     * Class attribute setter
114     * 
115     * @param receiverKey the receiverKey to set
116     */

117    public void setReceiverKey(long receiverKey)
118    {
119        this.receiverKey = receiverKey;
120    }

121    
122    /**
123     * Class attribute getter
124     * 
125     * @return the sectionNumber
126     */

127    public double getSectionNumber()
128    {
129        return this.sectionNumber;
130    }

131    
132    /**
133     * Class attribute setter
134     * 
135     * @param sectionNumber the sectionNumber to set
136     */

137    public void setSectionNumber(double sectionNumber)
138    {
139        this.sectionNumber = sectionNumber;
140    }

141    
142    /**
143     * Class attribute getter
144     * 
145     * @return the sender
146     */

147    public String getSender()
148    {
149        return this.sender;
150    }

151    
152    /**
153     * Class attribute setter
154     * 
155     * @param sender the sender to set
156     */

157    public void setSender(String sender)
158    {
159        this.sender = sender;
160    }

161    
162    /**
163     * Class attribute getter
164     * 
165     * @return the senderKey
166     */

167    public long getSenderKey()
168    {
169        return this.senderKey;
170    }

171    
172    /**
173     * Class attribute setter
174     * 
175     * @param senderKey the senderKey to set
176     */

177    public void setSenderKey(long senderKey)
178    {
179        this.senderKey = senderKey;
180    }

181    
182    /**
183     * Class attribute getter
184     * 
185     * @return the validBytes
186     */

187    public double getValidBytes()
188    {
189        return this.validBytes;
190    }

191    
192    /**
193     * Class attribute setter
194     * 
195     * @param validBytes the validBytes to set
196     */

197    public void setValidBytes(double validBytes)
198    {
199        this.validBytes = validBytes;
200    }

201}

202
  1package networking;
  2
  3import java.awt.event.*;
  4import java.io.*;
  5import java.util.*;
  6import javax.swing.*;
  7
  8/**
  9 * Class that will deal with the file receive
 10 * @author 
 11 * @version 1.0
 12 */

 13public class FileReceiver
 14{
 15    /**
 16     * Attributes
 17     */

 18    private ClientGUI clientGUI;
 19    private Client client;
 20    private FileOutputStream fos;
 21    private RequestFrame rf;
 22    private Map<Long, FileReceiver> receivingFiles;
 23    private boolean okToReceive;
 24    private JoeyLabel joeyLabel;
 25    private JoeyButton joeyAcceptButton;
 26    private JoeyButton joeyCancelButton;
 27    private long fileSize;
 28    private double totalSections;
 29    private double packageSize;
 30    private long key;
 31    private long senderKey;
 32    private String sender;
 33    private String receiver;
 34    private String fileName;
 35    
 36    /**
 37     * Constructor
 38     */

 39    public FileReceiver(ClientGUI clientGUI, Client client, RequestFrame rf,
 40            Map<Long, FileReceiver> receivingFiles, JoeyLabel joeyLabel,
 41            JoeyButton joeyAcceptButton, JoeyButton joeyCancelButton,
 42            long fileSize, double totalSections, double packageSize,
 43            long senderKey, String sender, String receiver,String fileName)
 44    {
 45        this.clientGUI = clientGUI;
 46        this.client = client;
 47        this.fos = null;
 48        this.rf = rf;
 49        this.receivingFiles = receivingFiles;
 50        this.okToReceive = false;
 51        this.joeyLabel = joeyLabel;
 52        this.joeyAcceptButton = joeyAcceptButton;
 53        this.joeyCancelButton = joeyCancelButton;
 54        this.joeyAcceptButton.addActionListener(new AcceptButtonAdapter());
 55        this.joeyCancelButton.addActionListener(new CancelButtonAdapter());
 56        this.fileSize = fileSize;
 57        this.totalSections = totalSections;
 58        this.packageSize = packageSize;
 59        this.senderKey = senderKey;
 60        this.sender = sender;
 61        this.receiver = receiver;
 62        this.fileName=fileName;
 63    }

 64    /**
 65     * private class that will deal with accept button click
 66     * @author 
 67     * @version 1.0
 68     */

 69    private class AcceptButtonAdapter implements ActionListener
 70    {
 71        public void actionPerformed(ActionEvent event)
 72        {
 73            try
 74            {
 75                JFileChooser jfc = new JFileChooser();
 76                jfc.setSelectedFile(new File(FileReceiver.this.fileName));
 77                int result = jfc.showOpenDialog(FileReceiver.this.clientGUI);
 78                switch(result)
 79                {
 80                    case JFileChooser.APPROVE_OPTION:
 81                        File file = jfc.getSelectedFile();
 82                        FileReceiver.this.fos = new FileOutputStream(file);
 83                        AcknowledgementFrame af = new AcknowledgementFrame(
 84                                FileReceiver.this.rf.getSender(),
 85                                FileReceiver.this.rf.getReceiver(),
 86                                FileReceiver.this.rf.getSenderKey(),
 87                                FileReceiver.this.key);
 88                        client.send("SendFileAcknowledgement", af);
 89                        FileReceiver.this.joeyAcceptButton.setVisible(false);
 90                        //FileReceiver.this.joeyCancelButton.setVisible(false);
 91                        break;
 92                    case JFileChooser.CANCEL_OPTION:
 93                        break;
 94                    case JFileChooser.ERROR_OPTION:
 95                        break;
 96                }

 97            }

 98            catch(Exception e)
 99            {
100            }

101        }

102    }

103    /**
104     * private class that will deal with the cancel button click
105     * @author 
106     * @version 1.0
107     */

108    private class CancelButtonAdapter implements ActionListener
109    {
110        public void actionPerformed(ActionEvent event)
111        {
112            try
113            {
114                if(fos != null)
115                {
116                    fos.close();
117                }

118                FileReceiver.this.receivingFiles.remove(FileReceiver.this.key);
119                FileReceiver.this.joeyLabel.setProcessing(false);
120                FileReceiver.this.joeyLabel.setText("I Canceled");
121                FileReceiver.this.joeyLabel.repaint();
122                FileReceiver.this.joeyAcceptButton.setVisible(false);
123                FileReceiver.this.joeyCancelButton.setVisible(false);
124                CancelFrame cf = new CancelFrame(FileReceiver.this.sender,
125                        FileReceiver.this.receiver,
126                        FileReceiver.this.senderKey, FileReceiver.this.key);
127                client.send("CancelFileSendFromReceiver", cf);
128            }

129            catch(Exception e)
130            {
131            }

132        }

133    }

134    
135    /**
136     * Method that will stop receive the file
137     */

138    public void stop()
139    {
140        try
141        {
142            this.receivingFiles.remove(this.key);
143            this.joeyLabel.setProcessing(false);
144            this.joeyLabel.setText("Sender Canceled");
145            this.joeyLabel.repaint();
146            this.joeyAcceptButton.setVisible(false);
147            this.joeyCancelButton.setVisible(false);
148            if(fos != null)
149            {
150                fos.close();
151            }

152        }

153        catch(Exception e)
154        {
155        }

156    }

157    
158    /**
159     * Method that will receive the fill frame
160     * @param ff
161     */

162    public void receive(FileFrame ff)
163    {
164        try
165        {
166            if(ff.getSectionNumber() < this.totalSections)
167            {
168                fos.write(ff.getBytes(), 0, (int)ff.getValidBytes());
169                this.joeyLabel.setProcessing(true);
170                this.joeyLabel.setRate(ff.getSectionNumber()
171                        / this.totalSections);
172                this.joeyLabel.repaint();
173            }

174            else
175            {
176                fos.write(ff.getBytes(), 0, (int)ff.getValidBytes());
177                fos.close();
178                this.receivingFiles.remove(this.key);
179                this.joeyLabel.setProcessing(false);
180                this.joeyLabel.setText("finished");
181                this.joeyLabel.repaint();
182                this.joeyAcceptButton.setVisible(false);
183                this.joeyCancelButton.setVisible(false);
184            }

185        }

186        catch(IOException e)
187        {
188        }

189    }

190    
191    /**
192     * Method that will register the file receiver to the map
193     */

194    public void register()
195    {
196        key = (new Date()).getTime();
197        receivingFiles.put(key, this);
198    }

199    
200    /**
201     * Class attribute getter
202     * 
203     * @return the client
204     */

205    public Client getClient()
206    {
207        return this.client;
208    }

209    
210    /**
211     * Class attribute setter
212     * 
213     * @param client the client to set
214     */

215    public void setClient(Client client)
216    {
217        this.client = client;
218    }

219    
220    /**
221     * Class attribute getter
222     * 
223     * @return the fos
224     */

225    public FileOutputStream getFos()
226    {
227        return this.fos;
228    }

229    
230    /**
231     * Class attribute setter
232     * 
233     * @param fos the fos to set
234     */

235    public void setFos(FileOutputStream fos)
236    {
237        this.fos = fos;
238    }

239    
240    /**
241     * Class attribute getter
242     * 
243     * @return the joeyAcceptButton
244     */

245    public JoeyButton getJoeyAcceptButton()
246    {
247        return this.joeyAcceptButton;
248    }

249    
250    /**
251     * Class attribute setter
252     * 
253     * @param joeyAcceptButton the joeyAcceptButton to set
254     */

255    public void setJoeyAcceptButton(JoeyButton joeyAcceptButton)
256    {
257        this.joeyAcceptButton = joeyAcceptButton;
258    }

259    
260    /**
261     * Class attribute getter
262     * 
263     * @return the joeyCancelButton
264     */

265    public JoeyButton getJoeyCancelButton()
266    {
267        return this.joeyCancelButton;
268    }

269    
270    /**
271     * Class attribute setter
272     * 
273     * @param joeyCancelButton the joeyCancelButton to set
274     */

275    public void setJoeyCancelButton(JoeyButton joeyCancelButton)
276    {
277        this.joeyCancelButton = joeyCancelButton;
278    }

279    
280    /**
281     * Class attribute getter
282     * 
283     * @return the joeyLabel
284     */

285    public JoeyLabel getJoeyLabel()
286    {
287        return this.joeyLabel;
288    }

289    
290    /**
291     * Class attribute setter
292     * 
293     * @param joeyLabel the joeyLabel to set
294     */

295    public void setJoeyLabel(JoeyLabel joeyLabel)
296    {
297        this.joeyLabel = joeyLabel;
298    }

299    
300    /**
301     * Class attribute getter
302     * 
303     * @return the okToReceive
304     */

305    public boolean isOkToReceive()
306    {
307        return this.okToReceive;
308    }

309    
310    /**
311     * Class attribute setter
312     * 
313     * @param okToReceive the okToReceive to set
314     */

315    public void setOkToReceive(boolean okToReceive)
316    {
317        this.okToReceive = okToReceive;
318    }

319    
320    /**
321     * Class attribute getter
322     * 
323     * @return the receivingFiles
324     */

325    public Map<Long, FileReceiver> getReceivingFiles()
326    {
327        return this.receivingFiles;
328    }

329    
330    /**
331     * Class attribute setter
332     * 
333     * @param receivingFiles the receivingFiles to set
334     */

335    public void setReceivingFiles(Map<Long, FileReceiver> receivingFiles)
336    {
337        this.receivingFiles = receivingFiles;
338    }

339    
340    /**
341     * Class attribute getter
342     * 
343     * @return the rf
344     */

345    public RequestFrame getRf()
346    {
347        return this.rf;
348    }

349    
350    /**
351     * Class attribute setter
352     * 
353     * @param rf the rf to set
354     */

355    public void setRf(RequestFrame rf)
356    {
357        this.rf = rf;
358    }

359    
360    /**
361     * Class attribute getter
362     * 
363     * @return the clientGUI
364     */

365    public ClientGUI getClientGUI()
366    {
367        return this.clientGUI;
368    }

369    
370    /**
371     * Class attribute setter
372     * 
373     * @param clientGUI the clientGUI to set
374     */

375    public void setClientGUI(ClientGUI clientGUI)
376    {
377        this.clientGUI = clientGUI;
378    }

379    
380    /**
381     * Class attribute getter
382     * 
383     * @return the key
384     */

385    public long getKey()
386    {
387        return this.key;
388    }

389    
390    /**
391     * Class attribute setter
392     * 
393     * @param key the key to set
394     */

395    public void setKey(long key)
396    {
397        this.key = key;
398    }

399    
400    /**
401     * Class attribute getter
402     * 
403     * @return the fileSize
404     */

405    public long getFileSize()
406    {
407        return this.fileSize;
408    }

409    
410    /**
411     * Class attribute setter
412     * 
413     * @param fileSize the fileSize to set
414     */

415    public void setFileSize(long fileSize)
416    {
417        this.fileSize = fileSize;
418    }

419    
420    /**
421     * Class attribute getter
422     * 
423     * @return the packageSize
424     */

425    public double getPackageSize()
426    {
427        return this.packageSize;
428    }

429    
430    /**
431     * Class attribute setter
432     * 
433     * @param packageSize the packageSize to set
434     */

435    public void setPackageSize(double packageSize)
436    {
437        this.packageSize = packageSize;
438    }

439    
440    /**
441     * Class attribute getter
442     * 
443     * @return the totalSections
444     */

445    public double getTotalSections()
446    {
447        return this.totalSections;
448    }

449    
450    /**
451     * Class attribute setter
452     * 
453     * @param totalSections the totalSections to set
454     */

455    public void setTotalSections(double totalSections)
456    {
457        this.totalSections = totalSections;
458    }

459    
460    /**
461     * Class attribute getter
462     * 
463     * @return the senderKey
464     */

465    public long getSenderKey()
466    {
467        return this.senderKey;
468    }

469    
470    /**
471     * Class attribute setter
472     * 
473     * @param senderKey the senderKey to set
474     */

475    public void setSenderKey(long senderKey)
476    {
477        this.senderKey = senderKey;
478    }

479    
480    /**
481     * Class attribute getter
482     * 
483     * @return the receiver
484     */

485    public String getReceiver()
486    {
487        return this.receiver;
488    }

489    
490    /**
491     * Class attribute setter
492     * 
493     * @param receiver the receiver to set
494     */

495    public void setReceiver(String receiver)
496    {
497        this.receiver = receiver;
498    }

499    
500    /**
501     * Class attribute getter
502     * 
503     * @return the sender
504     */

505    public String getSender()
506    {
507        return this.sender;
508    }

509    
510    /**
511     * Class attribute setter
512     * 
513     * @param sender the sender to set
514     */

515    public void setSender(String sender)
516    {
517        this.sender = sender;
518    }

519}

520
  1package networking;
  2
  3import java.awt.event.ActionEvent;
  4import java.awt.event.ActionListener;
  5import java.io.*;
  6import java.util.*;
  7
  8
 

/**
  9 * Class that will deal with the file send
 10 * @author 
 11 
 * @version 1.0
 12*/ 13publicclass FileSender
 14{
    
     

                                                                     
    
     

                
                                                                                                                                                            
                

    
     

                                      
             
                        


    
    
     

                 
    
    
     

                 
                                 

                

    
    
     

                                                       
                                                                                        
                                                                                                                                         

                                   
                    
                                                                                                                                                                                                                                  
                                                                                                                         


    
    
     

                 
    
    
     

                 
                                                 
                

    
    
     

         
    
    
     

         
    
    
     

                                              
                 

    
    
     

                  
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                  
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                  
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                  
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                 
    
    
     

                  
    
    
     

                 
    
    
     

                 
    
    
     

                 
 15/** 16     * Attributes
 17*/ 18private Client client;
 19private FileInputStream fis;
 20privatestaticfinalint PACKAGE_SIZE =1024*10;
 21private File file;
 22privatelong fileSize;
 23privatedouble totalSections;
 24private Map<Long, FileSender> sendingFiles;
 25privateboolean okToSend;
 26private String sender;
 27private String receiver;
 28privatelong key;
 29private JoeyLabel joeyLabel;
 30private JoeyButton joeyButton;
 31privatelong receiverKey;
 32 33/** 34     * Constructor
 35*/ 36public FileSender(Client client, File file,
 37            Map<Long, FileSender> sendingFiles, String sender, String receiver,
 38            JoeyLabel joeyLabel, JoeyButton joeyButton)
 39{
 40try 41{
 42this.client = client;
 43this.file = file;
 44this.fileSize = file.length();
 45this.fis =new FileInputStream(file);
 46this.totalSections =this.fileSize / PACKAGE_SIZE +1;
 47this.sendingFiles = sendingFiles;
 48this.okToSend =false;
 49this.sender = sender;
 50this.receiver = receiver;
 51this.joeyLabel = joeyLabel;
 52this.joeyButton = joeyButton;
 53this.joeyButton.addActionListener(new CancelButtonAdapter());
 54        } 55catch(Exception e)
 56{
 57        } 58    } 59/** 60     * private class that will deal with the cancel button click
 61     * @author 
 62     * @version 1.0
 63*/ 64privateclass CancelButtonAdapter implements ActionListener
 65{
 66publicvoid actionPerformed(ActionEvent event)
 67{
 68try 69{
 70                FileSender.this.setOkToSend(false);
 71                fis.close();
 72                FileSender.this.joeyLabel.setProcessing(false);
 73                FileSender.this.joeyLabel.setText("I canceled");
 74                FileSender.this.joeyLabel.repaint();
 75                CancelFrame cf =new CancelFrame(FileSender.this.sender,
 76                        FileSender.this.receiver, FileSender.this.key,
 77                        FileSender.this.receiverKey);
 78                client.send("CancelFileSendFromSender", cf);
 79                FileSender.this.sendingFiles.remove(FileSender.this.key);
 80                FileSender.this.joeyButton.setVisible(false);
 81            } 82catch(Exception e)
 83{
 84            } 85        } 86    } 87 88/** 89     * Class attribute getter
 90     * 
 91     * @return the joeyButton
 92*/ 93public JoeyButton getJoeyButton()
 94{
 95returnthis.joeyButton;
 96    } 97 98/** 99     * Method that will stop the send progress
100*/101publicvoid stop()
102{
103try104{
105            FileSender.this.setOkToSend(false);
106if(fis !=null)
107{
108                fis.close();
109            }110            FileSender.this.joeyLabel.setProcessing(false);
111            FileSender.this.joeyLabel.setText("Receiver canceled");
112            FileSender.this.joeyLabel.repaint();
113            FileSender.this.sendingFiles.remove(FileSender.this.key);
114            FileSender.this.joeyButton.setVisible(false);
115        }116catch(IOException e)
117{
118        }119    }120121/**122     * Method that will send the file
123*/124publicvoid send()
125{
126double currentSection =1;
127this.joeyLabel.setProcessing(true);
128while(this.okToSend && currentSection <this.totalSections)
129{
130try131{
132byte[] bytes =newbyte[PACKAGE_SIZE];
133                read(0, PACKAGE_SIZE, bytes);
134                FileFrame ff =new FileFrame(PACKAGE_SIZE, PACKAGE_SIZE,
135                        currentSection, bytes, this.key, receiverKey,
136this.sender, this.receiver);
137                client.send("SendFileSection", ff);
138this.joeyLabel.setRate(currentSection /this.totalSections);
139this.joeyLabel.repaint();
140                currentSection++;
141            }142catch(Exception e)
143{
144this.okToSend =false;
145this.joeyLabel.setProcessing(false);
146this.joeyLabel.setText("sending error,canceled");
147this.joeyLabel.repaint();
148this.sendingFiles.remove(this.key);
149this.joeyButton.setVisible(false);
150return;
151            }152        }153if(this.okToSend
154&&this.fileSize - (this.totalSections -1* PACKAGE_SIZE >0)// last
155// section156{
157try158{
159byte[] bytes =newbyte[PACKAGE_SIZE];
160                read(0, (int)(this.fileSize - (this.totalSections -1)
161* PACKAGE_SIZE), bytes);
162                FileFrame ff =new FileFrame(PACKAGE_SIZE,
163                        (int)(this.fileSize - (this.totalSections -1)
164* PACKAGE_SIZE), currentSection, bytes,
165this.key, receiverKey, this.sender, this.receiver);
166                client.send("SendFileSection", ff);
167this.okToSend =false;
168this.joeyButton.setVisible(false);
169this.joeyLabel.setProcessing(false);
170this.joeyLabel.setText("finished");
171this.joeyLabel.repaint();
172this.fis.close();
173this.sendingFiles.remove(this.key);
174            }175catch(Exception e)
176{
177this.okToSend =false;
178this.joeyLabel.setProcessing(false);
179this.joeyLabel.setText("sending error,canceled");
180this.joeyLabel.repaint();
181this.sendingFiles.remove(this.key);
182this.joeyButton.setVisible(false);
183            }184        }185    }186187/**188     * Class attribute getter
189     * 
190     * @return the joeyLabel
191*/192public JoeyLabel getJoeyLabel()
193{
194returnthis.joeyLabel;
195    }196197/**198     * Method that will send the request
199*/200publicvoid request()
201{
202try203{
204            RequestFrame rf =new RequestFrame(this.sender, this.receiver,
205this.key, this.fileSize, this.totalSections, PACKAGE_SIZE,
206this.file.getName());
207            client.send("SendFileRequest", rf);
208        }209catch(Exception e)
210{
211        }212    }213214/**215     * Method that will register the sender class to the map
216*/217publicvoid register()
218{
219        key = (new Date()).getTime();
220        sendingFiles.put(key, this);
221    }222223/**224     * Method that will remove this file sender
225     * @param key
226*/227publicvoid secede(long key)
228{
229        sendingFiles.remove(key);
230    }231232/**233     * Method that will read the file
234     * @param offset
235     * @param length
236     * @param bytes
237     * @throws Exception
238*/239publicvoid read(int offset, int length, byte[] bytes) throws Exception
240{
241int actualRead = fis.read(bytes, offset, length);
242if(actualRead <=0)
243{
244return;
245        }246elseif(actualRead < length - offset)
247{
248            read(offset + actualRead, length - actualRead, bytes);
249        }250    }251252/**253     * Class attribute getter
254     * 
255     * @return the okToSend
256*/257publicboolean isOkToSend()
258{
259returnthis.okToSend;
260    }261262/**263     * Class attribute setter
264     * 
265     * @param okToSend the okToSend to set
266*/267publicvoid setOkToSend(boolean okToSend)
268{
269this.okToSend = okToSend;
270    }271272/**273     * Class attribute getter
274     * 
275     * @return the client
276*/277public Client getClient()
278{
279returnthis.client;
280    }281282/**283     * Class attribute setter
284     * 
285     * @param client the client to set
286*/287publicvoid setClient(Client client)
288{
289this.client = client;
290    }291292/**293     * Class attribute getter
294     * 
295     * @return the file
296*/297public File getFile()
298{
299returnthis.file;
300    }301302/**303     * Class attribute setter
304     * 
305     * @param file the file to set
306*/307publicvoid setFile(File file)
308{
309this.file = file;
310    }311312/**313     * Class attribute getter
314     * 
315     * @return the fileSize
316*/317publiclong getFileSize()
318{
319returnthis.fileSize;
320    }321322/**323     * Class attribute setter
324     * 
325     * @param fileSize the fileSize to set
326*/327publicvoid setFileSize(long fileSize)
328{
329this.fileSize = fileSize;
330    }331332/**333     * Class attribute getter
334     * 
335     * @return the fis
336*/337public FileInputStream getFis()
338{
339returnthis.fis;
340    }341342/**343     * Class attribute setter
344     * 
345     * @param fis the fis to set
346*/347publicvoid setFis(FileInputStream fis)
348{
349this.fis = fis;
350    }351352/**353     * Class attribute getter
354     * 
355     * @return the key
356*/357publiclong getKey()
358{
359returnthis.key;
360    }361362/**363     * Class attribute setter
364     * 
365     * @param key the key to set
366*/367publicvoid setKey(long key)
368{
369this.key = key;
370    }371372/**373     * Class attribute getter
374     * 
375     * @return the receiver
376*/377public String getReceiver()
378{
379returnthis.receiver;
380    }381382/**383     * Class attribute setter
384     * 
385     * @param receiver the receiver to set
386*/387publicvoid setReceiver(String receiver)
388{
389this.receiver = receiver;
390    }391392/**393     * Class attribute getter
394     * 
395     * @return the receiverKey
396*/397publiclong getReceiverKey()
398{
399returnthis.receiverKey;
400    }401402/**403     * Class attribute setter
404     * 
405     * @param receiverKey the receiverKey to set
406*/407publicvoid setReceiverKey(long receiverKey)
408{
409this.receiverKey = receiverKey;
410    }411412/**413     * Class attribute getter
414     * 
415     * @return the sender
416*/417public String getSender()
418{
419returnthis.sender;
420    }421422/**423     * Class attribute setter
424     * 
425     * @param sender the sender to set
426*/427publicvoid setSender(String sender)
428{
429this.sender = sender;
430    }431432/**433     * Class attribute getter
434     * 
435     * @return the sendingFiles
436*/437public Map<Long, FileSender> getSendingFiles()
438{
439returnthis.sendingFiles;
440    }441442/**443     * Class attribute setter
444     * 
445     * @param sendingFiles the sendingFiles to set
446*/447publicvoid setSendingFiles(Map<Long, FileSender> sendingFiles)
448{
449this.sendingFiles = sendingFiles;
450    }451452/**453     * Class attribute getter
454     * 
455     * @return the totalSections
456*/457publicdouble getTotalSections()
458{
459returnthis.totalSections;
460    }461462/**463     * Class attribute setter
464     * 
465     * @param totalSections the totalSections to set
466*/467publicvoid setTotalSections(double totalSections)
468{
469this.totalSections = totalSections;
470    }471472/**473     * Class attribute setter
474     * 
475     * @param joeyButton the joeyButton to set
476*/477publicvoid setJoeyButton(JoeyButton joeyButton)
478{
479this.joeyButton = joeyButton;
480    }481482/**483     * Class attribute setter
484     * 
485     * @param joeyLabel the joeyLabel to set
486*/487publicvoid setJoeyLabel(JoeyLabel joeyLabel)
488{
489this.joeyLabel = joeyLabel;
490    }491}492

 1package networking;
 2
 3/**
 4 * Class that will deal with the file send progress
 5 * @author 
 6 * @version 1.0
 7 */

 8public class FileSendThreadHandler implements Runnable
 9{
10    /**
11     * Attributes
12     */

13    private FileSender fs;
14    private long receiverKey;
15    /* (non-Javadoc)
16     * @see java.lang.Runnable#run()
17     */

18    public void run()
19    {
20        fs.setReceiverKey(receiverKey);
21        fs.send();
22    }

23    /**
24     * Constructor
25     */

26    public FileSendThreadHandler(FileSender fs,long receiverKey)
27    {
28        this.receiverKey=receiverKey;
29        this.fs=fs;
30    }

31}

32
 1package networking;
 2
 3import javax.swing.*;
 4
 5/**
 6 * Joey's button class
 7 * @author 
 8 * @version 1.0
 9 */

10public class JoeyButton extends JButton
11{
12    /**
13     * Attributes
14     */

15    private static final long serialVersionUID = 1L;
16    
17    /**
18     * Constructor
19     */

20    public JoeyButton()
21    {
22        super();
23    }

24}

25
  1package networking;
  2
  3import java.awt.*;
  4import javax.swing.*;
  5
  6
 

/**
  7 * Joey's label class
  8 * @author 
  9 
 * @version 1.0
 10*/ 11publicclass JoeyLabel extends JLabel
 12{
    
     

                      
    
     

                          
    
    
     

                  
    
    
     

                 
    
         
                                     
        
                                               
                                                    
            
             


    
    
     

                  
    
    
     

                 
 13/** 14     * Attributes
 15*/ 16privatestaticfinallong serialVersionUID =1L;
 17privateboolean isProcessing;
 18privatedouble rate;
 19 20/** 21     * Constructor
 22*/ 23public JoeyLabel()
 24{
 25super();
 26        isProcessing =false;
 27this.rate =0;
 28    } 29 30/** 31     * Getter
 32     * @return boolean
 33*/ 34publicboolean isProcessing()
 35{
 36returnthis.isProcessing;
 37    } 38 39/** 40     * Getter
 41     * @param isProcessing
 42*/ 43publicvoid setProcessing(boolean isProcessing)
 44{
 45this.isProcessing = isProcessing;
 46    } 47 48/* (non-Javadoc)
 49     * @see javax.swing.JComponent#paint(java.awt.Graphics)
 50*/ 51publicvoid paint(Graphics g)
 52{
 53if(!this.isProcessing)
 54{
 55super.paint(g);
 56        } 57else 58{
 59int leftArea = (int)(this.getWidth() *this.rate);
 60if(leftArea ==0)
 61{
 62                Image tempImage =this.createImage(this.getWidth(), 20);
 63                Graphics tempGraphics = tempImage.getGraphics();
 64                tempGraphics.setColor(Color.RED);
 65                tempGraphics.fillRect(00this.getWidth(), 20);
 66                g.drawImage(tempImage, 00this);
 67            } 68elseif(leftArea <this.getWidth())
 69{
 70                Image leftImage =this.createImage(leftArea, 20);
 71                Graphics leftGraphics = leftImage.getGraphics();
 72                leftGraphics.setColor(Color.BLUE);
 73                leftGraphics.fillRect(00, leftArea, 20);
 74                g.drawImage(leftImage, 00this);
 75                Image rightImage =this.createImage(this.getWidth() - leftArea,
 7620);
 77                Graphics rightGraphics = rightImage.getGraphics();
 78                rightGraphics.setColor(Color.RED);
 79                rightGraphics.fillRect(00this.getWidth() - leftArea, 20);
 80                g.drawImage(rightImage, leftArea, 0this);
 81            } 82else 83{
 84                Image tempImage =this.createImage(this.getWidth(), 20);
 85                Graphics tempGraphics = tempImage.getGraphics();
 86                tempGraphics.setColor(Color.BLUE);
 87                tempGraphics.fillRect(00this.getWidth(), 20);
 88                g.drawImage(tempImage, 00this);
 89            } 90        } 91    } 92 93/** 94     * Getter
 95     * @return double
 96*/ 97publicdouble getRate()
 98{
 99returnthis.rate;
100    }101102/**103     * Setter
104     * @param rate
105*/106publicvoid setRate(double rate)
107{
108this.rate = rate;
109    }110}111

posted @ 2007-08-10 10:29  N/A2011  阅读(194)  评论(0编辑  收藏  举报