Example usage for javax.swing.tree TreeNode getParent

List of usage examples for javax.swing.tree TreeNode getParent

Introduction

In this page you can find the example usage for javax.swing.tree TreeNode getParent.

Prototype

TreeNode getParent();

Source Link

Document

Returns the parent TreeNode of the receiver.

Usage

From source file:org.onehippo.taxonomy.plugin.TaxonomyEditorPlugin.java

/**
 * Constructor which adds all the UI components.
 * The UI components include taxonomy tree, toolbar, and detail form container.
 * The detail form container holds all the category detail fields such as name, description and synonyms.
 *
 * @param context//from  w  ww.ja va  2 s  .com
 * @param config
 */
public TaxonomyEditorPlugin(final IPluginContext context, final IPluginConfig config) {
    super(context, config);

    final boolean editing = "edit".equals(config.getString("mode"));
    useUrlKeyEncoding = config.getAsBoolean("keys.urlencode", false);
    taxonomy = newTaxonomy(getModel(), editing);

    availableLanguageSelections = getAvailableLanguageSelections();
    currentLanguageSelection = new LanguageSelection(getLocale(), getLocale());

    synonymModel = new IModel<String[]>() {

        private static final long serialVersionUID = 1L;

        public String[] getObject() {
            EditableCategoryInfo info = taxonomy.getCategoryByKey(key)
                    .getInfo(currentLanguageSelection.getLanguageCode());
            return info.getSynonyms();
        }

        public void setObject(String[] object) {
            EditableCategoryInfo info = taxonomy.getCategoryByKey(key)
                    .getInfo(currentLanguageSelection.getLanguageCode());
            try {
                info.setSynonyms(object);
            } catch (TaxonomyException e) {
                redraw();
            }
        }

        public void detach() {
        }

    };
    final IModel<Taxonomy> taxonomyModel = new Model<Taxonomy>(taxonomy);
    String currentLanguageCode = currentLanguageSelection.getLanguageCode();
    tree = new TaxonomyTree("tree", new TaxonomyTreeModel(taxonomyModel, currentLanguageCode),
            currentLanguageCode) {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isEnabled() {
            return editing;
        }

        @Override
        protected void onNodeLinkClicked(AjaxRequestTarget target, TreeNode node) {
            if (node instanceof CategoryNode) {
                key = ((CategoryNode) node).getCategory().getKey();
                path = ((CategoryNode) node).getCategory().getPath();
            } else {
                key = null;
                path = null;
            }
            redraw();
            super.onNodeLinkClicked(target, node);
        }

    };
    tree.setOutputMarkupId(true);
    add(tree);
    if (editing) {
        TreeNode rootNode = (TreeNode) tree.getModelObject().getRoot();
        tree.getTreeState().selectNode(rootNode, true);
    }

    holder = new WebMarkupContainer("container-holder");
    holder.setOutputMarkupId(true);

    toolbarHolder = new WebMarkupContainer("toolbar-container-holder");
    toolbarHolder.setOutputMarkupId(true);
    AjaxLink<Void> addCategory = new AjaxLink<Void>("add-category") {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isEnabled() {
            return editing;
        }

        @Override
        public void onClick(AjaxRequestTarget target) {
            IDialogService dialogService = getDialogService();
            dialogService.show(new NewCategoryDialog(taxonomyModel, path) {

                private static final long serialVersionUID = 1L;

                @Override
                public boolean useKeyUrlEncoding() {
                    return useUrlKeyEncoding;
                }

                @Override
                public StringCodec getNodeNameCodec() {
                    final ISettingsService settingsService = getPluginContext()
                            .getService(ISettingsService.SERVICE_ID, ISettingsService.class);
                    final StringCodecFactory stringCodecFactory = settingsService.getStringCodecFactory();
                    final StringCodec stringCodec = stringCodecFactory.getStringCodec("encoding.node");
                    if (stringCodec == null) {
                        return new StringCodecFactory.UriEncoding();
                    }
                    return stringCodec;
                }

                @Override
                protected void onOk() {
                    EditableCategory category = taxonomy.getCategoryByKey(key);
                    TreeNode node;
                    if (category != null) {
                        node = new CategoryNode(new CategoryModel(taxonomyModel, key),
                                currentLanguageSelection.getLanguageCode());
                    } else {
                        node = new TaxonomyNode(taxonomyModel, currentLanguageSelection.getLanguageCode());
                    }
                    try {
                        String newKey = getKey();
                        if (category != null) {
                            category.addCategory(newKey, getName(), currentLanguageSelection.getLanguageCode());
                        } else {
                            taxonomy.addCategory(newKey, getName(), currentLanguageSelection.getLanguageCode());
                        }
                        TreeNode child = new CategoryNode(new CategoryModel(taxonomyModel, newKey),
                                currentLanguageSelection.getLanguageCode());
                        tree.getTreeState().selectNode(child, true);
                        key = newKey;
                    } catch (TaxonomyException e) {
                        error(e.getMessage());
                    }
                    tree.getTreeState().expandNode(node);
                    tree.markNodeChildrenDirty(node);
                    redraw();
                }
            });
        }

    };
    addCategory.add(new Image("add-category-icon",
            new ResourceReference(TaxonomyEditorPlugin.class, "res/new-category-16.png")));
    if (!editing) {
        addCategory.add(new AttributeAppender("class", new Model<String>("disabled"), " "));
    }
    toolbarHolder.add(addCategory);

    // <HCT>
    AjaxLink<Void> removeCategory = new AjaxLink<Void>("remove-category") {

        private static final long serialVersionUID = 5538299138211283825L;

        @Override
        protected IAjaxCallDecorator getAjaxCallDecorator() {
            return new AjaxPreprocessingCallDecorator(super.getAjaxCallDecorator()) {

                private static final long serialVersionUID = -7927968187160354605L;

                @Override
                public CharSequence preDecorateScript(final CharSequence script) {
                    return "if (confirm('" + getString("confirmDelete") + "'))" + "{" + script + "}";
                }
            };
        }

        @Override
        public boolean isEnabled() {
            return editing;
        }

        @Override
        public void onClick(final AjaxRequestTarget target) {
            EditableCategory category = taxonomy.getCategoryByKey(key);
            if (category != null) {
                CategoryNode node = (CategoryNode) tree.getTreeState().getSelectedNodes().iterator().next();
                TreeNode parent = node.getParent();

                try {
                    category.remove();

                    key = null;

                    tree.getTreeState().collapseNode(parent);
                    tree.getTreeState().expandNode(parent);
                    redraw();
                } catch (TaxonomyException e) {
                    LOG.error("Could not remove category {} [{}]",
                            new Object[] { category.getName(), category.getPath(), e });
                }
            }
        }
    };
    removeCategory.add(new Image("remove-category-icon",
            new ResourceReference(TaxonomyEditorPlugin.class, "res/remove-category-16.png")));
    if (!editing) {
        addCategory.add(new AttributeAppender("class", new Model<String>("disabled"), " "));
    }
    toolbarHolder.add(removeCategory);
    // </HCT>

    container = new Form("container") {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return taxonomy.getCategoryByKey(key) != null;
        }
    };

    ChoiceRenderer<LanguageSelection> choiceRenderer = new ChoiceRenderer<LanguageSelection>("displayName",
            "languageCode");
    DropDownChoice<LanguageSelection> languageSelectionChoice = new DropDownChoice<LanguageSelection>(
            "language", new PropertyModel<LanguageSelection>(this, "currentLanguageSelection"),
            availableLanguageSelections, choiceRenderer);
    languageSelectionChoice.add(new OnChangeAjaxBehavior() {

        private static final long serialVersionUID = -151291731388673682L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            redraw();
        }
    });
    languageSelectionChoice.setOutputMarkupId(true);
    languageSelectionChoice.setEnabled(!CollectionUtils.isEmpty(availableLanguageSelections));
    container.add(languageSelectionChoice);
    // show key value key:
    final Label label = new Label("widgetKey", new KeyModel());
    container.add(label);

    if (editing) {
        MarkupContainer name = new Fragment("name", "fragmentname", this);
        FormComponent<String> nameField = new TextField<String>("widget", new NameModel());
        nameField.add(new OnChangeAjaxBehavior() {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                tree.markNodeDirty(getSelectedNode());
            }
        });
        name.add(nameField);
        container.add(name);

        container.add(new TextAreaWidget("description", new DescriptionModel()));
    } else {
        container.add(new Label("name", new NameModel()));
        TextField<String> myKey = new TextField<String>("key");
        myKey.setVisible(false);
        container.add(myKey);
        container.add(new MultiLineLabel("description", new DescriptionModel()));
    }

    container.add(new RefreshingView<String>("view") {

        private static final long serialVersionUID = 1L;

        @Override
        protected Iterator<IModel<String>> getItemModels() {
            return getSynonymList().iterator();
        }

        @Override
        protected void populateItem(final Item<String> item) {
            item.add(new AjaxLink("up") {

                private static final long serialVersionUID = 1L;

                @Override
                public boolean isEnabled() {
                    return item.getIndex() > 0;
                }

                @Override
                public boolean isVisible() {
                    return editing;
                }

                @Override
                public void onClick(AjaxRequestTarget target) {
                    String[] synonyms = synonymModel.getObject();
                    int index = item.getIndex();
                    String tmp = synonyms[index];
                    synonyms[index] = synonyms[index - 1];
                    synonyms[index - 1] = tmp;
                    synonymModel.setObject(synonyms);
                    target.addComponent(holder);
                }
            });
            item.add(new AjaxLink("down") {

                private static final long serialVersionUID = 1L;

                @Override
                public boolean isEnabled() {
                    String[] synonyms = synonymModel.getObject();
                    return item.getIndex() < synonyms.length - 1;
                }

                @Override
                public boolean isVisible() {
                    return editing;
                }

                @Override
                public void onClick(AjaxRequestTarget target) {
                    String[] synonyms = synonymModel.getObject();
                    int index = item.getIndex();
                    String tmp = synonyms[index];
                    synonyms[index] = synonyms[index + 1];
                    synonyms[index + 1] = tmp;
                    synonymModel.setObject(synonyms);
                    target.addComponent(holder);
                }
            });
            item.add(new AjaxLink("remove") {

                private static final long serialVersionUID = 1L;

                @Override
                public boolean isVisible() {
                    return editing;
                }

                @Override
                public void onClick(AjaxRequestTarget target) {
                    String[] synonyms = synonymModel.getObject();
                    String[] syns = new String[synonyms.length - 1];
                    System.arraycopy(synonyms, 0, syns, 0, item.getIndex());
                    System.arraycopy(synonyms, item.getIndex() + 1, syns, item.getIndex(),
                            synonyms.length - item.getIndex() - 1);
                    synonymModel.setObject(syns);
                    target.addComponent(holder);
                }
            });
            if (editing) {
                TextFieldWidget input = new TextFieldWidget("synonym", item.getModel());
                FormComponent fc = (FormComponent) input.get("widget");
                fc.add(new MinimumLengthValidator(1));
                item.add(input);
            } else {
                item.add(new Label("synonym", item.getModel()));
            }
        }
    });

    container.add(new AjaxLink("add") {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return editing;
        }

        @Override
        public void onClick(AjaxRequestTarget target) {
            String[] synonyms = synonymModel.getObject();
            String[] newSyns = new String[synonyms.length + 1];
            System.arraycopy(synonyms, 0, newSyns, 0, synonyms.length);
            newSyns[synonyms.length] = "";
            synonymModel.setObject(newSyns);
            target.addComponent(holder);
        }

    });

    holder.add(container);
    add(toolbarHolder);
    add(holder);

    add(CSSPackageResource.getHeaderContribution(TaxonomyEditorPlugin.class, "res/style.css"));
}

