File: CRONOMETER.java

package info (click to toggle)
cronometer 0.9.9%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,752 kB
  • sloc: java: 17,382; xml: 909; makefile: 19; sh: 18
file content (605 lines) | stat: -rw-r--r-- 20,291 bytes parent folder | download | duplicates (4)
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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package ca.spaz.cron;
 
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ca.spaz.cron.actions.CopyServingsToUserAction;
import ca.spaz.cron.actions.CreateRecipeAction;
import ca.spaz.cron.datasource.Datasources;
import ca.spaz.cron.datasource.XMLFoodLoader;
import ca.spaz.cron.foods.Food;
import ca.spaz.cron.foods.FoodEditor;
import ca.spaz.cron.targets.DRITargetModel;
import ca.spaz.cron.targets.TargetEditor;
import ca.spaz.cron.ui.*;
import ca.spaz.cron.ui.SplashScreen;
import ca.spaz.cron.user.*;
import ca.spaz.gui.*;
import ca.spaz.task.Task;
import ca.spaz.task.TaskListener;
import ca.spaz.util.*;

import com.apple.mrj.MRJAboutHandler;
import com.apple.mrj.MRJQuitHandler;

/**
 * The main app.  
 *
 * @author davidson
 */
public class CRONOMETER extends JFrame implements TaskListener, MRJQuitHandler, MRJAboutHandler, ClipboardOwner {

   public static final String TITLE = "CRON-o-Meter";
   public static final String VERSION = "0.9.9";
   public static final int BUILD = 19;
   public static JFrame mainFrame = null;

   private static Clipboard clipboard = new Clipboard ("CRON-o-Meter");

   private static DailySummary ds;

   private SpazMenuBar menu;
   private HelpBrowser help;
   private JPanel mainPanel;    

   private static CRONOMETER instance;

   public static CRONOMETER getInstance() {
      if (null == instance) {
         instance = new CRONOMETER();         
      }
      return instance;
   }

   /**
    * Constructor
    */
   public CRONOMETER() {
      setupForMacOSX();
   }
   
   public static Clipboard getClipboard() {
      return clipboard;
   }

   public static Image getWindowIcon() {
      return ImageFactory.getInstance().loadImage("/img/icon.png");
   }
   
