KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > lookandfeel > CustomOceanEditor


1 package snow.lookandfeel;
2
3 import snow.utils.storage.AppProperties;
4 import snow.utils.gui.*;
5 import snow.sortabletable.*;
6 import snow.Language.Language;
7
8 import java.io.*;
9 import javax.swing.*;
10 import javax.swing.event.*;
11 import javax.swing.plaf.metal.*;
12 import javax.swing.plaf.*;
13 import java.awt.*;
14 import java.awt.event.*;
15 import java.util.*;
16
17 public final class CustomOceanEditor extends JDialog
18 {
19   final private CustomOceanTheme theme;
20   final private UIKeysTableModel tableModel;
21   final private SortableTableModel sortableTableModel;
22   final private JTable table;
23
24   public CustomOceanEditor(JFrame parent, final CustomOceanTheme theme, final AppProperties props)
25   {
26     super(parent, "Theme editor", false);
27     getContentPane().setLayout(new BorderLayout());
28
29     this.theme = theme;
30
31     tableModel = new UIKeysTableModel(theme.getKeysForEdition());
32     sortableTableModel = new SortableTableModel(tableModel, 0, true);
33     table = new JTable(sortableTableModel);
34     sortableTableModel.installGUI(table);
35
36     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
37     sortableTableModel.installGUI(table);
38
39     table.setDefaultRenderer(Object JavaDoc.class, new UniversalTableCellRenderer(sortableTableModel));
40     table.setDefaultEditor(Object JavaDoc.class, new UniversalTableEditor());
41
42     AdvancedSearchPanel asp = new AdvancedSearchPanel(Language.translate("Search")+": ", null, sortableTableModel, true);
43     getContentPane().add(asp, BorderLayout.NORTH);
44
45     JButton clearButton = new JButton(Language.translate("Reset theme"));
46     asp.add(clearButton);
47     clearButton.addActionListener(new ActionListener()
48     {
49       public void actionPerformed(ActionEvent ae)
50       {
51          theme.clearUserSettings();
52          ThemesManager.getInstance().saveThemes();
53          ThemesManager.getInstance().installSelectedTheme();
54          tableModel.updateWholeTable();
55       }
56     });
57
58
59
60     getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
61
62     CloseControlPanel ccp = new CloseControlPanel(this, false, false, "Close");
63     getContentPane().add(ccp, BorderLayout.SOUTH);
64
65     this.setLocation(300,300);
66     setSize(550, 550);
67     setVisible(true);
68
69   } // Constructor
70

71
72
73   class UIKeysTableModel extends FineGrainTableModel
74   {
75      final private Vector<String JavaDoc> keys = new Vector<String JavaDoc>();
76      public UIKeysTableModel(Vector<String JavaDoc> keys)
77      {
78         this.keys.addAll(keys);
79      }
80
81
82      private char[] representableCharsetChars = null;
83
84      String JavaDoc[] COLUMN_NAMES = new String JavaDoc[]{"name", "value" };
85
86      int[] COLUMN_PREFERED_SIZES = new int[]{20, 20, 25};
87      public int getPreferredColumnWidth(int column)
88      {
89        if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column];
90        return -1;
91      }
92
93      public String JavaDoc getColumnName(int col) { return COLUMN_NAMES[col]; }
94
95      public int getColumnAlignment(int column)
96      {
97        return JLabel.LEFT;
98      }
99
100
101
102      public int getColumnCount() { return COLUMN_NAMES.length; }
103      public int getRowCount() { return keys.size(); }
104
105
106      public Object JavaDoc getValueAt(int row, int col)
107      {
108        String JavaDoc key = keys.elementAt(row);
109        Object JavaDoc val = theme.getResource(key);
110
111        if(col==0) return key;
112        if(col==1)
113        {
114          if(val==null) return "null value";
115          return val;
116        }
117
118        return "?";
119      }
120
121      public boolean isCellEditable(int row, int col)
122      {
123        return col==1;
124      }
125
126      public void updateWholeTable()
127      {
128        this.fireTableModelWillChange();
129        this.fireTableDataChanged();
130        this.fireTableModelHasChanged();
131      }
132
133
134      public void setValueAt(Object JavaDoc value, int row, int col)
135      {
136        if(col!=1) return;
137
138        boolean updateUI = false;
139        if(value instanceof Color)
140        {
141          value = new ColorUIResource((Color) value);
142          updateUI = true;
143        }
144
145        if(value instanceof Font)
146        {
147          value = new FontUIResource((Font) value);
148          updateUI = true;
149        }
150
151        if(row>=keys.size())
152        {
153           System.out.println("Key position "+row+" son't exist, keys size is only "+keys.size());
154           return;
155        }
156
157        String JavaDoc key = keys.elementAt(row);
158        Object JavaDoc old = theme.getResource(key);
159
160        if(old!=null && value!=null && !(old.getClass() == value.getClass()))
161        {
162          System.out.println("Attemp to changing class type ! set value cancelled");
163          System.out.println(old.getClass().getName()+" => "+value.getClass().getName());
164
165         // return;
166
}
167
168        this.fireTableModelWillChange();
169
170        theme.setResource(key, value);
171
172        this.fireTableDataChanged();
173        this.fireTableModelHasChanged();
174
175        ThemesManager.getInstance().saveThemes();
176        if(updateUI)
177        {
178          ThemesManager.getInstance().installSelectedTheme();
179        }
180      }
181   }
182
183
184 } // CustomOceanEditor
Popular Tags