1 | package org.greenstone.admin.guiext.mat;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.io.*;
|
---|
10 | import org.greenstone.admin.Configuration;
|
---|
11 | import javax.swing.*;
|
---|
12 | import javax.swing.table.AbstractTableModel;
|
---|
13 | import javax.swing.table.TableCellRenderer;
|
---|
14 |
|
---|
15 | import org.greenstone.admin.guiext.ExtPane;
|
---|
16 |
|
---|
17 | public class MatExtensionConfigurePrompt extends JFrame {
|
---|
18 |
|
---|
19 | JPanel main_pane;
|
---|
20 | JScrollPane table_pane;
|
---|
21 | JTable configure_table;
|
---|
22 |
|
---|
23 | JButton save_button;
|
---|
24 | JButton cancel_button;
|
---|
25 | JPanel control_pane;
|
---|
26 | JPanel button_pane;
|
---|
27 |
|
---|
28 | JLabel prompt_title;
|
---|
29 | JPanel label_pane;
|
---|
30 |
|
---|
31 | String heading;
|
---|
32 | String destination;
|
---|
33 |
|
---|
34 | static final String Setting = "setting";
|
---|
35 | static final String Value = "value";
|
---|
36 | static final String Property = "property";
|
---|
37 |
|
---|
38 | Object[][] data;
|
---|
39 | String title;
|
---|
40 |
|
---|
41 | public MatExtensionConfigurePrompt(Object[][] o, String directory, String extensionName, String titleString){
|
---|
42 |
|
---|
43 | String[] columnNames = {"Setting","Value"};
|
---|
44 |
|
---|
45 | title = titleString;
|
---|
46 | data = o;
|
---|
47 | destination = directory;
|
---|
48 |
|
---|
49 | main_pane = new JPanel();
|
---|
50 | main_pane.setBorder(BorderFactory.createLoweredBevelBorder());
|
---|
51 | main_pane.setBackground(Configuration.getColor("coloring.workspace_selection_background"));
|
---|
52 | main_pane.setForeground(Configuration.getColor("coloring.workspace_selection_foreground"));
|
---|
53 |
|
---|
54 | configure_table = new JTable(new MyTableModel(columnNames, data)){
|
---|
55 | public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int colIndex){
|
---|
56 | Component c = super.prepareRenderer(renderer, rowIndex, colIndex);
|
---|
57 | if (colIndex == 0) {
|
---|
58 | c.setBackground(Configuration.getColor("coloring.table_noneditable_background"));
|
---|
59 | } else {
|
---|
60 | c.setBackground(Configuration.getColor("coloring.table_editable_background"));
|
---|
61 | }
|
---|
62 |
|
---|
63 | return c;
|
---|
64 | }};
|
---|
65 | configure_table.setIntercellSpacing(new Dimension(ExtPane.left_padding,0));
|
---|
66 | table_pane = new JScrollPane(configure_table);
|
---|
67 | table_pane.setVisible(true);
|
---|
68 |
|
---|
69 | save_button = new JButton("Save");
|
---|
70 | save_button.addActionListener(new Save_button_Adapter(this));
|
---|
71 |
|
---|
72 | cancel_button= new JButton("Cancel");
|
---|
73 | cancel_button.addActionListener(new Cancel_button_Adapter(this));
|
---|
74 | button_pane = new JPanel();
|
---|
75 | button_pane.setLayout(new GridLayout(1,2));
|
---|
76 | button_pane.add(save_button);
|
---|
77 | button_pane.add(cancel_button);
|
---|
78 |
|
---|
79 | control_pane = new JPanel();
|
---|
80 | control_pane.setLayout (new BorderLayout());
|
---|
81 | control_pane.setBorder(BorderFactory.createEmptyBorder(05,10,5,10));
|
---|
82 | control_pane.add (button_pane, BorderLayout.CENTER);
|
---|
83 |
|
---|
84 | prompt_title = new JLabel();
|
---|
85 | prompt_title.setOpaque(true);
|
---|
86 | prompt_title.setBackground(Configuration.getColor("coloring.workspace_selection_background"));
|
---|
87 | prompt_title.setForeground(Configuration.getColor("coloring.workspace_selection_foreground"));
|
---|
88 | prompt_title.setText(extensionName);
|
---|
89 | prompt_title.setBorder(BorderFactory.createEmptyBorder(0, ExtPane.left_padding, 0, 0));
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | class MyTableModel extends AbstractTableModel {
|
---|
94 |
|
---|
95 | private String [] columnNames;
|
---|
96 | private Object[][] data;
|
---|
97 |
|
---|
98 | public MyTableModel(String[] s , Object[][]o){
|
---|
99 | columnNames = s;
|
---|
100 | data = o;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public int getColumnCount() {
|
---|
104 | return columnNames.length;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public int getRowCount() {
|
---|
108 | return data.length;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public String getColumnName(int col) {
|
---|
112 | return columnNames[col];
|
---|
113 | }
|
---|
114 |
|
---|
115 | public Object getValueAt(int row, int col) {
|
---|
116 | return data[row][col];
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | public Class getColumnClass(int c) {
|
---|
121 | return getValueAt(0, c).getClass();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public boolean isCellEditable(int row, int col) {
|
---|
125 | if (col < 1) {
|
---|
126 | return false;
|
---|
127 | } else {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void setValueAt(Object value, int row, int col) {
|
---|
133 | if(isCellEditable(row, col)){
|
---|
134 | data[row][col] = value;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void display(){
|
---|
140 |
|
---|
141 | main_pane.setLayout(new BorderLayout());
|
---|
142 | main_pane.add(prompt_title, BorderLayout.NORTH);
|
---|
143 | main_pane.add(table_pane, BorderLayout.CENTER);
|
---|
144 | main_pane.add(control_pane, BorderLayout.SOUTH);
|
---|
145 |
|
---|
146 | this.getContentPane().add(main_pane);
|
---|
147 | //this.setPreferredSize(new Dimension(500,300));
|
---|
148 | this.setSize(new Dimension(500,300));
|
---|
149 | this.setVisible(true);
|
---|
150 | this.setTitle(title);
|
---|
151 | this.pack();
|
---|
152 | this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void cancel_button_actionPerformed(ActionEvent actionevent){
|
---|
157 |
|
---|
158 | this.dispose();
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void save_button_actionPerformed(ActionEvent actionevent){
|
---|
163 |
|
---|
164 | this.saveConfiguration();
|
---|
165 | this.dispose();
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | private void saveConfiguration(){
|
---|
171 |
|
---|
172 | File propertyFile = new File(destination);
|
---|
173 | boolean fileExists = propertyFile.exists();
|
---|
174 | Writer writer = null;
|
---|
175 |
|
---|
176 | if(fileExists){
|
---|
177 | try{
|
---|
178 | propertyFile.delete();
|
---|
179 | propertyFile.createNewFile();
|
---|
180 | writer = new BufferedWriter(new FileWriter(propertyFile));
|
---|
181 |
|
---|
182 | for(int i = 0; i< data.length; i++){
|
---|
183 | String setting = (String)data[i][0];
|
---|
184 | String value = (String) data[i][1];
|
---|
185 | writer.write(setting + " = " + value+"\n\n");
|
---|
186 | }
|
---|
187 |
|
---|
188 | writer.close();
|
---|
189 | }catch (IOException ex){
|
---|
190 | ex.printStackTrace();
|
---|
191 | }
|
---|
192 | }
|
---|
193 | else{
|
---|
194 | String errMsg ="These settings cannot be saved.";
|
---|
195 | JOptionPane.showMessageDialog(new JFrame(),errMsg);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | class Cancel_button_Adapter implements ActionListener {
|
---|
201 |
|
---|
202 | private MatExtensionConfigurePrompt adaptee;
|
---|
203 |
|
---|
204 | Cancel_button_Adapter(MatExtensionConfigurePrompt adaptee) {
|
---|
205 | this.adaptee = adaptee;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void actionPerformed(ActionEvent e) {
|
---|
209 | adaptee.cancel_button_actionPerformed(e);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | class Save_button_Adapter implements ActionListener {
|
---|
214 |
|
---|
215 | private MatExtensionConfigurePrompt adaptee;
|
---|
216 |
|
---|
217 | Save_button_Adapter(MatExtensionConfigurePrompt adaptee) {
|
---|
218 | this.adaptee = adaptee;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void actionPerformed(ActionEvent e) {
|
---|
222 | adaptee.save_button_actionPerformed(e);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|