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

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

New code for GAI extension manager

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