source: main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/PropertiesStep.java@ 21993

Last change on this file since 21993 was 21993, checked in by sjm84, 14 years ago

JTable grid lines are white by default on Mac (making them invisible), they have now been explicitly set to black

File size: 8.8 KB
Line 
1package org.greenstone.admin.guiext;
2
3import javax.swing.BorderFactory;
4import javax.swing.JLabel;
5import javax.swing.JPanel;
6import javax.swing.JFrame;
7import javax.swing.JButton;
8import javax.swing.JScrollPane;
9import javax.swing.JTable;
10import javax.swing.SwingConstants;
11
12import javax.swing.table.AbstractTableModel;
13import javax.swing.table.TableCellEditor;
14
15import java.awt.event.ActionListener;
16import java.awt.event.ActionEvent;
17import java.awt.event.KeyListener;
18import java.awt.event.KeyEvent;
19
20import java.awt.BorderLayout;
21import java.awt.Color;
22import java.awt.Font;
23import java.awt.GridLayout;
24
25import java.util.Properties;
26
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30
31import org.w3c.dom.Element;
32
33import org.greenstone.admin.Configuration;
34import org.greenstone.admin.LoggedMessageArea;
35
36public class PropertiesStep extends Step
37{
38 OptionList[] _optionLists = null;
39 JTable[] _tables = null;
40 OptionList[] _modifiedOptionLists = null;
41
42 public PropertiesStep(Element propertiesStepElement, SequenceList parent)
43 {
44 super(propertiesStepElement, parent);
45
46 if(propertiesStepElement != null){
47 ExtensionInformation info = _parent.getParent();
48
49 Element[] optionListElements = ExtXMLHelper.getMultipleChildElements(propertiesStepElement, ExtXMLHelper.OPTION_LIST, true);
50
51 if(optionListElements != null){
52
53 _optionLists = new OptionList[optionListElements.length];
54 _modifiedOptionLists = new OptionList[optionListElements.length];
55
56 for(int i = 0; i < optionListElements.length; i++){
57 _optionLists[i] = new OptionList(optionListElements[i], this, false);
58 _modifiedOptionLists[i] = new OptionList(optionListElements[i], this, true);
59 }
60 }
61 else{
62 System.err.println("This properties <" + ExtXMLHelper.STEP + "> element has no <" + ExtXMLHelper.OPTION_LIST + "> elements");
63 }
64 }
65 else{
66 System.err.println("This properties <" + ExtXMLHelper.STEP + "> element is null" );
67 }
68
69 _button.addActionListener(new PropertiesButtonListener());
70 }
71
72 public void setPropertiesToDefaults()
73 {
74 for(int i = 0; i < _optionLists.length; i++){
75 Option[] _modifiedOptions = _modifiedOptionLists[i].getOptions();
76 Option[] _defaultOptions = _optionLists[i].getOptions();
77
78 for(int j = 0; j < _defaultOptions.length; j++){
79 _modifiedOptions[j].setValue(_defaultOptions[j].getValue());
80 }
81 }
82 }
83
84 public class PropertiesButtonListener implements ActionListener
85 {
86 public void actionPerformed(ActionEvent e)
87 {
88 ExtensionInformation extension = _parent.getParent();
89
90 String fileStem = extension.getFileStem();
91
92 JPanel mainPanel = new JPanel();
93 mainPanel.setBorder(BorderFactory.createLoweredBevelBorder());
94 mainPanel.setBackground(Configuration.getColor("coloring.workspace_selection_background"));
95 mainPanel.setForeground(Configuration.getColor("coloring.workspace_selection_foreground"));
96 mainPanel.setLayout(new BorderLayout());
97
98 JPanel tablePanel = new JPanel(new GridLayout(_optionLists.length, 1));
99
100 if(_tables == null){
101 _tables = new JTable[_optionLists.length];
102 }
103 for(int i = 0; i < _optionLists.length; i++){
104 OptionList currentList = _modifiedOptionLists[i];
105
106 JLabel singleTableLabel = new JLabel(currentList.getLabel());
107 singleTableLabel.setHorizontalAlignment(SwingConstants.CENTER);
108 singleTableLabel.setFont(new Font("Arial", Font.BOLD, 13));
109
110 JPanel singleTablePanel = new JPanel();
111 singleTablePanel.setLayout(new BorderLayout());
112 singleTablePanel.add(singleTableLabel, BorderLayout.NORTH);
113
114 Option[] options = currentList.getOptions();
115
116 JTable propertiesTable = new JTable(new CustomTableModel(new String[]{"Setting", "Value"}, options));
117 //The line below is necessary as the default grid colour on mac is white, which makes the lines invisible.
118 propertiesTable.setGridColor(Color.BLACK);
119
120 if(_tables[i] == null){
121 _tables[i] = propertiesTable;
122 }
123
124 //The line below is a workaround for a bug in the JTable class that does not store properties if focus is given to another component.
125 propertiesTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
126
127 singleTablePanel.add(propertiesTable, BorderLayout.CENTER);
128
129 tablePanel.add(singleTablePanel);
130 }
131
132 JButton defaultsButton = new JButton("Restore default settings");
133 defaultsButton.addActionListener(new DefaultSettingsButtonListener());
134
135 JButton saveButton = new JButton("Save");
136 saveButton.addActionListener(new SaveButtonListener());
137
138 JPanel buttonPanel = new JPanel();
139 buttonPanel.setLayout(new GridLayout(1, 2));
140 buttonPanel.add(saveButton);
141 buttonPanel.add(defaultsButton);
142
143 mainPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
144 mainPanel.setBackground(Configuration.getColor("coloring.workspace_selection_background"));
145 mainPanel.setForeground(Configuration.getColor("coloring.workspace_selection_foreground"));
146 mainPanel.add(tablePanel, BorderLayout.CENTER);
147 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
148
149 _parent.getParent().changeExtPane(mainPanel);
150 }
151 }
152
153 public class SaveButtonListener implements ActionListener
154 {
155 public void actionPerformed(ActionEvent e)
156 {
157 for(int i = 0; i < _optionLists.length; i++){
158 OptionList currentOptionList = _modifiedOptionLists[i];
159 Option[] currentOptions = currentOptionList.getOptions();
160
161 Properties newProperties = new Properties();
162 File propertiesFile = null;
163 if(currentOptionList.getFilename().equals("")){
164 propertiesFile = new File(_parent.getParent().getExtensionDirectory() + System.getProperty("file.separator") + "build.properties");
165 }
166 else{
167 propertiesFile = new File(_parent.getParent().getExtensionDirectory() + System.getProperty("file.separator") + currentOptionList.getFilename());
168 }
169
170 if(propertiesFile.exists()){
171 try{
172 newProperties.load(new FileInputStream(propertiesFile));
173 }
174 catch(Exception ex){
175 System.err.println("Could not load properties file before saving to it, existing values will be removed. Possible reasons for this include: \n - Invalid properties file format\n - File is write protected");
176 }
177 }
178
179 for(int j = 0; j < currentOptions.length; j++){
180 Option currentOption = currentOptions[j];
181 if(currentOptionList.getId().equals("")){
182 newProperties.setProperty(currentOption.getId(), currentOption.getValue());
183 }
184 else{
185 newProperties.setProperty(currentOptionList.getId() + "." + currentOption.getId(), currentOption.getValue());
186 }
187 }
188
189 try{
190 if(currentOptionList.getFilename().equals("")){
191 newProperties.store(new FileOutputStream(_parent.getParent().getExtensionDirectory() + System.getProperty("file.separator") + "build.properties"), null);
192 }
193 else{
194 newProperties.store(new FileOutputStream(_parent.getParent().getExtensionDirectory() + System.getProperty("file.separator") + currentOptionList.getFilename()), null);
195 }
196 }
197 catch(Exception ex){
198 System.err.println("Could not save to properties file.");
199 }
200 }
201
202 _parent.registerStepCompletion(PropertiesStep.this);
203 }
204 }
205
206 public class DefaultSettingsButtonListener implements ActionListener
207 {
208 public void actionPerformed(ActionEvent e)
209 {
210 for(int i = 0; i < _tables.length; i++){
211 JTable currentTable = _tables[i];
212 Option[] currentDefaultOptions = _optionLists[i].getOptions();
213
214 for(int j = 0; j < currentDefaultOptions.length; j++){
215 Option currentOption = currentDefaultOptions[j];
216 currentTable.setValueAt(currentOption.getValue(), j, 1);
217 }
218
219 currentTable.revalidate();
220 currentTable.repaint();
221 }
222 }
223 }
224
225 public class CustomTableModel extends AbstractTableModel
226 {
227 private String[] _columnNames;
228 private Option[] _data;
229
230 public CustomTableModel(String[] columnNames, Option[] data){
231 _columnNames = columnNames;
232 _data = data;
233 }
234
235 public int getColumnCount() {
236 return _columnNames.length;
237 }
238
239 public int getRowCount() {
240 return _data.length;
241 }
242
243 public String getColumnName(int col) {
244 return _columnNames[col];
245 }
246
247 public Object getValueAt(int row, int col) {
248 Option o = _data[row];
249 if(col == 0){
250 return o.getName();
251 }
252 else if(col == 1){
253 return o.getValue();
254 }
255 else{
256 return null;
257 }
258 }
259
260
261 public Class getColumnClass(int c) {
262 return getValueAt(0, c).getClass();
263 }
264
265 public boolean isCellEditable(int row, int col) {
266 if (col < 1) {
267 return false;
268 } else {
269 return true;
270 }
271 }
272
273 public void setValueAt(Object value, int row, int col) {
274 if(isCellEditable(row, col)){
275 _data[row].setValue((String)value);
276 }
277 fireTableDataChanged();
278 }
279 }
280}
Note: See TracBrowser for help on using the repository browser.