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

Diff of /jsampler/trunk/src/org/jsampler/view/classic/InstrumentChooser.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1203 by iliev, Mon Apr 2 21:18:31 2007 UTC revision 1204 by iliev, Thu May 24 21:43:45 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *   JSampler - a java front-end for LinuxSampler   *   JSampler - a java front-end for LinuxSampler
3   *   *
4   *   Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com>   *   Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>
5   *   *
6   *   This file is part of JSampler.   *   This file is part of JSampler.
7   *   *
# Line 31  import java.awt.Insets; Line 31  import java.awt.Insets;
31  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
33    
34    import java.awt.event.FocusAdapter;
35    import java.awt.event.FocusEvent;
36    
37    import javax.swing.BorderFactory;
38    import javax.swing.Box;
39    import javax.swing.BoxLayout;
40    import javax.swing.ButtonGroup;
41  import javax.swing.JButton;  import javax.swing.JButton;
42    import javax.swing.JComboBox;
43  import javax.swing.JFileChooser;  import javax.swing.JFileChooser;
44  import javax.swing.JLabel;  import javax.swing.JLabel;
45  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
46  import javax.swing.JPanel;  import javax.swing.JPanel;
47    import javax.swing.JRadioButton;
48  import javax.swing.JSpinner;  import javax.swing.JSpinner;
49  import javax.swing.JTextField;  import javax.swing.JTextField;
50  import javax.swing.SpinnerNumberModel;  import javax.swing.SpinnerNumberModel;
51    
52    import javax.swing.event.ChangeEvent;
53    import javax.swing.event.ChangeListener;
54    import javax.swing.event.DocumentEvent;
55    import javax.swing.event.DocumentListener;
56    
57  import net.sf.juife.OkCancelDialog;  import net.sf.juife.OkCancelDialog;
58    
59    import net.sf.juife.event.TaskEvent;
60    import net.sf.juife.event.TaskListener;
61    
62    import org.jsampler.CC;
63    import org.jsampler.Instrument;
64    import org.jsampler.OrchestraModel;
65    
66    import org.jsampler.task.InstrumentsDb;
67    
68  import static org.jsampler.view.classic.ClassicI18n.i18n;  import static org.jsampler.view.classic.ClassicI18n.i18n;
69    
70    
# Line 50  import static org.jsampler.view.classic. Line 73  import static org.jsampler.view.classic.
73   * @author Grigor Iliev   * @author Grigor Iliev
74   */   */
75  public class InstrumentChooser extends OkCancelDialog {  public class InstrumentChooser extends OkCancelDialog {
76            private final JRadioButton rbSelectFromOrchestra =
77                    new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromOrchestra"));
78            private final JRadioButton rbSelectFromDb =
79                    new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromDb"));
80            private final JRadioButton rbSelectFromFile =
81                    new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromFile"));
82            
83            
84            private final JLabel lOrchestra = new JLabel(i18n.getLabel("InstrumentChooser.lOrchestra"));
85            
86            private final JLabel lInstrument =
87                    new JLabel(i18n.getLabel("InstrumentChooser.lInstrument"));
88            
89            private final JComboBox cbOrchestras = new JComboBox();
90            private final JComboBox cbInstruments = new JComboBox();
91            
92            
93            private final JTextField tfDbInstrument = new JTextField();
94            private final JButton btnBrowseDb = new JButton(Res.iconFolderOpen16);
95            
96          private final JLabel lFilename = new JLabel(i18n.getLabel("InstrumentChooser.lFilename"));          private final JLabel lFilename = new JLabel(i18n.getLabel("InstrumentChooser.lFilename"));
97          private final JLabel lIndex = new JLabel(i18n.getLabel("InstrumentChooser.lIndex"));          private final JLabel lIndex = new JLabel(i18n.getLabel("InstrumentChooser.lIndex"));
98                    
99          private final JTextField tfFilename = new JTextField();          private final JTextField tfFilename = new JTextField();
100          private final JSpinner spinnerIndex = new JSpinner(new SpinnerNumberModel(0, 0, 500, 1));          private final JSpinner spinnerIndex = new JSpinner(new SpinnerNumberModel(0, 0, 500, 1));
101                    
102          private final JButton btnBrowse =          private final JButton btnBrowse = new JButton(Res.iconFolderOpen16);
103                  new JButton(Res.iconFolderOpen16);          
104            private String instrumentFile = null;
105            private int instrumentIndex = 0;
106            private String engine = null;
107                    
108          /** Creates a new instance of InstrumentChooser */          /** Creates a new instance of InstrumentChooser */
109          public InstrumentChooser(Frame owner) {          public InstrumentChooser(Frame owner) {
110                  super(owner, i18n.getLabel("InstrumentChooser.title"));                  super(owner, i18n.getLabel("InstrumentChooser.title"));
111                                    
112                    btnOk.setEnabled(false);
113                    
114                    ButtonGroup group = new ButtonGroup();
115                    group.add(rbSelectFromOrchestra);
116                    group.add(rbSelectFromDb);
117                    group.add(rbSelectFromFile);
118                    
119                    rbSelectFromOrchestra.addActionListener(getHandler());
120                    rbSelectFromDb.addActionListener(getHandler());
121                    rbSelectFromFile.addActionListener(getHandler());
122                    rbSelectFromOrchestra.doClick(0);
123                    
124                    cbOrchestras.addFocusListener(getHandler());
125                    cbInstruments.addFocusListener(getHandler());
126                    
127                    tfDbInstrument.addFocusListener(getHandler());
128                    tfDbInstrument.getDocument().addDocumentListener(getHandler());
129                    
130                    tfFilename.addFocusListener(getHandler());
131                    tfFilename.getDocument().addDocumentListener(getHandler());
132                    
133                    spinnerIndex.addChangeListener(new ChangeListener() {
134                            public void
135                            stateChanged(ChangeEvent e) {
136                                    if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
137                            }
138                    });
139                    
140                    JPanel mainPane = new JPanel();
141                    mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
142                    
143                    rbSelectFromOrchestra.setAlignmentX(LEFT_ALIGNMENT);
144                    mainPane.add(rbSelectFromOrchestra);
145                    
146                    JPanel orchestraPane = createOrchestraPane();
147                    orchestraPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
148                    mainPane.add(orchestraPane);
149                    
150                    rbSelectFromDb.setAlignmentX(LEFT_ALIGNMENT);
151                    mainPane.add(rbSelectFromDb);
152                    
153                    JPanel dbPane = createDbPane();
154                    dbPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
155                    mainPane.add(dbPane);
156                    
157                    rbSelectFromFile.setAlignmentX(LEFT_ALIGNMENT);
158                    mainPane.add(rbSelectFromFile);
159                    
160                    JPanel filePane = createFilePane();
161                    filePane.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
162                    mainPane.add(filePane);
163                    
164                    setMainPane(mainPane);
165                    
166                    if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) {
167                            rbSelectFromDb.setEnabled(false);
168                            tfDbInstrument.setEnabled(false);
169                            btnBrowseDb.setEnabled(false);
170                    } else {
171                            btnBrowseDb.requestFocusInWindow();
172                    }
173            }
174            
175            private JPanel
176            createOrchestraPane() {
177                  GridBagLayout gridbag = new GridBagLayout();                  GridBagLayout gridbag = new GridBagLayout();
178                  GridBagConstraints c = new GridBagConstraints();                  GridBagConstraints c = new GridBagConstraints();
179                                    
180                  JPanel mainPane = new JPanel();                  JPanel p = new JPanel();
181                    
182                    p.setLayout(gridbag);
183                    
184                    c.fill = GridBagConstraints.NONE;
185                    
186                    c.gridx = 0;
187                    c.gridy = 0;
188                    c.anchor = GridBagConstraints.EAST;
189                    c.insets = new Insets(0, 0, 6, 6);
190                    gridbag.setConstraints(lOrchestra, c);
191                    p.add(lOrchestra);
192                    
193                    c.gridx = 0;
194                    c.gridy = 1;
195                    c.insets = new Insets(0, 0, 0, 6);
196                    gridbag.setConstraints(lInstrument, c);
197                    p.add(lInstrument);
198                    
199                    c.gridx = 1;
200                    c.gridy = 0;
201                    c.weightx = 1.0;
202                    c.insets = new Insets(0, 0, 6, 0);
203                    c.fill = GridBagConstraints.HORIZONTAL;
204                    c.anchor = GridBagConstraints.NORTH;
205                    gridbag.setConstraints(cbOrchestras, c);
206                    p.add(cbOrchestras);
207                    
208                    c.gridx = 1;
209                    c.gridy = 1;
210                    c.insets = new Insets(0, 0, 0, 0);
211                    gridbag.setConstraints(cbInstruments, c);
212                    p.add(cbInstruments);
213                    
214                    cbOrchestras.addActionListener(new ActionListener() {
215                            public void
216                            actionPerformed(ActionEvent e) { orchestraChanged(); }
217                    });
218                                    
219                  mainPane.setLayout(gridbag);                  cbInstruments.addActionListener(new ActionListener() {
220                            public void
221                            actionPerformed(ActionEvent e) { instrumentChanged(); }
222                    });
223                    
224                    for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
225                            cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i));
226                    }
227                    
228                    p.setAlignmentX(LEFT_ALIGNMENT);
229                    return p;
230            }
231            
232            private void
233            orchestraChanged() {
234                    OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
235                    String s = om == null ? null : om.getDescription();
236                    if(s != null && s.length() == 0) s = null;
237                    cbOrchestras.setToolTipText(s);
238                    
239                    cbInstruments.removeAllItems();
240                    if(om == null || om.getInstrumentCount() == 0) {
241                            cbInstruments.setEnabled(false);
242                            return;
243                    }
244                    
245                    cbInstruments.setEnabled(true);
246                    
247                    for(int i = 0; i < om.getInstrumentCount(); i++) {
248                            cbInstruments.addItem(om.getInstrument(i));
249                    }
250            }
251            
252            private void
253            instrumentChanged() {
254                    Instrument instr = (Instrument)cbInstruments.getSelectedItem();
255                    String s = instr == null ? null : instr.getDescription();
256                    if(s != null && s.length() == 0) s = null;
257                    cbInstruments.setToolTipText(s);
258                    
259                    btnOk.setEnabled(instr != null);
260            }
261            
262            
263            
264            private JPanel
265            createDbPane() {
266                    JPanel p = new JPanel();
267                    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
268                    p.add(tfDbInstrument);
269                    p.add(Box.createRigidArea(new Dimension(6, 0)));
270                    btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
271                    p.add(btnBrowseDb);
272                    //p.setMaximumSize(new Dimension(Short.MAX_VALUE, p.getPreferredSize().height));
273                    
274                    btnBrowseDb.addActionListener(new ActionListener() {
275                            public void
276                            actionPerformed(ActionEvent e) {
277                                    if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
278                                    DbInstrumentChooser dlg;
279                                    dlg = new DbInstrumentChooser(InstrumentChooser.this);
280                                    String s = tfDbInstrument.getText();
281                                    if(s.length() > 0) dlg.setSelectedInstrument(s);
282                                    dlg.setVisible(true);
283                                    if(dlg.isCancelled()) return;
284                                    tfDbInstrument.setText(dlg.getSelectedInstrument());
285                            }
286                    });
287                    
288                    p.setAlignmentX(LEFT_ALIGNMENT);
289                    
290                    return p;
291            }
292            
293            private JPanel
294            createFilePane() {
295                    GridBagLayout gridbag = new GridBagLayout();
296                    GridBagConstraints c = new GridBagConstraints();
297                    
298                    JPanel filePane = new JPanel();
299                    
300                    filePane.setLayout(gridbag);
301                                    
302                  c.fill = GridBagConstraints.NONE;                  c.fill = GridBagConstraints.NONE;
303                                    
# Line 77  public class InstrumentChooser extends O Line 306  public class InstrumentChooser extends O
306                  c.anchor = GridBagConstraints.EAST;                  c.anchor = GridBagConstraints.EAST;
307                  c.insets = new Insets(3, 3, 3, 3);                  c.insets = new Insets(3, 3, 3, 3);
308                  gridbag.setConstraints(lFilename, c);                  gridbag.setConstraints(lFilename, c);
309                  mainPane.add(lFilename);                  filePane.add(lFilename);
310                                    
311                  c.gridx = 0;                  c.gridx = 0;
312                  c.gridy = 1;                  c.gridy = 1;
313                  gridbag.setConstraints(lIndex, c);                  gridbag.setConstraints(lIndex, c);
314                  mainPane.add(lIndex);                  filePane.add(lIndex);
315                                    
316                  btnBrowse.setMargin(new Insets(0, 0, 0, 0));                  btnBrowse.setMargin(new Insets(0, 0, 0, 0));
317                  btnBrowse.setToolTipText(i18n.getLabel("InstrumentChooser.btnBrowse"));                  btnBrowse.setToolTipText(i18n.getLabel("InstrumentChooser.btnBrowse"));
318                  c.gridx = 2;                  c.gridx = 2;
319                  c.gridy = 0;                  c.gridy = 0;
320                  gridbag.setConstraints(btnBrowse, c);                  gridbag.setConstraints(btnBrowse, c);
321                  mainPane.add(btnBrowse);                  filePane.add(btnBrowse);
322                                    
323                  c.fill = GridBagConstraints.HORIZONTAL;                  c.fill = GridBagConstraints.HORIZONTAL;
324                  c.gridx = 1;                  c.gridx = 1;
# Line 97  public class InstrumentChooser extends O Line 326  public class InstrumentChooser extends O
326                  c.weightx = 1.0;                  c.weightx = 1.0;
327                  c.anchor = GridBagConstraints.WEST;                  c.anchor = GridBagConstraints.WEST;
328                  gridbag.setConstraints(tfFilename, c);                  gridbag.setConstraints(tfFilename, c);
329                  mainPane.add(tfFilename);                  filePane.add(tfFilename);
330                                                    
331                  c.gridx = 1;                  c.gridx = 1;
332                  c.gridy = 1;                  c.gridy = 1;
333                  gridbag.setConstraints(spinnerIndex, c);                  gridbag.setConstraints(spinnerIndex, c);
334                  mainPane.add(spinnerIndex);                  filePane.add(spinnerIndex);
335                                    
336                  tfFilename.setPreferredSize (                  tfFilename.setPreferredSize (
337                          new Dimension(200, tfFilename.getPreferredSize().height)                          new Dimension(200, tfFilename.getPreferredSize().height)
338                  );                  );
339                                    
                 setMainPane(mainPane);  
                   
340                  btnBrowse.addActionListener(new ActionListener() {                  btnBrowse.addActionListener(new ActionListener() {
341                          public void                          public void
342                          actionPerformed(ActionEvent e) { onBrowse(); }                          actionPerformed(ActionEvent e) { onBrowse(); }
343                  });                  });
344                                    
345                  btnBrowse.requestFocusInWindow();                  filePane.setAlignmentX(LEFT_ALIGNMENT);
346                    return filePane;
347          }          }
348                    
349          protected void          protected void
350          onOk() {          onOk() {
351                  if(tfFilename.getText().length() == 0) {                  if(!btnOk.isEnabled()) return;
352                          JOptionPane.showMessageDialog (                  if(rbSelectFromOrchestra.isSelected()) {
353                                  this, i18n.getLabel("InstrumentChooser.selectFile"),                          Instrument instr = (Instrument)cbInstruments.getSelectedItem();
354                                  "",                          instrumentFile = instr.getPath();
355                                  JOptionPane.INFORMATION_MESSAGE                          instrumentIndex = instr.getInstrumentIndex();
356                          );                          engine = instr.getEngine();
357                          setCancelled(true);                          setVisible(false);
358                            return;
359                    }
360                    
361                    if(rbSelectFromFile.isSelected()) {
362                            instrumentFile = tfFilename.getText();
363                            instrumentIndex = Integer.parseInt(spinnerIndex.getValue().toString());
364                            setVisible(false);
365                          return;                          return;
366                  }                  }
367                                    
368                  setVisible(false);                  if(!rbSelectFromDb.isSelected()) return;
369                    
370                    String instr = tfDbInstrument.getText();
371                    final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr);
372                    
373                    t.addTaskListener(new TaskListener() {
374                            public void
375                            taskPerformed(TaskEvent e) {
376                                    updateState();
377                                    if(t.doneWithErrors()) return;
378                                    
379                                    instrumentFile = t.getResult().getFilePath();
380                                    instrumentIndex = t.getResult().getInstrumentIndex();
381                                    engine = t.getResult().getFormatFamily(); // TODO: fix this
382                                    setVisible(false);
383                            }
384                    });
385                    
386                    btnOk.setEnabled(false);
387                    CC.getTaskQueue().add(t);
388          }          }
389                    
390          protected void          protected void
# Line 138  public class InstrumentChooser extends O Line 392  public class InstrumentChooser extends O
392                    
393          private void          private void
394          onBrowse() {          onBrowse() {
395                    if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
396                  JFileChooser fc = new JFileChooser();                  JFileChooser fc = new JFileChooser();
397                  int result = fc.showOpenDialog(this);                  int result = fc.showOpenDialog(this);
398                  if(result == JFileChooser.APPROVE_OPTION) {                  if(result == JFileChooser.APPROVE_OPTION) {
# Line 151  public class InstrumentChooser extends O Line 406  public class InstrumentChooser extends O
406           * @return The name of the selected instrument file.           * @return The name of the selected instrument file.
407           */           */
408          public String          public String
409          getFileName() { return tfFilename.getText(); }          getInstrumentFile() { return instrumentFile; }
410                    
411          /**          /**
412           * Gets the index of the instrument in the instrument file.           * Gets the index of the instrument in the instrument file.
413           * @return The index of the instrument in the instrument file.           * @return The index of the instrument in the instrument file.
414           */           */
415          public int          public int
416          getInstrumentIndex() { return Integer.parseInt(spinnerIndex.getValue().toString()); }          getInstrumentIndex() { return instrumentIndex; }
417            
418            public String
419            getEngine() { return engine; }
420            
421            private void
422            updateState() {
423                    boolean b = false;
424                    if(rbSelectFromOrchestra.isSelected()) {
425                            b = cbInstruments.getSelectedItem() != null;
426                    } else if(rbSelectFromDb.isSelected()) {
427                            b = tfDbInstrument.getText().length() > 0;
428                    } else if(rbSelectFromFile.isSelected()) {
429                            b = tfFilename.getText().length() > 0;
430                    }
431                    
432                    btnOk.setEnabled(b);
433            }
434            
435            private final EventHandler eventHandler = new EventHandler();
436            
437            private EventHandler
438            getHandler() { return eventHandler; }
439            
440            private class EventHandler extends FocusAdapter
441                                    implements ActionListener, DocumentListener {
442                    
443                    public void
444                    actionPerformed(ActionEvent e) {
445                            updateState();
446                    }
447                    
448                    // DocumentListener
449                    public void
450                    insertUpdate(DocumentEvent e) { updateState(); }
451                    
452                    public void
453                    removeUpdate(DocumentEvent e) { updateState(); }
454                    
455                    public void
456                    changedUpdate(DocumentEvent e) { updateState(); }
457                    
458                    // FocusListener
459                    public void
460                    focusGained(FocusEvent e) {
461                            Object src = e.getSource();
462                            if(src == cbInstruments || src == cbOrchestras) {
463                                    if(!rbSelectFromOrchestra.isSelected()) {
464                                            rbSelectFromOrchestra.doClick(0);
465                                    }
466                            } else if(src == tfDbInstrument) {
467                                    if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
468                            } else if(src == tfFilename) {
469                                    if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
470                            }
471                    }
472            }
473  }  }

Legend:
Removed from v.1203  
changed lines
  Added in v.1204

  ViewVC Help
Powered by ViewVC