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.Dimension; |
26 |
import java.awt.Frame; |
27 |
|
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
|
31 |
import javax.swing.BorderFactory; |
32 |
import javax.swing.Box; |
33 |
import javax.swing.BoxLayout; |
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JDialog; |
36 |
import javax.swing.JLabel; |
37 |
import javax.swing.JPanel; |
38 |
import javax.swing.JProgressBar; |
39 |
import javax.swing.SwingUtilities; |
40 |
|
41 |
import org.jsampler.CC; |
42 |
import org.jsampler.HF; |
43 |
import org.jsampler.view.JSProgress; |
44 |
|
45 |
import net.sf.juife.JuifeUtils; |
46 |
|
47 |
import static org.jsampler.view.std.StdI18n.i18n; |
48 |
|
49 |
|
50 |
/** |
51 |
* |
52 |
* @author Grigor Iliev |
53 |
*/ |
54 |
public class JSProgressDlg extends JDialog implements JSProgress { |
55 |
private final JPanel mainPane = new JPanel(); |
56 |
private final JLabel l = new JLabel(" "); |
57 |
private JProgressBar pb = new JProgressBar(); |
58 |
private final JButton btnCancel = new JButton(i18n.getButtonLabel("cancel")); |
59 |
|
60 |
|
61 |
/** |
62 |
* Creates a new instance of JSProgressDlg |
63 |
*/ |
64 |
public |
65 |
JSProgressDlg() { |
66 |
super((Frame)null, "", true); |
67 |
|
68 |
pb.setIndeterminate(true); |
69 |
//pb.setStringPainted(true); |
70 |
|
71 |
l.setAlignmentX(CENTER_ALIGNMENT); |
72 |
pb.setAlignmentX(CENTER_ALIGNMENT); |
73 |
btnCancel.setAlignmentX(CENTER_ALIGNMENT); |
74 |
|
75 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
76 |
|
77 |
mainPane.add(l); |
78 |
mainPane.add(Box.createRigidArea(new Dimension(0, 6))); |
79 |
mainPane.add(pb); |
80 |
mainPane.add(Box.createRigidArea(new Dimension(0, 17))); |
81 |
mainPane.add(btnCancel); |
82 |
|
83 |
mainPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); |
84 |
add(mainPane); |
85 |
|
86 |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
87 |
|
88 |
btnCancel.addActionListener(new ActionListener() { |
89 |
public void |
90 |
actionPerformed(ActionEvent e) { onCancel(); } |
91 |
}); |
92 |
|
93 |
Dimension d = getPreferredSize(); |
94 |
d.width = d.width > 300 ? d.width : 300; |
95 |
setPreferredSize(d); |
96 |
|
97 |
pack(); |
98 |
setResizable(false); |
99 |
|
100 |
setLocation(JuifeUtils.centerLocation(this, CC.getMainFrame())); |
101 |
} |
102 |
|
103 |
private void |
104 |
onCancel() { |
105 |
int i = CC.getTaskQueue().getPendingTaskCount(); |
106 |
if(i > 0) { |
107 |
String s; |
108 |
if(i == 1) s = i18n.getMessage("JSProgressDlg.cancel?"); |
109 |
else s = i18n.getMessage("JSProgressDlg.cancel2?", i); |
110 |
if(!HF.showYesNoDialog(CC.getMainFrame(), s)) { |
111 |
CC.getTaskQueue().start(); |
112 |
return; |
113 |
} |
114 |
} |
115 |
|
116 |
CC.getTaskQueue().removePendingTasks(); |
117 |
net.sf.juife.Task t = CC.getTaskQueue().getRunningTask(); |
118 |
if(t != null) t.stop(); |
119 |
|
120 |
setVisible(false); |
121 |
} |
122 |
|
123 |
/** |
124 |
* Sets the progress string. |
125 |
* @param s The value of the progress string. |
126 |
*/ |
127 |
@Override |
128 |
public void |
129 |
setString(String s) { l.setText(s); } |
130 |
|
131 |
private void |
132 |
initProgressDlg() { |
133 |
pack(); |
134 |
Dimension d = getPreferredSize(); |
135 |
d.width = d.width > 300 ? d.width : 300; |
136 |
setSize(d); |
137 |
setResizable(false); |
138 |
|
139 |
setLocation(JuifeUtils.centerLocation(this, CC.getMainFrame())); |
140 |
} |
141 |
|
142 |
/** Starts to indicate that an operation is ongoing. */ |
143 |
@Override |
144 |
public void |
145 |
start() { |
146 |
setLocation(JuifeUtils.centerLocation(this, CC.getMainFrame())); |
147 |
|
148 |
SwingUtilities.invokeLater(new Runnable() { |
149 |
public void |
150 |
run() { |
151 |
initProgressDlg(); |
152 |
setVisible(true); |
153 |
} |
154 |
}); |
155 |
} |
156 |
|
157 |
/** Stops the indication that an operation is ongoing. */ |
158 |
@Override |
159 |
public void |
160 |
stop() { |
161 |
SwingUtilities.invokeLater(new Runnable() { |
162 |
public void |
163 |
run() { dispose(); } |
164 |
}); |
165 |
} |
166 |
} |