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