/[svn]/jsampler/trunk/src/org/jsampler/view/classic/PrefsDlg.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/classic/PrefsDlg.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 787 - (hide annotations) (download)
Mon Oct 10 16:03:12 2005 UTC (18 years, 6 months ago) by iliev
File size: 10893 byte(s)
* The first alpha-release of JSampler

1 iliev 787 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4     * Copyright (C) 2005 Grigor Kirilov Iliev
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.classic;
24    
25     import java.awt.Container;
26     import java.awt.Dimension;
27     import java.awt.GraphicsEnvironment;
28     import java.awt.Font;
29     import java.awt.Frame;
30     import java.awt.GridBagConstraints;
31     import java.awt.GridBagLayout;
32     import java.awt.Insets;
33    
34     import java.awt.event.ActionEvent;
35     import java.awt.event.ActionListener;
36    
37     import java.util.Locale;
38    
39     import javax.swing.BorderFactory;
40     import javax.swing.Box;
41     import javax.swing.BoxLayout;
42     import javax.swing.JButton;
43     import javax.swing.JComboBox;
44     import javax.swing.JLabel;
45     import javax.swing.JOptionPane;
46     import javax.swing.JPanel;
47     import javax.swing.JPasswordField;
48     import javax.swing.JTabbedPane;
49     import javax.swing.JTextField;
50    
51     import net.sf.juife.EnhancedDialog;
52     import net.sf.juife.JuifeUtils;
53    
54     import org.jsampler.HF;
55     import org.jsampler.JSI18n;
56     import org.jsampler.JSampler;
57     import org.jsampler.Prefs;
58    
59     import static org.jsampler.view.classic.ClassicI18n.i18n;
60    
61    
62     /**
63     *
64     * @author Grigor Iliev
65     */
66     public class PrefsDlg extends EnhancedDialog {
67     private final ViewPane viewPane = new ViewPane();
68     private final GeneralPane genPane = new GeneralPane();
69     private final ConnectionPane conPane = new ConnectionPane();
70    
71     private final JButton btnApply = new JButton(i18n.getButtonLabel("apply"));
72     private final JButton btnClose = new JButton(i18n.getButtonLabel("close"));
73    
74    
75     public
76     PrefsDlg(Frame frm) {
77     super(frm, i18n.getLabel("PrefsDlg"), true);
78    
79     initPrefsDlg();
80     handleEvents();
81     initPrefs();
82    
83     setLocation(JuifeUtils.centerLocation(this, frm));
84     }
85    
86     private void
87     initPrefsDlg() {
88     JTabbedPane tp = new JTabbedPane();
89     tp.addTab(i18n.getLabel("PrefsDlg.tabGeneral"), genPane);
90     tp.addTab(i18n.getLabel("PrefsDlg.tabView"), viewPane);
91     tp.addTab(i18n.getLabel("PrefsDlg.tabConnection"), conPane);
92     tp.setAlignmentX(RIGHT_ALIGNMENT);
93    
94     // Set preferred size for Apply & Exit buttons
95     Dimension d = JuifeUtils.getUnionSize(btnApply, btnClose);
96     btnApply.setPreferredSize(d);
97     btnClose.setPreferredSize(d);
98    
99     JPanel btnPane = new JPanel();
100     btnPane.setLayout(new BoxLayout(btnPane, BoxLayout.X_AXIS));
101     btnPane.add(btnApply);
102     btnPane.add(Box.createRigidArea(new Dimension(5, 0)));
103     btnPane.add(btnClose);
104     btnPane.setAlignmentX(RIGHT_ALIGNMENT);
105    
106     JPanel mainPane = new JPanel();
107     mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
108     mainPane.add(tp);
109     mainPane.add(Box.createRigidArea(new Dimension(0, 12)));
110     mainPane.add(btnPane);
111     mainPane.setBorder(BorderFactory.createEmptyBorder(11, 12, 12, 12));
112    
113     getContentPane().add(mainPane);
114    
115     pack();
116     setResizable(false);
117     }
118    
119     private void
120     handleEvents() {
121     btnApply.addActionListener(new ActionListener() {
122     public void
123     actionPerformed(ActionEvent e) { onApply(); }
124     });
125    
126     btnClose.addActionListener(new ActionListener() {
127     public void
128     actionPerformed(ActionEvent e) { onExit(); }
129     });
130     }
131    
132     protected void
133     onOk() { onApply(); }
134    
135     protected void
136     onCancel() { onExit(); }
137    
138     private void
139     initPrefs() {
140     setLSAddress(Prefs.getLSAddress());
141     setLSPort(Prefs.getLSPort());
142     }
143    
144     private void
145     onApply() {
146     // GENERAL
147     boolean b = Prefs.setInterfaceLanguage(viewPane.getInterfaceLanguage());
148     boolean b2 = Prefs.setInterfaceCountry(viewPane.getInterfaceCountry());
149     if (b || b2) JOptionPane.showMessageDialog (
150     this,
151     i18n.getMessage("PrefsDlg.ifaceChangeInfo", JSampler.NAME),
152     null,
153     JOptionPane.INFORMATION_MESSAGE
154     );
155    
156     String fontName = viewPane.getInterfaceFontName();
157     if(fontName.equals("[Default]")) {
158     if(Prefs.setInterfaceFont(null)) JOptionPane.showMessageDialog (
159     this,
160     i18n.getMessage("PrefsDlg.ifaceFontChangeInfo", JSampler.NAME),
161     null,
162     JOptionPane.INFORMATION_MESSAGE
163     );
164     } else if(Prefs.setInterfaceFont(fontName)) {
165     HF.setUIDefaultFont(fontName);
166     }
167    
168     // CONNECTION
169     Prefs.setLSAddress(getLSAddress());
170    
171     b = true;
172     String s = getLSPort();
173     try {
174     if(s.length() > 0) {
175     int port = Integer.parseInt(s);
176     if(port > 0 && port < 0xffff)
177     Prefs.setAuthSrvPort(port);
178     else b = false;
179     } else Prefs.setAuthSrvPort(-1); // -1 resets to default value
180     } catch(NumberFormatException x) {
181     b = false;
182     }
183    
184     if(!b) {
185     JOptionPane.showMessageDialog (
186     this,
187     i18n.getError("PrefsDlg.invalidPort", s),
188     i18n.getError("error"),
189     JOptionPane.ERROR_MESSAGE
190     );
191    
192     return;
193     }
194    
195     setVisible(false);
196     }
197    
198     private void
199     onExit() { setVisible(false); }
200    
201     private String
202     getLSAddress() { return conPane.getLSAddress().trim(); }
203    
204     private void
205     setLSAddress(String s) { conPane.setLSAddress(s); }
206    
207     private String
208     getLSPort() { return conPane.getLSPort().trim(); }
209    
210     private void
211     setLSPort(int port) { conPane.setLSPort(String.valueOf(port)); }
212     }
213    
214     class GeneralPane extends JPanel {
215     public
216     GeneralPane() { initGeneralPane(); }
217    
218     private void
219     initGeneralPane() {
220    
221     }
222     }
223    
224     class ViewPane extends JPanel {
225     private final JLabel lIfaceLanguage =
226     new JLabel(i18n.getLabel("ViewPane.lIfaceLanguage"));
227     private final JComboBox cbIfaceLanguage = new JComboBox();
228    
229     private final JLabel lIfaceFont =
230     new JLabel(i18n.getLabel("ViewPane.lIfaceFont"));
231     private final JComboBox cbIfaceFont = new JComboBox();
232    
233     public
234     ViewPane() { initViewPane(); }
235    
236     private void
237     initViewPane() {
238     cbIfaceLanguage.setMaximumSize (
239     new Dimension(Short.MAX_VALUE, cbIfaceLanguage.getPreferredSize().height)
240     );
241    
242     for(Locale l : JSI18n.getAvailableLocales()) {
243     LocaleBox box = new LocaleBox(l);
244     cbIfaceLanguage.addItem(box);
245     if ( l.getLanguage().equals(Prefs.getInterfaceLanguage()) &&
246     l.getCountry().equals(Prefs.getInterfaceCountry())
247     ) cbIfaceLanguage.setSelectedItem(box);
248     }
249    
250     cbIfaceFont.setMaximumSize (
251     new Dimension(Short.MAX_VALUE, cbIfaceFont.getPreferredSize().height)
252     );
253    
254     cbIfaceFont.addItem("[Default]");
255    
256     String[] fontS =
257     GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
258    
259     for(String f : fontS) cbIfaceFont.addItem(f);
260    
261     if(Prefs.getInterfaceFont() == null) cbIfaceFont.setSelectedItem("[Default]");
262     else cbIfaceFont.setSelectedItem(Prefs.getInterfaceFont());
263    
264     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
265    
266     JPanel ifacePane = new JPanel();
267     ifacePane.setLayout(new BoxLayout(ifacePane, BoxLayout.X_AXIS));
268     ifacePane.add(lIfaceLanguage);
269     ifacePane.add(Box.createRigidArea(new Dimension(5, 0)));
270     ifacePane.add(cbIfaceLanguage);
271    
272     add(ifacePane);
273    
274     add(Box.createRigidArea(new Dimension(0, 6)));
275    
276     JPanel fontPane = new JPanel();
277     fontPane.setLayout(new BoxLayout(fontPane, BoxLayout.X_AXIS));
278     fontPane.add(lIfaceFont);
279     fontPane.add(Box.createRigidArea(new Dimension(5, 0)));
280     fontPane.add(cbIfaceFont);
281    
282     add(fontPane);
283     setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
284     }
285    
286     protected String
287     getInterfaceLanguage() {
288     LocaleBox box = (LocaleBox)cbIfaceLanguage.getSelectedItem();
289     if(box == null) return null;
290     return box.getLocale().getLanguage();
291     }
292    
293     protected String
294     getInterfaceCountry() {
295     LocaleBox box = (LocaleBox)cbIfaceLanguage.getSelectedItem();
296     if(box == null) return null;
297     return box.getLocale().getCountry();
298     }
299    
300     protected String
301     getInterfaceFontName() { return cbIfaceFont.getSelectedItem().toString(); }
302    
303     class LocaleBox {
304     private Locale locale;
305    
306     LocaleBox(Locale locale) { this.locale = locale; }
307    
308     public Locale
309     getLocale() { return locale; }
310    
311     public String
312     toString() { return locale.getDisplayLanguage(JSI18n.i18n.getCurrentLocale()); }
313     }
314     }
315    
316     class ConnectionPane extends JPanel {
317     final LSPrefsPane lsPrefsPane = new LSPrefsPane();
318    
319     public
320     ConnectionPane() { initConnectionPane(); }
321    
322     private void
323     initConnectionPane() {
324     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
325    
326     add(lsPrefsPane);
327     add(Box.createGlue());
328     setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
329     }
330    
331     public String
332     getLSAddress() { return lsPrefsPane.getLSAddress(); }
333    
334     public void
335     setLSAddress(String address) { lsPrefsPane.setLSAddress(address); }
336    
337     public String
338     getLSPort() { return lsPrefsPane.getLSPort(); }
339    
340     public void
341     setLSPort(String port) { lsPrefsPane.setLSPort(port); }
342     }
343    
344     class LSPrefsPane extends JPanel {
345     private final JLabel lAddress = new JLabel(i18n.getLabel("LSPrefsPane.Address"));
346     private final JLabel lPort = new JLabel(i18n.getLabel("LSPrefsPane.Port"));
347     private final JTextField tfAddress = new JTextField();
348     private final JTextField tfPort = new JTextField();
349    
350    
351     public
352     LSPrefsPane() { initLSPrefsPane(); }
353    
354     private void
355     initLSPrefsPane() {
356     GridBagLayout gridbag = new GridBagLayout();
357     GridBagConstraints c = new GridBagConstraints();
358    
359     setLayout(gridbag);
360    
361     // Set preferred size for username & password fields
362     int w1 = (int) tfAddress.getMinimumSize().getWidth();
363     int h1 = (int) tfAddress.getMinimumSize().getHeight();
364     Dimension d = new Dimension(w1 > 150 ? w1 : 150, h1);
365     tfAddress.setMinimumSize(d);
366     tfAddress.setPreferredSize(d);
367    
368     w1 = (int) tfPort.getMinimumSize().getWidth();
369     h1 = (int) tfPort.getMinimumSize().getHeight();
370     d = new Dimension(w1 > 150 ? w1 : 150, h1);
371     tfPort.setMinimumSize(d);
372     tfPort.setPreferredSize(d);
373    
374     c.fill = GridBagConstraints.NONE;
375    
376     c.gridx = 0;
377     c.gridy = 0;
378     c.anchor = GridBagConstraints.EAST;
379     c.insets = new Insets(3, 3, 3, 3);
380     gridbag.setConstraints(lAddress, c);
381     add(lAddress);
382    
383     c.gridx = 0;
384     c.gridy = 1;
385     gridbag.setConstraints(lPort, c);
386     add(lPort);
387    
388     c.fill = GridBagConstraints.HORIZONTAL;
389     c.gridx = 1;
390     c.gridy = 0;
391     c.weightx = 1.0;
392     c.anchor = GridBagConstraints.WEST;
393     gridbag.setConstraints(tfAddress, c);
394     add(tfAddress);
395    
396     c.gridx = 1;
397     c.gridy = 1;
398     gridbag.setConstraints(tfPort, c);
399     add(tfPort);
400    
401     setBorder(BorderFactory.createTitledBorder(i18n.getLabel("LSPrefsPane")));
402     }
403    
404     public String
405     getLSAddress() { return tfAddress.getText(); }
406    
407     public void
408     setLSAddress(String address) { tfAddress.setText(address); }
409    
410     public String
411     getLSPort() { return tfPort.getText(); }
412    
413     public void
414     setLSPort(String port) { tfPort.setText(port); }
415     }

  ViewVC Help
Powered by ViewVC