1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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 |
|
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
|
32 |
import javax.swing.BorderFactory; |
33 |
import javax.swing.Box; |
34 |
import javax.swing.BoxLayout; |
35 |
import javax.swing.JButton; |
36 |
import javax.swing.JDialog; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JProgressBar; |
39 |
import javax.swing.SwingUtilities; |
40 |
|
41 |
import net.sf.juife.JuifeUtils; |
42 |
|
43 |
import net.sf.juife.event.TaskEvent; |
44 |
import net.sf.juife.event.TaskListener; |
45 |
|
46 |
import org.jsampler.CC; |
47 |
import org.jsampler.HF; |
48 |
import org.jsampler.task.InstrumentsDb.GetScanJobInfo; |
49 |
|
50 |
import org.linuxsampler.lscp.ScanJobInfo; |
51 |
|
52 |
import org.linuxsampler.lscp.event.InstrumentsDbAdapter; |
53 |
import org.linuxsampler.lscp.event.InstrumentsDbEvent; |
54 |
|
55 |
import static org.jsampler.view.std.StdI18n.i18n; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Grigor Iliev |
60 |
*/ |
61 |
public class JSAddDbInstrumentsProgressDlg extends JDialog { |
62 |
private final JProgressBar progressJobStatus = new JProgressBar(0, 100); |
63 |
private final JProgressBar progressFileStatus = new JProgressBar(0, 100); |
64 |
private final JButton btnHide = |
65 |
new JButton(i18n.getButtonLabel("JSAddDbInstrumentsProgressDlg.btnHide")); |
66 |
|
67 |
private final int jobId; |
68 |
|
69 |
/** |
70 |
* Creates a new instance of <code>JSAddDbInstrumentsProgressDlg</code> |
71 |
*/ |
72 |
public |
73 |
JSAddDbInstrumentsProgressDlg(Frame owner, int jobId) { |
74 |
super(owner, i18n.getLabel("JSAddDbInstrumentsProgressDlg.title"), true); |
75 |
this.jobId = jobId; |
76 |
|
77 |
initAddDbInstrumentsProgressDlg(); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Creates a new instance of <code>JSAddDbInstrumentsProgressDlg</code> |
82 |
*/ |
83 |
public |
84 |
JSAddDbInstrumentsProgressDlg(Dialog owner, int jobId) { |
85 |
super(owner, i18n.getLabel("JSAddDbInstrumentsProgressDlg.title"), true); |
86 |
this.jobId = jobId; |
87 |
|
88 |
initAddDbInstrumentsProgressDlg(); |
89 |
} |
90 |
|
91 |
private void |
92 |
initAddDbInstrumentsProgressDlg() { |
93 |
JPanel p = new JPanel(); |
94 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
95 |
progressJobStatus.setAlignmentX(CENTER_ALIGNMENT); |
96 |
p.add(progressJobStatus); |
97 |
p.add(Box.createRigidArea(new Dimension(0, 6))); |
98 |
progressFileStatus.setAlignmentX(CENTER_ALIGNMENT); |
99 |
p.add(progressFileStatus); |
100 |
p.add(Box.createRigidArea(new Dimension(0, 6))); |
101 |
btnHide.setAlignmentX(CENTER_ALIGNMENT); |
102 |
p.add(btnHide); |
103 |
|
104 |
progressJobStatus.setStringPainted(true); |
105 |
progressFileStatus.setStringPainted(true); |
106 |
|
107 |
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
108 |
|
109 |
add(p); |
110 |
|
111 |
Dimension d = p.getPreferredSize(); |
112 |
p.setPreferredSize(new Dimension(400, d.height)); |
113 |
|
114 |
pack(); |
115 |
setMinimumSize(getPreferredSize()); |
116 |
setLocation(JuifeUtils.centerLocation(this, getOwner())); |
117 |
|
118 |
btnHide.addActionListener(new ActionListener() { |
119 |
public void |
120 |
actionPerformed(ActionEvent e) { |
121 |
// TODO: vvv this should be done out of the event-dispatching thread |
122 |
CC.getClient().removeInstrumentsDbListener(getHandler()); |
123 |
////// |
124 |
setVisible(false); |
125 |
} |
126 |
}); |
127 |
|
128 |
// TODO: vvv this should be done out of the event-dispatching thread |
129 |
CC.getClient().addInstrumentsDbListener(getHandler()); |
130 |
////// |
131 |
} |
132 |
|
133 |
public void |
134 |
updateStatus() { |
135 |
final GetScanJobInfo t = new GetScanJobInfo(jobId); |
136 |
|
137 |
t.addTaskListener(new TaskListener() { |
138 |
public void |
139 |
taskPerformed(TaskEvent e) { |
140 |
if(t.doneWithErrors()) { |
141 |
failed(); |
142 |
return; |
143 |
} |
144 |
|
145 |
updateStatus(t.getResult()); |
146 |
} |
147 |
}); |
148 |
|
149 |
CC.scheduleTask(t); |
150 |
} |
151 |
|
152 |
private void |
153 |
updateStatus(ScanJobInfo info) { |
154 |
if(info.isFinished()) { |
155 |
// TODO: vvv this should be done out of the event-dispatching thread |
156 |
CC.getClient().removeInstrumentsDbListener(getHandler()); |
157 |
////// |
158 |
|
159 |
if(info.status < 0) { |
160 |
failed(); |
161 |
return; |
162 |
} |
163 |
|
164 |
progressJobStatus.setValue(progressJobStatus.getMaximum()); |
165 |
progressFileStatus.setValue(progressFileStatus.getMaximum()); |
166 |
|
167 |
setVisible(false); |
168 |
getOwner().setVisible(false); |
169 |
return; |
170 |
} |
171 |
|
172 |
if(progressJobStatus.getMaximum() != info.filesTotal * 100) { |
173 |
progressJobStatus.setMaximum(info.filesTotal * 100); |
174 |
} |
175 |
|
176 |
String s = i18n.getMessage ( |
177 |
"JSAddDbInstrumentsProgressDlg.jobStatus", info.filesScanned, info.filesTotal |
178 |
); |
179 |
progressJobStatus.setString(s); |
180 |
|
181 |
progressFileStatus.setValue(info.status); |
182 |
progressFileStatus.setString(info.scanning); |
183 |
|
184 |
progressJobStatus.setValue((info.filesScanned * 100) + info.status); |
185 |
} |
186 |
|
187 |
private void |
188 |
failed() { |
189 |
HF.showErrorMessage(i18n.getMessage("JSAddDbInstrumentsProgressDlg.failed"), this); |
190 |
setVisible(false); |
191 |
} |
192 |
|
193 |
private final EventHandler eventHandler = new EventHandler(); |
194 |
|
195 |
private EventHandler |
196 |
getHandler() { return eventHandler; } |
197 |
|
198 |
private class EventHandler extends InstrumentsDbAdapter { |
199 |
/** Invoked when the status of particular job has changed. */ |
200 |
public void |
201 |
jobStatusChanged(InstrumentsDbEvent e) { |
202 |
if(e.getJobId() != jobId) return; |
203 |
|
204 |
SwingUtilities.invokeLater(new Runnable() { |
205 |
public void |
206 |
run() { updateStatus(); } |
207 |
}); |
208 |
} |
209 |
} |
210 |
} |