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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (hide annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 5 months ago) by iliev
File size: 5559 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

1 iliev 1308 /*
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     package org.jsampler.view.std;
23    
24 iliev 1496 import java.awt.Desktop;
25    
26 iliev 1540 import java.awt.event.ActionEvent;
27     import java.awt.event.ActionListener;
28     import java.awt.event.MouseAdapter;
29     import java.awt.event.MouseEvent;
30    
31     import java.beans.PropertyChangeEvent;
32     import java.beans.PropertyChangeListener;
33    
34 iliev 1496 import java.net.URI;
35    
36 iliev 1540 import java.text.NumberFormat;
37    
38 iliev 1308 import java.util.Vector;
39    
40 iliev 1540 import javax.swing.JComboBox;
41     import javax.swing.JSlider;
42     import javax.swing.JToolTip;
43     import javax.swing.Popup;
44     import javax.swing.PopupFactory;
45    
46     import javax.swing.event.ChangeEvent;
47     import javax.swing.event.ChangeListener;
48 iliev 1308 import org.jsampler.CC;
49 iliev 1496 import org.jsampler.HF;
50 iliev 1308 import org.jsampler.JSPrefs;
51    
52 iliev 1496 import static org.jsampler.view.std.StdI18n.i18n;
53 iliev 1540 import static org.jsampler.view.std.StdPrefs.*;
54 iliev 1308
55 iliev 1496
56 iliev 1308 /**
57     *
58     * @author Grigor Iliev
59     */
60     public class StdUtils {
61    
62     /** Forbids the instantiation of this class */
63     private
64     StdUtils() { }
65    
66     private static JSPrefs
67     preferences() { return CC.getViewConfig().preferences(); }
68    
69 iliev 1540 public static JComboBox
70     createPathComboBox() {
71     final JComboBox cb = new JComboBox();
72     cb.setEditable(true);
73     cb.addActionListener(new ActionListener() {
74     public void
75     actionPerformed(ActionEvent e) {
76     if(cb.getSelectedItem() == null) {
77     cb.setToolTipText(null);
78     return;
79     }
80     String s = cb.getSelectedItem().toString();
81     if(s.length() < 15) cb.setToolTipText(null);
82     else cb.setToolTipText(s);
83     }
84     });
85    
86     return cb;
87     }
88    
89 iliev 1308 /**
90     * Updates the specified string list property by adding the specified
91     * element on the top. Also restricts the maximum number of elements to 12.
92     */
93     public static void
94     updateRecentElements(String property, String newElement) {
95     String[] elements = preferences().getStringListProperty(property);
96     Vector<String> v = new Vector<String>();
97     v.add(newElement);
98     for(String s : elements) {
99     if(!newElement.equals(s)) v.add(s);
100     }
101     if(v.size() > 12) v.setSize(12);
102    
103     elements = v.toArray(new String[v.size()]);
104     preferences().setStringListProperty(property, elements);
105     }
106 iliev 1496
107     public static boolean
108     checkDesktopSupported() {
109     if(Desktop.isDesktopSupported()) return true;
110    
111     String s = i18n.getError("StdUtils.DesktopApiNotSupported");
112     HF.showErrorMessage(s, CC.getMainFrame());
113    
114     return false;
115     }
116    
117     public static void
118     browse(String uri) {
119     if(!checkDesktopSupported()) return;
120    
121     try { Desktop.getDesktop().browse(new URI(uri)); }
122     catch(Exception x) { x.printStackTrace(); }
123     }
124    
125     public static void
126     mail(String uri) {
127     if(!StdUtils.checkDesktopSupported()) return;
128    
129     Desktop desktop = Desktop.getDesktop();
130    
131     try { Desktop.getDesktop().mail(new URI(uri)); }
132     catch(Exception x) { x.printStackTrace(); }
133     }
134 iliev 1540
135     public static JSlider
136     createVolumeSlider() {
137     return new VolumeSlider();
138     }
139    
140     private static class VolumeSlider extends JSlider {
141     private Popup popup = null;
142     private final JToolTip tip = new JToolTip();
143     private static NumberFormat numberFormat = NumberFormat.getInstance();
144    
145     VolumeSlider() {
146     super(0, 100, 100);
147     numberFormat.setMaximumFractionDigits(1);
148     // Setting the tooltip size
149     tip.setTipText(i18n.getLabel("StdUtils.volume", 10000));
150     tip.setMinimumSize(tip.getPreferredSize());
151     tip.setPreferredSize(tip.getPreferredSize()); // workaround for preserving that size
152     tip.setComponent(this);
153     tip.setTipText(i18n.getLabel("StdUtils.volume", 0));
154     ///////
155    
156     updateVolumeInfo();
157    
158     addMouseListener(new MouseAdapter() {
159     public void
160     mousePressed(MouseEvent e) {
161     if(popup != null) {
162     popup.hide();
163     popup = null;
164     }
165    
166     if(!VolumeSlider.this.isEnabled()) return;
167    
168     java.awt.Point p = VolumeSlider.this.getLocationOnScreen();
169     PopupFactory pf = PopupFactory.getSharedInstance();
170     popup = pf.getPopup(VolumeSlider.this, tip, p.x, p.y - 22);
171     popup.show();
172     }
173    
174     public void
175     mouseReleased(MouseEvent e) {
176     if(popup != null) {
177     popup.hide();
178     popup = null;
179     }
180     }
181     });
182    
183     addChangeListener(new ChangeListener() {
184     public void
185     stateChanged(ChangeEvent e) { updateVolumeInfo(); }
186     });
187    
188     String s = VOL_MEASUREMENT_UNIT_DECIBEL;
189     preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
190     public void
191     propertyChange(PropertyChangeEvent e) {
192     updateVolumeInfo();
193     }
194     });
195     }
196    
197     private void
198     updateVolumeInfo() {
199     String s;
200     if(CC.getViewConfig().isMeasurementUnitDecibel()) {
201     double d = HF.percentsToDecibels(getValue());
202     s = i18n.getLabel("StdUtils.volumeDecibels", numberFormat.format(d));
203     } else {
204     s = i18n.getLabel("StdUtils.volume", getValue());
205     }
206    
207     setToolTipText(s);
208     tip.setTipText(s);
209     tip.repaint();
210     }
211     }
212 iliev 1308 }

  ViewVC Help
Powered by ViewVC