OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / TableDemo.java
1 /* TableDemo.java -- Demonstrates the use of JTable.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath 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, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.classpath.examples.swing;
40
41 import java.awt.BorderLayout;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import javax.swing.AbstractCellEditor;
45 import javax.swing.BorderFactory;
46 import javax.swing.DefaultCellEditor;
47 import javax.swing.Icon;
48 import javax.swing.JComboBox;
49 import javax.swing.JComponent;
50 import javax.swing.JFrame;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollBar;
53 import javax.swing.JScrollPane;
54 import javax.swing.JSlider;
55 import javax.swing.JTable;
56 import javax.swing.SwingUtilities;
57 import javax.swing.border.Border;
58 import javax.swing.plaf.metal.MetalIconFactory;
59 import javax.swing.table.DefaultTableColumnModel;
60 import javax.swing.table.DefaultTableModel;
61 import javax.swing.table.TableCellEditor;
62 import javax.swing.table.TableCellRenderer;
63 import javax.swing.table.TableColumn;
64 import javax.swing.table.TableColumnModel;
65
66 /**
67  * Displays the editable table. The first column consists of check boxes.
68  * 
69  * @author Audrius Meskauskas (audriusa@bioinformatics.org)
70  */
71 public class TableDemo extends JPanel
72
73   /**
74    * The initial row count for this table.
75    */ 
76   static int rows = 32;
77   
78   /**
79    * The initial column count for this table.
80    */ 
81   static int cols = 7;
82   
83   
84   /**
85    * The table model.
86    */
87   class TModel extends DefaultTableModel
88   {
89     
90     /**
91      * All cells are editable in our table.
92      */
93     public boolean isCellEditable(int row, int column)
94     {
95       return true;
96     }
97     
98     /**
99      * Get the number of the table rows.
100      */
101     public int getRowCount()
102     {
103       return rows;
104     }
105
106     /**
107      * Get the number of the table columns.
108      */
109     public int getColumnCount()
110     {
111       return cols;
112     }
113     
114     /**
115      * Set the value at the given position
116      */
117     public void setValueAt(Object aValue, int aRow, int aColumn)
118     {
119       values[aRow][aColumn] = aValue;
120     }
121     
122     /**
123      * Get the value at the given position.
124      */
125     public Object getValueAt(int aRow, int aColumn)
126     {
127       return values[aRow][aColumn];
128     }
129     
130     /**
131      * The column name, as suggested by model. This header should not be
132      * visible, as it is overridden by setting the header name with
133      * {@link TableColumn#setHeaderValue} in {@link TableDemo#createContent}.
134      */
135     public String getColumnName(int column)
136     {
137        return "Error "+column;
138     }
139     
140     /**
141      * The first column contains booleans, the second - icons, 
142      * others - default class.
143      */
144     public Class getColumnClass(int column)
145     {
146       if (column == 0)
147         return Boolean.class;
148       else if (column == 1)
149         return Icon.class;
150       else
151         return super.getColumnClass(column);
152     }    
153   }
154
155   /**
156    * The scroll bar renderer.
157    */
158   class SliderCell
159       extends AbstractCellEditor
160       implements TableCellEditor, TableCellRenderer
161   {
162     /**
163      * The editor bar.
164      */
165     JSlider bar;
166     
167     /**
168      * The renderer bar.
169      */
170     JSlider rendererBar;
171     
172     /**
173      * The border around the bar, if required.
174      */
175     Border border = BorderFactory.createLineBorder(table.getGridColor());
176
177     SliderCell()
178     {
179       bar = new JSlider();
180       bar.setOrientation(JScrollBar.HORIZONTAL);
181       bar.setMinimum(0);
182       bar.setMaximum(rows);      
183       bar.setBorder(border);
184       
185       rendererBar = new JSlider();
186       rendererBar.setMinimum(0);
187       rendererBar.setMaximum(rows);
188       rendererBar.setEnabled(false);
189     }
190
191     /**
192      * Get the editor.
193      */
194     public Component getTableCellEditorComponent(JTable table, Object value,
195                                                  boolean isSelected, int row,
196                                                  int column)
197     {
198       if (value instanceof Integer)
199         bar.setValue(((Integer) value).intValue());
200       return bar;
201     }
202     
203     /**
204      * Get the renderer.
205      */
206     public Component getTableCellRendererComponent(JTable table, Object value,
207                                                    boolean isSelected,
208                                                    boolean hasFocus, int row,
209                                                    int column)
210     {
211       rendererBar.setValue(((Integer) value).intValue());
212       if (hasFocus)
213         rendererBar.setBorder(border);
214       else
215         rendererBar.setBorder(null);
216       return rendererBar;
217     }
218
219     public Object getCellEditorValue()
220     {
221       return new Integer(bar.getValue());
222     }
223
224   }  
225   
226   /**
227    * The table being displayed.
228    */
229   JTable table = new JTable();
230  
231   /**
232    * The table model.
233    */
234   TModel model = new TModel();
235
236   /**
237    * The table value array.
238    */
239   Object[][] values;
240   
241   /**
242    * The icons that appear in the icon column.
243    */ 
244   Icon[] icons = new Icon[]
245     {                            
246       MetalIconFactory.getTreeComputerIcon(),
247       MetalIconFactory.getTreeHardDriveIcon(),
248       MetalIconFactory.getTreeFolderIcon(),
249     };
250     
251   /**
252    * The choices in the combo boxes
253    */
254   String [] sides = new String[]
255     {
256       "north", "south", "east", "west"                           
257     };
258   
259   
260   /**
261    * Create the table demo with the given titel.
262    */
263   public TableDemo()
264   {
265     super();
266     createContent();
267   }
268   
269   /**
270    * Returns a panel with the demo content. The panel uses a BorderLayout(), and
271    * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
272    * the bottom of the panel if they want to (a close button is added if this
273    * demo is being run as a standalone demo).
274    */
275   private void createContent()
276   {
277     setLayout(new BorderLayout());
278     values = new Object[rows][];
279     
280     for (int i = 0; i < values.length; i++)
281       {
282         values[i] = new Object[cols];
283         for (int j = 3; j < cols; j++)
284           {
285             values[i][j] = "" + ((char) ('a' + j)) + i;
286           }
287         values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
288         values [i][1] = icons [ i % icons.length ];
289         values [i][2] = sides [ i % sides.length ];
290         values [i][4] = new Integer(i);
291       }
292         
293     table.setModel(model);        
294         
295     // Make the columns with gradually increasing width:
296     DefaultTableColumnModel cm = new DefaultTableColumnModel();
297     table.setColumnModel(cm);
298     
299     for (int i = 0; i < cols; i++)
300       {
301         TableColumn column = new TableColumn(i);
302             
303         // Showing the variable width columns.
304         int width = 100+10*i;
305         column.setPreferredWidth(width);
306             
307         // If we do not set the header value here, the value, returned
308         // by model, is used.
309         column.setHeaderValue("Width +"+(20*i));
310             
311         cm.addColumn(column);            
312       }
313     
314     setCustomEditors();
315     setInformativeHeaders();
316
317     // Create the table, place it into scroll pane and place
318     // the pane into this frame.
319     JScrollPane scroll = new JScrollPane();
320         
321     // The horizontal scroll bar is never needed.
322     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
323     scroll.getViewport().add(table);
324     add(scroll, BorderLayout.CENTER);
325     
326     // Increase the row height to make the icons and sliders look better.
327     table.setRowHeight(table.getRowHeight()+2);
328   }
329   
330   /**
331    * Set the more informative column headers for specific columns.
332    */
333   void setInformativeHeaders()
334   {
335     TableColumnModel cm = table.getColumnModel();
336
337     cm.getColumn(0).setHeaderValue("check");
338     cm.getColumn(1).setHeaderValue("icon");
339     cm.getColumn(2).setHeaderValue("combo");
340     cm.getColumn(3).setHeaderValue("edit combo");
341     cm.getColumn(4).setHeaderValue("slider");
342   }
343   
344   /**
345    * Set the custom editors for combo boxes. This method also sets one
346    * custom renderer.
347    */
348   void setCustomEditors()
349   {
350     TableColumnModel cm = table.getColumnModel();
351     
352     // Set combo-box based editor for icons (note that no custom
353     // renderer is needed for JComboBox to work with icons.
354     JComboBox combo0 = new JComboBox(icons);
355     cm.getColumn(1).setCellEditor(new DefaultCellEditor(combo0));
356     
357     // Set the simple combo box editor for the third column:
358     JComboBox combo1 = new JComboBox(sides);
359     cm.getColumn(2).setCellEditor(new DefaultCellEditor(combo1));
360     
361     // Set the editable combo box for the forth column:
362     JComboBox combo2 = new JComboBox(sides);
363     combo2.setEditable(true);
364     cm.getColumn(3).setCellEditor(new DefaultCellEditor(combo2));
365     
366     SliderCell scrollView = new SliderCell();
367     cm.getColumn(4).setCellEditor(scrollView);
368     cm.getColumn(4).setCellRenderer(scrollView);    
369
370     table.setColumnModel(cm);
371   }
372   
373   /**
374    * The executable method to display the editable table.
375    * 
376    * @param args
377    *          unused.
378    */
379   public static void main(String[] args)
380   {
381     SwingUtilities.invokeLater
382     (new Runnable()
383      {
384        public void run()
385        {
386          TableDemo demo = new TableDemo();
387          JFrame frame = new JFrame();
388          frame.getContentPane().add(demo);
389          frame.setSize(new Dimension(640, 100));
390          frame.setVisible(true);
391        }
392      });
393   }
394
395   /**
396    * Returns a DemoFactory that creates a TableDemo.
397    *
398    * @return a DemoFactory that creates a TableDemo
399    */
400   public static DemoFactory createDemoFactory()
401   {
402     return new DemoFactory()
403     {
404       public JComponent createDemo()
405       {
406         return new TableDemo();
407       }
408     };
409   }
410 }
411