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