Skip to content

Commit 798e224

Browse files
authored
Refactor/export CSV+PDF (#166)
* copyright + remove empty lines * make functions private * use basic constructor * avoid redundant initialization = null * append.append * remove unused FontMapper * use basic constructor * make pdfheight a local variable * make properties private
1 parent 65d93b2 commit 798e224

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/main/java/net/atomique/ksar/export/FileCSV.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 The kSAR Project. All rights reserved.
2+
* Copyright 2018 The kSAR Project. All rights reserved.
33
* See the LICENSE file in the project root for more information.
44
*/
55

@@ -33,19 +33,18 @@ public class FileCSV implements Runnable {
3333
public FileCSV(String filename, kSar hissar) {
3434
csvfilename = filename;
3535
mysar = hissar;
36-
3736
}
3837

3938
public FileCSV(String filename, kSar hissar, JProgressBar g, JDialog d) {
40-
csvfilename = filename;
41-
mysar = hissar;
39+
this(filename, hissar);
40+
4241
progress_bar = g;
4342
dialog = d;
4443
}
4544

4645
public void run() {
4746
// open file
48-
BufferedWriter out = null;
47+
BufferedWriter out;
4948
try {
5049
out = new BufferedWriter(new FileWriter(csvfilename));
5150
} catch (IOException ex) {
@@ -71,7 +70,7 @@ public void run() {
7170

7271
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss");
7372
String text = tmpLDT.format(formatter);
74-
tmpcsv.append(text + ";");
73+
tmpcsv.append(text).append(";");
7574
export_treenode_data(mysar.graphtree, tmp);
7675
tmpcsv.append("\n");
7776
}
@@ -89,8 +88,7 @@ public void run() {
8988

9089
}
9190

92-
93-
public void export_treenode_header(SortedTreeNode node) {
91+
private void export_treenode_header(SortedTreeNode node) {
9492
int num = node.getChildCount();
9593

9694
if (num > 0) {
@@ -116,7 +114,7 @@ public void export_treenode_header(SortedTreeNode node) {
116114
}
117115
}
118116

119-
public void export_treenode_data(SortedTreeNode node, RegularTimePeriod time) {
117+
private void export_treenode_data(SortedTreeNode node, RegularTimePeriod time) {
120118
int num = node.getChildCount();
121119

122120
if (num > 0) {
@@ -151,11 +149,10 @@ private void update_ui() {
151149

152150
}
153151

154-
155152
private StringBuilder tmpcsv = new StringBuilder();
156153
private int progress_info = 0;
157-
private String csvfilename = null;
158-
private kSar mysar = null;
154+
private String csvfilename;
155+
private kSar mysar;
159156
private JProgressBar progress_bar = null;
160157
private JDialog dialog = null;
161158
}

src/main/java/net/atomique/ksar/export/FilePDF.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package net.atomique.ksar.export;
77

8-
import com.itextpdf.awt.DefaultFontMapper;
9-
import com.itextpdf.awt.FontMapper;
108
import com.itextpdf.awt.PdfGraphics2D;
119
import com.itextpdf.text.BaseColor;
1210
import com.itextpdf.text.Document;
@@ -51,12 +49,11 @@ public class FilePDF extends PdfPageEventHelper implements Runnable {
5149
public FilePDF(String filename, kSar hissar) {
5250
pdffilename = filename;
5351
mysar = hissar;
54-
5552
}
5653

5754
public FilePDF(String filename, kSar hissar, JProgressBar g, JDialog d) {
58-
pdffilename = filename;
59-
mysar = hissar;
55+
this(filename, hissar);
56+
6057
progress_bar = g;
6158
dialog = d;
6259
}
@@ -83,10 +80,11 @@ public void run() {
8380
break;
8481
}
8582

86-
pdfheight = document.getPageSize().getHeight();
83+
float pdfheight = document.getPageSize().getHeight();
8784
pdfwidth = document.getPageSize().getWidth();
8885
pageheight = pdfheight - (2 * pdfmargins);
8986
pagewidth = pdfwidth - (2 * pdfmargins);
87+
9088
try {
9189
writer = PdfWriter.getInstance(document, new FileOutputStream(pdffilename));
9290
} catch (DocumentException | FileNotFoundException ex) {
@@ -217,19 +215,17 @@ public void IndexPage(PdfWriter writer, Document document) {
217215
}
218216

219217
private int progress_info = 0;
220-
private float pdfheight;
221218
private float pdfwidth;
222219
private int pdfmargins = 10;
223-
float pageheight;
224-
float pagewidth;
220+
private float pageheight;
221+
private float pagewidth;
225222
private int total_pages = 1; // page 1 (index)
226223
private String pdffilename = null;
227224
private Document document = null;
228225
private PdfWriter writer = null;
229226
private PdfContentByte pdfcb;
230227
private kSar mysar = null;
231-
FontMapper mapper = new DefaultFontMapper();
232-
BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
228+
private BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
233229
private JProgressBar progress_bar = null;
234230
private JDialog dialog = null;
235231
private ChartRenderingInfo chartinfo = null;

0 commit comments

Comments
 (0)