Advertisement
Porto881

Java script

Dec 23rd, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.event.WindowEvent;
  3. import java.io.FileNotFoundException;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.jdesktop.application.Action;
  7. import org.jdesktop.application.ResourceMap;
  8. import org.jdesktop.application.SingleFrameApplication;
  9. import org.jdesktop.application.FrameView;
  10. import org.jdesktop.application.TaskMonitor;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.WindowListener;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Scanner;
  23. import javax.swing.Timer;
  24. import javax.swing.Icon;
  25. import javax.swing.JButton;
  26. import javax.swing.JDialog;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JFrame;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.table.DefaultTableModel;
  31.  
  32. /**
  33. * The application's main frame.
  34. */
  35. public class NMRLogView extends FrameView {
  36. String warningLog = "";
  37. SimpleDateFormat shortForm = new SimpleDateFormat("MMM d, yyyy");
  38. File prefs = new File("sub.txt");
  39. ArrayList<User> users = new ArrayList<User>();
  40. public NMRLogView(SingleFrameApplication app) {
  41. super(app);
  42. initComponents();
  43.  
  44. this.getFrame().addWindowListener(new WindowListener() {
  45.  
  46. public void windowOpened(WindowEvent e) {
  47.  
  48. }
  49.  
  50. public void windowClosing(WindowEvent e) {
  51. FileWriter fw = null;
  52. try {
  53. fw = new FileWriter(prefs);
  54. fw.write(startMonth.getSelectedIndex() + "," + startYear.getText() + ","
  55. + endMonth.getSelectedIndex() + "," + endYear.getText() + "," +
  56. moneyField.getText());
  57. fw.flush();
  58. fw.close();
  59. } catch (IOException ex) {
  60. Logger.getLogger(NMRLogView.class.getName()).log(Level.SEVERE, null, ex);
  61. } finally {
  62. try {
  63. fw.close();
  64. } catch (IOException ex) {
  65. Logger.getLogger(NMRLogView.class.getName()).log(Level.SEVERE, null, ex);
  66. }
  67. }
  68. }
  69.  
  70. public void windowClosed(WindowEvent e) {
  71.  
  72. }
  73.  
  74. public void windowIconified(WindowEvent e) {
  75.  
  76. }
  77.  
  78. public void windowDeiconified(WindowEvent e) {
  79.  
  80. }
  81.  
  82. public void windowActivated(WindowEvent e) {
  83.  
  84. }
  85.  
  86. public void windowDeactivated(WindowEvent e) {
  87.  
  88. }
  89.  
  90. });
  91.  
  92. boolean prefsLoaded = false;
  93. if(prefs.exists()) {
  94. try {
  95. Scanner sc = new Scanner(new FileReader(prefs));
  96. sc.useDelimiter(",");
  97. startMonth.setSelectedIndex(Integer.parseInt(sc.next()));
  98. startYear.setText(sc.next());
  99. endMonth.setSelectedIndex(Integer.parseInt(sc.next()));
  100. endYear.setText(sc.next());
  101. moneyField.setText(sc.next());
  102. prefsLoaded = true;
  103. } catch (FileNotFoundException ex) {
  104. Logger.getLogger(NMRLogView.class.getName()).log(Level.SEVERE, null, ex);
  105. } catch (NumberFormatException e) {
  106. System.out.println("Preferences file not formatted correctly, resetting it.");
  107. }
  108. }
  109. else if(!prefsLoaded) {
  110. try {
  111. prefs.createNewFile();
  112. FileWriter fw = new FileWriter(prefs);
  113. fw.write(startMonth.getSelectedIndex() + "," + startYear.getText() + ","
  114. + endMonth.getSelectedIndex() + "," + endYear.getText() + "," +
  115. moneyField.getText());
  116. fw.flush();
  117. fw.close();
  118. } catch (IOException ex) {
  119. Logger.getLogger(NMRLogView.class.getName()).log(Level.SEVERE, null, ex);
  120. }
  121. }
  122.  
  123. logButton.setVisible(false);
  124. // status bar initialization - message timeout, idle icon and busy animation, etc
  125. ResourceMap resourceMap = getResourceMap();
  126. int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
  127. messageTimer = new Timer(messageTimeout, new ActionListener() {
  128. public void actionPerformed(ActionEvent e) {
  129. }
  130. });
  131. messageTimer.setRepeats(false);
  132. int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
  133. for (int i = 0; i < busyIcons.length; i++) {
  134. busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
  135. }
  136. busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
  137. public void actionPerformed(ActionEvent e) {
  138. busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
  139.  
  140. }
  141. });
  142. idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
  143.  
  144.  
  145. // connecting action tasks to status bar via TaskMonitor
  146. TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
  147.  
  148. }
  149.  
  150. @Action
  151. public void showAboutBox() {
  152. if (aboutBox == null) {
  153. JFrame mainFrame = NMRLogApp.getApplication().getMainFrame();
  154. aboutBox = new NMRLogAboutBox(mainFrame);
  155. aboutBox.setLocationRelativeTo(mainFrame);
  156. }
  157. NMRLogApp.getApplication().show(aboutBox);
  158. }
  159.  
  160. /** This method is called from within the constructor to
  161. * initialize the form.
  162. * WARNING: Do NOT modify this code. The content of this method is
  163. * always regenerated by the Form Editor.
  164. */
  165. @SuppressWarnings("unchecked")
  166. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  167. private void initComponents() {
  168.  
  169. mainPanel = new javax.swing.JPanel();
  170. startMonth = new javax.swing.JComboBox();
  171. startYear = new javax.swing.JTextField();
  172. endMonth = new javax.swing.JComboBox();
  173. endYear = new javax.swing.JTextField();
  174. runButton = new javax.swing.JToggleButton();
  175. jLabel1 = new javax.swing.JLabel();
  176. moneyField = new javax.swing.JTextField();
  177. logButton = new javax.swing.JButton();
  178. jScrollPane1 = new javax.swing.JScrollPane();
  179. table = new javax.swing.JTable();
  180. jLabel2 = new javax.swing.JLabel();
  181. menuBar = new javax.swing.JMenuBar();
  182. javax.swing.JMenu fileMenu = new javax.swing.JMenu();
  183. jMenuItem1 = new javax.swing.JMenuItem();
  184. jMenuItem2 = new javax.swing.JMenuItem();
  185. javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
  186. javax.swing.JMenu helpMenu = new javax.swing.JMenu();
  187. javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
  188.  
  189. mainPanel.setName("mainPanel"); // NOI18N
  190.  
  191. startMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
  192. startMonth.setName("startMonth"); // NOI18N
  193. startMonth.addComponentListener(new java.awt.event.ComponentAdapter() {
  194. public void componentHidden(java.awt.event.ComponentEvent evt) {
  195. startMonthComponentHidden(evt);
  196. }
  197. });
  198.  
  199. org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nmrlog.NMRLogApp.class).getContext().getResourceMap(NMRLogView.class);
  200. startYear.setText(resourceMap.getString("startYear.text")); // NOI18N
  201. startYear.setName("startYear"); // NOI18N
  202.  
  203. endMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
  204. endMonth.setName("endMonth"); // NOI18N
  205.  
  206. endYear.setText(resourceMap.getString("endYear.text")); // NOI18N
  207. endYear.setName("endYear"); // NOI18N
  208.  
  209. runButton.setText(resourceMap.getString("runButton.text")); // NOI18N
  210. runButton.setName("runButton"); // NOI18N
  211. runButton.addActionListener(new java.awt.event.ActionListener() {
  212. public void actionPerformed(java.awt.event.ActionEvent evt) {
  213. runButtonActionPerformed(evt);
  214. }
  215. });
  216.  
  217. jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
  218. jLabel1.setName("jLabel1"); // NOI18N
  219.  
  220. moneyField.setText(resourceMap.getString("moneyField.text")); // NOI18N
  221. moneyField.setName("moneyField"); // NOI18N
  222.  
  223. logButton.setText(resourceMap.getString("logButton.text")); // NOI18N
  224. logButton.setName("logButton"); // NOI18N
  225. logButton.addActionListener(new java.awt.event.ActionListener() {
  226. public void actionPerformed(java.awt.event.ActionEvent evt) {
  227. logButtonActionPerformed(evt);
  228. }
  229. });
  230.  
  231. jScrollPane1.setName("jScrollPane1"); // NOI18N
  232.  
  233. table.setAutoCreateRowSorter(true);
  234. table.setModel(new javax.swing.table.DefaultTableModel(
  235. new Object [][] {
  236.  
  237. },
  238. new String [] {
  239. "User", "Time", "Cost"
  240. }
  241. ));
  242. table.setName("table"); // NOI18N
  243. table.setShowHorizontalLines(false);
  244. table.setShowVerticalLines(false);
  245. jScrollPane1.setViewportView(table);
  246. table.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("table.columnModel.title0")); // NOI18N
  247. table.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("table.columnModel.title1")); // NOI18N
  248. table.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("table.columnModel.title2")); // NOI18N
  249.  
  250. jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
  251. jLabel2.setName("jLabel2"); // NOI18N
  252.  
  253. javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
  254. mainPanel.setLayout(mainPanelLayout);
  255. mainPanelLayout.setHorizontalGroup(
  256. mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  257. .addGroup(mainPanelLayout.createSequentialGroup()
  258. .addContainerGap()
  259. .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  260. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
  261. .addGroup(mainPanelLayout.createSequentialGroup()
  262. .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  263. .addComponent(logButton)
  264. .addComponent(startMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  265. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  266. .addComponent(startYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  267. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  268. .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  269. .addGroup(mainPanelLayout.createSequentialGroup()
  270. .addComponent(jLabel1)
  271. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  272. .addComponent(endMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  273. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  274. .addComponent(endYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  275. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
  276. .addComponent(jLabel2)
  277. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  278. .addComponent(moneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  279. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  280. .addComponent(runButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
  281. .addGap(143, 143, 143)))
  282. .addContainerGap())
  283. );
  284. mainPanelLayout.setVerticalGroup(
  285. mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  286. .addGroup(mainPanelLayout.createSequentialGroup()
  287. .addContainerGap()
  288. .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  289. .addComponent(startMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  290. .addComponent(startYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  291. .addComponent(jLabel1)
  292. .addComponent(endMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  293. .addComponent(endYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  294. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  295. .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  296. .addComponent(logButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
  297. .addComponent(runButton)
  298. .addComponent(moneyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  299. .addComponent(jLabel2))
  300. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  301. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
  302. .addContainerGap())
  303. );
  304.  
  305. menuBar.setName("menuBar"); // NOI18N
  306.  
  307. fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
  308. fileMenu.setName("fileMenu"); // NOI18N
  309.  
  310. javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nmrlog.NMRLogApp.class).getContext().getActionMap(NMRLogView.class, this);
  311. jMenuItem1.setAction(actionMap.get("save")); // NOI18N
  312. jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
  313. jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
  314. jMenuItem1.setName("jMenuItem1"); // NOI18N
  315. jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  316. public void actionPerformed(java.awt.event.ActionEvent evt) {
  317. jMenuItem1ActionPerformed(evt);
  318. }
  319. });
  320. fileMenu.add(jMenuItem1);
  321.  
  322. jMenuItem2.setAction(actionMap.get("inspect")); // NOI18N
  323. jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
  324. jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
  325. jMenuItem2.setName("jMenuItem2"); // NOI18N
  326. fileMenu.add(jMenuItem2);
  327.  
  328. exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
  329. exitMenuItem.setName("exitMenuItem"); // NOI18N
  330. fileMenu.add(exitMenuItem);
  331.  
  332. menuBar.add(fileMenu);
  333.  
  334. helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
  335. helpMenu.setName("helpMenu"); // NOI18N
  336.  
  337. aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
  338. aboutMenuItem.setName("aboutMenuItem"); // NOI18N
  339. helpMenu.add(aboutMenuItem);
  340.  
  341. menuBar.add(helpMenu);
  342.  
  343. setComponent(mainPanel);
  344. setMenuBar(menuBar);
  345. }// </editor-fold>
  346.  
  347. private void startMonthComponentHidden(java.awt.event.ComponentEvent evt) {
  348. // TODO add your handling code here:
  349. }
  350. //When you run!
  351. private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
  352. int sMonth = startMonth.getSelectedIndex();
  353. int eMonth = endMonth.getSelectedIndex();
  354. int sYear;
  355. int eYear;
  356. double money;
  357. warningLog = "";
  358.  
  359. DefaultTableModel dm = (DefaultTableModel) table.getModel();
  360. int rowCount = dm.getRowCount();
  361. //Remove rows one by one from the end of the table
  362. for (int i = rowCount - 1; i >= 0; i--) {
  363. dm.removeRow(i);
  364. }
  365.  
  366. boolean warning = false;
  367. File logFile = new File("log.txt");
  368. try {
  369. //Read the starting and ending conditions from the JComboBox and JTextBox
  370. sYear = Integer.parseInt(startYear.getText());
  371. eYear = Integer.parseInt(endYear.getText());
  372. money = Double.parseDouble(moneyField.getText());
  373. //instantiates start and end calendars, processes year and month
  374. Calendar entryStartCal = Calendar.getInstance();
  375. Calendar entryEndCal = Calendar.getInstance();
  376. entryStartCal.set(sYear,sMonth,1);
  377. entryEndCal.set(eYear,eMonth,1);
  378. entryStartCal.set(Calendar.HOUR_OF_DAY,0);
  379. entryEndCal.set(Calendar.HOUR_OF_DAY,0);
  380. entryStartCal.set(Calendar.MINUTE,0);
  381. entryEndCal.set(Calendar.MINUTE,0);
  382. entryStartCal.set(Calendar.SECOND,0);
  383. entryEndCal.set(Calendar.SECOND,0);
  384. System.out.println(entryEndCal.getTime());
  385. //verifies start date is before end date
  386. if(entryEndCal.before(entryStartCal)) {
  387. JOptionPane.showMessageDialog(this.mainPanel, "The beginning date (left) must be before the end date (right). \r\n log file will not be read.");
  388. return;
  389. }
  390.  
  391. //Begin reading entries from logFile.
  392. ArrayList<Entry> entries = new ArrayList<Entry>();
  393. int entryCount = 0;
  394. String temp = "";
  395. try {
  396. Scanner sc = new Scanner(new FileReader(new File("log.txt")));
  397. sc.useDelimiter("\n");
  398. SimpleDateFormat form = new SimpleDateFormat("yyyy");
  399. int year = Integer.parseInt(form.format(Calendar.getInstance().getTime()));
  400. entryCount = 0;
  401. int lastMonth = -1;
  402. while(sc.hasNext()) {
  403. temp = sc.next();
  404. if(temp.contains("console") && !temp.contains("still logged in")) {
  405. int thisMonth = Entry.getMonth(temp);
  406. if(thisMonth == 11 && lastMonth == 0)
  407. year--;
  408. entries.add(new Entry(temp,entryCount,year));
  409. entryCount++;
  410. lastMonth = Entry.getMonth(temp);
  411. }
  412. }
  413. } catch (IOException e) { e.printStackTrace();}
  414. catch (ParseException e) { warning = true; String warningString = "Parse error on entry" + entryCount + "\r\n" +
  415. "String: " + temp;
  416. warningLog += warningString;}
  417. catch (NumberFormatException e) { warning = true; String warningString = "Number format exception caught on entry " + entryCount + "\r\n" +
  418. "String: " + temp;
  419. warningLog += warningString; }
  420. catch(StringIndexOutOfBoundsException e) {warning = true; String warningString = "Parse error on entry " + entryCount + "\r\n" +
  421. "String: " + temp;
  422. warningLog += warningString;
  423.  
  424. }
  425.  
  426. warningLog += entryCount + " entries processed.\r\n\r\n";
  427.  
  428. //Begin processing entries that start on entryStartCal and end on entryEndCal
  429. users = new ArrayList<User>();
  430. int entriesInRange = 0;
  431. for(int i=0;i<entries.size();i++) {
  432. Entry entry = entries.get(i);
  433. Calendar loginCalendar = Calendar.getInstance();
  434. loginCalendar.setTime(entry.getDate());
  435. //Check if it's in the right date range
  436. if(entryStartCal.before(loginCalendar) && entryEndCal.after(loginCalendar)){
  437. boolean foundUser = false;
  438. //Go through the users...
  439. for(User user : users) {
  440. //If the user is already in the list of users, add the minutes
  441. if(entry.getUser().equals(user.getName())) {
  442. foundUser = true;
  443. user.addMinutes(entry.getMinutes());
  444. if(entry.getMinutes() > 2880) {
  445. warning = true;
  446. int dayNum = entry.getMinutes() / 1440;
  447. String warningString = "Entry longer than " + dayNum + " days detected for user " + user.getName() +
  448. "." + "\r\n" + " Details: Entry line " + i + " on " + shortForm.format(loginCalendar.getTime()) + "\r\n";
  449. warningLog += warningString;
  450. }
  451. entriesInRange++;
  452. user.addEntry(entry.getMinutes() + " m on " + shortForm.format(entry.getDate()));
  453. break;
  454. }
  455. }
  456. //If it didn't find the user, add a new user with those minutes
  457. if(!foundUser) {
  458. int minutes = entry.getMinutes();
  459.  
  460. if(minutes > 2880) {
  461. warning = true;
  462. int dayNum = entry.getMinutes() / 1440;
  463. String warningString = "Entry longer than " + dayNum + " days detected for user " + entry.getUser() +
  464. "." + "\r\n" + " Details: Entry line " + i + " on " + shortForm.format(loginCalendar.getTime()) + "\r\n";
  465. warningLog += warningString;
  466. }
  467. User newUser = new User(entry.getUser(),minutes);
  468. users.add(newUser);
  469. entriesInRange++;
  470. newUser.addEntry(minutes + " m on " + shortForm.format(entry.getDate()));
  471. }
  472. }
  473. }
  474. warningLog += entriesInRange + " entries processed in the time range.\r\n\r\n";
  475. //Print out what we've found by processing the entries
  476. String toPrint = "";
  477. for(User user : users) {
  478. Double cost = (double) user.getMinutes();
  479. cost = cost / 60.0;
  480. cost = cost * money;
  481. String costString = String.format("%.2f",cost);
  482. toPrint += user + " $" + costString + "\r\n";
  483. DefaultTableModel model = (DefaultTableModel) table.getModel();
  484. model.addRow(new String[]{user.getName(),user.getTime(),"$" + costString});
  485. }
  486. if(warning) {
  487. logButton.setBackground(Color.red);
  488. logButton.setVisible(true);
  489. }
  490. else {
  491. logButton.setVisible(true);
  492. warningLog += "NMRLog ran without errors.";
  493. }
  494.  
  495.  
  496. } catch (NumberFormatException e) {
  497. JOptionPane.showMessageDialog(this.mainPanel, "Subs funning");
  498. }
  499.  
  500. }
  501.  
  502. @Action
  503. public void save() {
  504. JFileChooser jfc = new JFileChooser(new File("output.txt"));
  505. int returnVal = jfc.showSaveDialog(this.getFrame());
  506. if(returnVal == JFileChooser.APPROVE_OPTION) {
  507. File outputFile = jfc.getSelectedFile();
  508. if(outputFile.toString().contains("log.txt")) {
  509. JOptionPane.showMessageDialog(this.mainPanel, "Subs.txt init");
  510. }
  511. else {
  512. try {
  513. if(!outputFile.getName().contains(".txt"))
  514. outputFile = new File(outputFile.getPath() + ".txt");
  515. if(outputFile.toString().contains("log.txt")) {
  516. JOptionPane.showMessageDialog(this.mainPanel, "subs.txt");
  517. }
  518. if(!outputFile.exists())
  519. outputFile.createNewFile();
  520.  
  521. FileWriter fw = new FileWriter(outputFile);
  522. fw.write("Run on: " + (Calendar.getInstance().getTime())+"\r\n");
  523. int sYear;
  524. int eYear;
  525. int sMonth = startMonth.getSelectedIndex();
  526. int eMonth = endMonth.getSelectedIndex();
  527. sYear = Integer.parseInt(startYear.getText());
  528. eYear = Integer.parseInt(endYear.getText());
  529.  
  530. //instantiates start and end calendars, processes year and month
  531. Calendar entryStartCal = Calendar.getInstance();
  532. Calendar entryEndCal = Calendar.getInstance();
  533. entryStartCal.set(sYear,sMonth,1);
  534. entryEndCal.set(eYear,eMonth,1);
  535. fw.write("From " + shortForm.format(entryStartCal.getTime()) + " to " +
  536. shortForm.format(entryEndCal.getTime()) + "\r\n\r\n");
  537. DefaultTableModel dm = (DefaultTableModel) table.getModel();
  538. int rowCount = dm.getRowCount();
  539. //Remove rows one by one from the end of the table
  540. for (int i = rowCount - 1; i >= 0; i--) {
  541. fw.write(dm.getValueAt(i, 0)+" " + dm.getValueAt(i, 1) + " " + dm.getValueAt(i, 2) + "\r\n");
  542. }
  543. fw.flush();
  544. fw.close();
  545. } catch (IOException ex) {
  546. JOptionPane.showMessageDialog(this.mainPanel, "Failed to write save file.");
  547. Logger.getLogger(NMRLogView.class.getName()).log(Level.SEVERE, null, ex);
  548. }
  549. }
  550. }
  551. }
  552. @Action
  553. public void inspect() {
  554. int rowIndex=table.getSelectedRow();
  555. String userSelected = (String) table.getValueAt(rowIndex,0);
  556. for(User user : users) {
  557. if(user.getName().contains(userSelected)) {
  558. JOptionPane.showMessageDialog(this.mainPanel, user.getEntrySummary());
  559. break;
  560. }
  561. }
  562. }
  563.  
  564. private void logButtonActionPerformed(java.awt.event.ActionEvent evt) {
  565. logButton.setBackground(new JButton().getBackground());
  566. logButton.setVisible(false);
  567. JOptionPane.showMessageDialog(this.mainPanel, warningLog);
  568. }
  569.  
  570. private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
  571.  
  572. }
  573.  
  574. // Variables declaration - do not modify
  575. private javax.swing.JComboBox endMonth;
  576. private javax.swing.JTextField endYear;
  577. private javax.swing.JLabel jLabel1;
  578. private javax.swing.JLabel jLabel2;
  579. private javax.swing.JMenuItem jMenuItem1;
  580. private javax.swing.JMenuItem jMenuItem2;
  581. private javax.swing.JScrollPane jScrollPane1;
  582. private javax.swing.JButton logButton;
  583. private javax.swing.JPanel mainPanel;
  584. private javax.swing.JMenuBar menuBar;
  585. private javax.swing.JTextField moneyField;
  586. private javax.swing.JToggleButton runButton;
  587. private javax.swing.JComboBox startMonth;
  588. private javax.swing.JTextField startYear;
  589. private javax.swing.JTable table;
  590. // End of variables declaration
  591.  
  592. private final Timer messageTimer;
  593. private final Timer busyIconTimer;
  594. private final Icon idleIcon;
  595. private final Icon[] busyIcons = new Icon[15];
  596. private int busyIconIndex = 0;
  597.  
  598. private JDialog aboutBox;
  599. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement