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