/[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 1916 - (hide annotations) (download)
Fri Jun 12 01:22:41 2009 UTC (14 years, 10 months ago) by iliev
File size: 12129 byte(s)
* added support for exporting the MIDI instrument maps
  as Rosegarden device file

1 iliev 1308 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 1871 * Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1308 *
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 iliev 1871 import java.awt.Dialog;
26     import java.awt.FileDialog;
27     import java.awt.Frame;
28 iliev 1786 import java.awt.Rectangle;
29 iliev 1871 import java.awt.Window;
30 iliev 1496
31 iliev 1540 import java.awt.event.ActionEvent;
32     import java.awt.event.ActionListener;
33     import java.awt.event.MouseAdapter;
34     import java.awt.event.MouseEvent;
35    
36     import java.beans.PropertyChangeEvent;
37     import java.beans.PropertyChangeListener;
38    
39 iliev 1871 import java.io.File;
40 iliev 1496 import java.net.URI;
41    
42 iliev 1540 import java.text.NumberFormat;
43    
44 iliev 1308 import java.util.Vector;
45 iliev 1786 import java.util.logging.Level;
46 iliev 1308
47 iliev 1540 import javax.swing.JComboBox;
48 iliev 1871 import javax.swing.JFileChooser;
49 iliev 1540 import javax.swing.JSlider;
50     import javax.swing.JToolTip;
51     import javax.swing.Popup;
52     import javax.swing.PopupFactory;
53    
54     import javax.swing.event.ChangeEvent;
55     import javax.swing.event.ChangeListener;
56 iliev 1308 import org.jsampler.CC;
57 iliev 1496 import org.jsampler.HF;
58 iliev 1308 import org.jsampler.JSPrefs;
59    
60 iliev 1871 import org.jsampler.view.JSFileFilter;
61 iliev 1496 import static org.jsampler.view.std.StdI18n.i18n;
62 iliev 1540 import static org.jsampler.view.std.StdPrefs.*;
63 iliev 1308
64 iliev 1496
65 iliev 1308 /**
66     *
67     * @author Grigor Iliev
68     */
69     public class StdUtils {
70    
71     /** Forbids the instantiation of this class */
72     private
73     StdUtils() { }
74    
75     private static JSPrefs
76     preferences() { return CC.getViewConfig().preferences(); }
77 iliev 1871
78 iliev 1540 public static JComboBox
79 iliev 1871 createEnhancedComboBox() {
80 iliev 1540 final JComboBox cb = new JComboBox();
81     cb.addActionListener(new ActionListener() {
82     public void
83     actionPerformed(ActionEvent e) {
84     if(cb.getSelectedItem() == null) {
85     cb.setToolTipText(null);
86     return;
87     }
88     String s = cb.getSelectedItem().toString();
89     if(s.length() < 15) cb.setToolTipText(null);
90     else cb.setToolTipText(s);
91     }
92     });
93 iliev 1871
94 iliev 1540 return cb;
95     }
96    
97 iliev 1871 public static JComboBox
98     createPathComboBox() {
99     JComboBox cb = createEnhancedComboBox();
100     cb.setEditable(true);
101     return cb;
102     }
103    
104 iliev 1308 /**
105     * Updates the specified string list property by adding the specified
106     * element on the top. Also restricts the maximum number of elements to 12.
107     */
108     public static void
109     updateRecentElements(String property, String newElement) {
110     String[] elements = preferences().getStringListProperty(property);
111     Vector<String> v = new Vector<String>();
112     v.add(newElement);
113     for(String s : elements) {
114     if(!newElement.equals(s)) v.add(s);
115     }
116     if(v.size() > 12) v.setSize(12);
117    
118     elements = v.toArray(new String[v.size()]);
119     preferences().setStringListProperty(property, elements);
120     }
121 iliev 1496
122     public static boolean
123     checkDesktopSupported() {
124     if(Desktop.isDesktopSupported()) return true;
125    
126     String s = i18n.getError("StdUtils.DesktopApiNotSupported");
127     HF.showErrorMessage(s, CC.getMainFrame());
128    
129     return false;
130     }
131    
132     public static void
133     browse(String uri) {
134     if(!checkDesktopSupported()) return;
135    
136     try { Desktop.getDesktop().browse(new URI(uri)); }
137     catch(Exception x) { x.printStackTrace(); }
138     }
139    
140     public static void
141     mail(String uri) {
142     if(!StdUtils.checkDesktopSupported()) return;
143    
144     Desktop desktop = Desktop.getDesktop();
145    
146     try { Desktop.getDesktop().mail(new URI(uri)); }
147     catch(Exception x) { x.printStackTrace(); }
148     }
149 iliev 1540
150 iliev 1786 /**
151     * Gets the windows bounds from the preferences for the specified window.
152     * @return The windows bounds saved in the preferences for the specified window
153     * or <code>null</code>.
154     */
155     public static Rectangle
156     getWindowBounds(String windowName) {
157     String s = windowName + ".windowSizeAndLocation";
158     s = CC.preferences().getStringProperty(s, null);
159     if(s == null) return null;
160    
161     try {
162     int i = s.indexOf(',');
163     int x = Integer.parseInt(s.substring(0, i));
164    
165     s = s.substring(i + 1);
166     i = s.indexOf(',');
167     int y = Integer.parseInt(s.substring(0, i));
168    
169     s = s.substring(i + 1);
170     i = s.indexOf(',');
171     int width = Integer.parseInt(s.substring(0, i));
172    
173     s = s.substring(i + 1);
174     int height = Integer.parseInt(s);
175    
176     return new Rectangle(x, y, width, height);
177     } catch(Exception x) {
178     String msg = windowName;
179     msg += ": Parsing of window size and location string failed";
180     CC.getLogger().log(Level.INFO, msg, x);
181     return null;
182     }
183     }
184    
185     /**
186     * Saves the windows bounds in the preferences for the specified window.
187     */
188     public static void
189     saveWindowBounds(String windowName, Rectangle r) {
190 iliev 1865 if(r.width < 50 || r.height < 50 || r.x < r.width * -1 || r.y < 0) {
191     CC.getLogger().warning("Invalid window size or location");
192     return;
193     }
194    
195 iliev 1786 StringBuffer sb = new StringBuffer();
196     sb.append(r.x).append(',').append(r.y).append(',');
197     sb.append(r.width).append(',').append(r.height);
198     String s = windowName + ".windowSizeAndLocation";
199     CC.preferences().setStringProperty(s, sb.toString());
200     }
201 iliev 1871
202     public static File
203     showOpenLscpFileChooser() {
204     return showLscpFileChooser(true);
205     }
206    
207     public static File
208     showOpenLscpFileChooser(Window owner) {
209     return showLscpFileChooser(true, owner);
210     }
211    
212     public static File
213     showSaveLscpFileChooser() {
214     return showLscpFileChooser(false);
215     }
216    
217     public static File
218     showSaveLscpFileChooser(Window owner) {
219     return showLscpFileChooser(false, owner);
220     }
221    
222     private static File
223     showLscpFileChooser(boolean openDialog) {
224     return showLscpFileChooser(openDialog, CC.getMainFrame());
225     }
226    
227     private static File
228     showLscpFileChooser(boolean openDialog, Window owner) {
229     return showFileChooser (
230 iliev 1915 openDialog, owner, false, new JSFileFilter.Lscp(), "lastScriptLocation"
231 iliev 1871 );
232     }
233    
234     public static File
235 iliev 1915 showSaveMidiMapsChooser() {
236     JSFileFilter filter = new JSFileFilter.MidiMaps();
237    
238     JSFileFilter[] filters = {
239 iliev 1916 new JSFileFilter.Lscp(), new JSFileFilter.Text(), new JSFileFilter.Html(),
240     new JSFileFilter.Rgd()
241 iliev 1915 };
242    
243     return showFileChooser (
244     false, CC.getMainFrame(), false, filter, filters, "lastScriptLocation"
245     );
246     }
247    
248     public static File
249 iliev 1871 showOpenInstrumentFileChooser(Window owner) {
250     return showFileChooser(true, owner, false, null, "lastInstrumentLocation");
251     }
252    
253     public static File
254     showOpenDirectoryChooser(Window owner, String locationProperty) {
255     return showFileChooser(true, owner, true, null, locationProperty);
256     }
257    
258     private static File
259     showFileChooser (
260     boolean openDialog,
261     Window owner,
262     boolean dirChooser,
263     JSFileFilter filter,
264     String locationProperty
265     ) {
266 iliev 1915 JSFileFilter[] filters = (filter == null) ? new JSFileFilter[0] : new JSFileFilter[1];
267     if(filter != null) filters[0] = filter;
268    
269     return showFileChooser(openDialog, owner, dirChooser, filter, filters, locationProperty);
270     }
271    
272     private static File
273     showFileChooser (
274     boolean openDialog,
275     Window owner,
276     boolean dirChooser,
277     JSFileFilter filter,
278     JSFileFilter[] choosableFilters,
279     String locationProperty
280     ) {
281 iliev 1871 boolean nativeFileChooser = preferences().getBoolProperty("nativeFileChoosers");
282     String oldPath = null;
283     if(locationProperty != null) {
284     oldPath = preferences().getStringProperty(locationProperty);
285     }
286     File f = null;
287     if(nativeFileChooser && CC.isMacOS()) {
288     if(dirChooser) {
289     System.setProperty("apple.awt.fileDialogForDirectories", "true");
290     }
291     FileDialog dlg;
292     if(owner instanceof Frame) dlg = new FileDialog((Frame)owner);
293     else if(owner instanceof Dialog) dlg = new FileDialog((Dialog)owner);
294     else dlg = new FileDialog(CC.getMainFrame());
295     dlg.setDirectory(oldPath);
296     dlg.setMode(openDialog ? FileDialog.LOAD : FileDialog.SAVE);
297     if(filter != null) dlg.setFilenameFilter(filter);
298     dlg.setVisible(true);
299     if(dirChooser) {
300     System.setProperty("apple.awt.fileDialogForDirectories", "false");
301     }
302     if(dlg.getFile() != null) {
303     f = new File(new File(dlg.getDirectory()), dlg.getFile());
304     }
305     } else {
306     JFileChooser fc = new JFileChooser(oldPath);
307 iliev 1915 for(JSFileFilter ff : choosableFilters) {
308     fc.addChoosableFileFilter(ff);
309     }
310     if(choosableFilters.length > 0) fc.setFileFilter(choosableFilters[0]);
311    
312 iliev 1871 if(dirChooser) fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
313     int result;
314     if(openDialog) result = fc.showOpenDialog(owner);
315     else result = fc.showSaveDialog(owner);
316 iliev 1915 if(result == JFileChooser.APPROVE_OPTION) {
317     f = fc.getSelectedFile();
318     }
319    
320     if(result == JFileChooser.APPROVE_OPTION && !openDialog) {
321     Object o = fc.getFileFilter();
322     for(JSFileFilter ff : choosableFilters) {
323     if(ff == o) {
324     String fn = f.getName().toLowerCase();
325     String ext = ff.getExtension().toLowerCase();
326     if(fn.endsWith(ext)) break;
327    
328     fn = f.getAbsolutePath() + ff.getExtension();
329     f = new File(fn);
330     break;
331     }
332     }
333     }
334 iliev 1871 }
335    
336     if(f == null) return null;
337     String path = f.getParent();
338     if(path != null && locationProperty != null) {
339     preferences().setStringProperty(locationProperty, path);
340     }
341     return f;
342     }
343 iliev 1786
344 iliev 1540 public static JSlider
345     createVolumeSlider() {
346     return new VolumeSlider();
347     }
348    
349     private static class VolumeSlider extends JSlider {
350     private Popup popup = null;
351     private final JToolTip tip = new JToolTip();
352     private static NumberFormat numberFormat = NumberFormat.getInstance();
353    
354     VolumeSlider() {
355     super(0, 100, 100);
356     numberFormat.setMaximumFractionDigits(1);
357 iliev 1567 // Setting the tooltip size (workaround for preserving that size)
358     boolean b = CC.getViewConfig().isMeasurementUnitDecibel();
359     if(b) tip.setTipText(i18n.getLabel("StdUtils.volumeDecibels", "-30.0"));
360     else tip.setTipText(i18n.getLabel("StdUtils.volume", "100"));
361     tip.setPreferredSize(tip.getPreferredSize());
362 iliev 1540 tip.setMinimumSize(tip.getPreferredSize());
363 iliev 1567 ///////
364 iliev 1540 tip.setComponent(this);
365     tip.setTipText(i18n.getLabel("StdUtils.volume", 0));
366    
367     updateVolumeInfo();
368    
369     addMouseListener(new MouseAdapter() {
370     public void
371     mousePressed(MouseEvent e) {
372     if(popup != null) {
373     popup.hide();
374     popup = null;
375     }
376    
377     if(!VolumeSlider.this.isEnabled()) return;
378    
379     java.awt.Point p = VolumeSlider.this.getLocationOnScreen();
380     PopupFactory pf = PopupFactory.getSharedInstance();
381     popup = pf.getPopup(VolumeSlider.this, tip, p.x, p.y - 22);
382     popup.show();
383     }
384    
385     public void
386     mouseReleased(MouseEvent e) {
387     if(popup != null) {
388     popup.hide();
389     popup = null;
390     }
391     }
392     });
393    
394     addChangeListener(new ChangeListener() {
395     public void
396     stateChanged(ChangeEvent e) { updateVolumeInfo(); }
397     });
398    
399     String s = VOL_MEASUREMENT_UNIT_DECIBEL;
400     preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
401     public void
402     propertyChange(PropertyChangeEvent e) {
403 iliev 1567 // We use this to set the size of the lVolume
404     // to prevent the frequent resizing of lVolume component
405     boolean b = CC.getViewConfig().isMeasurementUnitDecibel();
406     tip.setPreferredSize(null);
407     String s;
408     if(b) s = i18n.getLabel("StdUtils.volumeDecibels", "-30.0");
409     else s = i18n.getLabel("StdUtils.volume", "100");
410     tip.setTipText(s);
411     tip.setPreferredSize(tip.getPreferredSize());
412     tip.setMinimumSize(tip.getPreferredSize());
413     ///////
414 iliev 1540 updateVolumeInfo();
415     }
416     });
417     }
418    
419     private void
420     updateVolumeInfo() {
421     String s;
422     if(CC.getViewConfig().isMeasurementUnitDecibel()) {
423     double d = HF.percentsToDecibels(getValue());
424     s = i18n.getLabel("StdUtils.volumeDecibels", numberFormat.format(d));
425     } else {
426     s = i18n.getLabel("StdUtils.volume", getValue());
427     }
428    
429     setToolTipText(s);
430     tip.setTipText(s);
431     tip.repaint();
432     }
433     }
434 iliev 1308 }

  ViewVC Help
Powered by ViewVC