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.fantasia; |
24 |
|
25 |
import java.awt.Cursor; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Insets; |
28 |
|
29 |
import java.awt.event.MouseAdapter; |
30 |
import java.awt.event.MouseEvent; |
31 |
|
32 |
import javax.swing.Box; |
33 |
import javax.swing.BoxLayout; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.ListSelectionModel; |
36 |
|
37 |
import net.sf.juife.ComponentList; |
38 |
import net.sf.juife.ComponentListModel; |
39 |
import net.sf.juife.DefaultComponentListModel; |
40 |
|
41 |
import org.jsampler.CC; |
42 |
import org.jsampler.AudioDeviceModel; |
43 |
|
44 |
import org.jsampler.event.ListEvent; |
45 |
import org.jsampler.event.ListListener; |
46 |
|
47 |
import static org.jsampler.view.fantasia.A4n.a4n; |
48 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
49 |
|
50 |
/** |
51 |
* |
52 |
* @author Grigor Iliev |
53 |
*/ |
54 |
public class AudioDevicesPane extends JPanel { |
55 |
private final DeviceListPane devList = new DeviceListPane(); |
56 |
private final DefaultComponentListModel<AudioDevicePane> listModel = |
57 |
new DefaultComponentListModel<AudioDevicePane>(); |
58 |
|
59 |
/** Creates a new instance of <code>AudioDevicesPane</code> */ |
60 |
public |
61 |
AudioDevicesPane() { |
62 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
63 |
|
64 |
devList.setModel(listModel); |
65 |
devList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
66 |
devList.setAlignmentX(LEFT_ALIGNMENT); |
67 |
|
68 |
add(devList); |
69 |
add(new NewDevicePane()); |
70 |
|
71 |
CC.getSamplerModel().addAudioDeviceListListener(getHandler()); |
72 |
|
73 |
for(AudioDeviceModel m : CC.getSamplerModel().getAudioDevices()) { |
74 |
addDevice(m); |
75 |
} |
76 |
} |
77 |
|
78 |
private void |
79 |
addDevice(AudioDeviceModel model) { |
80 |
for(int i = 0; i < listModel.getSize(); i++) { |
81 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
82 |
CC.getLogger().warning("Audio device exist: " + model.getDeviceId()); |
83 |
return; |
84 |
} |
85 |
} |
86 |
|
87 |
listModel.add(new AudioDevicePane(model)); |
88 |
} |
89 |
|
90 |
private void |
91 |
removeDevice(AudioDeviceModel model) { |
92 |
for(int i = 0; i < listModel.getSize(); i++) { |
93 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
94 |
listModel.remove(i); |
95 |
return; |
96 |
} |
97 |
} |
98 |
|
99 |
CC.getLogger().warning("Audio device does not exist: " + model.getDeviceId()); |
100 |
} |
101 |
|
102 |
class DeviceListPane extends ComponentList { |
103 |
private Dimension maxSize = new Dimension(); |
104 |
|
105 |
public Dimension |
106 |
getMaximumSize() { |
107 |
maxSize.width = Short.MAX_VALUE; |
108 |
maxSize.height = getPreferredSize().height; |
109 |
return maxSize; |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
class NewDevicePane extends PixmapPane { |
115 |
private final PixmapButton btnNew = |
116 |
new PixmapButton(a4n.createAudioDevice, Res.gfxPowerOff18); |
117 |
|
118 |
NewDevicePane() { |
119 |
super(Res.gfxDeviceBg); |
120 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
121 |
|
122 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
123 |
add(Box.createRigidArea(new Dimension(3, 0))); |
124 |
add(btnNew); |
125 |
add(Box.createRigidArea(new Dimension(3, 0))); |
126 |
|
127 |
add(createVSeparator()); |
128 |
|
129 |
Dimension d = new Dimension(77, 24); |
130 |
setPreferredSize(d); |
131 |
setMinimumSize(d); |
132 |
setMaximumSize(new Dimension(Short.MAX_VALUE, 24)); |
133 |
|
134 |
btnNew.setPressedIcon(Res.gfxPowerOn18); |
135 |
|
136 |
addMouseListener(new MouseAdapter() { |
137 |
public void |
138 |
mouseClicked(MouseEvent e) { |
139 |
if(e.getButton() != e.BUTTON1) { |
140 |
return; |
141 |
} |
142 |
|
143 |
a4n.createAudioDevice.actionPerformed(null); |
144 |
} |
145 |
}); |
146 |
|
147 |
setAlignmentX(LEFT_ALIGNMENT); |
148 |
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
149 |
|
150 |
String s = i18n.getLabel("AudioDevicesPane.newDevice"); |
151 |
btnNew.setToolTipText(s); |
152 |
setToolTipText(s); |
153 |
} |
154 |
|
155 |
private JPanel |
156 |
createVSeparator() { |
157 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
158 |
p.setOpaque(false); |
159 |
p.setPreferredSize(new Dimension(2, 24)); |
160 |
p.setMinimumSize(p.getPreferredSize()); |
161 |
p.setMaximumSize(p.getPreferredSize()); |
162 |
return p; |
163 |
} |
164 |
} |
165 |
|
166 |
private final EventHandler eventHandler = new EventHandler(); |
167 |
|
168 |
private EventHandler |
169 |
getHandler() { return eventHandler; } |
170 |
|
171 |
private class EventHandler implements ListListener<AudioDeviceModel> { |
172 |
public void |
173 |
entryAdded(ListEvent<AudioDeviceModel> e) { |
174 |
addDevice(e.getEntry()); |
175 |
} |
176 |
|
177 |
public void |
178 |
entryRemoved(ListEvent<AudioDeviceModel> e) { |
179 |
removeDevice(e.getEntry()); |
180 |
} |
181 |
} |
182 |
} |