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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520


2

3

4

5

6

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25


2

3

4

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(0, 0, this.getWidth(), 20);
66 g.drawImage(tempImage, 0, 0, this);
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(0, 0, leftArea, 20);
74 g.drawImage(leftImage, 0, 0, this);
75 Image rightImage =this.createImage(this.getWidth() - leftArea,
7620);
77 Graphics rightGraphics = rightImage.getGraphics();
78 rightGraphics.setColor(Color.RED);
79 rightGraphics.fillRect(0, 0, this.getWidth() - leftArea, 20);
80 g.drawImage(rightImage, leftArea, 0, this);
81 } 82else 83{
84 Image tempImage =this.createImage(this.getWidth(), 20);
85 Graphics tempGraphics = tempImage.getGraphics();
86 tempGraphics.setColor(Color.BLUE);
87 tempGraphics.fillRect(0, 0, this.getWidth(), 20);
88 g.drawImage(tempImage, 0, 0, this);
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