001: package logconfig;
002:
003: import java.awt.GridBagConstraints;
004: import java.awt.Insets;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.awt.event.ItemEvent;
008: import java.awt.event.ItemListener;
009: import java.awt.event.KeyEvent;
010: import java.io.FileInputStream;
011: import java.io.FileNotFoundException;
012: import java.io.FileWriter;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Random;
020:
021: import javax.swing.AbstractAction;
022: import javax.swing.ActionMap;
023: import javax.swing.InputMap;
024: import javax.swing.KeyStroke;
025: import javax.swing.event.ListSelectionEvent;
026: import javax.swing.event.ListSelectionListener;
027: import javax.swing.event.TreeModelEvent;
028: import javax.swing.event.TreeSelectionEvent;
029: import javax.swing.event.TreeSelectionListener;
030: import javax.swing.tree.TreePath;
031: import javax.swing.tree.TreeSelectionModel;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.log4j.xml.DOMConfigurator;
036: import org.dom4j.Document;
037: import org.dom4j.DocumentHelper;
038: import org.dom4j.Element;
039: import org.dom4j.Node;
040: import org.dom4j.io.OutputFormat;
041: import org.dom4j.io.SAXReader;
042: import org.dom4j.io.XMLWriter;
043: import org.wings.SAnchor;
044: import org.wings.SBorderLayout;
045: import org.wings.SBoxLayout;
046: import org.wings.SButton;
047: import org.wings.SButtonGroup;
048: import org.wings.SCheckBox;
049: import org.wings.SComboBox;
050: import org.wings.SComponent;
051: import org.wings.SConstants;
052: import org.wings.SContainer;
053: import org.wings.SDimension;
054: import org.wings.SForm;
055: import org.wings.SFrame;
056: import org.wings.SGridBagLayout;
057: import org.wings.SGridLayout;
058: import org.wings.SLabel;
059: import org.wings.SList;
060: import org.wings.SPanel;
061: import org.wings.SRadioButton;
062: import org.wings.SScrollPane;
063: import org.wings.SSpacer;
064: import org.wings.STemplateLayout;
065: import org.wings.STextField;
066: import org.wings.STree;
067: import org.wings.SURLIcon;
068: import org.wings.event.SDocumentEvent;
069: import org.wings.event.SDocumentListener;
070: import org.wings.header.StyleSheetHeader;
071: import org.wings.plaf.css.script.OnPageRenderedScript;
072: import org.wings.script.JavaScriptEvent;
073: import org.wings.script.JavaScriptListener;
074: import org.wings.session.ScriptManager;
075: import org.wings.session.Session;
076: import org.wings.session.SessionManager;
077: import org.wings.style.CSSProperty;
078: import org.xml.sax.EntityResolver;
079: import org.xml.sax.InputSource;
080:
081: public class LogConfig {
082: private static final Log log = LogFactory.getLog(LogConfig.class);
083:
084: private static final boolean SHOW_DEBUGGING_PANEL = true;
085:
086: private static final SURLIcon INSERT_IMG = new SURLIcon(
087: "../images/insert.gif");
088: private static final SURLIcon UPDATE_IMG = new SURLIcon(
089: "../images/update.gif");
090: private static final SDimension BU_DIM = new SDimension(100,
091: SDimension.AUTO_INT);
092: private static final SDimension IN_DIM = SDimension.FULLWIDTH;
093:
094: private SFrame fr_frame;
095:
096: private String logConfigDir;
097: private String log4jXmlPath;
098: private String log4jDtdPath;
099:
100: private Document document;
101: private DomModel treeModel;
102: private List<String> listModel;
103: private Element rootNode;
104: private Node selectedNode;
105:
106: private SForm fo_form;
107: private STree tr_domTree;
108: private SScrollPane sp_tree;
109: private SButtonGroup bg_insertOrUpdate;
110: private SRadioButton rb_insertNode;
111: private SRadioButton rb_updateNode;
112: private STextField tf_editCategoryName;
113: private STextField tf_editPriorityValue;
114: private SComboBox cb_editAdditivityFlag;
115: private SList li_editAppenderRef;
116: private SLabel la_status;
117: private SButton bu_saveNode;
118: private SButton bu_deleteNode;
119: private SButton bu_commitChanges;
120:
121: public LogConfig() {
122: final Session session = SessionManager.getSession();
123: logConfigDir = session.getServletContext().getRealPath("/");
124: log4jXmlPath = logConfigDir
125: + session.getProperty("log4j.xml.path").toString();
126: log4jDtdPath = logConfigDir
127: + session.getProperty("log4j.dtd.path").toString();
128:
129: DOMConfigurator.configureAndWatch(log4jXmlPath, 3000);
130:
131: try {
132: SAXReader reader = new SAXReader(false);
133: reader.setEntityResolver(new EntityResolver() {
134: public InputSource resolveEntity(String publicId,
135: String systemId) {
136: if (systemId != null
137: && systemId.endsWith("log4j.dtd")) {
138: try {
139: return new InputSource(new FileInputStream(
140: log4jDtdPath));
141: } catch (FileNotFoundException ex) {
142: log
143: .error(
144: "Could not load 'log4j.dtd'.",
145: ex);
146: }
147: }
148: return null;
149: }
150: });
151: reader.setIgnoreComments(true);
152: document = reader.read(new FileInputStream(log4jXmlPath));
153: rootNode = document.getRootElement();
154: treeModel = new DomModel(rootNode);
155: } catch (Exception ex) {
156: log.error("Could not load 'log4j.xml'.", ex);
157: }
158:
159: SLabel la_header = new SLabel(new SURLIcon(
160: "../images/header.jpg"));
161: la_header.setPreferredSize(SDimension.FULLWIDTH);
162: la_header.setStyle(la_header.getStyle() + " la_header");
163:
164: la_status = new SLabel();
165: la_status.setStyle(la_status.getStyle() + " la_status");
166: la_status.setPreferredSize(SDimension.FULLWIDTH);
167: resetStatusLabel();
168:
169: SPanel pa_application = new SPanel(new SBorderLayout());
170: pa_application.setStyle(pa_application.getStyle()
171: + " pa_application");
172: pa_application.setPreferredSize(new SDimension(730, 540));
173: pa_application.add(la_header, SBorderLayout.NORTH);
174: pa_application.add(document == null ? createErrorPanel()
175: : createMainPanel(), SBorderLayout.CENTER);
176: pa_application.add(la_status, SBorderLayout.SOUTH);
177:
178: fr_frame = new SFrame("Log4J - Configurator");
179: fo_form = (SForm) fr_frame.getContentPane();
180:
181: SContainer pa_content = fr_frame.getContentPane();
182: pa_content.setStyle(pa_content.getStyle() + " pa_content");
183: try {
184: java.net.URL templateURL = SessionManager.getSession()
185: .getServletContext().getResource(
186: "/templates/main.thtml");
187: if (templateURL == null) {
188: pa_content.add(new SLabel(
189: "Could not find template file!"));
190: return;
191: }
192: pa_content.setLayout(new STemplateLayout(templateURL));
193: } catch (java.io.IOException ex) {
194: log.error("Could not find template file!", ex);
195: }
196: pa_content.add(pa_application, "application");
197: if (document != null && SHOW_DEBUGGING_PANEL) {
198: SPanel pa_debugging = createDebuggingPanel();
199: pa_debugging.setPreferredSize(new SDimension(-1, 540));
200: pa_content.add(pa_debugging, "debugging");
201: }
202:
203: InputMap inputMap = new InputMap();
204: inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F12,
205: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F12");
206: ActionMap actionMap = new ActionMap();
207: actionMap.put("Shift F12", new AbstractAction() {
208: private static final long serialVersionUID = 573765095316826768L;
209:
210: public void actionPerformed(ActionEvent e) {
211: ScriptManager.getInstance().addScriptListener(
212: new OnPageRenderedScript(
213: "wingS.ajax.toggleDebugView()"));
214: }
215: });
216: pa_content.setInputMap(SComponent.WHEN_IN_FOCUSED_FRAME,
217: inputMap);
218: pa_content.setActionMap(actionMap);
219:
220: fr_frame.addHeader(new StyleSheetHeader("../css/custom.css"));
221: fr_frame.setVisible(true);
222:
223: testSomething(false);
224: }
225:
226: private SPanel createErrorPanel() {
227: String msg = "<html><b>ERROR >>> Unable to load log4j.xml!</b><br />"
228: + "Please check the paths in your web.xml.";
229: SLabel la_error = new SLabel(msg);
230: SPanel pa_error = new SPanel();
231: pa_error.setStyle(pa_error.getStyle() + " pa_error");
232: pa_error.setPreferredSize(SDimension.FULLAREA);
233: pa_error.setHorizontalAlignment(SConstants.CENTER_ALIGN);
234: pa_error.add(la_error);
235: return pa_error;
236: }
237:
238: private SPanel createMainPanel() {
239: tr_domTree = new STree(treeModel);
240: tr_domTree.setCellRenderer(new DomRenderer());
241: tr_domTree.setPreferredSize(SDimension.FULLWIDTH);
242: tr_domTree.getSelectionModel().setSelectionMode(
243: TreeSelectionModel.SINGLE_TREE_SELECTION);
244: tr_domTree
245: .addTreeSelectionListener(new TreeSelectionListener() {
246: public void valueChanged(TreeSelectionEvent tse) {
247: selectedNode = (Node) tse.getPath()
248: .getLastPathComponent();
249: Node categoryNode = selectedNode
250: .selectSingleNode("ancestor-or-self::category");
251:
252: if (tr_domTree.getSelectionCount() > 0
253: && categoryNode != null) {
254: fillEditFields(categoryNode);
255: rb_updateNode.setSelected(true);
256: rb_updateNode.setEnabled(true);
257: bu_deleteNode.setEnabled(true);
258: } else {
259: clearEditFields();
260: rb_insertNode.setSelected(true);
261: rb_updateNode.setEnabled(false);
262: bu_deleteNode.setEnabled(false);
263: }
264: }
265: });
266:
267: sp_tree = new SScrollPane(tr_domTree);
268: sp_tree.setStyle(sp_tree.getStyle() + " sp_tree");
269: sp_tree.setPreferredSize(SDimension.FULLAREA);
270: sp_tree.setMode(SScrollPane.MODE_COMPLETE);
271:
272: bg_insertOrUpdate = new SButtonGroup();
273: rb_insertNode = new SRadioButton("Insert node");
274: rb_insertNode.setSelected(true);
275: rb_insertNode.setShowAsFormComponent(false);
276: rb_insertNode.addActionListener(new ActionListener() {
277: public void actionPerformed(ActionEvent e) {
278: if (rb_insertNode.isSelected()) {
279: clearEditFields();
280: }
281: }
282: });
283: rb_updateNode = new SRadioButton("Update node");
284: rb_updateNode.setEnabled(false);
285: rb_updateNode.setShowAsFormComponent(false);
286: rb_updateNode.addActionListener(new ActionListener() {
287: public void actionPerformed(ActionEvent e) {
288: if (rb_updateNode.isSelected()) {
289: fillEditFields(selectedNode
290: .selectSingleNode("ancestor-or-self::category"));
291: }
292: }
293: });
294: bg_insertOrUpdate.add(rb_insertNode);
295: bg_insertOrUpdate.add(rb_updateNode);
296:
297: tf_editCategoryName = new STextField();
298: tf_editCategoryName.setPreferredSize(IN_DIM);
299: tf_editPriorityValue = new STextField();
300: tf_editPriorityValue.setPreferredSize(IN_DIM);
301:
302: String[] additivityModel = { "true", "false" };
303: cb_editAdditivityFlag = new SComboBox(additivityModel);
304: cb_editAdditivityFlag.setPreferredSize(IN_DIM);
305:
306: listModel = new ArrayList<String>();
307: List appenderNames = rootNode.selectNodes("./appender/@name");
308: for (Iterator i = appenderNames.iterator(); i.hasNext();) {
309: listModel.add(((Node) i.next()).getText());
310: }
311: li_editAppenderRef = new SList(listModel);
312: li_editAppenderRef.setVisibleRowCount(2);
313: li_editAppenderRef.setPreferredSize(IN_DIM);
314:
315: bu_saveNode = new SButton("Insert", new SURLIcon(
316: "../images/insert.gif"));
317: bu_saveNode.setPreferredSize(BU_DIM);
318: bu_saveNode.setHorizontalAlignment(SConstants.CENTER_ALIGN);
319: bu_saveNode.addActionListener(new ActionListener() {
320: public void actionPerformed(ActionEvent e) {
321: saveEditFields();
322: }
323: });
324:
325: bu_deleteNode = new SButton("Delete", new SURLIcon(
326: "../images/delete.gif"));
327: bu_deleteNode.setDisabledIcon(new SURLIcon(
328: "../images/delete_disabled.gif"));
329: bu_deleteNode.setPreferredSize(BU_DIM);
330: bu_deleteNode.setHorizontalAlignment(SConstants.CENTER_ALIGN);
331: bu_deleteNode.addActionListener(new ActionListener() {
332: public void actionPerformed(ActionEvent e) {
333: Element categoryNode = getSelectedCategory();
334: if (categoryNode == null) {
335: throw new IllegalStateException(
336: "a category must be selected for deletion");
337: }
338:
339: Node[] path = { rootNode };
340: int[] childIndices = { treeModel.getIndexOfChild(
341: rootNode, categoryNode) };
342: Node[] children = { categoryNode };
343: categoryNode.detach();
344: treeModel.fireTreeNodesRemoved(new TreeModelEvent(
345: LogConfig.this , path, childIndices, children));
346:
347: clearEditFields();
348: rb_insertNode.setSelected(true);
349: rb_updateNode.setEnabled(false);
350: bu_deleteNode.setEnabled(false);
351: }
352: });
353: bu_deleteNode.setEnabled(false);
354:
355: bu_commitChanges = new SButton("Commit", new SURLIcon(
356: "../images/commit.gif"));
357: bu_commitChanges.setPreferredSize(BU_DIM);
358: bu_commitChanges
359: .setHorizontalAlignment(SConstants.CENTER_ALIGN);
360: bu_commitChanges.addActionListener(new ActionListener() {
361: public void actionPerformed(ActionEvent e) {
362: try {
363: XMLWriter writer = new XMLWriter(new FileWriter(
364: log4jXmlPath), OutputFormat
365: .createPrettyPrint());
366: writer.write(document);
367: writer.close();
368: la_status
369: .setText(" Your changes have been successfully written to 'log4j.xml'!");
370: } catch (IOException ex) {
371: log.error("Could not write file!", ex);
372: la_status
373: .setText(" Couldn't write changes to 'log4j.xml'! See log for details.");
374: }
375: }
376: });
377:
378: SPanel pa_edit = createControlPanel("Insert / update category nodes:");
379: SPanel pa_mode = new SPanel(new SGridLayout(2));
380: pa_mode.setStyle(pa_mode.getStyle() + " pa_mode");
381: pa_mode.setPreferredSize(SDimension.FULLWIDTH);
382: rb_insertNode.setHorizontalAlignment(SConstants.LEFT_ALIGN);
383: rb_updateNode.setHorizontalAlignment(SConstants.RIGHT_ALIGN);
384: pa_mode.add(rb_insertNode);
385: pa_mode.add(rb_updateNode);
386: pa_edit.add(pa_mode);
387: pa_edit.add(new SLabel("Category name:"));
388: pa_edit.add(tf_editCategoryName);
389: pa_edit.add(new SLabel("Priority value:"));
390: pa_edit.add(tf_editPriorityValue);
391: pa_edit.add(new SLabel("Additivity flag:"));
392: pa_edit.add(cb_editAdditivityFlag);
393: pa_edit.add(new SLabel("Appender reference:"));
394: pa_edit.add(li_editAppenderRef);
395: pa_edit.add(verticalSpace(0));
396: pa_edit.add(bu_saveNode);
397:
398: SPanel pa_delete = createControlPanel("Delete selected category node:");
399: pa_delete.add(bu_deleteNode);
400:
401: SPanel pa_commit = createControlPanel("Commit changes to 'log4j.xml':");
402: pa_commit.add(bu_commitChanges);
403:
404: SLabel la_activityIndicator = new SLabel(
405: " Loading data, wait!", new SURLIcon(
406: "../images/progress.gif"));
407: la_activityIndicator.setName("ajaxActivityIndicator");
408:
409: SPanel pa_controls = new SPanel(new SBoxLayout(
410: SBoxLayout.VERTICAL));
411: pa_controls.setVerticalAlignment(SConstants.TOP_ALIGN);
412: pa_controls.add(pa_edit);
413: pa_controls.add(verticalSpace(10));
414: pa_controls.add(pa_delete);
415: pa_controls.add(verticalSpace(10));
416: pa_controls.add(pa_commit);
417: pa_controls.add(verticalSpace(10));
418: pa_controls.add(la_activityIndicator);
419:
420: SPanel pa_main = new SPanel(new SGridBagLayout());
421: pa_main.setPreferredSize(SDimension.FULLAREA);
422: pa_main.setStyle(pa_main.getStyle() + " pa_main");
423: GridBagConstraints gbc = new GridBagConstraints();
424: gbc.insets = new Insets(10, 10, 10, 5);
425: gbc.gridx = 0;
426: gbc.gridy = 0;
427: gbc.weightx = 1;
428: gbc.weighty = 1;
429: pa_main.add(sp_tree, gbc);
430: gbc.insets = new Insets(10, 5, 10, 10);
431: gbc.gridx = 1;
432: gbc.gridy = 0;
433: gbc.weightx = 0;
434: gbc.weighty = 1;
435: pa_main.add(pa_controls, gbc);
436: return pa_main;
437: }
438:
439: private void testSomething(boolean enabled) {
440: if (!enabled)
441: return;
442:
443: // TESTING BACK BUTTON & HISTORY
444: fr_frame.setNoCaching(false);
445: tr_domTree.setEpochCheckEnabled(false);
446: fo_form.setPostMethod(false);
447:
448: // TESTING ON-CHANGE-SUBMIT-LISTENERS
449: bg_insertOrUpdate.addActionListener(new ActionListener() {
450: public void actionPerformed(ActionEvent e) {
451: log.info("ActionListener of ButtonGroup!!!");
452: }
453: });
454: cb_editAdditivityFlag.addItemListener(new ItemListener() {
455: public void itemStateChanged(ItemEvent e) {
456: log.info("ItemListener of ComboBox!!!");
457: }
458: });
459: li_editAppenderRef
460: .addListSelectionListener(new ListSelectionListener() {
461: public void valueChanged(ListSelectionEvent e) {
462: log.info("ListSelectionListener of List!!!");
463: }
464: });
465: tf_editCategoryName
466: .addDocumentListener(new SDocumentListener() {
467: public void changedUpdate(SDocumentEvent e) {
468: log
469: .info("DocumentListener of TextField - changed!!!");
470: }
471:
472: public void insertUpdate(SDocumentEvent e) {
473: log
474: .info("DocumentListener of TextField - insert!!!");
475: }
476:
477: public void removeUpdate(SDocumentEvent e) {
478: log
479: .info("DocumentListener of TextField - remove!!!");
480: }
481: });
482: }
483:
484: // private Vector getExpandedPaths() {
485: // Vector expandedPaths = new Vector();
486: // for (int i = 0; i < tr_domTree.getRowCount(); ++i) {
487: // TreePath path = tr_domTree.getPathForRow(i);
488: // if (tr_domTree.isExpanded(path)) {
489: // expandedPaths.add(path);
490: // }
491: // }
492: // return expandedPaths;
493: // }
494: //
495: // private void setExpandedPaths(Vector expandedPaths) {
496: // for (int i = 0; i < expandedPaths.size(); ++i) {
497: // tr_domTree.expandRow((TreePath) expandedPaths.get(i));
498: // }
499: // }
500:
501: private void saveEditFields() {
502: String catName = tf_editCategoryName.getText();
503: String priValue = tf_editPriorityValue.getText();
504: String addFlag = cb_editAdditivityFlag.getSelectedItem()
505: .toString();
506: Object[] appRefNames = li_editAppenderRef.getSelectedValues();
507:
508: if (catName.equals("") || priValue.equals("")) {
509: la_status
510: .setText(" You have to provide at least a category name and a priority value!");
511: return;
512: }
513:
514: if (rb_insertNode.isSelected()) {
515: Element categoryNode = DocumentHelper.createElement(
516: "category").addAttribute("name", catName);
517: categoryNode.addElement("priority").addAttribute("value",
518: priValue);
519: categoryNode.addAttribute("additivity", addFlag);
520: for (int i = 0; i < appRefNames.length; ++i) {
521: categoryNode.addElement("appender-ref").addAttribute(
522: "ref", appRefNames[i].toString());
523: }
524:
525: Node firstCategory = rootNode
526: .selectSingleNode("./category[1]");
527: if (firstCategory == null) {
528: rootNode.add(categoryNode);
529: } else {
530: rootNode.content().add(rootNode.indexOf(firstCategory),
531: categoryNode);
532: }
533: int index = rootNode.selectNodes("./* | ./@*").indexOf(
534: categoryNode);
535:
536: Node[] path = { rootNode };
537: int[] childIndices = { index };
538: Node[] children = { categoryNode };
539: treeModel.fireTreeNodesInserted(new TreeModelEvent(
540: LogConfig.this , path, childIndices, children));
541:
542: selectedNode = categoryNode;
543: Node[] selectedPath = { rootNode, categoryNode };
544: tr_domTree.setSelectionPath(new TreePath(selectedPath));
545:
546: fillEditFields(categoryNode);
547: } else if (rb_updateNode.isSelected()) {
548: Element categoryNode = getSelectedCategory();
549: if (categoryNode == null) {
550: throw new IllegalStateException(
551: "a category must be selected for update");
552: }
553: categoryNode.selectSingleNode("./@name").setText(catName);
554: categoryNode.selectSingleNode("./priority/@value").setText(
555: priValue);
556: categoryNode.selectSingleNode("./@additivity").setText(
557: addFlag);
558:
559: Node[] path = { rootNode, categoryNode };
560:
561: List oldAppRefNodes = categoryNode
562: .selectNodes("./appender-ref");
563: int[] oldChildIndices = new int[oldAppRefNodes.size()];
564: for (int i = 0; i < oldAppRefNodes.size(); ++i) {
565: Node oldAppenderNode = (Node) oldAppRefNodes.get(i);
566: oldChildIndices[i] = treeModel.getIndexOfChild(
567: categoryNode, oldAppenderNode);
568: oldAppenderNode.detach();
569: }
570: treeModel.fireTreeNodesRemoved(new TreeModelEvent(
571: LogConfig.this , path, oldChildIndices,
572: oldAppRefNodes.toArray()));
573:
574: List<Node> newAppRefNodes = new ArrayList<Node>();
575: int[] newChildIndices = new int[appRefNames.length];
576: for (int i = 0; i < appRefNames.length; ++i) {
577: Node newAppenderNode = (Node) categoryNode.addElement(
578: "appender-ref").addAttribute("ref",
579: appRefNames[i].toString());
580: newAppRefNodes.add(newAppenderNode);
581: newChildIndices[i] = treeModel.getIndexOfChild(
582: categoryNode, newAppenderNode);
583: }
584: treeModel.fireTreeNodesInserted(new TreeModelEvent(
585: LogConfig.this , path, newChildIndices,
586: newAppRefNodes.toArray()));
587:
588: selectedNode = categoryNode;
589: tr_domTree.setSelectionPath(new TreePath(path));
590: }
591: }
592:
593: private void clearEditFields() {
594: tf_editCategoryName.setText("");
595: tf_editPriorityValue.setText("");
596: cb_editAdditivityFlag.setSelectedItem("true");
597: li_editAppenderRef.clearSelection();
598: bu_saveNode.setText("Insert");
599: bu_saveNode.setIcon(INSERT_IMG);
600: resetStatusLabel();
601: }
602:
603: private void fillEditFields(Node categoryNode) {
604: tf_editCategoryName.setText(categoryNode.valueOf("./@name"));
605: tf_editPriorityValue.setText(categoryNode
606: .valueOf("./priority/@value"));
607: cb_editAdditivityFlag.setSelectedItem(categoryNode
608: .valueOf("./@additivity"));
609: List appenderRefs = categoryNode
610: .selectNodes("./appender-ref/@ref");
611: int[] selectedIndices = new int[appenderRefs.size()];
612: for (int i = 0; i < appenderRefs.size(); ++i) {
613: selectedIndices[i] = listModel.indexOf(((Node) appenderRefs
614: .get(i)).getText());
615: }
616: li_editAppenderRef.setSelectedIndices(selectedIndices);
617: bu_saveNode.setText("Update");
618: bu_saveNode.setIcon(UPDATE_IMG);
619: resetStatusLabel();
620: }
621:
622: private void resetStatusLabel() {
623: la_status
624: .setText(" Usage: Insert, update or delete category nodes by means of the"
625: + " according widgets - then commit all changes to 'log4j.xml'.");
626: }
627:
628: private Element getSelectedCategory() {
629: if (selectedNode == null)
630: return null;
631: Node selectedCategoryNode = selectedNode
632: .selectSingleNode("ancestor-or-self::category");
633: if (selectedCategoryNode == null)
634: return null;
635:
636: List categoryElements = rootNode.elements("category");
637: for (Iterator i = categoryElements.iterator(); i.hasNext();) {
638: Element currentElement = (Element) i.next();
639: if (currentElement == selectedCategoryNode) {
640: return currentElement;
641: }
642: }
643: return null;
644: }
645:
646: private SComponent verticalSpace(int height) {
647: SLabel label = new SLabel();
648: label.setAttribute(CSSProperty.HEIGHT, height + "px");
649: return label;
650: }
651:
652: private SPanel createControlPanel(String title) {
653: SBoxLayout boxLayout = new SBoxLayout(SBoxLayout.VERTICAL);
654: boxLayout.setHgap(20);
655: boxLayout.setVgap(5);
656:
657: SPanel panel = new SPanel();
658: panel.setLayout(boxLayout);
659: panel.setStyle(panel.getStyle() + " pa_control");
660:
661: SLabel la_title = new SLabel(title);
662: la_title.setStyle(la_title.getStyle() + " la_title");
663:
664: panel.add(la_title);
665: panel.add(verticalSpace(0));
666: return panel;
667: }
668:
669: private SPanel createDebuggingPanel() {
670: SBoxLayout boxLayout = new SBoxLayout(SBoxLayout.VERTICAL);
671: boxLayout.setHgap(20);
672: boxLayout.setVgap(5);
673:
674: SPanel pa_debug = new SPanel(boxLayout);
675: pa_debug.setStyle(pa_debug.getStyle() + " pa_debug");
676: pa_debug.setVerticalAlignment(SConstants.TOP_ALIGN);
677:
678: SLabel la_title = new SLabel("Playground for debugging Ajax:");
679: la_title.setStyle(la_title.getStyle() + " la_title");
680: pa_debug.add(la_title);
681:
682: boolean selected;
683: final String[] cb_texts = { "Frame: updates enabled --> ",
684: "Frame: cursor enabled --> ",
685: "Form: reload forced --> ", "Tree: reload forced --> " };
686:
687: selected = fr_frame.isUpdateEnabled();
688: final SCheckBox cb_toggleFrameUpdateEnabled = new SCheckBox(
689: cb_texts[0] + selected, selected);
690: cb_toggleFrameUpdateEnabled
691: .addActionListener(new ActionListener() {
692: public void actionPerformed(ActionEvent e) {
693: boolean state = fr_frame.isUpdateEnabled();
694: fr_frame.setUpdateEnabled(!state);
695: cb_toggleFrameUpdateEnabled.setText(cb_texts[0]
696: + !state);
697: }
698: });
699:
700: Map<String, Object> updateCursor = fr_frame.getUpdateCursor();
701: selected = ((Boolean) updateCursor.get("enabled"))
702: .booleanValue();
703: final SCheckBox cb_toggleFrameUpdateCursor = new SCheckBox(
704: cb_texts[1] + selected, selected);
705: cb_toggleFrameUpdateCursor
706: .addActionListener(new ActionListener() {
707: public void actionPerformed(ActionEvent e) {
708: Map<String, Object> updateCursor = new HashMap<String, Object>(
709: fr_frame.getUpdateCursor());
710: boolean state = ((Boolean) updateCursor
711: .get("enabled")).booleanValue();
712: updateCursor.put("enabled", !state);
713: fr_frame.setUpdateCursor(updateCursor);
714: cb_toggleFrameUpdateCursor.setText(cb_texts[1]
715: + !state);
716: }
717: });
718:
719: selected = fo_form.isReloadForced();
720: final SCheckBox cb_toggleFormReloadForced = new SCheckBox(
721: cb_texts[2] + selected, selected);
722: cb_toggleFormReloadForced
723: .addActionListener(new ActionListener() {
724: public void actionPerformed(ActionEvent e) {
725: boolean state = fo_form.isReloadForced();
726: fo_form.setReloadForced(!state);
727: cb_toggleFormReloadForced.setText(cb_texts[2]
728: + !state);
729: }
730: });
731:
732: selected = tr_domTree.isReloadForced();
733: final SCheckBox cb_toggleTreeReloadForced = new SCheckBox(
734: cb_texts[3] + selected, selected);
735: cb_toggleTreeReloadForced
736: .addActionListener(new ActionListener() {
737: public void actionPerformed(ActionEvent e) {
738: boolean state = tr_domTree.isReloadForced();
739: tr_domTree.setReloadForced(!state);
740: cb_toggleTreeReloadForced.setText(cb_texts[3]
741: + !state);
742: }
743: });
744:
745: final SButton bu_markFrameDirty = new SButton(
746: "Reload the entire frame (mark it dirty)");
747: bu_markFrameDirty.addActionListener(new ActionListener() {
748: public void actionPerformed(ActionEvent e) {
749: fr_frame.reload();
750: }
751: });
752:
753: final SButton bu_doSomethingSpecial = new SButton(
754: "Do something special for 10 seconds");
755: bu_doSomethingSpecial.addActionListener(new ActionListener() {
756: public void actionPerformed(ActionEvent e) {
757: try {
758: Thread.sleep(10000);
759: } catch (InterruptedException e1) {
760: }
761: }
762: });
763:
764: final SButton bu_forceServerError = new SButton(
765: "Force a stupid NullPointerException");
766: bu_forceServerError.addActionListener(new ActionListener() {
767: public void actionPerformed(ActionEvent e) {
768: String error = null;
769: error.toString();
770: }
771: });
772:
773: final SAnchor an_abortCurrentAjaxRequest = new SAnchor();
774: an_abortCurrentAjaxRequest.setLayout(new SBorderLayout());
775: an_abortCurrentAjaxRequest
776: .addScriptListener(new JavaScriptListener(
777: JavaScriptEvent.ON_CLICK,
778: "wingS.ajax.abortRequest(); return false;"));
779: an_abortCurrentAjaxRequest.add(new SLabel(
780: "Try to abort the current Ajax request"));
781:
782: final SButton bu_logDebug = new SButton(
783: "Log message with level: DEBUG");
784: bu_logDebug.addActionListener(new ActionListener() {
785: public void actionPerformed(ActionEvent e) {
786: log.debug("I'm a log message with level: DEBUG");
787: }
788: });
789:
790: final SButton bu_logInfo = new SButton(
791: "Log message with level: INFO");
792: bu_logInfo.addActionListener(new ActionListener() {
793: public void actionPerformed(ActionEvent e) {
794: log.info("I'm a log message with level: INFO");
795: }
796: });
797:
798: final SButton bu_logWarn = new SButton(
799: "Log message with level: WARN");
800: bu_logWarn.addActionListener(new ActionListener() {
801: public void actionPerformed(ActionEvent e) {
802: if (log.isWarnEnabled())
803: log.warn("I'm a log message with level: WARN");
804: }
805: });
806:
807: final SButton bu_logError = new SButton(
808: "Log message with level: ERROR");
809: bu_logError.addActionListener(new ActionListener() {
810: public void actionPerformed(ActionEvent e) {
811: log.error("I'm a log message with level: ERROR");
812: }
813: });
814:
815: final SButton bu_logFatal = new SButton(
816: "Log message with level: FATAL");
817: bu_logFatal.addActionListener(new ActionListener() {
818: public void actionPerformed(ActionEvent e) {
819: log.fatal("I'm a log message with level: FATAL");
820: }
821: });
822:
823: final SButton bu_toggleAjaxDebugView = new SButton(
824: "Toggle Ajax debug view --- (Shift F12)");
825: bu_toggleAjaxDebugView
826: .addScriptListener(new JavaScriptListener(
827: JavaScriptEvent.ON_CLICK,
828: "wingS.ajax.toggleDebugView(); return false;"));
829:
830: addToDebuggingPanel(pa_debug, verticalSpace(0));
831: addToDebuggingPanel(pa_debug, cb_toggleFrameUpdateEnabled);
832: addToDebuggingPanel(pa_debug, cb_toggleFrameUpdateCursor);
833: addToDebuggingPanel(pa_debug, cb_toggleFormReloadForced);
834: addToDebuggingPanel(pa_debug, cb_toggleTreeReloadForced);
835:
836: addToDebuggingPanel(pa_debug, verticalSpace(5));
837: addToDebuggingPanel(pa_debug, bu_markFrameDirty);
838: addToDebuggingPanel(pa_debug, bu_doSomethingSpecial);
839: addToDebuggingPanel(pa_debug, bu_forceServerError);
840: addToDebuggingPanel(pa_debug, an_abortCurrentAjaxRequest);
841:
842: addToDebuggingPanel(pa_debug, verticalSpace(5));
843: addToDebuggingPanel(pa_debug, createRandomResultPanel());
844:
845: addToDebuggingPanel(pa_debug, verticalSpace(5));
846: addToDebuggingPanel(pa_debug, bu_logDebug);
847: addToDebuggingPanel(pa_debug, bu_logInfo);
848: addToDebuggingPanel(pa_debug, bu_logWarn);
849: addToDebuggingPanel(pa_debug, bu_logError);
850: addToDebuggingPanel(pa_debug, bu_logFatal);
851:
852: addToDebuggingPanel(pa_debug, verticalSpace(5));
853: addToDebuggingPanel(pa_debug, bu_toggleAjaxDebugView);
854:
855: return pa_debug;
856: }
857:
858: private void addToDebuggingPanel(SPanel debug, SComponent component) {
859: component.setHorizontalAlignment(SConstants.LEFT_ALIGN);
860: component.setShowAsFormComponent(false);
861: debug.add(component);
862: }
863:
864: private SPanel createRandomResultPanel() {
865: int links = 6;
866:
867: class RandomResultGenerator implements ActionListener {
868: private SLabel label;
869: private boolean sleep;
870:
871: public RandomResultGenerator(SLabel label, boolean sleep) {
872: this .label = label;
873: this .sleep = sleep;
874: }
875:
876: public void actionPerformed(ActionEvent e) {
877: if (sleep) {
878: int ms = new Random().nextInt(3001);
879: try {
880: Thread.sleep(ms);
881: } catch (InterruptedException e1) {
882: }
883: label.setText(ms + " ms");
884: } else {
885: int nr = new Random().nextInt(9000) + 1000;
886: label.setText(nr + "");
887: }
888: }
889: }
890:
891: SPanel panel = new SPanel(new SGridLayout(links, 3));
892: for (int i = 0; i < links; ++i) {
893: SLabel label = new SLabel();
894: SButton button = new SButton();
895: button.setShowAsFormComponent(false);
896: button.setHorizontalAlignment(SConstants.LEFT_ALIGN);
897: if (i < links - 1) {
898: button.setText("Sleep for a while! >>");
899: button.addActionListener(new RandomResultGenerator(
900: label, true));
901: } else {
902: button.setText("Say any number! >>");
903: button.addActionListener(new RandomResultGenerator(
904: label, false));
905: panel.add(new SSpacer(0, 10));
906: panel.add(new SSpacer(0, 5));
907: panel.add(new SSpacer(0, 10));
908: }
909: panel.add(button);
910: panel.add(new SSpacer(0, 5));
911: panel.add(label);
912: }
913: return panel;
914: }
915:
916: }
|