1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.dialogs; |
5 | |
|
6 | |
import java.awt.BorderLayout; |
7 | |
import java.awt.Component; |
8 | |
import java.awt.Dimension; |
9 | |
import java.awt.FlowLayout; |
10 | |
import java.awt.event.ActionEvent; |
11 | |
import java.awt.event.ActionListener; |
12 | |
import java.io.File; |
13 | |
import java.util.Date; |
14 | |
import java.util.logging.Level; |
15 | |
import java.util.logging.Logger; |
16 | |
|
17 | |
import javax.swing.DefaultListCellRenderer; |
18 | |
import javax.swing.JButton; |
19 | |
import javax.swing.JList; |
20 | |
import javax.swing.JOptionPane; |
21 | |
import javax.swing.JPanel; |
22 | |
import javax.swing.JScrollPane; |
23 | |
import javax.swing.event.ListSelectionEvent; |
24 | |
import javax.swing.event.ListSelectionListener; |
25 | |
|
26 | |
import org.homeunix.thecave.buddi.Const; |
27 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
28 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
29 | |
import org.homeunix.thecave.buddi.model.Document; |
30 | |
import org.homeunix.thecave.buddi.model.impl.ConcurrentSaveException; |
31 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
32 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
33 | |
import org.homeunix.thecave.buddi.model.swing.BackupManagerListModel; |
34 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
35 | |
import org.homeunix.thecave.buddi.util.Formatter; |
36 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
37 | |
import org.homeunix.thecave.buddi.util.OperationCancelledException; |
38 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
39 | |
import org.jdesktop.swingx.JXList; |
40 | |
import org.jdesktop.swingx.decorator.HighlighterFactory; |
41 | |
|
42 | |
import ca.digitalcave.moss.application.document.exception.DocumentLoadException; |
43 | |
import ca.digitalcave.moss.application.document.exception.DocumentSaveException; |
44 | |
import ca.digitalcave.moss.swing.MossDialog; |
45 | |
import ca.digitalcave.moss.swing.MossDocumentFrame; |
46 | |
import ca.digitalcave.moss.swing.exception.WindowOpenException; |
47 | |
|
48 | 0 | public class BackupManagerDialog extends MossDialog implements ActionListener { |
49 | |
public static final long serialVersionUID = 0; |
50 | |
|
51 | |
private final JXList backupList; |
52 | |
private final BackupManagerListModel model; |
53 | |
private final Document document; |
54 | |
private final JButton loadButton; |
55 | |
private final JButton cancelButton; |
56 | |
|
57 | |
public BackupManagerDialog(MossDocumentFrame owner) { |
58 | 0 | super(owner); |
59 | |
|
60 | 0 | document = (Document) owner.getDocument(); |
61 | 0 | model = new BackupManagerListModel(document); |
62 | |
|
63 | 0 | backupList = new JXList(model); |
64 | 0 | loadButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_LOAD)); |
65 | 0 | cancelButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_CANCEL)); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public void init() { |
70 | 0 | super.init(); |
71 | |
|
72 | 0 | cancelButton.setPreferredSize(InternalFormatter.getButtonSize(cancelButton)); |
73 | 0 | loadButton.setPreferredSize(InternalFormatter.getButtonSize(loadButton)); |
74 | |
|
75 | 0 | cancelButton.addActionListener(this); |
76 | 0 | loadButton.addActionListener(this); |
77 | |
|
78 | 0 | backupList.addListSelectionListener(new ListSelectionListener(){ |
79 | |
public void valueChanged(ListSelectionEvent e) { |
80 | 0 | updateButtons(); |
81 | 0 | } |
82 | |
}); |
83 | |
|
84 | 0 | backupList.addHighlighter(HighlighterFactory.createAlternateStriping(Const.COLOR_EVEN_ROW, Const.COLOR_ODD_ROW)); |
85 | 0 | backupList.setCellRenderer(new DefaultListCellRenderer(){ |
86 | |
public static final long serialVersionUID = 0; |
87 | |
|
88 | |
@Override |
89 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
90 | 0 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
91 | |
|
92 | 0 | if (value instanceof File){ |
93 | 0 | File f = (File) value; |
94 | 0 | Date date = model.getDate(f); |
95 | 0 | String formattedDate = Formatter.getDateFormat(PrefsModel.getInstance().getDateFormat() + " HH:mm:ss").format(date); |
96 | |
|
97 | 0 | this.setText(formattedDate + " (" + f.getName() + ")"); |
98 | |
} |
99 | |
|
100 | 0 | return this; |
101 | |
} |
102 | |
}); |
103 | |
|
104 | 0 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
105 | 0 | buttonPanel.add(cancelButton); |
106 | 0 | buttonPanel.add(loadButton); |
107 | |
|
108 | 0 | JScrollPane scroller = new JScrollPane(backupList); |
109 | 0 | scroller.setPreferredSize(new Dimension(350, 250)); |
110 | |
|
111 | 0 | this.setTitle(TextFormatter.getTranslation(BuddiKeys.BACKUP_MANAGER)); |
112 | 0 | this.setLayout(new BorderLayout()); |
113 | 0 | this.add(scroller, BorderLayout.CENTER); |
114 | 0 | this.add(buttonPanel, BorderLayout.SOUTH); |
115 | 0 | this.getRootPane().setDefaultButton(loadButton); |
116 | |
|
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
public void updateButtons() { |
121 | 0 | super.updateButtons(); |
122 | |
|
123 | 0 | loadButton.setEnabled(backupList.getSelectedIndex() != -1); |
124 | 0 | } |
125 | |
|
126 | |
public void actionPerformed(ActionEvent e) { |
127 | 0 | if (e.getSource().equals(cancelButton)){ |
128 | 0 | this.closeWindow(); |
129 | |
} |
130 | 0 | else if (e.getSource().equals(loadButton)){ |
131 | |
|
132 | 0 | String[] options = new String[2]; |
133 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_YES); |
134 | 0 | options[1] = TextFormatter.getTranslation(ButtonKeys.BUTTON_NO); |
135 | |
|
136 | 0 | if (JOptionPane.showOptionDialog( |
137 | |
null, |
138 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_RESTORE_FROM_BACKUP), |
139 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_RESTORE_FROM_BACKUP_TITLE), |
140 | |
JOptionPane.YES_NO_OPTION, |
141 | |
JOptionPane.WARNING_MESSAGE, |
142 | |
null, |
143 | |
options, |
144 | |
options[0] |
145 | |
) == 0) { |
146 | |
try { |
147 | |
try { |
148 | 0 | document.save(); |
149 | |
} |
150 | 0 | catch (ConcurrentSaveException cse){ |
151 | 0 | JOptionPane.showMessageDialog( |
152 | |
null, |
153 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_CONCURRENT_SAVE_EXCEPTION_TEXT), |
154 | |
TextFormatter.getTranslation(BuddiKeys.WARNING), |
155 | |
JOptionPane.WARNING_MESSAGE); |
156 | |
} |
157 | 0 | catch (DocumentSaveException dse){ |
158 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error saving data file prior to restore. Continuing with restore anyway.", dse); |
159 | 0 | } |
160 | |
|
161 | 0 | Document newDoc = ModelFactory.createDocument((File) backupList.getSelectedValue()); |
162 | 0 | newDoc.setFile(document.getFile()); |
163 | |
|
164 | 0 | MainFrame mainWndow = new MainFrame(newDoc); |
165 | 0 | mainWndow.openWindow( |
166 | |
PrefsModel.getInstance().getWindowSize(newDoc.getFile() + ""), |
167 | |
PrefsModel.getInstance().getWindowLocation(newDoc.getFile() + ""), |
168 | |
true); |
169 | |
|
170 | 0 | Logger.getLogger(this.getClass().getName()).info("User restored file " + ((File) backupList.getSelectedValue()).getName() + " to " + document.getFile().getAbsolutePath()); |
171 | 0 | closeWindow(); |
172 | |
} |
173 | 0 | catch (OperationCancelledException oce){} |
174 | 0 | catch (WindowOpenException woe){} |
175 | 0 | catch (DocumentLoadException dle){ |
176 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "There was an error loading the file " + document.getFile(), dle); |
177 | 0 | } |
178 | |
} |
179 | |
else { |
180 | 0 | Logger.getLogger(ModelFactory.class.getName()).info("User cancelled file restore."); |
181 | |
} |
182 | |
|
183 | |
} |
184 | 0 | } |
185 | |
} |