/[svn]/jsampler/trunk/src/org/jsampler/view/std/JSAddDbInstrumentsProgressDlg.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/std/JSAddDbInstrumentsProgressDlg.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (hide annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 6 months ago) by iliev
File size: 5925 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 2288 * Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1286 *
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 iliev 2288 import net.sf.juife.swing.JuifeUtils;
42 iliev 1286
43     import net.sf.juife.event.TaskEvent;
44     import net.sf.juife.event.TaskListener;
45    
46     import org.jsampler.CC;
47     import org.jsampler.task.InstrumentsDb.GetScanJobInfo;
48 iliev 2288 import org.jsampler.view.swing.SHF;
49 iliev 1286
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 iliev 1729 private boolean finished = false;
69 iliev 1286
70     /**
71     * Creates a new instance of <code>JSAddDbInstrumentsProgressDlg</code>
72     */
73     public
74     JSAddDbInstrumentsProgressDlg(Frame owner, int jobId) {
75 iliev 1729 super(owner, i18n.getLabel("JSAddDbInstrumentsProgressDlg.title"));
76 iliev 1286 this.jobId = jobId;
77    
78     initAddDbInstrumentsProgressDlg();
79     }
80    
81     /**
82     * Creates a new instance of <code>JSAddDbInstrumentsProgressDlg</code>
83     */
84     public
85     JSAddDbInstrumentsProgressDlg(Dialog owner, int jobId) {
86 iliev 1729 super(owner, i18n.getLabel("JSAddDbInstrumentsProgressDlg.title"));
87 iliev 1286 this.jobId = jobId;
88    
89     initAddDbInstrumentsProgressDlg();
90     }
91    
92     private void
93     initAddDbInstrumentsProgressDlg() {
94     JPanel p = new JPanel();
95     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
96     progressJobStatus.setAlignmentX(CENTER_ALIGNMENT);
97     p.add(progressJobStatus);
98     p.add(Box.createRigidArea(new Dimension(0, 6)));
99     progressFileStatus.setAlignmentX(CENTER_ALIGNMENT);
100     p.add(progressFileStatus);
101     p.add(Box.createRigidArea(new Dimension(0, 6)));
102     btnHide.setAlignmentX(CENTER_ALIGNMENT);
103     p.add(btnHide);
104    
105     progressJobStatus.setStringPainted(true);
106     progressFileStatus.setStringPainted(true);
107    
108     p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
109    
110     add(p);
111    
112     Dimension d = p.getPreferredSize();
113     p.setPreferredSize(new Dimension(400, d.height));
114    
115     pack();
116     setMinimumSize(getPreferredSize());
117     setLocation(JuifeUtils.centerLocation(this, getOwner()));
118    
119     btnHide.addActionListener(new ActionListener() {
120     public void
121     actionPerformed(ActionEvent e) {
122     // TODO: vvv this should be done out of the event-dispatching thread
123     CC.getClient().removeInstrumentsDbListener(getHandler());
124     //////
125     setVisible(false);
126     }
127     });
128    
129     // TODO: vvv this should be done out of the event-dispatching thread
130     CC.getClient().addInstrumentsDbListener(getHandler());
131     //////
132     }
133    
134     public void
135     updateStatus() {
136     final GetScanJobInfo t = new GetScanJobInfo(jobId);
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 iliev 1729 finished = true;
156    
157 iliev 1286 // TODO: vvv this should be done out of the event-dispatching thread
158     CC.getClient().removeInstrumentsDbListener(getHandler());
159     //////
160    
161     if(info.status < 0) {
162     failed();
163     return;
164     }
165    
166     progressJobStatus.setValue(progressJobStatus.getMaximum());
167     progressFileStatus.setValue(progressFileStatus.getMaximum());
168    
169 iliev 1729 dispose();
170 iliev 1286 getOwner().setVisible(false);
171     return;
172     }
173    
174     if(progressJobStatus.getMaximum() != info.filesTotal * 100) {
175     progressJobStatus.setMaximum(info.filesTotal * 100);
176     }
177    
178     String s = i18n.getMessage (
179     "JSAddDbInstrumentsProgressDlg.jobStatus", info.filesScanned, info.filesTotal
180     );
181     progressJobStatus.setString(s);
182    
183     progressFileStatus.setValue(info.status);
184     progressFileStatus.setString(info.scanning);
185    
186     progressJobStatus.setValue((info.filesScanned * 100) + info.status);
187     }
188    
189     private void
190     failed() {
191 iliev 1729 SwingUtilities.invokeLater(new Runnable() {
192     public void
193     run() { failed0(); }
194     });
195     }
196    
197     private void
198     failed0() {
199 iliev 2288 SHF.showErrorMessage(i18n.getMessage("JSAddDbInstrumentsProgressDlg.failed"), this);
200 iliev 1286 setVisible(false);
201     }
202    
203     private final EventHandler eventHandler = new EventHandler();
204    
205     private EventHandler
206     getHandler() { return eventHandler; }
207    
208     private class EventHandler extends InstrumentsDbAdapter {
209     /** Invoked when the status of particular job has changed. */
210     public void
211     jobStatusChanged(InstrumentsDbEvent e) {
212     if(e.getJobId() != jobId) return;
213     SwingUtilities.invokeLater(new Runnable() {
214     public void
215     run() { updateStatus(); }
216     });
217     }
218     }
219     }

  ViewVC Help
Powered by ViewVC