-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
481 lines (388 loc) · 18.1 KB
/
Main.java
1
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
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Main extends JFrame {
private JPanel Panel;
private JButton chooseBookButton;
private JButton loadChaptersButton;
private JButton pickChapterButton;
private JButton nextChapterButton;
private JButton previousChapterButton;
private JTextArea textArea;
private JMenuBar menuBar;
private JMenu changeColor;
private JMenuItem BackgroundColorItem;
private JMenuItem TextColorItem;
private JMenu NewMenu;
private JMenuItem newBook;
private JMenu FileOperation;
private JMenuItem LoadChapters;
private JMenuItem PickChapter;
private JMenuItem Exit;
private JMenuItem ShowFullBook;
private JMenuItem LineWidthItem;
private JMenuItem GetNextChapterItem;
private JMenuItem GetPreviousChapterItem;
private File read;
private LinkedHashMap<Integer,String> chapters;
private LinkedHashMap<Integer,Integer>chapterStart;
private int lineWidth=30;
private int chapterNumber;
private Charset charset= StandardCharsets.UTF_8;
Main(){
setContentPane(Panel);
setTitle("BookReader");
setSize(670,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//buttons Action Listeners
chooseBookButton.addActionListener(this::chooseBook);
loadChaptersButton.addActionListener(this::LoadChapters);
pickChapterButton.addActionListener(this::pickChapterAction);
nextChapterButton.addActionListener(this::getNextChapter);
previousChapterButton.addActionListener(this::getPreviousChapter);
//Menu Item Action Listeners
BackgroundColorItem.addActionListener(this::setBackgroundColor);
TextColorItem.addActionListener(this::setTextColor);
newBook.addActionListener(this::chooseBook);
LoadChapters.addActionListener(this::LoadChapters);
PickChapter.addActionListener(this::pickChapterAction);
Exit.addActionListener(this::exitProgram);
ShowFullBook.addActionListener(this::printFullBook);
LineWidthItem.addActionListener(this::changeLineWidth);
GetNextChapterItem.addActionListener(this::getNextChapter);
GetPreviousChapterItem.addActionListener(this::getPreviousChapter);
}
public void chooseBook(ActionEvent e) {
chapterNumber= -100;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter=new FileNameExtensionFilter("Text File(.txt)","txt");
fileChooser.addChoosableFileFilter(filter);
int selection = fileChooser.showOpenDialog(getParent());
if (selection == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try{
File prevFile = read;
read=new File(selectedFile.toURI());
InputStream in = new FileInputStream(read);
BufferedReader reader = new BufferedReader(new InputStreamReader(in,charset));
textArea.setText("FILE LOADED!!!\n");
//to determine if we get the author or book name!
boolean authorOrName=false;
String value;
while ( (value = reader.readLine()) !=null){
value=value.replaceAll("\\t","");
if (!authorOrName){
if(value.trim().isEmpty()){
textArea.setText("Not a text file from chitanka.info");
read=prevFile;
break;
}else{
textArea.append("Author:"+value);
authorOrName=true;
}
}else{
if(value.trim().isEmpty()){
textArea.setText("Not a text file from chitanka.info");
read=prevFile;
}else {
textArea.append("\nBook Name:" + value);
setTitle(value);
}
break;
}
}
reader.close();
}catch (FileNotFoundException f){
JOptionPane.showMessageDialog(null, f.toString() );
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString() );
}
}
}
public void LoadChapters(ActionEvent e) {
if(read!=null) {
try {
InputStream in = new FileInputStream(read);
BufferedReader reader = new BufferedReader(new InputStreamReader(in,charset));
String chapterName="";
String currentLine;
int chapterStartPage=0;
int index=2;
int numberOfChapter=1;
boolean firstChapter=true;
chapters=new LinkedHashMap<>();
chapterStart=new LinkedHashMap<>();
for (int i = 0; i < 2; i++) {
reader.readLine();
reader.readLine();
}
while ((currentLine = reader.readLine() )!= null){
boolean EvenAmountOfEmptySpaces=false;
currentLine=currentLine.replaceAll("\\t","");
index++;
if(currentLine.isEmpty() && (currentLine = reader.readLine() )!= null){
currentLine=currentLine.replaceAll("\\t","");
index++;
while (currentLine.isEmpty() && (currentLine = reader.readLine() )!= null){
currentLine=currentLine.replaceAll("\\t","");
index++;
EvenAmountOfEmptySpaces= !EvenAmountOfEmptySpaces;
}
//empty spaces are even amount
if(EvenAmountOfEmptySpaces && chapterStartPage!=0){
if(index-chapterStartPage>=20 && Character.isUpperCase(chapterName.charAt(0))
&& Character.isUpperCase(currentLine.charAt(0)) || Character.isDigit(currentLine.charAt(0))) {
if(currentLine.length()<=23) {
if (firstChapter) {
chapters.put(0, "Start of the Book");
chapterStart.put(0, 2);
firstChapter = false;
}
chapters.put(numberOfChapter, chapterName);
chapterStart.put(numberOfChapter, chapterStartPage);
numberOfChapter++;
chapterStartPage = index;
chapterName = currentLine;
}else {
index++;
if(reader.readLine().replaceAll("\\t", "").isEmpty() && currentLine.split(" ").length<=5){
if (firstChapter) {
chapters.put(0, "Start of the Book");
chapterStart.put(0, 2);
firstChapter = false;
}
chapters.put(numberOfChapter, chapterName);
chapterStart.put(numberOfChapter, chapterStartPage);
numberOfChapter++;
chapterStartPage = index;
chapterName = currentLine;
}
}
}
}else if(EvenAmountOfEmptySpaces && (Character.isUpperCase(currentLine.charAt(0)) || Character.isDigit(currentLine.charAt(0)))&& currentLine.length()<=23){
chapterStartPage = index;
chapterName = currentLine;
}else if(!EvenAmountOfEmptySpaces && currentLine.length()<=5 && Character.isDigit(currentLine.charAt(0))){
chapters.put(numberOfChapter, chapterName);
chapterStart.put(numberOfChapter, chapterStartPage);
numberOfChapter++;
chapterStartPage = index;
chapterName = currentLine;
}
}
}
chapters.put(numberOfChapter, chapterName);
chapterStart.put(numberOfChapter, chapterStartPage);
textArea.setText("");
for (Map.Entry<Integer,String>entry:chapters.entrySet()) {
textArea.append(entry.getKey()+": "+entry.getValue()+"\n");
}
textArea.setCaretPosition(0);
reader.close();
} catch (FileNotFoundException f) {
JOptionPane.showMessageDialog(null, f.toString() );
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString() );
}
}else textArea.setText("No text file opened!");
}
public void pickChapterAction(ActionEvent e) {
try {
chapterNumber = Integer.parseInt(JOptionPane.showInputDialog("Write the number of the chapter you want to read!"));
getChapter(chapterNumber);
}catch (NumberFormatException n){
}
}
public void getNextChapter(ActionEvent e){
chapterNumber=chapterNumber+1;
getChapter(chapterNumber);
if(chapters!=null && !chapters.containsKey(chapterNumber))
chapterNumber=chapterNumber-1;
}
public void getPreviousChapter(ActionEvent e){
chapterNumber=chapterNumber-1;
getChapter(chapterNumber);
if(chapters!=null && !chapters.containsKey(chapterNumber))
chapterNumber=chapterNumber+1;
}
public void getToChapter(BufferedReader reader, int index) throws IOException {
int currentPos=0;
while (currentPos!=index){
reader.readLine();
currentPos++;
}
}
public void splitLine(String text,int lineWidth){
String [] wordsArray=text.split(" ");
int curr=0;
int total=0;
while(true){
total+=lineWidth;
if(total>=wordsArray.length){
for (int i = curr; i < wordsArray.length; i++) {
textArea.append(wordsArray[i]+" ");
}
textArea.append("\n");
break;
}else{
for (int i = curr; i < total; i++) {
textArea.append(wordsArray[i]+" ");
}
textArea.append("\n");
}
curr+=lineWidth;
}
}
public void setBackgroundColor(ActionEvent e) {
Color color=JColorChooser.showDialog(null, "Choose a color",textArea.getBackground());
textArea.setBackground(Objects.requireNonNullElseGet(color, () -> new Color(5,13,12)));
}
public void setTextColor(ActionEvent e) {
Color color=JColorChooser.showDialog(null, "Choose a color",textArea.getForeground());
textArea.setForeground(Objects.requireNonNullElseGet(color, () -> new Color(235,235,235)));
}
public void exitProgram(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(null, "Exit?");
if(confirm==0)
System.exit(0);
}
public void printFullBook(ActionEvent e) {
if(read!=null) {
int confirm = JOptionPane.showConfirmDialog(null, "Do you really want to do this? It will be SLOW!");
if (confirm == 0){
chapterNumber=99018;
try {
textArea.setText("\n");
String next;
InputStream in = new FileInputStream(read);
BufferedReader reader = new BufferedReader(new InputStreamReader(in,charset));
while ( (next = reader.readLine()) !=null) {
next = next.replaceAll("\\t", "");
if (next.length() > 60) {
splitLine(next,lineWidth);
} else {
textArea.append(next + "\n");
}
}
textArea.setCaretPosition(0);
reader.close();
} catch (FileNotFoundException | NullPointerException n) {
JOptionPane.showMessageDialog(null, n.toString());
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
}
}else textArea.setText("No text file opened!");
}
public void changeLineWidth(ActionEvent e){
int prevWidth=lineWidth;
lineWidth=Integer.parseInt(JOptionPane.showInputDialog("Write the maximum amount of words(width) in a line!"));
if(lineWidth<=0)
lineWidth=prevWidth;
if(read!=null) {
try {
textArea.setText("\n");
InputStream in = new FileInputStream(read);
BufferedReader reader = new BufferedReader(new InputStreamReader(in,charset));
if(chapters!=null && chapters.containsKey(chapterNumber)){
getToChapter(reader,chapterStart.get(chapterNumber));
int startIndex=chapterStart.get(chapterNumber);
if(chapterStart.containsKey(chapterNumber+1)){
String next;
int endIndex=chapterStart.get(chapterNumber+1);
while((next=reader.readLine()) != null && startIndex!=endIndex){
next=next.replaceAll("\\t","");
if(next.length()>60){
splitLine(next,lineWidth);
}else {
textArea.append(next + "\n");
}
startIndex++;
}
}else{
String next;
while((next=reader.readLine()) != null){
next=next.replaceAll("\\t","");
if(next.length()>60){
splitLine(next,lineWidth);
}else {
textArea.append(next+"\n");
}
}
}
textArea.setCaretPosition(0);
}else if(chapterNumber==99018){
textArea.setText("\n");
String next;
while ((next=reader.readLine()) != null) {
next = next.replaceAll("\\t", "");
if (next.length() > 60) {
splitLine(next,lineWidth);
} else {
textArea.append(next + "\n");
}
}
textArea.setCaretPosition(0);
}
reader.close();
}catch (NumberFormatException | FileNotFoundException | NullPointerException n){
JOptionPane.showMessageDialog(null, n.toString() );
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString() );
}
}
}
public void getChapter(int chapter){
if(read!=null) {
try {
InputStream in = new FileInputStream(read);
BufferedReader reader = new BufferedReader(new InputStreamReader(in,charset));
if(chapters.containsKey(chapter)){
textArea.setText("\n");
getToChapter(reader,chapterStart.get(chapter));
int startIndex=chapterStart.get(chapter);
if(chapterStart.containsKey(chapter+1)){
int endIndex=chapterStart.get(chapter+1);
String next;
while((next=reader.readLine() )!=null && startIndex!=endIndex){
next=next.replaceAll("\\t","");
if(next.length()>60){
splitLine(next,lineWidth);
}else {
textArea.append(next + "\n");
}
startIndex++;
}
}else{
String next;
while((next=reader.readLine() )!=null){
next=next.replaceAll("\\t","");
if(next.length()>60){
splitLine(next,lineWidth);
}else {
textArea.append(next+"\n");
}
}
}
textArea.setCaretPosition(0);
reader.close();
}else JOptionPane.showMessageDialog(null,"This chapter does not exist");
}catch (FileNotFoundException | NullPointerException n){
JOptionPane.showMessageDialog(null, n.toString() );
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString() );
}
}else textArea.setText("No text file opened!");
}
public static void main(String[] args) {
new Main();
}
}