1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.std; |
24 |
|
25 |
import java.awt.Dialog; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Frame; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
|
32 |
import java.awt.event.ActionEvent; |
33 |
import java.awt.event.ActionListener; |
34 |
|
35 |
import java.io.File; |
36 |
|
37 |
import javax.swing.BorderFactory; |
38 |
import javax.swing.Box; |
39 |
import javax.swing.BoxLayout; |
40 |
import javax.swing.Icon; |
41 |
import javax.swing.JButton; |
42 |
import javax.swing.JLabel; |
43 |
import javax.swing.JPanel; |
44 |
import javax.swing.JScrollPane; |
45 |
import javax.swing.JTextArea; |
46 |
import javax.swing.JTextField; |
47 |
import javax.swing.ListSelectionModel; |
48 |
|
49 |
import javax.swing.event.ChangeEvent; |
50 |
import javax.swing.event.ChangeListener; |
51 |
import javax.swing.event.DocumentEvent; |
52 |
import javax.swing.event.DocumentListener; |
53 |
import javax.swing.event.ListSelectionEvent; |
54 |
import javax.swing.event.ListSelectionListener; |
55 |
|
56 |
import net.sf.juife.InformationDialog; |
57 |
import net.sf.juife.Task; |
58 |
|
59 |
import org.jsampler.CC; |
60 |
import org.jsampler.task.InstrumentsDb.SetInstrumentFilePath; |
61 |
import org.jsampler.view.LostFilesTable; |
62 |
|
63 |
import static org.jsampler.view.std.StdI18n.i18n; |
64 |
|
65 |
/** |
66 |
* |
67 |
* @author Grigor Iliev |
68 |
*/ |
69 |
public class JSLostFilesDlg extends InformationDialog { |
70 |
private final LostFilesTable lostFilesTable = new LostFilesTable(); |
71 |
|
72 |
private final JButton btnReplace = new JButton(i18n.getButtonLabel("JSLostFilesDlg.btnReplace")); |
73 |
private final JButton btnRename = new JButton(i18n.getButtonLabel("rename")); |
74 |
private final JButton btnUpdate = new JButton(i18n.getButtonLabel("update")); |
75 |
|
76 |
/** Creates a new instance of <code>JSLostFilesDlg</code> */ |
77 |
public |
78 |
JSLostFilesDlg(Frame owner) { |
79 |
super(owner, i18n.getLabel("JSLostFilesDlg.title")); |
80 |
|
81 |
JPanel mainPane = new JPanel(); |
82 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
83 |
|
84 |
JScrollPane sp = new JScrollPane(lostFilesTable); |
85 |
sp.setPreferredSize(new Dimension(500, 250)); |
86 |
mainPane.add(sp); |
87 |
|
88 |
JPanel p = new JPanel(); |
89 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
90 |
p.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); |
91 |
btnRename.setEnabled(false); |
92 |
p.add(btnRename); |
93 |
p.add(Box.createRigidArea(new Dimension(5, 0))); |
94 |
p.add(btnReplace); |
95 |
p.add(Box.createGlue()); |
96 |
p.add(btnUpdate); |
97 |
|
98 |
mainPane.add(p); |
99 |
|
100 |
CC.getLostFilesModel().addChangeListener(getHandler()); |
101 |
CC.getLostFilesModel().update(); |
102 |
setMainPane(mainPane); |
103 |
|
104 |
lostFilesTable.getSelectionModel().addListSelectionListener(getHandler()); |
105 |
|
106 |
btnRename.addActionListener(new ActionListener() { |
107 |
public void |
108 |
actionPerformed(ActionEvent e) { lostFilesTable.editSelectedFile(); } |
109 |
}); |
110 |
|
111 |
btnReplace.addActionListener(new ActionListener() { |
112 |
public void |
113 |
actionPerformed(ActionEvent e) { |
114 |
new JSReplaceLostFilesDlg(JSLostFilesDlg.this).setVisible(true); |
115 |
} |
116 |
}); |
117 |
|
118 |
btnUpdate.addActionListener(new ActionListener() { |
119 |
public void |
120 |
actionPerformed(ActionEvent e) { CC.getLostFilesModel().update(); } |
121 |
}); |
122 |
} |
123 |
|
124 |
|
125 |
private final EventHandler eventHandler = new EventHandler(); |
126 |
|
127 |
private EventHandler |
128 |
getHandler() { return eventHandler; } |
129 |
|
130 |
private class EventHandler implements ListSelectionListener, ChangeListener { |
131 |
@Override |
132 |
public void |
133 |
valueChanged(ListSelectionEvent e) { |
134 |
if(e.getValueIsAdjusting()) return; |
135 |
|
136 |
ListSelectionModel lsm = (ListSelectionModel)e.getSource(); |
137 |
btnRename.setEnabled(!lsm.isSelectionEmpty()); |
138 |
} |
139 |
|
140 |
@Override |
141 |
public void |
142 |
stateChanged(ChangeEvent e) { |
143 |
boolean b = CC.getLostFilesModel().getLostFileCount() != 0; |
144 |
btnReplace.setEnabled(b); |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
class JSReplaceLostFilesDlg extends InformationDialog { |
150 |
private final JLabel lFind = new JLabel(i18n.getLabel("JSReplaceLostFilesDlg.lFind")); |
151 |
private final JLabel lReplace = new JLabel(i18n.getLabel("JSReplaceLostFilesDlg.lReplace")); |
152 |
|
153 |
private final JTextField tfFind = new JTextField(); |
154 |
private final JTextField tfReplace = new JTextField(); |
155 |
|
156 |
private final JButton btnBrowse; |
157 |
private final JButton btnBrowse2; |
158 |
|
159 |
private final JButton btnPreview = |
160 |
new JButton(i18n.getButtonLabel("JSReplaceLostFilesDlg.btnPreview")); |
161 |
|
162 |
private final JButton btnReplace = |
163 |
new JButton(i18n.getButtonLabel("JSReplaceLostFilesDlg.btnReplace")); |
164 |
|
165 |
private final JButton btnCancel = new JButton(i18n.getButtonLabel("cancel")); |
166 |
|
167 |
JSReplaceLostFilesDlg(Dialog owner) { |
168 |
super(owner, i18n.getLabel("JSReplaceLostFilesDlg.title")); |
169 |
showCloseButton(false); |
170 |
|
171 |
GridBagLayout gridbag = new GridBagLayout(); |
172 |
GridBagConstraints c = new GridBagConstraints(); |
173 |
|
174 |
Icon iconBrowse = CC.getViewConfig().getInstrumentsDbTreeView().getOpenIcon(); |
175 |
btnBrowse = new JButton(iconBrowse); |
176 |
btnBrowse2 = new JButton(iconBrowse); |
177 |
|
178 |
btnBrowse.setMargin(new Insets(0, 0, 0, 0)); |
179 |
btnBrowse2.setMargin(new Insets(0, 0, 0, 0)); |
180 |
|
181 |
if(!CC.getCurrentServer().isLocal()) { |
182 |
btnBrowse.setEnabled(false); |
183 |
btnBrowse2.setEnabled(false); |
184 |
} |
185 |
|
186 |
JPanel p = new JPanel(); |
187 |
p.setLayout(gridbag); |
188 |
|
189 |
c.fill = GridBagConstraints.NONE; |
190 |
|
191 |
c.gridx = 0; |
192 |
c.gridy = 0; |
193 |
c.anchor = GridBagConstraints.EAST; |
194 |
c.insets = new Insets(3, 3, 3, 3); |
195 |
gridbag.setConstraints(lFind, c); |
196 |
p.add(lFind); |
197 |
|
198 |
c.gridx = 0; |
199 |
c.gridy = 1; |
200 |
gridbag.setConstraints(lReplace, c); |
201 |
p.add(lReplace); |
202 |
|
203 |
c.gridx = 2; |
204 |
c.gridy = 0; |
205 |
gridbag.setConstraints(btnBrowse, c); |
206 |
p.add(btnBrowse); |
207 |
|
208 |
c.gridx = 2; |
209 |
c.gridy = 1; |
210 |
gridbag.setConstraints(btnBrowse2, c); |
211 |
p.add(btnBrowse2); |
212 |
|
213 |
c.fill = GridBagConstraints.HORIZONTAL; |
214 |
c.gridx = 1; |
215 |
c.gridy = 0; |
216 |
c.weightx = 1.0; |
217 |
c.anchor = GridBagConstraints.WEST; |
218 |
gridbag.setConstraints(tfFind, c); |
219 |
p.add(tfFind); |
220 |
|
221 |
c.gridx = 1; |
222 |
c.gridy = 1; |
223 |
gridbag.setConstraints(tfReplace, c); |
224 |
p.add(tfReplace); |
225 |
|
226 |
JPanel p2 = new JPanel(); |
227 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
228 |
p2.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); |
229 |
|
230 |
btnPreview.setEnabled(false); |
231 |
btnReplace.setEnabled(false); |
232 |
p2.add(btnPreview); |
233 |
p2.add(Box.createRigidArea(new Dimension(6, 0))); |
234 |
p2.add(btnReplace); |
235 |
p2.add(Box.createGlue()); |
236 |
p2.add(Box.createRigidArea(new Dimension(17, 0))); |
237 |
p2.add(btnCancel); |
238 |
|
239 |
c.gridx = 1; |
240 |
c.gridy = 2; |
241 |
c.gridwidth = 2; |
242 |
gridbag.setConstraints(p2, c); |
243 |
p.add(p2); |
244 |
|
245 |
p.setPreferredSize(new Dimension(360, p.getPreferredSize().height)); |
246 |
|
247 |
setMainPane(p); |
248 |
|
249 |
btnBrowse.addActionListener(new ActionListener() { |
250 |
public void |
251 |
actionPerformed(ActionEvent e) { |
252 |
String s = browse(); |
253 |
if(s != null) tfFind.setText(s); |
254 |
} |
255 |
}); |
256 |
|
257 |
btnBrowse2.addActionListener(new ActionListener() { |
258 |
public void |
259 |
actionPerformed(ActionEvent e) { |
260 |
String s = browse(); |
261 |
if(s != null) tfReplace.setText(s); |
262 |
} |
263 |
}); |
264 |
|
265 |
btnPreview.addActionListener(new ActionListener() { |
266 |
public void |
267 |
actionPerformed(ActionEvent e) { onPreview(); } |
268 |
}); |
269 |
|
270 |
btnReplace.addActionListener(new ActionListener() { |
271 |
public void |
272 |
actionPerformed(ActionEvent e) { onReplace(); } |
273 |
}); |
274 |
|
275 |
btnCancel.addActionListener(new ActionListener() { |
276 |
public void |
277 |
actionPerformed(ActionEvent e) { onCancel(); } |
278 |
}); |
279 |
|
280 |
tfFind.getDocument().addDocumentListener(getHandler()); |
281 |
tfReplace.getDocument().addDocumentListener(getHandler()); |
282 |
} |
283 |
|
284 |
private void |
285 |
onPreview() { |
286 |
String f = tfFind.getText(); |
287 |
String r = tfReplace.getText(); |
288 |
StringBuffer sb = new StringBuffer(); |
289 |
|
290 |
for(int i = 0; i < CC.getLostFilesModel().getLostFileCount(); i++) { |
291 |
String s = CC.getLostFilesModel().getLostFile(i); |
292 |
if(!s.startsWith(f)) continue; |
293 |
|
294 |
sb.append(s).append(" -> "); |
295 |
sb.append(r).append(s.substring(f.length())).append("\n"); |
296 |
} |
297 |
|
298 |
JSPreviewLostFilesDlg dlg = new JSPreviewLostFilesDlg(this, sb.toString()); |
299 |
dlg.setVisible(true); |
300 |
} |
301 |
|
302 |
private void |
303 |
onReplace() { |
304 |
String f = tfFind.getText(); |
305 |
String r = tfReplace.getText(); |
306 |
|
307 |
for(int i = 0; i < CC.getLostFilesModel().getLostFileCount(); i++) { |
308 |
String s = CC.getLostFilesModel().getLostFile(i); |
309 |
if(!s.startsWith(f)) continue; |
310 |
|
311 |
String s2 = r + s.substring(f.length()); |
312 |
Task t = new SetInstrumentFilePath(s, s2); |
313 |
CC.getTaskQueue().add(t); |
314 |
} |
315 |
|
316 |
CC.getLostFilesModel().update(); |
317 |
|
318 |
onCancel(); |
319 |
} |
320 |
|
321 |
private void |
322 |
updateState() { |
323 |
boolean b = tfFind.getText().length() != 0 && tfReplace.getText().length() != 0; |
324 |
btnPreview.setEnabled(b); |
325 |
btnReplace.setEnabled(b); |
326 |
} |
327 |
|
328 |
private String |
329 |
browse() { |
330 |
File f = StdUtils.showOpenDirectoryChooser(this, null); |
331 |
if(f == null) return null; |
332 |
|
333 |
String path = f.getAbsolutePath(); |
334 |
if(path.length() < 2) return path; |
335 |
|
336 |
char sep = java.io.File.separatorChar; |
337 |
if(path.charAt(path.length() - 1) != sep) path += sep; |
338 |
|
339 |
return path; |
340 |
} |
341 |
|
342 |
private final EventHandler eventHandler = new EventHandler(); |
343 |
|
344 |
private EventHandler |
345 |
getHandler() { return eventHandler; } |
346 |
|
347 |
private class EventHandler implements DocumentListener { |
348 |
// DocumentListener |
349 |
@Override |
350 |
public void |
351 |
insertUpdate(DocumentEvent e) { updateState(); } |
352 |
|
353 |
@Override |
354 |
public void |
355 |
removeUpdate(DocumentEvent e) { updateState(); } |
356 |
|
357 |
@Override |
358 |
public void |
359 |
changedUpdate(DocumentEvent e) { updateState(); } |
360 |
} |
361 |
} |
362 |
|
363 |
class JSPreviewLostFilesDlg extends InformationDialog { |
364 |
JSPreviewLostFilesDlg(Dialog owner, String text) { |
365 |
super(owner, i18n.getLabel("JSPreviewLostFilesDlg.title")); |
366 |
JTextArea ta = new JTextArea(); |
367 |
ta.setText(text); |
368 |
ta.setEditable(false); |
369 |
JScrollPane sp = new JScrollPane(ta); |
370 |
sp.setPreferredSize(new Dimension(600, 200)); |
371 |
setMainPane(sp); |
372 |
} |
373 |
} |