/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/PianoKeyboardPane.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/fantasia/PianoKeyboardPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2146 - (hide annotations) (download)
Mon Oct 11 09:31:27 2010 UTC (13 years, 6 months ago) by iliev
File size: 15303 byte(s)
* Fantasia: Migrated to substance 6.1
* Fantasia: Some minor GUI enhancements

1 iliev 1776 /*
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.fantasia;
24    
25 iliev 1778 import java.awt.AlphaComposite;
26     import java.awt.Color;
27     import java.awt.Composite;
28     import java.awt.Dimension;
29     import java.awt.GradientPaint;
30     import java.awt.Graphics;
31     import java.awt.Graphics2D;
32     import java.awt.GridBagConstraints;
33     import java.awt.GridBagLayout;
34 iliev 1776 import java.awt.Insets;
35 iliev 1778 import java.awt.Paint;
36     import java.awt.RenderingHints;
37 iliev 1776
38 iliev 1778 import java.awt.event.ActionEvent;
39     import java.awt.event.ActionListener;
40     import java.awt.event.FocusEvent;
41     import java.awt.event.FocusListener;
42     import java.awt.event.MouseEvent;
43     import java.awt.event.MouseAdapter;
44    
45     import java.awt.geom.Line2D;
46     import java.awt.geom.Rectangle2D;
47    
48 iliev 1776 import java.beans.PropertyChangeEvent;
49     import java.beans.PropertyChangeListener;
50    
51     import javax.swing.BorderFactory;
52 iliev 1778 import javax.swing.Box;
53     import javax.swing.BoxLayout;
54     import javax.swing.ImageIcon;
55     import javax.swing.JButton;
56     import javax.swing.JPanel;
57     import javax.swing.JToggleButton;
58 iliev 1776
59     import javax.swing.event.ListSelectionEvent;
60     import javax.swing.event.ListSelectionListener;
61    
62     import net.sf.juife.event.TaskEvent;
63     import net.sf.juife.event.TaskListener;
64    
65     import org.jsampler.CC;
66     import org.jsampler.SamplerChannelModel;
67    
68     import org.jsampler.event.SamplerChannelAdapter;
69     import org.jsampler.event.SamplerChannelEvent;
70     import org.jsampler.event.SamplerChannelListListener;
71     import org.jsampler.event.SamplerChannelListEvent;
72    
73     import org.jsampler.view.JSChannel;
74 iliev 1785 import org.jsampler.view.fantasia.basic.*;
75 iliev 1776 import org.jsampler.view.std.JSPianoRoll;
76    
77     import org.linuxsampler.lscp.Instrument;
78 iliev 1778 import org.linuxsampler.lscp.SamplerChannel;
79 iliev 1776
80     import org.linuxsampler.lscp.event.MidiDataEvent;
81     import org.linuxsampler.lscp.event.MidiDataListener;
82    
83 iliev 1778 import static javax.swing.Action.SMALL_ICON;
84 iliev 1776 import static org.jsampler.task.Global.GetFileInstrument;
85 iliev 1778 import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
86 iliev 1776
87     /**
88     *
89     * @author Grigor Iliev
90     */
91 iliev 1778 public class PianoKeyboardPane extends FantasiaPanel
92 iliev 1776 implements ListSelectionListener, SamplerChannelListListener {
93    
94 iliev 1778 protected final JToggleButton btnPower = new PowerButton();
95     private final FantasiaLabel lDisplay = new FantasiaLabel(" ", true);
96 iliev 1818 //private final PitchWheel pitchWheel = new PitchWheel();
97     //private final ModWheel modWheel = new ModWheel();
98 iliev 1776 private final JSPianoRoll pianoRoll = new JSPianoRoll();
99     private SamplerChannelModel channel = null;
100    
101     private String file = null;
102     private int index = -1;
103    
104     public
105     PianoKeyboardPane() {
106 iliev 1778 pianoRoll.actionDecreaseKeyNumber.putValue(SMALL_ICON, Res.gfxBtnDecrease);
107     pianoRoll.actionIncreaseKeyNumber.putValue(SMALL_ICON, Res.gfxBtnIncrease);
108     pianoRoll.actionScrollLeft.putValue(SMALL_ICON, Res.gfxBtnScrollLeft);
109     pianoRoll.actionScrollRight.putValue(SMALL_ICON, Res.gfxBtnScrollRight);
110    
111 iliev 1776 setOpaque(false);
112    
113 iliev 1778 GridBagLayout gridbag = new GridBagLayout();
114     GridBagConstraints c = new GridBagConstraints();
115 iliev 1776
116 iliev 1778 setLayout(gridbag);
117 iliev 1776
118 iliev 1778 setBorder(BorderFactory.createEmptyBorder(0, 3, 5, 3));
119    
120     c.fill = GridBagConstraints.NONE;
121    
122     c.gridx = 0;
123     c.gridy = 0;
124     c.anchor = GridBagConstraints.NORTHWEST;
125     c.insets = new Insets(3, 3, 3, 3);
126     gridbag.setConstraints(btnPower, c);
127     add(btnPower);
128    
129     JPanel p = createVSeparator();
130     c.gridx = 1;
131     c.gridy = 0;
132     c.gridheight = 2;
133     c.insets = new Insets(3, 0, 5, 3);
134     c.fill = GridBagConstraints.VERTICAL;
135     c.weighty = 1.0;
136     gridbag.setConstraints(p, c);
137     add(p);
138    
139 iliev 1818 /*c.gridx = 2;
140     c.gridy = 1;
141     c.gridheight = 1;
142     c.insets = new Insets(0, 3, 5, 3);
143     c.anchor = GridBagConstraints.CENTER;
144     gridbag.setConstraints(pitchWheel, c);
145     add(pitchWheel);
146    
147     c.gridx = 3;
148     c.gridy = 1;
149     c.insets = new Insets(0, 0, 5, 6);
150     gridbag.setConstraints(modWheel, c);
151     add(modWheel);*/
152    
153 iliev 1778 p = new KeyRangePropsPane();
154    
155     c.gridx = 2;
156     c.gridy = 0;
157 iliev 1818 c.gridwidth = 3;
158 iliev 1778 c.gridheight = 1;
159     c.insets = new Insets(0, 0, 0, 0);
160     c.fill = GridBagConstraints.NONE;
161     c.weighty = 0.0;
162     c.anchor = GridBagConstraints.WEST;
163     gridbag.setConstraints(p, c);
164     add(p);
165    
166     p = new KeyRangePropsPane();
167    
168 iliev 1818 c.gridx = 8;
169 iliev 1778 c.gridy = 0;
170 iliev 1818 c.gridwidth = 1;
171 iliev 1778 c.anchor = GridBagConstraints.EAST;
172     c.insets = new Insets(0, 0, 0, 10);
173     gridbag.setConstraints(p, c);
174     add(p);
175    
176     lDisplay.setPreferredSize(new Dimension(300, lDisplay.getPreferredSize().height));
177    
178 iliev 1818 c.gridx = 6;
179 iliev 1778 c.gridy = 0;
180     c.insets = new Insets(3, 3, 3, 12);
181     c.fill = GridBagConstraints.NONE;
182     c.weightx = 0.0;
183     gridbag.setConstraints(lDisplay, c);
184     add(lDisplay);
185    
186     p = new JPanel();
187     p.setOpaque(false);
188 iliev 1818 c.gridx = 5;
189 iliev 1778 c.gridy = 0;
190     c.insets = new Insets(0, 0, 0, 0);
191     c.fill = GridBagConstraints.HORIZONTAL;
192     c.weightx = 1.0;
193     gridbag.setConstraints(p, c);
194     add(p);
195    
196     p = new JPanel();
197     p.setOpaque(false);
198 iliev 1818 c.gridx = 7;
199 iliev 1778 c.gridy = 0;
200     gridbag.setConstraints(p, c);
201     add(p);
202    
203     pianoRoll.setFocusable(false);
204     pianoRoll.setBackground(new Color(0x2e2e2e));
205     //pianoRoll.setOpaque(false);
206    
207 iliev 1776 disablePianoRoll();
208    
209 iliev 1818 c.gridx = 4;
210 iliev 1778 c.gridy = 1;
211     c.gridwidth = 5;
212     c.insets = new Insets(0, 3, 0, 12);
213     c.fill = GridBagConstraints.BOTH;
214     c.weightx = 1.0;
215     c.weighty = 1.0;
216     gridbag.setConstraints(pianoRoll, c);
217     add(pianoRoll);
218    
219 iliev 1776 CC.getSamplerModel().addSamplerChannelListListener(this);
220     pianoRoll.addMidiDataListener(getHandler());
221    
222     updateKeyRange();
223    
224     PropertyChangeListener l = new PropertyChangeListener() {
225     public void
226     propertyChange(PropertyChangeEvent e) {
227     updateKeyRange();
228     }
229     };
230    
231     CC.preferences().addPropertyChangeListener("midiKeyboard.firstKey", l);
232     CC.preferences().addPropertyChangeListener("midiKeyboard.lastKey", l);
233 iliev 1778
234     addFocusListener(getHandler());
235    
236     MouseAdapter l2 = new MouseAdapter() {
237     public void
238     mouseClicked(MouseEvent e) { requestFocusInWindow(); }
239     };
240    
241     addKeyListener(pianoRoll.getKeyListener());
242     pianoRoll.registerKeys(this);
243    
244     addMouseListener(l2);
245     pianoRoll.addMouseListener(l2);
246     lDisplay.addMouseListener(l2);
247 iliev 1776 }
248    
249     public JSPianoRoll
250     getPianoRoll() { return pianoRoll; }
251    
252     private void
253     updateKeyRange() {
254     int firstKey = CC.preferences().getIntProperty("midiKeyboard.firstKey");
255     int lastKey = CC.preferences().getIntProperty("midiKeyboard.lastKey");
256     pianoRoll.setKeyRange(firstKey, lastKey);
257     }
258    
259     @Override public void
260     valueChanged(ListSelectionEvent e) {
261     if(e.getValueIsAdjusting()) return;
262    
263 iliev 1785 JSChannel[] chnS = CC.getMainFrame().getSelectedChannelsPane().getSelectedChannels();
264 iliev 1776 if(chnS == null || chnS.length == 0) {
265     disconnectChannel();
266     return;
267     }
268    
269     if(chnS[0].getModel() == channel) return;
270     disconnectChannel();
271     connectChannel(chnS[0].getModel());
272     }
273    
274     private void
275     updateInstrumentInfo() {
276     final GetFileInstrument i = new GetFileInstrument(file, index);
277    
278     i.addTaskListener(new TaskListener() {
279     public void
280     taskPerformed(TaskEvent e) {
281     if(i.doneWithErrors()) return;
282     updatePianoKeyboard(i.getResult());
283     }
284     });
285    
286     CC.getTaskQueue().add(i);
287     }
288    
289     private void
290     disablePianoRoll() {
291     pianoRoll.reset(true);
292     pianoRoll.setPlayingEnabled(false);
293     }
294    
295     private void
296     updatePianoKeyboard(Instrument instr) {
297     // The selected channel may have changed before this update
298     // due to concurency, so also checking whether file is null
299     if(instr == null || file == null) {
300     disablePianoRoll();
301     return;
302     }
303    
304     pianoRoll.setPlayingEnabled(true);
305    
306     pianoRoll.setShouldRepaint(false);
307     pianoRoll.reset(true);
308     pianoRoll.setDisabled(instr.getKeyMapping(), false);
309     pianoRoll.setKeyswitches(instr.getKeyswitchMapping(), true);
310     pianoRoll.setShouldRepaint(true);
311     pianoRoll.repaint();
312     }
313    
314     private void
315     connectChannel(SamplerChannelModel chn) {
316     channel = chn;
317     channel.addMidiDataListener(pianoRoll);
318     channel.addSamplerChannelListener(getHandler());
319    
320 iliev 1778 updateDisplay();
321    
322 iliev 1776 file = channel.getChannelInfo().getInstrumentFile();
323     if(file == null) {
324     disablePianoRoll();
325     return;
326     }
327     index = channel.getChannelInfo().getInstrumentIndex();
328     updateInstrumentInfo();
329 iliev 2146
330     //modWheel.SetChannel(channel);
331 iliev 1776 }
332    
333     private void
334     disconnectChannel() {
335     if(channel != null) {
336     channel.removeMidiDataListener(pianoRoll);
337     channel.removeSamplerChannelListener(getHandler());
338     channel = null;
339     file = null;
340     index = -1;
341     disablePianoRoll();
342     }
343 iliev 1778
344     updateDisplay();
345 iliev 2146 //modWheel.SetChannel(null);
346 iliev 1776 }
347    
348     @Override public void
349 iliev 1778 channelAdded(SamplerChannelListEvent e) {
350 iliev 1880 if(CC.getSamplerModel().getChannelListIsAdjusting()) return;
351 iliev 1778 updateDisplay();
352     }
353 iliev 1776
354     @Override public void
355     channelRemoved(SamplerChannelListEvent e) {
356     if(e.getChannelModel() == channel) {
357     disconnectChannel();
358     }
359 iliev 1778
360     updateDisplay();
361 iliev 1776 }
362    
363 iliev 1778 private JPanel
364     createVSeparator() {
365     PixmapPane p = new PixmapPane(Res.gfxVLine);
366     p.setAlignmentY(JPanel.TOP_ALIGNMENT);
367 iliev 1818 p.setMinimumSize(new Dimension(2, 30));
368 iliev 1778 p.setPreferredSize(new Dimension(2, 60));
369     p.setMaximumSize(new Dimension(2, Short.MAX_VALUE));
370     return p;
371     }
372    
373     private Color color1 = new Color(0x7a7a7a);
374     private Color color2 = new Color(0x5e5e5e);
375     private Color color3 = new Color(0x2e2e2e);
376    
377     @Override
378     public void
379     paintComponent(Graphics g) {
380     Graphics2D g2 = (Graphics2D)g;
381    
382     Paint oldPaint = g2.getPaint();
383     Composite oldComposite = g2.getComposite();
384 iliev 2146
385     FantasiaPainter.paintSurface1(this, g);
386 iliev 1778
387     double prX = pianoRoll.getLocation().getX();
388     double prY = pianoRoll.getLocation().getY();
389     drawInBorder (
390     g2, prX - 2, prY - 2,
391     prX + pianoRoll.getSize().getWidth() + 1, prY + pianoRoll.getSize().getHeight() - 3
392     );
393    
394     g2.setPaint(oldPaint);
395     g2.setComposite(oldComposite);
396     }
397    
398     private void
399     drawInBorder(Graphics2D g2, double x1, double y1, double x2, double y2) {
400     AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f);
401     g2.setComposite(ac);
402    
403     g2.setPaint(Color.WHITE);
404     Line2D.Double l = new Line2D.Double(x1, y2, x2, y2);
405     //g2.draw(l);
406    
407     g2.setComposite(ac.derive(0.255f));
408    
409     l = new Line2D.Double(x2 - 1, y1 + 1, x2 - 1, y2 + 1);
410     g2.draw(l);
411    
412     g2.setComposite(ac.derive(0.13f));
413    
414     l = new Line2D.Double(x2, y1, x2, y2);
415     g2.draw(l);
416    
417     g2.setComposite(ac.derive(0.20f));
418     g2.setPaint(Color.BLACK);
419    
420     l = new Line2D.Double(x1, y1, x2, y1);
421     g2.draw(l);
422    
423     g2.setComposite(ac.derive(0.40f));
424     g2.setPaint(Color.BLACK);
425    
426     l = new Line2D.Double(x1 + 1, y1 + 1, x2 - 1, y1 + 1);
427     g2.draw(l);
428    
429     g2.setComposite(ac.derive(0.20f));
430    
431     l = new Line2D.Double(x1, y1, x1, y2);
432     g2.draw(l);
433    
434     g2.setComposite(ac.derive(0.40f));
435    
436     l = new Line2D.Double(x1 + 1, y1 + 1, x1 + 1, y2 + 1);
437     g2.draw(l);
438     }
439    
440     private void
441     updateDisplay() {
442     // TODO: called too often?
443     if(channel == null) {
444     lDisplay.setText(i18n.getLabel("PianoKeyboardPane.noChannel"));
445     return;
446     }
447    
448     SamplerChannel sc = channel.getChannelInfo();
449 iliev 1785 String s = CC.getMainFrame().getChannelPath(channel);
450 iliev 1778
451     StringBuffer sb = new StringBuffer();
452 iliev 1785 sb.append(i18n.getLabel("PianoKeyboardPane.channel", s)).append(" - ");
453 iliev 1778
454     int status = sc.getInstrumentStatus();
455     if(status >= 0 && status < 100) {
456     sb.append(i18n.getLabel("ChannelScreen.loadingInstrument", status));
457     } else if(status == -1) {
458     sb.append(i18n.getLabel("PianoKeyboardPane.noInstrument"));
459     } else if(status < -1) {
460     sb.append(i18n.getLabel("ChannelScreen.errorLoadingInstrument"));
461     } else {
462     if(sc.getInstrumentName() != null) sb.append(sc.getInstrumentName());
463     }
464    
465     lDisplay.setText(sb.toString());
466     }
467    
468    
469     class KeyRangePropsPane extends JPanel {
470     private final JButton btnIncrease =
471     new PixmapButton(pianoRoll.actionIncreaseKeyNumber, Res.gfxBtnIncrease);
472    
473     private final JButton btnDecrease =
474     new PixmapButton(pianoRoll.actionDecreaseKeyNumber, Res.gfxBtnDecrease);
475    
476     private final JButton btnScrollLeft =
477     new PixmapButton(pianoRoll.actionScrollLeft, Res.gfxBtnScrollLeft);
478    
479     private final JButton btnScrollRight =
480     new PixmapButton(pianoRoll.actionScrollRight, Res.gfxBtnScrollRight);
481    
482     KeyRangePropsPane() {
483     setOpaque(false);
484     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
485    
486     btnIncrease.setFocusable(false);
487     btnDecrease.setFocusable(false);
488     btnScrollLeft.setFocusable(false);
489     btnScrollRight.setFocusable(false);
490    
491    
492     btnDecrease.setPressedIcon(Res.gfxBtnDecreaseRO);
493     add(btnDecrease);
494    
495     btnIncrease.setPressedIcon(Res.gfxBtnIncreaseRO);
496     add(btnIncrease);
497    
498     add(Box.createRigidArea(new Dimension(6, 0)));
499    
500     btnScrollLeft.setPressedIcon(Res.gfxBtnScrollLeftRO);
501     add(btnScrollLeft);
502    
503     btnScrollRight.setPressedIcon(Res.gfxBtnScrollRightRO);
504     add(btnScrollRight);
505     }
506     }
507    
508    
509     public static class PowerButton extends PixmapToggleButton implements ActionListener {
510     PowerButton() {
511     this(Res.gfxPowerOff, Res.gfxPowerOn);
512     }
513    
514     PowerButton(ImageIcon defaultIcon, ImageIcon selectedIcon) {
515     super(defaultIcon, selectedIcon);
516    
517     setSelected(true);
518     addActionListener(this);
519     }
520    
521     @Override
522     public void
523     actionPerformed(ActionEvent e) {
524     boolean b = isSelected();
525     MainFrame frm = (MainFrame)CC.getMainFrame();
526     if(frm == null) return;
527     frm.setMidiKeyboardVisible(b);
528     }
529    
530     @Override
531     public boolean
532     contains(int x, int y) { return (x - 11)*(x - 11) + (y - 11)*(y - 11) < 71; }
533     }
534    
535 iliev 1776 private final Handler handler = new Handler();
536    
537     private Handler
538     getHandler() { return handler; }
539    
540 iliev 1778 private class Handler extends SamplerChannelAdapter implements FocusListener, MidiDataListener {
541     @Override
542 iliev 1776 public void
543     midiDataArrived(MidiDataEvent e) {
544     if(channel == null) return;
545     channel.sendBackendMidiData(e);
546     }
547    
548 iliev 1778 @Override
549 iliev 1776 public void
550     channelChanged(SamplerChannelEvent e) {
551 iliev 1778 updateDisplay();
552    
553 iliev 1776 String newFile = channel.getChannelInfo().getInstrumentFile();
554     int newIndex = channel.getChannelInfo().getInstrumentIndex();
555    
556     if(channel.getChannelInfo().getInstrumentStatus() != 100) {
557     //don't use disablePianoRoll because of unnecessary repainting
558     pianoRoll.setAllKeysPressed(false);
559     pianoRoll.removeAllKeyswitches();
560     pianoRoll.setAllKeysDisabled(true);
561     pianoRoll.setPlayingEnabled(false);
562     return;
563     }
564    
565     if(newFile == null) {
566     if(file != null) disablePianoRoll();
567     file = null;
568     index = -1;
569     return;
570     }
571    
572     if(newFile.equals(file) && newIndex == index) return;
573    
574     file = newFile;
575     index = newIndex;
576     updateInstrumentInfo();
577     }
578 iliev 1778
579     @Override
580     public void
581     focusGained(FocusEvent e) {
582    
583     }
584    
585     @Override
586     public void
587     focusLost(FocusEvent e) {
588    
589     }
590 iliev 1776 }
591     }

  ViewVC Help
Powered by ViewVC