001: package snow.sortabletable;
002:
003: import snow.utils.StringUtils;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.*;
007: import javax.swing.plaf.*;
008: import javax.swing.event.*;
009: import javax.swing.table.*;
010: import java.nio.charset.*;
011: import java.nio.*;
012: import java.util.*;
013: import java.io.*;
014:
015: /** A simple app to view the UI Keys. + code generation for the selected item
016: */
017: public class UIKeysViewer extends JFrame {
018: private final Vector<String> standardKeys = new Vector<String>();
019: {
020: standardKeys.add("Primary1");
021: }
022:
023: final private UIKeysTableModel basicTableModel = new UIKeysTableModel();
024: final private SortableTableModel sortableTableModel = new SortableTableModel(
025: basicTableModel, 0, true);
026: final private JTable table = new JTable(sortableTableModel);
027: final private JTextField codeField = new JTextField(50);
028:
029: public UIKeysViewer(boolean standalone) {
030: super ("Swing UI Keys explorer");
031: if (standalone) {
032: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
033: }
034:
035: setLayout(new BorderLayout());
036: add(new JScrollPane(table), BorderLayout.CENTER);
037:
038: JPanel sp = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0));
039: add(sp, BorderLayout.SOUTH);
040: sp.add(new JLabel("Code to paste: "));
041: sp.add(codeField);
042: codeField.addFocusListener(new FocusAdapter() {
043: @Override
044: public void focusGained(FocusEvent fe) {
045: codeField.selectAll();
046: }
047: });
048: codeField.setEditable(false);
049:
050: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
051: sortableTableModel.installGUI(table);
052:
053: UniversalTableCellRenderer usr = new UniversalTableCellRenderer(
054: sortableTableModel);
055: table.setDefaultRenderer(Object.class, usr);
056:
057: table
058: .setDefaultEditor(Object.class,
059: new UniversalTableEditor());
060:
061: AdvancedSearchPanel asp = new AdvancedSearchPanel("Filter: ",
062: null, sortableTableModel, false);
063: add(asp, BorderLayout.NORTH);
064: asp.setAdvancedMode(true);
065:
066: table.getSelectionModel().addListSelectionListener(
067: new ListSelectionListener() {
068: public void valueChanged(ListSelectionEvent lse) {
069: codeField.setText(generateCode());
070: }
071: });
072:
073: this .setSize(850, 800);
074: this .setLocation(200, 100);
075: this .setVisible(true);
076:
077: } // Constructor
078:
079: private String generateCode() {
080: int sel = table.getSelectedRow();
081: if (sel == -1)
082: return "";
083: final int posMod = sortableTableModel
084: .getIndexInUnsortedFromTablePos(sel);
085: final String key = "" + basicTableModel.getValueAt(posMod, 0);
086: final String className = ""
087: + basicTableModel.getValueAt(posMod, 1);
088: final Object obj = basicTableModel.getValueAt(posMod, 2);
089:
090: String name = StringUtils.extractFromFirstToNext_Excluded(
091: className, ".", "UIResource");
092:
093: if (name != null && name.length() > 0) {
094: name = StringUtils.keepAfterLastExcl(name, ".");
095: } else if (obj instanceof Integer) {
096: name = "Int";
097: } else {
098: if (obj != null && !obj.equals("null value")) {
099: String simpleName = obj.getClass().getSimpleName();
100: if (simpleName.indexOf('$') < 0
101: && !simpleName.endsWith(";")
102: && !simpleName.endsWith("]")) {
103: name = simpleName;
104: }
105: }
106: }
107:
108: StringBuilder sb = new StringBuilder();
109: sb.append("UIManager.get");
110: if (name != null
111: && (name.equals("Boolean") || name.equals("Int")
112: || name.equals("String") || name.equals("Font")
113: || name.equals("Insets")
114: || name.equals("Color")
115: || name.equals("Border") || name.equals("Icon") || name
116: .equals("Dimension"))) {
117: sb.append("" + name);
118: } // let the rest be accessed with get()
119: else if (name.equals("PrintColor")) {
120: sb.append("Color");
121: } else {
122: System.out.println("UIKeysViewer: not a simple name: "
123: + name);
124: }
125: sb.append("(\"" + key + "\")");
126: return sb.toString();
127: }
128:
129: public static void main(String[] a) {
130: UIManager.put("Hello1", Boolean.TRUE);
131: UIManager.put("Hello2", new Double(Math.PI));
132: UIManager.put("Hello3", new Float((float) Math.PI));
133:
134: EventQueue.invokeLater(new Runnable() {
135: public void run() {
136: new UIKeysViewer(true);
137: }
138: });
139: }
140:
141: class UIKeysTableModel extends FineGrainTableModel {
142: final private Vector<Object> keys = new Vector<Object>();
143:
144: public UIKeysTableModel() {
145: Enumeration<Object> enume = UIManager
146: .getLookAndFeelDefaults().keys();
147: while (enume.hasMoreElements()) {
148: keys.addElement(enume.nextElement());
149: }
150: keys.addElement("Hello1");
151: keys.addElement("Hello2");
152: keys.addElement("Hello3");
153: }
154:
155: public UIKeysTableModel(Vector<String> keys) {
156: this .keys.addAll(keys);
157: }
158:
159: private char[] representableCharsetChars = null;
160:
161: String[] COLUMN_NAMES = new String[] { "name", "class", "value" };
162:
163: int[] COLUMN_PREFERED_SIZES = new int[] { 20, 20, 25 };
164:
165: @Override
166: public int getPreferredColumnWidth(int column) {
167: if (column >= 0 && column < COLUMN_PREFERED_SIZES.length)
168: return COLUMN_PREFERED_SIZES[column];
169: return -1;
170: }
171:
172: @Override
173: public String getColumnName(int col) {
174: return COLUMN_NAMES[col];
175: }
176:
177: @Override
178: public int getColumnAlignment(int column) {
179: return JLabel.LEFT;
180: }
181:
182: @Override
183: public Class getColumnClass(int column) {
184: if (column == 0)
185: return String.class;
186: if (column == 1)
187: return String.class;
188: return Object.class; // important...
189: }
190:
191: public int getColumnCount() {
192: return COLUMN_NAMES.length;
193: }
194:
195: public int getRowCount() {
196: return keys.size();
197: }
198:
199: public Object getValueAt(int row, int col) {
200: Object key = keys.get(row);
201: Object val = UIManager.get(key);
202: if (col == 0)
203: return key;
204: if (col == 1) {
205: if (val == null)
206: return "null value";
207: return val.getClass().getName();
208: }
209: if (col == 2) {
210: if (val == null)
211: return "null value";
212: return val;
213: }
214:
215: return "?";
216: }
217:
218: @Override
219: public boolean isCellEditable(int row, int col) {
220: return col == 2;
221: }
222:
223: @Override
224: public void setValueAt(Object value, int row, int col) {
225: if (col != 2)
226: return;
227:
228: boolean updateUI = false;
229: if (value instanceof Color) {
230: value = new ColorUIResource((Color) value);
231: updateUI = true;
232: }
233:
234: if (value instanceof Font) {
235: value = new FontUIResource((Font) value);
236: updateUI = true;
237: }
238:
239: if (row >= keys.size()) {
240: System.out.println("Key position " + row
241: + " son't exist, keys size is only "
242: + keys.size());
243: return;
244: }
245:
246: String key = (String) keys.get(row);
247: Object old = UIManager.get(key);
248:
249: if (old != null && value != null
250: && !(old.getClass() == value.getClass())) {
251: System.out
252: .println("Attemp to changing class type ! set value cancelled");
253: System.out.println(old.getClass().getName() + " => "
254: + value.getClass().getName());
255:
256: return;
257: }
258:
259: this.fireTableModelWillChange();
260:
261: UIManager.put(key, value);
262:
263: this.fireTableDataChanged();
264: this.fireTableModelHasChanged();
265: }
266: }
267:
268: }
|