From source file:org.zaproxy.zap.extension.ascan.CustomScanDialog.java

private TreePath getTechPath(TreeNode node) {
    List<TreeNode> list = new ArrayList<>();

    // Add all nodes to list
    while (node != null) {
        list.add(node);/*from w w w.j a  v  a  2  s.  com*/
        node = node.getParent();
    }
    Collections.reverse(list);

    // Convert array of nodes to TreePath
    return new TreePath(list.toArray());
}

From source file:org.zaproxy.zap.extension.customFire.TechnologyTreePanel.java

private TreePath getPath(TreeNode node) {
    List<TreeNode> list = new ArrayList<>();

    // Add all nodes to list
    while (node != null) {
        list.add(node);//from  w ww  . j  av  a2s .  c  o m
        node = node.getParent();
    }
    Collections.reverse(list);

    // Convert array of nodes to TreePath
    return new TreePath(list.toArray());
}

From source file:plugin.notes.gui.NotesTreeNode.java

/**
 * Gets the nodeAncestor attribute of the NotesTreeNode object
 *
 * @param node//from   w  w  w . j  a v a2s .  com
 *          Description of the Parameter
 * @return The nodeAncestor value
 */
public boolean isNodeAncestor(TreeNode node) {
    if (node == null) {
        return false;
    }

    TreeNode ancestor = this;

    do {
        if (ancestor == node) {
            return true;
        }
    } while ((ancestor = ancestor.getParent()) != null);

    return false;
}

From source file:plugin.notes.gui.NotesTreeNode.java

/**
 * Gets the nodeChild attribute of the NotesTreeNode object
 *
 * @param node// w  ww  .  j av a 2 s  .  c  o m
 *          Description of the Parameter
 * @return The nodeChild value
 */
public boolean isNodeChild(TreeNode node) {
    boolean retval;

    if (node == null) {
        retval = false;
    } else {
        if (getChildCount() == 0) {
            retval = false;
        } else {
            retval = (node.getParent() == this);
        }
    }

    return retval;
}