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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1915 - (show annotations) (download)
Thu Jun 11 09:35:29 2009 UTC (14 years, 10 months ago) by iliev
File size: 12102 byte(s)
* added support for exporting the MIDI instrument maps
  as text file or web page

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2009 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 import java.awt.Desktop;
25 import java.awt.Dialog;
26 import java.awt.FileDialog;
27 import java.awt.Frame;
28 import java.awt.Rectangle;
29 import java.awt.Window;
30
31 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 import java.io.File;
40 import java.net.URI;
41
42 import java.text.NumberFormat;
43
44 import java.util.Vector;
45 import java.util.logging.Level;
46
47 import javax.swing.JComboBox;
48 import javax.swing.JFileChooser;
49 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 import org.jsampler.CC;
57 import org.jsampler.HF;
58 import org.jsampler.JSPrefs;
59
60 import org.jsampler.view.JSFileFilter;
61 import static org.jsampler.view.std.StdI18n.i18n;
62 import static org.jsampler.view.std.StdPrefs.*;
63
64
65 /**
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
78 public static JComboBox
79 createEnhancedComboBox() {
80 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
94 return cb;
95 }
96
97 public static JComboBox
98 createPathComboBox() {
99 JComboBox cb = createEnhancedComboBox();
100 cb.setEditable(true);
101 return cb;
102 }
103
104 /**
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
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
150 /**
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 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 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
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 openDialog, owner, false, new JSFileFilter.Lscp(), "lastScriptLocation"
231 );
232 }
233
234 public static File
235 showSaveMidiMapsChooser() {
236 JSFileFilter filter = new JSFileFilter.MidiMaps();
237
238 JSFileFilter[] filters = {
239 new JSFileFilter.Lscp(), new JSFileFilter.Text(), new JSFileFilter.Html()
240 };
241
242 return showFileChooser (
243 false, CC.getMainFrame(), false, filter, filters, "lastScriptLocation"
244 );
245 }
246
247 public static File
248 showOpenInstrumentFileChooser(Window owner) {
249 return showFileChooser(true, owner, false, null, "lastInstrumentLocation");
250 }
251
252 public static File
253 showOpenDirectoryChooser(Window owner, String locationProperty) {
254 return showFileChooser(true, owner, true, null, locationProperty);
255 }
256
257 private static File
258 showFileChooser (
259 boolean openDialog,
260 Window owner,
261 boolean dirChooser,
262 JSFileFilter filter,
263 String locationProperty
264 ) {
265 JSFileFilter[] filters = (filter == null) ? new JSFileFilter[0] : new JSFileFilter[1];
266 if(filter != null) filters[0] = filter;
267
268 return showFileChooser(openDialog, owner, dirChooser, filter, filters, locationProperty);
269 }
270
271 private static File
272 showFileChooser (
273 boolean openDialog,
274 Window owner,
275 boolean dirChooser,
276 JSFileFilter filter,
277 JSFileFilter[] choosableFilters,
278 String locationProperty
279 ) {
280 boolean nativeFileChooser = preferences().getBoolProperty("nativeFileChoosers");
281 String oldPath = null;
282 if(locationProperty != null) {
283 oldPath = preferences().getStringProperty(locationProperty);
284 }
285 File f = null;
286 if(nativeFileChooser && CC.isMacOS()) {
287 if(dirChooser) {
288 System.setProperty("apple.awt.fileDialogForDirectories", "true");
289 }
290 FileDialog dlg;
291 if(owner instanceof Frame) dlg = new FileDialog((Frame)owner);
292 else if(owner instanceof Dialog) dlg = new FileDialog((Dialog)owner);
293 else dlg = new FileDialog(CC.getMainFrame());
294 dlg.setDirectory(oldPath);
295 dlg.setMode(openDialog ? FileDialog.LOAD : FileDialog.SAVE);
296 if(filter != null) dlg.setFilenameFilter(filter);
297 dlg.setVisible(true);
298 if(dirChooser) {
299 System.setProperty("apple.awt.fileDialogForDirectories", "false");
300 }
301 if(dlg.getFile() != null) {
302 f = new File(new File(dlg.getDirectory()), dlg.getFile());
303 }
304 } else {
305 JFileChooser fc = new JFileChooser(oldPath);
306 for(JSFileFilter ff : choosableFilters) {
307 fc.addChoosableFileFilter(ff);
308 }
309 if(choosableFilters.length > 0) fc.setFileFilter(choosableFilters[0]);
310
311 if(dirChooser) fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
312 int result;
313 if(openDialog) result = fc.showOpenDialog(owner);
314 else result = fc.showSaveDialog(owner);
315 if(result == JFileChooser.APPROVE_OPTION) {
316 f = fc.getSelectedFile();
317 }
318
319 if(result == JFileChooser.APPROVE_OPTION && !openDialog) {
320 Object o = fc.getFileFilter();
321 for(JSFileFilter ff : choosableFilters) {
322 if(ff == o) {
323 String fn = f.getName().toLowerCase();
324 String ext = ff.getExtension().toLowerCase();
325 if(fn.endsWith(ext)) break;
326
327 fn = f.getAbsolutePath() + ff.getExtension();
328 f = new File(fn);
329 break;
330 }
331 }
332 }
333 }
334
335 if(f == null) return null;
336 String path = f.getParent();
337 if(path != null && locationProperty != null) {
338 preferences().setStringProperty(locationProperty, path);
339 }
340 return f;
341 }
342
343 public static JSlider
344 createVolumeSlider() {
345 return new VolumeSlider();
346 }
347
348 private static class VolumeSlider extends JSlider {
349 private Popup popup = null;
350 private final JToolTip tip = new JToolTip();
351 private static NumberFormat numberFormat = NumberFormat.getInstance();
352
353 VolumeSlider() {
354 super(0, 100, 100);
355 numberFormat.setMaximumFractionDigits(1);
356 // Setting the tooltip size (workaround for preserving that size)
357 boolean b = CC.getViewConfig().isMeasurementUnitDecibel();
358 if(b) tip.setTipText(i18n.getLabel("StdUtils.volumeDecibels", "-30.0"));
359 else tip.setTipText(i18n.getLabel("StdUtils.volume", "100"));
360 tip.setPreferredSize(tip.getPreferredSize());
361 tip.setMinimumSize(tip.getPreferredSize());
362 ///////
363 tip.setComponent(this);
364 tip.setTipText(i18n.getLabel("StdUtils.volume", 0));
365
366 updateVolumeInfo();
367
368 addMouseListener(new MouseAdapter() {
369 public void
370 mousePressed(MouseEvent e) {
371 if(popup != null) {
372 popup.hide();
373 popup = null;
374 }
375
376 if(!VolumeSlider.this.isEnabled()) return;
377
378 java.awt.Point p = VolumeSlider.this.getLocationOnScreen();
379 PopupFactory pf = PopupFactory.getSharedInstance();
380 popup = pf.getPopup(VolumeSlider.this, tip, p.x, p.y - 22);
381 popup.show();
382 }
383
384 public void
385 mouseReleased(MouseEvent e) {
386 if(popup != null) {
387 popup.hide();
388 popup = null;
389 }
390 }
391 });
392
393 addChangeListener(new ChangeListener() {
394 public void
395 stateChanged(ChangeEvent e) { updateVolumeInfo(); }
396 });
397
398 String s = VOL_MEASUREMENT_UNIT_DECIBEL;
399 preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
400 public void
401 propertyChange(PropertyChangeEvent e) {
402 // We use this to set the size of the lVolume
403 // to prevent the frequent resizing of lVolume component
404 boolean b = CC.getViewConfig().isMeasurementUnitDecibel();
405 tip.setPreferredSize(null);
406 String s;
407 if(b) s = i18n.getLabel("StdUtils.volumeDecibels", "-30.0");
408 else s = i18n.getLabel("StdUtils.volume", "100");
409 tip.setTipText(s);
410 tip.setPreferredSize(tip.getPreferredSize());
411 tip.setMinimumSize(tip.getPreferredSize());
412 ///////
413 updateVolumeInfo();
414 }
415 });
416 }
417
418 private void
419 updateVolumeInfo() {
420 String s;
421 if(CC.getViewConfig().isMeasurementUnitDecibel()) {
422 double d = HF.percentsToDecibels(getValue());
423 s = i18n.getLabel("StdUtils.volumeDecibels", numberFormat.format(d));
424 } else {
425 s = i18n.getLabel("StdUtils.volume", getValue());
426 }
427
428 setToolTipText(s);
429 tip.setTipText(s);
430 tip.repaint();
431 }
432 }
433 }

  ViewVC Help
Powered by ViewVC