Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/fluidsynth/gui/SettingsTable.java


1   /*
2    * Copyright (C) 2003 Ken Ellinwood.
3    * 
4    * This file is part of FluidGUI.
5    * 
6    * FluidGUI is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  
21  package org.fluidsynth.gui;
22  
23  import org.fluidsynth.api.*;
24  import org.fluidsynth.api.event.*;
25  import org.fluidsynth.api.settings.*;
26  
27  import java.awt.*;
28  import javax.swing.*;
29  import javax.swing.table.*;
30  
31  import java.beans.*;
32  import java.util.*;
33  
34  /** A {@link javax.swing.JTable} subclass that supports display and
35   *  modification of fluidsynth settings.
36   */
37  public class SettingsTable extends JTable 
38      implements PropertyChangeListener, SettingsEventListener
39  {
40      Renderer renderer = new Renderer();
41      Settings settings;
42      
43      static java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/fluidsynth/gui/i18n/bundle");
44      
45      static String[] columnNames = new String[] { 
46          bundle.getString("settings.table.column.name"), 
47          bundle.getString("settings.table.column.value"), 
48          bundle.getString("settings.table.column.realtime")
49      };
50      
51      public SettingsTable()
52      {
53          setModel( ConfigManager.onlyInstance().getCurrentConfiguration());
54  
55          ConfigManager.onlyInstance().addPropertyChangeListener( this);
56          ConfigManager.onlyInstance().addSettingsEventListener(this);
57      }
58  
59      private void setModel( Configuration config)
60      {
61          // If settings have not been "merged", then just display user modified settings.
62          if (!config.getSettingsMerged())
63              config = config.getModifiedConfiguration();
64          
65          settings = config.getSettings();
66          setModel( new Model( settings));
67      }
68      
69      public void propertyChange(PropertyChangeEvent evt)
70      {
71          if (evt.getPropertyName().equals( ConfigManager.PROP_CONFIGURATION))
72          {
73              if (evt.getNewValue() == null) {
74                  setModel( new Model());
75                  settings = new Settings();
76              }
77              else {
78                  setModel( (Configuration)evt.getNewValue());
79              }
80          }
81          
82      }
83      
84      public void settingsChanged(SettingsEvent event) {
85          setModel( ConfigManager.onlyInstance().getCurrentConfiguration());
86      }
87      
88      public TableCellEditor getCellEditor(int row, int column) {
89  
90          // Return a cell editor for the cell...
91          
92          String settingName = (String)getModel().getValueAt( row, 0);
93          Setting setting = settings.lookup( settingName);
94          
95          if (setting instanceof StringSetting || setting instanceof AbstractNumberSetting)
96              return super.getCellEditor( row, column);
97          
98          if (setting instanceof BooleanSetting) {
99              JCheckBox cb = new JCheckBox();
100             cb.setBackground( getBackground());
101             return new DefaultCellEditor( cb);
102         }
103         else if (setting instanceof OptionSetting)
104         {
105             OptionSetting os = (OptionSetting)setting;
106             if (os.options().size() > 0) {
107                 JComboBox cb = new JComboBox( os.options().toArray());
108                 cb.setSelectedItem( setting.getValue());
109                 cb.setBackground( getBackground());
110                 cb.setFont( getFont());
111                 return new DefaultCellEditor( cb);
112             }
113             else return super.getCellEditor( row, column); // If option list is empty, allow direct text editing
114         }
115 
116         throw new IllegalArgumentException( "No table cell renderer for " + settingName + ", "+ setting);
117     }
118     
119     class Model extends DefaultTableModel
120     {
121         public Model()
122         {
123             super( columnNames, 0);
124         }
125         
126         public Model( Settings settings)
127         {
128             this();
129             
130             Setting[] sarray = settings.getSetting();
131             
132             for (int i = 0; i < sarray.length; i++)
133             {
134                 Setting s = sarray[i];
135 
136                 Boolean rt = (s.isRealtime()) ? Boolean.TRUE : null;
137                 
138                 addRow( new Object[] { s.getName(), s.objectGet(), rt });
139             }
140         }
141 
142         public  boolean isCellEditable(int row, int column)
143         {
144             return column == 1;
145         }
146 
147         public void setValueAt(Object aValue, int row, int column)
148         {
149             // Validate the new value here...
150             try {
151                 String settingName = (String)getModel().getValueAt( row, 0);
152                 Setting setting = settings.lookup( settingName);
153                 setting.objectSet( aValue);
154                 super.setValueAt( aValue, row, column);
155 
156                 if (Executive.isRunning()) {
157                     if (setting.isRealtime())
158                     {
159                         try {
160                             Executive.instance().invoke("set " + setting.getName() + " " + setting.getValue());
161                         }
162                         catch (Exception e) {} // Ignore errors changing realtime settings for now
163                     }
164                     else if (FluidGuiPrefs.getShowNonRealtimeWarning())
165                     {
166                         NonRealtimeWarningDialog warning = 
167                             new NonRealtimeWarningDialog(null, true);
168                         Application.center( warning);
169                         warning.show();
170                     }
171                 }
172             } catch (IllegalArgumentException e) {
173                 JOptionPane.showMessageDialog(null, e.getMessage());
174                 return;
175             }
176         }
177     }
178 
179     public TableCellRenderer getCellRenderer(int row, int column)
180     {
181         return renderer;
182     }
183     
184 
185     
186     class Renderer extends DefaultTableCellRenderer
187     {
188     
189     
190         public Component getTableCellRendererComponent(javax.swing.JTable table,
191                                                        Object value,
192                                                        boolean isSelected,
193                                                        boolean hasFocus,
194                                                        int row,
195                                                        int column)
196         {
197             // Return the component appropriate for rendering the value.
198             if (column == 0)
199                 return super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column);
200 
201             String settingName = (String)getModel().getValueAt( row, 0);
202             Setting setting = settings.lookup( settingName);
203 
204             if (column == 1) {
205                 if (setting instanceof StringSetting ||
206                     setting instanceof AbstractNumberSetting ||
207                     setting instanceof OptionSetting)
208                 {
209                     return super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column);
210                 }
211             
212                 if (setting instanceof BooleanSetting) {
213                     JCheckBox cb = new JCheckBox( (String)null, Boolean.TRUE.equals( setting.objectGet()));
214                     cb.setBackground( getBackground());
215                     return cb;
216                 }
217                 throw new IllegalArgumentException( "No table cell renderer for " + settingName + ", "+ setting);
218             }
219             else {
220                 Boolean rt = (Boolean)getModel().getValueAt( row, 2);
221                 if (rt == null)
222                     return super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column);
223                 else {
224                     JCheckBox cb = new JCheckBox( (String)null, true);
225                     cb.setBackground( getBackground());
226                     return cb;
227                 }
228             }
229 
230         }
231     }
232 }