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