001: package snow.lookandfeel;
002:
003: import java.awt.Insets;
004: import snow.utils.storage.AppProperties;
005: import snow.utils.gui.*;
006: import snow.sortabletable.*;
007:
008: import java.io.*;
009: import javax.swing.*;
010: import javax.swing.event.*;
011: import javax.swing.plaf.metal.*;
012: import javax.swing.plaf.*;
013: import java.awt.BorderLayout;
014: import java.awt.Color;
015: import java.awt.Font;
016: import java.awt.event.*;
017: import java.util.*;
018:
019: public class CustomOceanEditor extends JDialog {
020: final private CustomOceanTheme theme;
021: final private UIKeysTableModel tableModel;
022: final private SortableTableModel sortableTableModel;
023: final private JTable table;
024:
025: public CustomOceanEditor(JFrame parent,
026: final CustomOceanTheme theme, final AppProperties props) {
027: super (parent, "Theme editor", false);
028: setLayout(new BorderLayout());
029:
030: this .theme = theme;
031:
032: tableModel = new UIKeysTableModel(theme.getKeysForEdition());
033: sortableTableModel = new SortableTableModel(tableModel, 0, true);
034: table = new JTable(sortableTableModel);
035: sortableTableModel.installGUI(table);
036:
037: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
038: sortableTableModel.installGUI(table);
039:
040: table.setDefaultRenderer(Object.class,
041: new UniversalTableCellRenderer(sortableTableModel));
042: table
043: .setDefaultEditor(Object.class,
044: new UniversalTableEditor());
045:
046: AdvancedSearchPanel asp = new AdvancedSearchPanel("Search: ",
047: null, sortableTableModel, false);
048: add(asp, BorderLayout.NORTH);
049:
050: JButton clearButton = new JButton("Reset theme");
051: clearButton.setMargin(new Insets(0, 2, 0, 2));
052: asp.add(clearButton);
053: clearButton.addActionListener(new ActionListener() {
054: public void actionPerformed(ActionEvent ae) {
055: theme.clearUserSettings();
056: ThemesManager.getInstance().saveThemes();
057: ThemesManager.getInstance().installSelectedTheme();
058: tableModel.updateWholeTable();
059: }
060: });
061:
062: add(new JScrollPane(table), BorderLayout.CENTER);
063:
064: CloseControlPanel ccp = new CloseControlPanel(this , false,
065: false, "Close");
066: add(ccp, BorderLayout.SOUTH);
067:
068: this .setLocation(300, 300);
069: setSize(550, 550);
070: setVisible(true);
071:
072: } // Constructor
073:
074: class UIKeysTableModel extends FineGrainTableModel {
075: final private List<String> keys = new ArrayList<String>();
076:
077: public UIKeysTableModel(List<String> keys) {
078: this .keys.addAll(keys);
079: }
080:
081: private char[] representableCharsetChars = null;
082:
083: String[] COLUMN_NAMES = new String[] { "name", "value" };
084:
085: int[] COLUMN_PREFERED_SIZES = new int[] { 20, 20, 25 };
086:
087: @Override
088: public int getPreferredColumnWidth(int column) {
089: if (column >= 0 && column < COLUMN_PREFERED_SIZES.length)
090: return COLUMN_PREFERED_SIZES[column];
091: return -1;
092: }
093:
094: @Override
095: public String getColumnName(int col) {
096: return COLUMN_NAMES[col];
097: }
098:
099: @Override
100: public int getColumnAlignment(int column) {
101: return JLabel.LEFT;
102: }
103:
104: public int getColumnCount() {
105: return COLUMN_NAMES.length;
106: }
107:
108: public int getRowCount() {
109: return keys.size();
110: }
111:
112: public Object getValueAt(int row, int col) {
113: String key = keys.get(row);
114: Object val = theme.getResource(key);
115:
116: if (col == 0)
117: return "" + key;
118: if (col == 1) {
119: if (val == null)
120: return "null value";
121: return val;
122: }
123:
124: return "?";
125: }
126:
127: @Override
128: public boolean isCellEditable(int row, int col) {
129: return col == 1;
130: }
131:
132: public void updateWholeTable() {
133: this .fireTableModelWillChange();
134: this .fireTableDataChanged();
135: this .fireTableModelHasChanged();
136: }
137:
138: @Override
139: public void setValueAt(Object value, int row, int col) {
140: if (col != 1)
141: return;
142:
143: boolean updateUI = false;
144: if (value instanceof Color) {
145: value = new ColorUIResource((Color) value);
146: updateUI = true;
147: }
148:
149: if (value instanceof Font) {
150: value = new FontUIResource((Font) value);
151: updateUI = true;
152: }
153:
154: if (row >= keys.size()) {
155: System.out.println("Key position " + row
156: + " son't exist, keys size is only "
157: + keys.size());
158: return;
159: }
160:
161: String key = keys.get(row);
162: Object old = theme.getResource(key);
163:
164: if (old != null && value != null
165: && !(old.getClass() == value.getClass())) {
166: System.out
167: .println("Attemp to changing class type ! set value cancelled");
168: System.out.println(old.getClass().getName() + " => "
169: + value.getClass().getName());
170:
171: // return;
172: }
173:
174: this.fireTableModelWillChange();
175:
176: theme.setResource(key, value);
177:
178: this.fireTableDataChanged();
179: this.fireTableModelHasChanged();
180:
181: ThemesManager.getInstance().saveThemes();
182: if (updateUI) {
183: ThemesManager.getInstance().installSelectedTheme();
184: }
185: }
186: }
187:
188: }
|