-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticsChartLauncher.java
More file actions
359 lines (297 loc) · 9.21 KB
/
StatisticsChartLauncher.java
File metadata and controls
359 lines (297 loc) · 9.21 KB
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
package Jvakt;
/*
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;
import Jvakt.ManFiles.StreamGobbler;
import Jvakt.ManFiles.runCMD;
public class StatisticsChartLauncher extends JFrame {
JTextField textField = new JTextField("", 25);
JButton button = new JButton("Launch");
JButton filebutton = new JButton("Statistics file");
JFileChooser fileChooser;
static File dir = null;
static String statsDir;
static String from = "2021-09-28 00:00:00";
static String tom = "2021-09-28 24:00:00";
static String[] tab = new String [1];
static String chdat = new String("yyyy-MM-dd HH:mm:ss");
static SimpleDateFormat dat_form;
static Date now;
static Date then;
static JFormattedTextField fomTextField = null;
static JFormattedTextField tomTextField = null;
private String cmdStats = null;
private String cmd;
public StatisticsChartLauncher() {
super("Jvakt.StatisticsChartLauncher "+getVersion()+".54");
setLayout(new FlowLayout());
ImageIcon img = new ImageIcon("console.png");
setIconImage(img.getImage());
getProps();
// // customizes appearance: font, foreground, background
// textField.setFont(new java.awt.Font("Arial", Font.ITALIC | Font.BOLD, 12));
// textField.setForeground(Color.BLUE);
// textField.setBackground(Color.YELLOW);
//
// // customizes text selection
// textField.setSelectionColor(Color.CYAN);
// textField.setSelectedTextColor(Color.RED);
// sets initial selection
// textField.setSelectionStart(8);
// textField.setSelectionEnd(12);
// adds event listener which listens to Enter key event
// textField.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent event) {
// JOptionPane.showMessageDialog(StatisticsChartLauncher.this,
// "You entered text #1:\n" + textField.getText());
// }
// });
// adds key event listener. Disables the button if input file name is not present
button.setEnabled(false);
textField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent event) {
String content = textField.getText();
System.out.println(" content.length() : "+content.length());
if (content.length()>5) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
});
// adds action listener for the button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String statF = textField.getText();
String from = fomTextField.getText();
String tom = tomTextField.getText();
statF = statF.replace(' ','?');
from = from.replace(' ','?');
tom = tom.replace(' ','?');
cmd= cmdStats+" -statF "+statF+" -from "+from+" -to "+tom;
// cmd= cmdStats+" -statF \""+textField.getText()+"\" -from \""+fomTextField.getText()+"\" -to \""+tomTextField.getText()+"\"";
System.out.println("cmd >> " +cmd);
if (cmdStats == null) {
JOptionPane.showMessageDialog(StatisticsChartLauncher.this,"Property \"cmdStats\" in command.properties file not found!");
System.out.println("cmdStats in command.properties file not found");
} else {
runCMD pp = new runCMD(cmd); // start the renderer program
if (pp.runCMDfile()) {
System.out.println(" -successfull cmd: "+cmd);
}
else {
JOptionPane.showMessageDialog(StatisticsChartLauncher.this,"Command Failed!\n"+cmd);
System.out.println(" -failed cmd: "+cmd);
}
}
}
});
// adds action listener for the file button
filebutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// Open the save dialog
System.out.println("* dir -> " + dir );
fileChooser = new JFileChooser();
if (statsDir!=null) {
dir = new File(statsDir);
fileChooser.setCurrentDirectory(dir);
} else fileChooser.setCurrentDirectory(null);
fileChooser.showOpenDialog(null);
// JOptionPane.showMessageDialog(test09.this,
// "Content of the text field:\n" + j.getSelectedFile().getAbsolutePath());
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
button.setEnabled(true);
}
});
JLabel fileLabel = new JLabel("Input file:");
// from date field
JLabel fomLabel = new JLabel("From Date :");
try {
fomTextField = new JFormattedTextField(
new MaskFormatter("####-##-## ##:##:##"));
fomTextField.setColumns(11);
fomTextField.setText(from.toString());
} catch (ParseException e) {
e.printStackTrace();
}
fomLabel.setLabelFor(fomTextField);
// to date field
JLabel tomLabel = new JLabel("To Date :");
// JFormattedTextField tomTextField = null;
try {
tomTextField = new JFormattedTextField(
new MaskFormatter("####-##-## ##:##:##"));
tomTextField.setColumns(11);
tomTextField.setText(tom.toString());
} catch (ParseException e) {
e.printStackTrace();
}
tomLabel.setLabelFor(tomTextField);
add(fileLabel);
add(textField);
add(filebutton);
add(fomLabel);
add(fomTextField);
add(tomLabel);
add(tomTextField);
add(button);
setSize(1200, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
dat_form = new SimpleDateFormat(chdat);
now = new Date();
then = new Date();
then.setTime(now.getTime()-86400000); // subtract 24 hours
now.setTime(now.getTime()+86400000); // add 24 hours
tom = dat_form.format(now);
from= dat_form.format(then);
System.out.println("Now :"+now);
System.out.println("Then :"+then);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StatisticsChartLauncher();
}
});
}
void getProps() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("console.properties");
prop.load(input);
// get the property value and print it out
cmdStats = prop.getProperty("cmdStats");
statsDir = prop.getProperty("statsDir");
System.out.println("* statsDir -> " + statsDir );
input.close();
} catch (IOException ex) {
// ex.printStackTrace();
}
}
// Class to Launch the renderer program ( start )
public class runCMD {
Date now;
String cmd;
boolean swSettings = false;
boolean swError = false;
boolean swDestroy = false;
boolean swGoon = false;
int nuWait = 0;
int exitVal;
public runCMD(String c) {
cmd = c;
now = new Date();
}
public boolean runCMDfile() {
now = new Date();
Process p;
// execute the command if there is one.
// default command handling
swError = false;
swDestroy = false;
System.out.println("* runCMD "+ now + " -> " + cmd );
try {
exitVal = 0;
p = Runtime.getRuntime().exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
Long w = new Long(2);
if (p.waitFor(w,TimeUnit.SECONDS)) {
swError = true;
System.out.println("** exitVal: " + exitVal);
}
}
catch (Exception e) {
// swError = true;
e.printStackTrace();
System.out.println("** exeption (p) ");
}
if (swError) {
System.out.println("-Unsuccessfull cmd: "+ cmd);
return false;
}
else {
// if (swList)
// System.out.println("-Successfull cmd: "+ cmd);
return true;
}
}
}
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + "> " + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
// Launch the renderer program ( stop )
}