   private void initGUI() {
      try {
         setJMenuBar(getMenu());         
         setIconImage(getWindowIcon());
         setTitle(getFullTitleWithUser());
         if (!UserManager.getSubdirectory().equalsIgnoreCase("cronometer")) {
            setTitle(getFullTitleWithUser() + " ["+UserManager.getSubdirectory()+"]");
         }
         getContentPane().add(getMainPanel());
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

         pack();
         Point p = ToolBox.centerFrame(this);
         if (!UserManager.getUserManager().firstCronRun()) {
            UserManager.getUserManager().restoreWindow(CRONOMETER.getInstance(), p);
            getDailySummary().getDietPanel().setDividerLocation(UserManager.getUserManager().getDietDivider(300));
         }
         setVisible(true);
         mainFrame = this;
         if (UserManager.getUserManager().firstCronRun()) {
            doShowReadMe();
            UserManager.getUserManager().setFirstCronRun(false);
            UserManager.getCurrentUser().doFirstRun(getMainPanel());
            TargetEditor.setDefaultTargets(new DRITargetModel(), UserManager.getCurrentUser());
         } else {
            if (UserManager.getUserManager().getLastBuild() < 19) {
               doShowCronometerDotCom();
            } else if (UserManager.getUserManager().getLastBuild() < BUILD) {
               doShowReleaseNotes();
            }
            if (UserManager.getUserManager().getLastBuild() < 7) {
               upgradeToB7();
            }
         }          
         UserManager.getUserManager().setLastBuild(CRONOMETER.BUILD);
         makeAutoSaveTimer();
         installSystemTray();
         addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               doQuit();
            }
            public void windowIconified(WindowEvent e) {
               if (UserManager.getUserManager().getHideWhenMinimized()) {
                  setVisible(false);
               }
            }
         });
      } catch (Exception e) {
         Logger.debug(e);
         ErrorReporter.showError(e, this); 
      }
   }
   
   /**
    * Adds a system tray, if the JVM and OS support it.
    */
   private void installSystemTray() {     
      if (ToolBox.classExists("java.awt.SystemTray")) {
         // this is a Java-6 feature only, so we use reflection to survive on older JVMs
         ToolBox.instantiate("ca.spaz.cron.ui.SysTray");
      }
   }

   private void upgradeToB7() {
      if (UserManager.getCurrentUser().isFemale()) {
         JOptionPane.showMessageDialog(getMainPanel(), 
               "Previous versions of CRON-o-Meter were incorrectly \n" +
               "suggesting male nutritional targets for women.\n" +
               "It is highly recommended that you reset your nutritional\n" +
               "targets to values appropriate for women.");
         UserSettingsDialog.showDialog(UserManager.getUserManager(), getMainPanel());
         TargetEditor.setDefaultTargets(new DRITargetModel(), UserManager.getCurrentUser());
         doEditUserSettings(); 
      }
   }
   
   public void doAddServing() {
      getDailySummary().getServingTable().doAddServing();
   }

   private void makeAutoSaveTimer() {
      Timer t = new Timer(6000*5, new ActionListener(){
         public void actionPerformed(ActionEvent e) {
            UserManager.getCurrentUser().saveUserData();
            getDailySummary().refreshTime();
         }
      });
      t.setCoalesce(true);
      t.setRepeats(true);
      t.start();
   }

   public void doShowReleaseNotes() {
      getHelpBrowser().showWindow();
      getHelpBrowser().showPage("release9.html"); 
   }

   public void doShowCronometerDotCom() {
      getHelpBrowser().showWindow();
      getHelpBrowser().showPage("webversion.html"); 
   }
   
   public void doExportToWebVersion() {
      ExportWizard.doExport();
   }
   
   public void doShowReadMe() {
      new ReadMe(this, "Licence Agreement", getClass().getResource("/docs/readme.html")) {
         public boolean isCancellable() {
            return true;
         }
         public void doCancel() {
            System.exit(1);
         }
      };      
   }

   public void doHelp() {
      getHelpBrowser().showWindow();
      getHelpBrowser().showPage("introduction.html");
   }
   
   
   public HelpBrowser getHelpBrowser() {
      if (help == null) {
         try {
            URL.setURLStreamHandlerFactory(new JarLoader());
            //help = new HelpBrowser("CRON-o-Meter Help", new File("docs").toURI().toURL(), false); 
            //help = new HelpBrowser("CRON-o-Meter Help", new URL("http://spaz.ca/cronometer/docs/"));
            help = new HelpBrowser("CRON-o-Meter Help", new URL("class://DocAnchor/"));
            help.setIconImage(CRONOMETER.getWindowIcon()); 
            ToolBox.centerFrame(help);
         } catch (Exception e) {
            e.printStackTrace();
            ErrorReporter.showError(e, this);
         }
      }
      return help;
   }

   public void doReportBug() {
      ToolBox.launchURL(CRONOMETER.getInstance(), "http://sourceforge.net/tracker/?atid=735995&group_id=136481&func=browse");
   }

   public void doRequestFeature() {
      ToolBox.launchURL(CRONOMETER.getInstance(), "http://sourceforge.net/tracker/?group_id=136481&atid=735998");
   }

   public static String getFullTitle() { 
      return TITLE + " v" + VERSION;       
   }
   
   public static String getFullTitleWithUser() {
      String heading = getFullTitle();
      User currentUser = UserManager.getCurrentUser();
      if (currentUser == null) {
         return heading;
      } else {
         return heading + " - " + currentUser.getUsername();
      }
   }
 
   
   private JMenuBar getMenu() {
      if (null == menu) {
         menu = new SpazMenuBar(getClass().getResourceAsStream("/menubar.xml"), this);
         
         // insert SWING edit menu:
         JMenu mainMenu = new JMenu("Edit");
         mainMenu.setMnemonic(KeyEvent.VK_E);
         TransferActionListener actionListener = new TransferActionListener();
         int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
         JMenuItem menuItem = new JMenuItem("Cut");
         menuItem.setActionCommand((String)TransferHandler.getCutAction().
                  getValue(Action.NAME));
         menuItem.addActionListener(actionListener);
         menuItem.setAccelerator(
           KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
         menuItem.setMnemonic(KeyEvent.VK_T);
         mainMenu.add(menuItem);
         menuItem = new JMenuItem("Copy");
         menuItem.setActionCommand((String)TransferHandler.getCopyAction().
                  getValue(Action.NAME));
         menuItem.addActionListener(actionListener);
         menuItem.setAccelerator(
           KeyStroke.getKeyStroke(KeyEvent.VK_C, mask));
         menuItem.setMnemonic(KeyEvent.VK_C);
         mainMenu.add(menuItem);
         menuItem = new JMenuItem("Copy To...");
         menuItem.setMnemonic('o');
         menuItem.setToolTipText("Copy selected servings to another user");
         menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               CopyServingsToUserAction.copyToUserDialog();
            }
         });
         mainMenu.add(menuItem);
         menuItem = new JMenuItem("Paste");
         menuItem.setActionCommand((String)TransferHandler.getPasteAction().getValue(Action.NAME));
         menuItem.addActionListener(actionListener);
         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask));
         menuItem.setMnemonic(KeyEvent.VK_P);
         mainMenu.add(menuItem);
         
         menu.add(mainMenu, 1);
         
      }
      return menu;
   }


   private JPanel getMainPanel() {
      if (null == mainPanel) {
         mainPanel = new JPanel(new BorderLayout(4, 4));
         mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

         mainPanel.add(getDailySummary(), BorderLayout.CENTER);       
      }
      return mainPanel;
   }
 

   public static DailySummary getDailySummary() {
      if (null == ds) {
         ds = new DailySummary();         
      }
      return ds;
   }

   public void doCreateNewFood() {
      FoodEditor.editFood(new Food());
      refreshDisplays();
   }

   public void doBrowseFoodDatabase() {
      getDailySummary().getServingTable().doAddServing();
      refreshDisplays();
   }

   public void doCreateNewRecipe() {
      List servings = getDailySummary().getServingTable().getSelectedServings();
      CreateRecipeAction.execute(servings);
      refreshDisplays();
   }
     
   public void doEditUserSettings() {
      TargetEditor.editTargets(); 
   }
   
   public void doManageUsers() {
      UserManager.startUserManagerDialog();
   }
   
   public void doImportFood() {
      JFileChooser fd = new JFileChooser();
      fd.setMultiSelectionEnabled(true);
      if (fd.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
         File [] foods = fd.getSelectedFiles();
         if (foods != null) {
            for(int i = 0; i < foods.length; ++i) {
               File f = foods[i];
               Food food = XMLFoodLoader.loadFood(f);
               if (food != null) {
                  Datasources.getUserFoods().addFood(food);
               }
            }
            
            JOptionPane.showMessageDialog(this, 
                  foods.length +" food(s) have been added to your foods.", 
                  "Food(s) Added", JOptionPane.INFORMATION_MESSAGE);
            refreshDisplays();  
         }            
      }
   }
   
   public void doAbout() {
      getHelpBrowser().showWindow();
      getHelpBrowser().showPage("about.html");
      //AboutScreen.showAbout(this);
   }
   
   public void doQuit() {
      try {
         // remember window size & position
         getDailySummary().getNotesEditor().saveCurrentNote();
         UserManager.getUserManager().saveWindow(this);
         UserManager.getUserManager().setDietDivider(getDailySummary().getDietPanel().getDividerLocation());
         Datasources.closeAll();
         UserManager.getUserManager().saveUserProperties();
      } catch (IOException e1) {
         e1.printStackTrace(); 
         ErrorReporter.showError(e1, this);
      }
      System.exit(0);
   }
    
   public void doPrint() {
      getDailySummary().getServingTable().doPrint();
   }
    
   public void taskStarted(Task t) {
      // Nothing
   }

   public void taskFinished(Task t) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            initGUI();
         }
      });
   }

   public void taskAborted(Task t) {
      Logger.error("Startup tasks aborted unexpectedly.  Halting...");
      System.exit(1);
   }

   public void handleQuit() {
      doQuit();
   }
   
   public void doNutritionReport() {
      getDailySummary().getNutritionSummaryPanel().generateReport(getDailySummary().getDate());
   }
   
   /**
    * Installs special apple event listeners for Mac OS X specific functions 
    */
   private void setupForMacOSX() {
      if (ToolBox.isMacOSX()) { 
         try {
            Class appleAppClass = Class.forName("com.apple.mrj.MRJApplicationUtils");
            
            Class quitClass = Class.forName("com.apple.mrj.MRJQuitHandler");           
            Class[] quitArgs = {quitClass};
            Method registerQuitMethod = appleAppClass.getDeclaredMethod("registerQuitHandler", quitArgs);
            if (registerQuitMethod != null) {
               Object[] args = {this};
               registerQuitMethod.invoke(appleAppClass, args);
            }
            
            Class aboutClass = Class.forName("com.apple.mrj.MRJAboutHandler");           
            Class[] aboutArgs = {aboutClass};
            Method registerAboutMethod = appleAppClass.getDeclaredMethod("registerAboutHandler", aboutArgs);
            if (registerAboutMethod != null) {
               Object[] args = {this};
               registerAboutMethod.invoke(appleAppClass, args);
            }
         } catch (Exception e) {
            Logger.debug(e);
            ErrorReporter.showError(e, this); 
         }         
      }
   }
   
   private static final class SplashScreenTask implements Task, ProgressListener {
      int prog = 0;

      public int getTaskProgress() {
         return prog;
      }

      public void abortTask() { }

      public boolean canAbortTask() {
         return false;
      }

      public String getTaskDescription() {
         return "Starting "+TITLE+"...";
      }

      public void run() {
         ToolBox.sleep(1000);
         Datasources.initialize(this);  
         // We never want to check for updates on Debian
         /*
         if (UserManager.getUserManager().getCheckForUpdates()) {
            Thread t = new Thread( new Runnable() {
               public void run() {
                  CRONOMETER.getInstance().doCheckForUpdates();
               }
            });
            t.start();
         }*/
      }
      

      public void progressStart() {
         prog = 0;
      }

      public void progressFinish() {
         prog = 100;
      }

      public void progress(int percent) {
         prog = percent;
      }
   } 

   public void doCheckForUpdates() {      
      try {
         URL url = new URL("http://spaz.ca/cronometer/updates.xml");     
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document d = db.parse(url.openStream());
         Element e = d.getDocumentElement();
         int b = XMLNode.getInt(e, "build");
         if (b > BUILD) {
            final String www = e.getAttribute("url");
            SwingUtilities.invokeLater(new Runnable() {
               public void run() {
            	   int result = JOptionPane.showOptionDialog(CRONOMETER.getInstance(),
            			   "A new version of " + TITLE + " is available!",
                           "New Version",
                           JOptionPane.OK_CANCEL_OPTION,
                           JOptionPane.INFORMATION_MESSAGE,
                           null,
                           null,
                           null);
                  if (result == JOptionPane.OK_OPTION  && www != null) {
                     ToolBox.launchURL(CRONOMETER.mainFrame, www);
                  }                  
               }
            });
         }
      } catch (Exception ex) {
         Logger.error(ex);         
      }
   }

   public void handleAbout() {
      doAbout();
   }   

   public void lostOwnership(Clipboard clipboard, Transferable contents) {
      // unused interface
      // grey out paste menu?
   }

   /**
    * Some unspecified change has occurred in the underlying data, so refresh the current views.
    * For instance a food may have been edited and the search results and servings table need to
    * reflect the changes made.
    */
   public void refreshDisplays() {
      repaint(); 
      // TODO: replace this with a direct user servings listener model
      CRONOMETER.getInstance().getDailySummary().notifyObservers();
   }

   /**
    * Get the toolbar buttons to look and act consistent on both Windows and Mac
    * @param btn the button to modify
    */
   public static void fixButton(final JButton btn) {
      btn.setOpaque(false); 
      btn.setRolloverEnabled(true); 
      if (ToolBox.isMacOSX()) {
         btn.setBorderPainted(false);
         btn.setContentAreaFilled(false);
         btn.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
               if (btn.isEnabled()) {
                  btn.setContentAreaFilled(true);
                  btn.setOpaque(false);
                  btn.setBorderPainted(true);
               }
            } 
            public void mouseExited(MouseEvent e) {
               btn.setContentAreaFilled(false);
               btn.setBorderPainted(false);
            }
         });
      }
   }

   /**
    * Show window normally
    */
   public void restoreWindow() {
      setVisible(true);
      setExtendedState(Frame.NORMAL);
      toFront();
   }
   
   /**
    * Show the user a dialog with an Ok button.
    * @param message the message to display
    * @param title the title of the window
    */
   public static void okDialog(String message, String title) {
      JOptionPane.showMessageDialog(getInstance(), message, title, JOptionPane.OK_OPTION);
   }

   public static void main(String[] args) {
      try {
         if (!ToolBox.isMacOSX()) {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         } else {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
         }
      } catch (Exception e) {
         Logger.error("Error: setLookAndFeel() failed", e);
      }
      // The optional program argument sets the name of the subdirectory for user data.
      // This permits using a different subdirectory for development and testing 
      // (by setting the argument in your IDE's run configuration) than for
      // daily use of the application.
      if (args.length > 0) {
         UserManager.setSubdirectory(args[0]);
      }
      
      final CRONOMETER cron = CRONOMETER.getInstance();
      SplashScreen scr = new SplashScreen(new SplashScreenTask());
      mainFrame = scr;
      scr.setIconImage(CRONOMETER.getWindowIcon());
      scr.addTaskListener(cron);
      scr.start();      
   }
   
}