1 //
2 // Neuros Database Manipulator
3 // Copyright (C) 2003 Neuros Database Manipulator
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // For information about Neuros Database Manipulator and its authors,
20 // please contact the Neuros Database Manipulator Web Site at
21 // http://neurosdbm.sourceforge.net
22 //
23 //
24
25 package net.sourceforge.neurosdbm.actions;
26
27
28 import javax.swing.JOptionPane;
29 import net.sourceforge.neurosdbm.MainFrame;
30 import net.sourceforge.neurosdbm.ActionLock;
31 import net.sourceforge.neurosdbm.ProgressMonitorListener;
32 import net.sourceforge.neurosdbm.db.Database;
33 import net.sourceforge.neurosdbm.db.Util;
34
35
36 public final class Synchronize extends BaseAction {
37
38 private Database database;
39
40 public Synchronize(MainFrame parent, Database database) {
41 super(parent, "Synchronize");
42 setToolTip("Synchronize to the Neuros");
43 setIcon("sync.gif");
44 setAcceleratorKey("alt S");
45 this.database = database;
46 }
47
48 void action() {
49 boolean success = true;
50 String exceptionMessage = null;
51 long timeDifference = 0;
52 final ProgressMonitorListener progress =
53 new ProgressMonitorListener(parent, "Sychronization Progress",
54 "Synchronization Progress");
55 progress.setMillisToDecideToPopup(1);
56 SynchronizeThread syncThread = new SynchronizeThread(progress);
57 syncThread.start();
58 }
59
60 class SynchronizeThread extends Thread {
61 final ProgressMonitorListener progress;
62 Database.SyncStats stats;
63
64 SynchronizeThread(ProgressMonitorListener progress) {
65 this.progress = progress;
66 }
67
68 public void run() {
69 ActionLock.lock();
70 stats = database.synchronize(progress);
71 progress.setProgress(progress.getMaximum());
72 complete();
73 }
74
75 void complete() {
76 if (stats.success) {
77 String successMessage = "Synchronization successful\n";
78 successMessage += "Synchronization Time: ";
79 successMessage += Util.timeDisplay(stats.time/1000);
80 JOptionPane.showMessageDialog(parent, successMessage,
81 "Synchronization Success",
82 JOptionPane.INFORMATION_MESSAGE);
83 } else {
84 JOptionPane.showMessageDialog(parent, stats.exceptionMessage,
85 "Synchronization Failed",
86 JOptionPane.ERROR_MESSAGE);
87 }
88 ActionLock.unlock();
89 parent.dataChanged();
90 }
91 }
92 }