Child: [r17] (diff)

Download this file

SettingsGUI.java    314 lines (276 with data), 11.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Provides a GUI for editing settings
*/
package GUI;
import java.awt.Toolkit;
import java.io.File;
import java.net.URL;
import java.util.Enumeration;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.FormSubmitEvent;
import javax.swing.text.html.HTMLEditorKit;
import utils.Settings;
import definitions.is;
import net.htmlparser.jericho.*;
import script.log;
/**
*
* @author Nuno Brito, 16th of April 2013 in Darmstadt, Germany.
*/
// 2013-APR-26 psc :
// do not write "_SubmitButton"
// collect "Plugins" values;
public class SettingsGUI extends javax.swing.JFrame {
private final String plugins = "$Plugins";
private final String components = "$Components";
private final String files = "$Files";
private final String submitBtn = "$SubmitButton";
/**
* Creates new form SettingsGUI
*/
public SettingsGUI() {
// adopt the default user interface menus and buttons
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
// get the window icon working
URL imgURL = this.getClass().getResource("reboot.png");
setIconImage(Toolkit.getDefaultToolkit().getImage(imgURL));
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("settings");
setBackground(new java.awt.Color(255, 255, 255));
jTextPane1.setEditable(false);
jTextPane1.setBorder(null);
jTextPane1.setContentType("text/html"); // NOI18N
jTextPane1.setMargin(new java.awt.Insets(10, 10, 10, 10));
jScrollPane1.setViewportView(jTextPane1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SettingsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SettingsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SettingsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SettingsGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SettingsGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration//GEN-END:variables
/**
* Part that intercepts the event triggered when the user submits an HTML
* form
*/
void doFormInterception() {
// add the URL interception
HTMLEditorKit kit = (HTMLEditorKit) jTextPane1.getEditorKit();
kit.setAutoFormSubmission(false);
jTextPane1.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e instanceof FormSubmitEvent) {
processFormSubmit(((FormSubmitEvent) e).getData());
}
}
});
}
/**
* Show the settings for specific item
*/
public void showItem(String webpage) {
// start the action when submitting the form
doFormInterception();
// are there settings available?
File configFile = null;
if ((configFile.exists()) && (configFile.isFile())) {
Settings config = new Settings(configFile, "");
// instantiate our HTML manipulating class
net.htmlparser.jericho.Source source =
new net.htmlparser.jericho.Source(webpage);
// get all the data available right now on the form
FormFields formFields = source.getFormFields();
//formFields.clearValues();
// go throught all the saved values
Enumeration e = config.getAllKeys();
FormField fld;
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (!key.equals("")) {
String value = config.read(key);
if (!key.startsWith("$")) {
// change them as necessary
formFields.addValue(key, value);
} else {
if (key.equals(plugins)) {
fld = formFields.get(plugins);
} else if (key.equals(components)) {
fld = formFields.get(components);
} else if (key.equals(files)) {
fld = formFields.get(files);
} else { // should be submitBtn
return;
}
fld.clearValues();
String[] plugs = value.split("\\|");
for (int i = 0; i < plugs.length; i++) {
fld.addValue(plugs[i]);
}
}
}
}
// save the output
OutputDocument outputDocument = new OutputDocument(source);
outputDocument.replace(formFields); // adds all segments necessary to effect changes
webpage = outputDocument.toString();
}
// do our stuff now, add the HTML on the screen
this.jTextPane1.setText(webpage);
// place the cursor on the first line for end user see top of page
this.jTextPane1.setCaretPosition(0);
this.setVisible(true);
}
/**
* Starts processing the form that was submitted by user, on this method we
* will decompose each of the parameters and write them for future use
*/
void processFormSubmit(String text) {
String pluginList = "";
String componentList = "";
String filesList = "";
String[] params = text.split("&");
boolean hasPlugins = false;
boolean hasFiles = false;
boolean hasComponents = false;
// Create our settings file
Settings formSettings = new Settings(null,
"Settings for " + "");
for (String param : params) {
String[] pair = param.split("=");
String value = "";
String key = "";
if (pair.length > 1) {
try {
key = java.net.URLDecoder.decode(pair[0], "UTF-8");
value = java.net.URLDecoder.decode(pair[1], "UTF-8");
//value = pair[1];
} catch (Exception e) {
log.write(is.ERROR,
"Failed to save settings for %1", "");
return;
}
} else if (pair.length == 1) {
try {
key = java.net.URLDecoder.decode(pair[0], "UTF-8");
//value = pair[1];
} catch (Exception e) {
log.write(is.ERROR,
"Failed to save settings for %1", "");
return;
}
}
if (key.startsWith("$")) {
if (key.equalsIgnoreCase(plugins)) {
hasPlugins = true;
if (!pluginList.equals("")) {
pluginList = pluginList.concat("|");
}
pluginList = pluginList.concat(value);
} else if (key.equalsIgnoreCase(files)) {
hasFiles = true;
if (!filesList.equals("")) {
filesList = filesList.concat("|");
}
filesList = filesList.concat(value);
} else if (key.equalsIgnoreCase(components)) {
hasComponents = true;
if (!componentList.equals("")) {
componentList = componentList.concat("|");
}
componentList = componentList.concat(value);
} // Do nothing with $SubmitButton
} else {
if (!key.startsWith("!")) {
formSettings.write(key, value.trim());
}
}
}
if (hasPlugins) {
formSettings.write(plugins, pluginList);
} else {
formSettings.delete(plugins);
}
if (hasFiles) {
formSettings.write(files, filesList);
} else {
formSettings.delete(files);
}
if (hasComponents) {
formSettings.write(components, componentList);
} else {
formSettings.delete(components);
}
log.write(is.COMPLETED,
"Settings for %1 were saved", "");
// hide and destroy this frame, no longer needed
setVisible(false); //you can't see me!
dispose(); //Destroy the JFrame object
}
}