Example usage for org.dom4j Node getUniquePath

List of usage examples for org.dom4j Node getUniquePath

Introduction

In this page you can find the example usage for org.dom4j Node getUniquePath.

Prototype

String getUniquePath();

Source Link

Document

Returns the XPath expression which will return a nodeset of one node which is the current node.

Usage

From source file:com.cladonia.xngreditor.actions.ToolsAddNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from   w ww .ja va  2 s  .  c o  m*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsAddNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError("Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }

    dialog.show(document.getDeclaredNamespaces(), currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Adding Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = addNode(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            (String) dialog.nodeTypeCombo.getSelectedItem(),
                            (String) dialog.namespaceCombo.getSelectedItem(), dialog.prefixTextField.getText(),
                            dialog.nameTextField.getText(), dialog.valueTextField.getText());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Error - Cannot Add Nodes", "Add Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsAddNodeToNamespaceAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from   w  w  w . j a  va 2s.c o  m*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsAddNodeToNamespaceDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    //create temporary document
    dialog.show(document.getDeclaredNamespaces(), currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Adding Nodes To Namespace ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                    String uri = (String) dialog.nsURICombo.getSelectedItem();
                    String prefix = dialog.nsPrefixTextField.getText();
                    if (uri.equalsIgnoreCase("none")) {
                        uri = "";
                    }
                    if (prefix.equalsIgnoreCase("none")) {
                        prefix = "";
                    }
                    Namespace ns = new Namespace(prefix, uri);

                    String newString = ToolsAddNodeToNamespaceAction.this.addNamespaceToNode(tempDoc,
                            dialog.xpathPanel.getXpathPredicate(), ns);

                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Add Nodes To Namespace",
                            "Tools Add Nodes To Namespace Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsCapitalizeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//  w  w  w  . j  av  a2s.c  o m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsCapitalizeDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {

        parent.setWait(true);
        parent.setStatus("Changing Capitals ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {

                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {
                        String newDocument = null;
                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                        boolean TRAVERSE_CHILDREN = true;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = ToolsCapitalizeAction.this.capitalize(tempDoc, xpathPredicate,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new capitalize document
                            newDocument = ToolsCapitalizeAction.this.capitalize(tempDoc,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);

                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Capitalize Document", "Tools Capitalize Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //            }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsConvertNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called after a user action.
 * /*from w w w .j a  va 2 s.c om*/
 * @param event
 *            the action event.
 */
public void actionPerformed(ActionEvent event) {

    if (dialog == null) {
        dialog = new ToolsConvertNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();
    //get the document
    final ExchangerDocument document = parent.getDocument();
    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Converting Nodes ...");
        // Run in Thread!!!
        Runnable runner = new Runnable() {

            public void run() {

                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                    String newString = convertNode(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            (String) dialog.nodeTypeCombo.getSelectedItem());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Convert Nodes", "Tools Convert Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };
        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //        }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsDeCapitalizeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//from w  ww.  j av a2s. com
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsDeCapitalizeDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Changing Capitals ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    //make sure one of the options was selected
                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {

                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                        String newDocument = null;

                        boolean TRAVERSE_CHILDREN = true;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = deCapitalize(tempDoc, xpathPredicate,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new deCapitalize document
                            newDocument = ToolsDeCapitalizeAction.this.deCapitalize(tempDoc,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot DeCapitalize Document",
                            "Tools DeCapitalize Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsLowercaseAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from w w  w. j  av a  2  s.  c om*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsLowercaseDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Changing Case ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    //make sure one of the options was selected
                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {

                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                        boolean TRAVERSE_CHILDREN = true;
                        String newDocument = null;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = lowercase(tempDoc, xpathPredicate, dialog.elementsRadio.isSelected(),
                                    dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new lowercase document
                            newDocument = lowercase(tempDoc, dialog.elementsRadio.isSelected(),
                                    dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }

                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Convert To Lowercase", "Tools Lowercase Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsRemoveNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from   www  . j  a v  a2  s  .  c om*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsRemoveNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Removing Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = removeByXPath(tempDoc, dialog.xpathPanel.getXpathPredicate());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Remove Nodes", "Tools Remove Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsRenameNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*w  ww .ja  v a 2 s.  co  m*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsRenameNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }

    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Renaming Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {

                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = renameByXPath(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            dialog.getNewName());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Rename Nodes", "Tools Rename Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsSetNodeValueAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from w  w  w .  ja  va  2s  .c o  m*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsSetNodeValueDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Setting Node Value ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = setNodeValue(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            dialog.valueTextField.getText());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Set Node Value", "Tools Set Node Value Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsSortNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called after a user action.
 * /*from   w w w .java 2 s  .  c om*/
 * @param event
 *            the action event.
 */
public void actionPerformed(ActionEvent event) {

    if (dialog == null) {
        dialog = new ToolsSortNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();
    //get the document
    final ExchangerDocument document = parent.getDocument();
    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }
    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);
    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Sorting Nodes ...");
        // Run in Thread!!!
        Runnable runner = new Runnable() {

            public void run() {

                try {
                    dialog.mappingPanel.save();
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                    String newDocument = sortByXPath(tempDoc, dialog.getXpathPredicate(),
                            dialog.getSortXpathPredicate(), dialog.ascendingRadio.isSelected());
                    if (newDocument != null) {
                        if (dialog.toNewDocumentRadio.isSelected()) {
                            //user has selected to create the result as a
                            // new document
                            parent.open(new ExchangerDocument(newDocument), null);
                        } else {
                            parent.getView().getEditor().setText(newDocument);
                            SwingUtilities.invokeLater(new Runnable() {

                                public void run() {

                                    parent.switchToEditor();
                                    parent.getView().updateModel();
                                }
                            });
                        }
                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Sort Nodes", "Tools Sort Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };
        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}