/[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 1871 - (show annotations) (download)
Sun Mar 22 18:11:39 2009 UTC (15 years, 1 month ago) by iliev
File size: 10861 byte(s)
* Mac OS integration, work in progress:
* Added option to use native file choosers
  (choose Edit/Preferences, then click the `View' tab)

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

  ViewVC Help
Powered by ViewVC