1 |
package appl.ext; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.util.Set; |
5 |
import java.util.Vector; |
6 |
|
7 |
import javax.swing.DefaultListModel; |
8 |
import javax.swing.JList; |
9 |
import javax.swing.JTable; |
10 |
import javax.swing.event.ListSelectionEvent; |
11 |
import javax.swing.event.ListSelectionListener; |
12 |
import javax.swing.event.TableModelEvent; |
13 |
import javax.swing.event.TableModelListener; |
14 |
import javax.swing.table.DefaultTableCellRenderer; |
15 |
import javax.swing.table.DefaultTableModel; |
16 |
|
17 |
import appl.util.NonEditableTableModel; |
18 |
|
19 |
/** |
20 |
* Handles the {@link ConfigurationEditorGUI}. The prefixes of the |
21 |
* {@link XuluConfig} are added to the {@link JList} and the suffixes to the |
22 |
* {@link JTable}. For the documentation column for every key with a name lets |
23 |
* say 'key' the value of "key.doc" is looked up in the configuration. The |
24 |
* prefixes which are values of the key "Filter.prefixes" in the |
25 |
* configuration are ignored. Also all keys which show up in "Filter.keys" |
26 |
* |
27 |
* @see XuluConfig |
28 |
* @author Dominik Appl |
29 |
*/ |
30 |
public class ConfigurationEditorEngine implements ListSelectionListener, |
31 |
TableModelListener { |
32 |
|
33 |
ConfigurationEditorGUI GUI = null; |
34 |
|
35 |
private DefaultTableModel tableModel; |
36 |
|
37 |
private JTable entryTable; |
38 |
|
39 |
private JList categoryList; |
40 |
|
41 |
private XuluConfig config; |
42 |
|
43 |
private String currentCategory; |
44 |
|
45 |
/** |
46 |
* creates a new Editor. Use {@link #getGUI()} to access the GUI. |
47 |
*/ |
48 |
public ConfigurationEditorEngine() { |
49 |
config = XuluConfig.getXuluConfig(); |
50 |
GUI = new ConfigurationEditorGUI(); |
51 |
initGUI(); |
52 |
} |
53 |
|
54 |
/** |
55 |
* inits the GUI |
56 |
*/ |
57 |
private void initGUI() { |
58 |
|
59 |
entryTable = GUI.entryTable; |
60 |
categoryList = GUI.keyList; |
61 |
initList(); |
62 |
initTable(); |
63 |
} |
64 |
|
65 |
/** |
66 |
* initializes the table |
67 |
*/ |
68 |
private void initTable() { |
69 |
// only the 2nd column is editable |
70 |
tableModel = new NonEditableTableModel( |
71 |
new Object[] { "Name", "Entry" }, |
72 |
new boolean[] { false, true }, 0); |
73 |
entryTable.setModel(tableModel); |
74 |
entryTable.setDefaultRenderer(Object.class, new ToolTipRenderer()); |
75 |
entryTable.getModel().addTableModelListener(this); |
76 |
entryTable.getSelectionModel().addListSelectionListener(this); |
77 |
String firstElement = (String) categoryList.getModel().getElementAt(0); |
78 |
if (firstElement != null) |
79 |
initTableData(firstElement); |
80 |
} |
81 |
|
82 |
/** |
83 |
* The table is initalized with the given category |
84 |
* |
85 |
* @param firstElement |
86 |
*/ |
87 |
private void initTableData(String categoryName) { |
88 |
// get ignored keys |
89 |
String[] filteredKeys = config.getMultiProperty("Filter.keys"); |
90 |
Vector<String> filter = new Vector<String>(); |
91 |
for (String string : filteredKeys) { |
92 |
filter.add(string); |
93 |
} |
94 |
Set<String> suffixes = config.getSuffixesOfPrefix(categoryName); |
95 |
// clear table |
96 |
tableModel.setRowCount(0); |
97 |
for (String suffix : suffixes) { |
98 |
// do not enter the documentation as seperate entries |
99 |
if (suffix.endsWith(".doc")) |
100 |
continue; |
101 |
String key = categoryName + "." + suffix; |
102 |
// do not enter filtered keys |
103 |
if (filter.contains(key)) |
104 |
continue; |
105 |
String value = config.getProperty(key); |
106 |
tableModel.addRow(new Object[] { suffix, value }); |
107 |
} |
108 |
currentCategory = categoryName; |
109 |
} |
110 |
|
111 |
public ConfigurationEditorGUI getGUI() { |
112 |
return GUI; |
113 |
} |
114 |
|
115 |
/** |
116 |
* |
117 |
*/ |
118 |
private void initList() { |
119 |
// get ignored prefixes |
120 |
String[] filteredPrefixes = config.getMultiProperty("Filter.prefixes"); |
121 |
Vector<String> filter = new Vector<String>(); |
122 |
for (String string : filteredPrefixes) { |
123 |
filter.add(string); |
124 |
} |
125 |
DefaultListModel listModel = new DefaultListModel(); |
126 |
Object[] prefixes = config.getPrefixes().toArray(); |
127 |
for (Object object : prefixes) { |
128 |
if (!filter.contains(object)) |
129 |
listModel.addElement(object); |
130 |
} |
131 |
this.GUI.keyList.setModel(listModel); |
132 |
// select first element |
133 |
categoryList.setSelectedIndex(0); |
134 |
categoryList.addListSelectionListener(this); |
135 |
} |
136 |
|
137 |
/** |
138 |
* Listens for selection changes in list and table |
139 |
* |
140 |
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent) |
141 |
*/ |
142 |
public void valueChanged(ListSelectionEvent e) { |
143 |
if (e.getSource() == categoryList) { |
144 |
String value = (String) categoryList.getSelectedValue(); |
145 |
if (value != null) |
146 |
initTableData(value); |
147 |
// clear documentation field |
148 |
GUI.docArea |
149 |
.setText("<select a table value to see its documentation>"); |
150 |
} |
151 |
// update documentation |
152 |
else if (e.getSource() == entryTable.getSelectionModel()) { |
153 |
int selRow = entryTable.getSelectedRow(); |
154 |
// if a row is selected: |
155 |
if (selRow >= 0) { |
156 |
String value = (String) entryTable.getModel().getValueAt( |
157 |
selRow, 0); |
158 |
String docKey = currentCategory + "." + value + ".doc"; |
159 |
String doc = config.getProperty(docKey); |
160 |
if (doc == null) |
161 |
doc = "<no documentation found for this entry>"; |
162 |
GUI.docArea.setText(doc); |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
/* |
168 |
* (non-Javadoc) |
169 |
* |
170 |
* @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent) |
171 |
*/ |
172 |
public void tableChanged(TableModelEvent e) { |
173 |
// handle the editing the user does |
174 |
if (e.getType() == TableModelEvent.UPDATE) { |
175 |
int changedRow = e.getFirstRow(); |
176 |
// only the 2nd col is editable |
177 |
String value = tableModel.getValueAt(changedRow, 1).toString(); |
178 |
String suffix = tableModel.getValueAt(changedRow, 0).toString(); |
179 |
String prefix = categoryList.getSelectedValue().toString(); |
180 |
String key = prefix + "." + suffix; |
181 |
config.setProperty(key, value); |
182 |
|
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* just for the tooltips |
188 |
* |
189 |
* @author Dominik Appl |
190 |
*/ |
191 |
private class ToolTipRenderer extends DefaultTableCellRenderer { |
192 |
|
193 |
public ToolTipRenderer() { |
194 |
setOpaque(true); |
195 |
} |
196 |
|
197 |
public Component getTableCellRendererComponent(JTable table, |
198 |
Object color, boolean isSelected, boolean hasFocus, int row, |
199 |
int column) { |
200 |
Object value = table.getModel().getValueAt(row, column); |
201 |
if (value != null) |
202 |
setToolTipText(value.toString()); |
203 |
return super.getTableCellRendererComponent(table, color, |
204 |
isSelected, hasFocus, row, column); |
205 |
} |
206 |
|
207 |
} |
208 |
} |