KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > sortabletable > UIKeysViewer


1 package snow.sortabletable;
2
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.swing.plaf.*;
8 import javax.swing.event.*;
9 import javax.swing.table.*;
10 import java.nio.charset.*;
11 import java.nio.*;
12 import java.util.*;
13 import java.io.*;
14
15
16 public final class UIKeysViewer extends JFrame
17 {
18   private final Vector<String JavaDoc> standardKeys = new Vector<String JavaDoc>();
19   {
20      standardKeys.add("Primary1");
21   }
22
23   final private UIKeysTableModel basicTableModel = new UIKeysTableModel();
24   final private SortableTableModel sortableTableModel = new SortableTableModel(basicTableModel, 0, true);
25   final private JTable table = new JTable(sortableTableModel);
26
27   public UIKeysViewer()
28   {
29     super("Swing UI Keys explorer");
30     getContentPane().setLayout(new BorderLayout());
31     getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
32     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
33     sortableTableModel.installGUI(table);
34
35     table.setDefaultRenderer(Object JavaDoc.class, new UniversalTableCellRenderer(sortableTableModel));
36     table.setDefaultEditor(Object JavaDoc.class, new UniversalTableEditor());
37
38     AdvancedSearchPanel asp = new AdvancedSearchPanel("Search: ", null, sortableTableModel, false);
39     getContentPane().add(asp, BorderLayout.NORTH);
40
41
42     this.setSize(850,800);
43     this.setLocation(200,100);
44     this.setVisible(true);
45
46
47   } // Constructor
48

49
50
51
52   public static void main(String JavaDoc[] a)
53   {
54      UIManager.put("Hello1", Boolean.TRUE);
55      UIManager.put("Hello2", Math.PI);
56      UIManager.put("Hello3", (float) Math.PI);
57
58      EventQueue.invokeLater(new Runnable JavaDoc()
59      { public void run()
60        {
61          new UIKeysViewer();
62        }
63      });
64   }
65
66
67
68
69
70   final private class UIKeysTableModel extends FineGrainTableModel
71   {
72      final private Vector<String JavaDoc> keys = new Vector<String JavaDoc>();
73
74      public UIKeysTableModel()
75      {
76         Enumeration enume = UIManager.getLookAndFeelDefaults().keys();
77         while(enume.hasMoreElements())
78         {
79           keys.addElement("" + enume.nextElement());
80         }
81         keys.addElement("Hello1");
82         keys.addElement("Hello2");
83         keys.addElement("Hello3");
84      }
85
86      public UIKeysTableModel(Vector<String JavaDoc> keys)
87      {
88         this.keys.addAll(keys);
89      }
90
91
92      private char[] representableCharsetChars = null;
93
94      String JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{"name", "class", "value" };
95
96      int[] COLUMN_PREFERED_SIZES = new int[]{20, 20, 25};
97      public int getPreferredColumnWidth(int column)
98      {
99        if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column];
100        return -1;
101      }
102
103      public String JavaDoc getColumnName(int col) { return COLUMN_NAMES[col]; }
104
105      public int getColumnAlignment(int column)
106      {
107        return JLabel.LEFT;
108      }
109
110
111
112      public int getColumnCount() { return COLUMN_NAMES.length; }
113      public int getRowCount() { return keys.size(); }
114
115
116      public Object JavaDoc getValueAt(int row, int col)
117      {
118        String JavaDoc key = keys.elementAt(row);
119        Object JavaDoc val = UIManager.get(key);
120        if(col==0) return key;
121        if(col==1)
122        {
123          if(val==null) return "null value";
124          return val.getClass().getName();
125        }
126        if(col==2)
127        {
128          if(val==null) return "null value";
129          return val;
130        }
131
132        return "?";
133      }
134
135      public boolean isCellEditable(int row, int col)
136      {
137        return col==2;
138      }
139
140
141      public void setValueAt(Object JavaDoc value, int row, int col)
142      {
143        if(col!=2) return;
144
145        boolean updateUI = false;
146        if(value instanceof Color)
147        {
148          value = new ColorUIResource((Color) value);
149          updateUI = true;
150        }
151
152        if(value instanceof Font)
153        {
154          value = new FontUIResource((Font) value);
155          updateUI = true;
156        }
157
158        if(row>=keys.size())
159        {
160           System.out.println("Key position "+row+" son't exist, keys size is only "+keys.size());
161           return;
162        }
163
164        String JavaDoc key = keys.elementAt(row);
165        Object JavaDoc old = UIManager.get(key);
166
167        if(old!=null && value!=null && !(old.getClass() == value.getClass()))
168        {
169          System.out.println("Attemp to changing class type ! set value cancelled");
170          System.out.println(old.getClass().getName()+" => "+value.getClass().getName());
171
172          return;
173        }
174
175        this.fireTableModelWillChange();
176
177        UIManager.put(key, value);
178
179        this.fireTableDataChanged();
180        this.fireTableModelHasChanged();
181
182        if(updateUI)
183        {
184          //updateUI(); //.repaint();
185
}
186      }
187   }
188
189
190 } // UIKeysViewer
Popular Tags