From 3bd0b79d45495d00a88391b8a9968fccb5307439 Mon Sep 17 00:00:00 2001 From: codistmonk Date: Wed, 7 Jul 2010 20:44:21 +0000 Subject: [PATCH] [Markups] Updated UI. Did some refactoring. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@138 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- .../src/net/sourceforge/aprog/markups/Markups.java | 48 +++++++++++++++++ .../sourceforge/aprog/markups/MarkupsActions.java | 34 ++++++++++++ .../aprog/markups/MarkupsComponents.java | 63 ++++++++-------------- .../aprog/markups/MarkupsConstants.java | 5 ++ 4 files changed, 110 insertions(+), 40 deletions(-) diff --git a/Markups/src/net/sourceforge/aprog/markups/Markups.java b/Markups/src/net/sourceforge/aprog/markups/Markups.java index be8112f..31903f1 100644 --- a/Markups/src/net/sourceforge/aprog/markups/Markups.java +++ b/Markups/src/net/sourceforge/aprog/markups/Markups.java @@ -30,10 +30,16 @@ import static net.sourceforge.aprog.tools.Tools.*; import static net.sourceforge.aprog.swing.SwingTools.*; import net.sourceforge.aprog.context.Context; +import net.sourceforge.aprog.events.Variable; +import net.sourceforge.aprog.events.Variable.ValueChangedEvent; import net.sourceforge.aprog.tools.IllegalInstantiationException; import net.sourceforge.aprog.xml.XMLTools; import net.sourceforge.jmacadapter.MacAdapterTools; +import org.w3c.dom.Node; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventListener; + /** * * @author codistmonk (creation 2010-07-03) @@ -79,10 +85,52 @@ public final class Markups { result.set(DOM, XMLTools.newDocument()); result.set(SELECTED_NODE, null); result.set(XPATH_EXPRESSION, null); + result.set(XPATH_RESULT, null); result.set(XPATH_ERROR, null); result.set(QUASI_XPATH_EXPRESSION, null); result.set(QUASI_XPATH_ERROR, null); + final Variable xPathExpressionVariable = result.getVariable(XPATH_EXPRESSION); + + xPathExpressionVariable.addListener(new Variable.Listener() { + + @Override + public final void valueChanged(final ValueChangedEvent event) { + MarkupsActions.evaluateXPathExpression(result); + } + + }); + + final EventListener domListener = new EventListener() { + + @Override + public final void handleEvent(final Event evt) { + MarkupsActions.evaluateXPathExpression(result); + } + + }; + + final Variable domVariable = result.getVariable(DOM); + + domVariable.addListener(new Variable.Listener() { + + @Override + public final void valueChanged(final ValueChangedEvent event) { + if (event.getOldValue() != null) { + XMLTools.removeDOMEventListener(event.getOldValue(), domListener); + } + + final Node dom = event.getNewValue(); + + if (dom != null) { + XMLTools.addDOMEventListener(dom, domListener); + } + + MarkupsActions.evaluateXPathExpression(result); + } + + }); + return result; } diff --git a/Markups/src/net/sourceforge/aprog/markups/MarkupsActions.java b/Markups/src/net/sourceforge/aprog/markups/MarkupsActions.java index 724837e..f50f2c9 100644 --- a/Markups/src/net/sourceforge/aprog/markups/MarkupsActions.java +++ b/Markups/src/net/sourceforge/aprog/markups/MarkupsActions.java @@ -287,4 +287,38 @@ public final class MarkupsActions { SubtitlesAdjusterActions.showTODOMessage(context); } + /** + * + * @param context + *
Not null + *
Input-output + */ + public static final void evaluateXPathExpression(final Context context) { + final Node node = context.get(SELECTED_NODE); + + try { + context.set(XPATH_RESULT, XMLTools.getNodes(node, (String) context.get(XPATH_EXPRESSION))); + context.set(XPATH_ERROR, null); + } catch (final Exception exception) { + context.set(XPATH_RESULT, null); + context.set(XPATH_ERROR, exception); + } + } + + /** + * + * @param context + *
Not null + *
Input-output + */ + public static final void evaluateQuasiXPathExpression(final Context context) { + try { + XMLTools.getOrCreateNode((Node) context.get(SELECTED_NODE), (String) context.get(QUASI_XPATH_EXPRESSION)); + + context.set(QUASI_XPATH_ERROR, null); + } catch (final Exception exception) { + context.set(QUASI_XPATH_ERROR, exception); + } + } + } diff --git a/Markups/src/net/sourceforge/aprog/markups/MarkupsComponents.java b/Markups/src/net/sourceforge/aprog/markups/MarkupsComponents.java index ef43c1f..4f77aeb 100644 --- a/Markups/src/net/sourceforge/aprog/markups/MarkupsComponents.java +++ b/Markups/src/net/sourceforge/aprog/markups/MarkupsComponents.java @@ -88,6 +88,7 @@ import net.sourceforge.jmacadapter.MacAdapterTools; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.w3c.dom.events.Event; import org.w3c.dom.events.EventListener; @@ -490,24 +491,30 @@ public final class MarkupsComponents { }); + final EventListener domListener = new EventListener() { + + @Override + public final void handleEvent(final Event event) { + result.repaint(); + } + + }; + final Variable selectedNodeVariable = context.getVariable(DOM); selectedNodeVariable.addListener(new Variable.Listener() { @Override public final void valueChanged(final ValueChangedEvent event) { + if (event.getOldValue() != null) { + removeDOMEventListener(event.getOldValue(), domListener); + } + final Node node = event.getNewValue(); debugPrint(node); - addDOMEventListener(node, new EventListener() { - - @Override - public final void handleEvent(final Event event) { - result.repaint(); - } - - }); + addDOMEventListener(node, domListener); } }); @@ -728,7 +735,7 @@ public final class MarkupsComponents { debugPrint(node, text); - if (node.getNodeValue().equals(text)) { + if (node == null || node.getNodeValue().equals(text)) { return; } @@ -903,24 +910,18 @@ public final class MarkupsComponents { final DefaultListModel model = new DefaultListModel(); final JList result = new JList(model); - final Variable xpathExpressionVariable = context.getVariable(XPATH_EXPRESSION); + final Variable xpathExpressionVariable = context.getVariable(XPATH_RESULT); - xpathExpressionVariable.addListener(new Variable.Listener() { + xpathExpressionVariable.addListener(new Variable.Listener() { @Override - public final void valueChanged(final ValueChangedEvent event) { + public final void valueChanged(final ValueChangedEvent event) { model.clear(); - final Node contextNode = context.get(SELECTED_NODE); - - try { - for (final Node node : toList(getNodes(contextNode, event.getNewValue()))) { + if (event.getNewValue() != null) { + for (final Node node : toList(event.getNewValue())) { model.addElement(node); } - - context.set(XPATH_ERROR, null); - } catch (final Exception exception) { - context.set(XPATH_ERROR, exception); } } @@ -997,26 +998,8 @@ public final class MarkupsComponents { public static final JButton newQuasiXPathCreateButton(final Context context) { final JButton result = translate(new JButton("Create Node")); - result.addActionListener(new ActionListener() { - - private Color defaultBackground; - - @Override - public final void actionPerformed(final ActionEvent event) { - if (this.defaultBackground == null) { - this.defaultBackground = result.getParent().getBackground(); - } - - try { - getOrCreateNode((Node) context.get(SELECTED_NODE), (String) context.get(QUASI_XPATH_EXPRESSION)); - - context.set(QUASI_XPATH_ERROR, null); - } catch (final Exception exception) { - context.set(QUASI_XPATH_ERROR, exception); - } - } - - }); + result.addActionListener(SwingTools.action( + MarkupsActions.class, "evaluateQuasiXPathExpression", context)); return result; } diff --git a/Markups/src/net/sourceforge/aprog/markups/MarkupsConstants.java b/Markups/src/net/sourceforge/aprog/markups/MarkupsConstants.java index 4aa2176..a3ab114 100644 --- a/Markups/src/net/sourceforge/aprog/markups/MarkupsConstants.java +++ b/Markups/src/net/sourceforge/aprog/markups/MarkupsConstants.java @@ -131,6 +131,11 @@ public final class MarkupsConstants { /** * {@value}. */ + public static final String XPATH_RESULT = "xPath.result"; + + /** + * {@value}. + */ public static final String XPATH_ERROR = "xPath.error"; /** -- 2.11.4.GIT