Skip to content

Commit

Permalink
Admin tree adapted to Wicket 8
Browse files Browse the repository at this point in the history
  • Loading branch information
twagoo committed Jan 27, 2020
1 parent 73ac6de commit 47df138
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<div class="container">
<div class="treeView">
<a wicket:id="reload">[Reload]</a>
<span wicket:id="tree">tree</span>
</div>
<div wicket:id="infoView" class="infoView">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.security.Principal;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

Expand Down Expand Up @@ -35,12 +34,17 @@
import clarin.cmdi.componentregistry.model.ProfileDescription;
import clarin.cmdi.componentregistry.model.RegistryUser;
import clarin.cmdi.componentregistry.persistence.ComponentDao;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.DefaultNestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.NestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.content.Folder;
import org.apache.wicket.extensions.markup.html.repeater.util.TreeModelProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
Expand All @@ -56,7 +60,7 @@ public class AdminHomePage extends SecureAdminWebPage {
private static final long serialVersionUID = 1L;
private final static Logger LOG = LoggerFactory.getLogger(AdminHomePage.class);
private final CMDItemInfo info;
private AbstractTree tree;
private NestedTree<AdminTreeNode> tree;
private transient AdminRegistry adminRegistry = new AdminRegistry();
@SpringBean(name = "componentRegistryFactory")
private ComponentRegistryFactory componentRegistryFactory;
Expand All @@ -69,6 +73,8 @@ public class AdminHomePage extends SecureAdminWebPage {

private Component infoView;

private IModel<Set<AdminTreeNode>> expansionModel = new Model(new HashSet<>());

public AdminHomePage(final PageParameters parameters) throws ComponentRegistryException, ItemNotFoundException {
super(parameters);
adminRegistry.setComponentRegistryFactory(componentRegistryFactory);
Expand Down Expand Up @@ -98,6 +104,22 @@ public AdminHomePage(final PageParameters parameters) throws ComponentRegistryEx
error("Cannot create tree: error = " + e);
}

add(new IndicatingAjaxLink<Void>("reload") {
@Override
public void onClick(AjaxRequestTarget target) {
try {
reloadTreeModel(info);
target.add(tree);
} catch (ItemNotFoundException | UserUnauthorizedException ex) {
error(ex.getMessage());
}
}
});
}

@Override
public final MarkupContainer add(Component... children) {
return super.add(children);
}

private Form createPublishDeleteForm() throws ItemNotFoundException, ComponentRegistryException {
Expand All @@ -115,7 +137,7 @@ protected void onSubmit(AjaxRequestTarget target) {
adminRegistry.delete(info, userPrincipal);
info("Item deleted.");
reloadTreeModel(info);
} catch (Exception e) {
} catch (ItemNotFoundException | UserUnauthorizedException | SubmitFailedException e) {
LOG.error("Admin: ", e);
error("Cannot delete: " + info.getName() + "\n error=" + e);
}
Expand All @@ -139,7 +161,7 @@ protected void onSubmit(AjaxRequestTarget target) {
adminRegistry.undelete(info);
info("Item put back.");
reloadTreeModel(info);
} catch (Exception e) {
} catch (ItemNotFoundException | UserUnauthorizedException | SubmitFailedException e) {
LOG.error("Admin: ", e);
error("Cannot undelete: " + info.getName() + "\n error=" + e);
}
Expand Down Expand Up @@ -192,9 +214,7 @@ protected void onSubmit(AjaxRequestTarget target) {
try {
reloadTreeModel(info);
target.add(tree);
} catch (UserUnauthorizedException ex) {
LOG.error("error reloading tree model", ex);
} catch (ItemNotFoundException ex) {
} catch (UserUnauthorizedException | ItemNotFoundException ex) {
LOG.error("error reloading tree model", ex);
}
}
Expand All @@ -210,19 +230,19 @@ public boolean isEnabled() {
}

private void submitEditForm(Form<?> form, FeedbackPanel feedback, AjaxRequestTarget target, boolean publish) throws DataAccessException {
CMDItemInfo info = (CMDItemInfo) form.getModelObject();
final CMDItemInfo itemInfo = (CMDItemInfo) form.getModelObject();
Principal userPrincipal = getUserPrincipal();
info("submitting:" + info.getName() + " id=(" + info.getId() + ")");
info("submitting:" + itemInfo.getName() + " id=(" + itemInfo.getId() + ")");
try {
adminRegistry.submitFile(info, userPrincipal, publish);
adminRegistry.submitFile(itemInfo, userPrincipal, publish);
info("submitting done.");

final BaseDescription newDescr = componentDao.getByCmdId(info.getId());
info.setDescription(ComponentUtils.toTypeByIdPrefix(newDescr));
info.setName(newDescr.getName());
} catch (Exception e) {
final BaseDescription newDescr = componentDao.getByCmdId(itemInfo.getId());
itemInfo.setDescription(ComponentUtils.toTypeByIdPrefix(newDescr));
itemInfo.setName(newDescr.getName());
} catch (ComponentRegistryException | UserUnauthorizedException | SubmitFailedException | DataAccessException e) {
LOG.error("Admin: ", e);
error("Cannot submit: " + info.getName() + "\n error=" + e);
error("Cannot submit: " + itemInfo.getName() + "\n error=" + e);
}

if (target != null) {
Expand All @@ -233,24 +253,25 @@ private void submitEditForm(Form<?> form, FeedbackPanel feedback, AjaxRequestTar

private void reloadTreeModel(CMDItemInfo info) throws UserUnauthorizedException, ItemNotFoundException {
try {
tree.setModelObject(createDBTreeModel());
tree = createTree("tree", createDBTreeModel());
addOrReplace(tree);
} catch (ComponentRegistryException e) {
LOG.error("Admin: ", e);
error("Cannot reload tree: " + info.getName() + "\n error=" + e);
}
}

private AbstractTree createTree(String id, TreeModel treeModel) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
TreeModelProvider<DefaultMutableTreeNode> provider = new TreeModelProvider<DefaultMutableTreeNode>(treeModel) {
private NestedTree<AdminTreeNode> createTree(String id, TreeModel treeModel) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
final TreeModelProvider<AdminTreeNode> treeModelProvider = new TreeModelProvider<AdminTreeNode>(treeModel) {
@Override
public IModel<DefaultMutableTreeNode> model(DefaultMutableTreeNode object) {
public IModel<AdminTreeNode> model(AdminTreeNode object) {
return Model.of(object);
}

};
final AbstractTree adminTree = new DefaultNestedTree<>(id, provider) {
final NestedTree<AdminTreeNode> adminTree = new DefaultNestedTree<>(id, treeModelProvider, expansionModel) {
@Override
protected Component newContentComponent(String id, IModel<DefaultMutableTreeNode> nodeModel) {
protected Component newContentComponent(String id, IModel<AdminTreeNode> nodeModel) {
if (nodeModel.getObject().isLeaf()) {
return new AdminTreeItemLeafNode(id, tree, nodeModel);
} else {
Expand All @@ -265,8 +286,8 @@ protected Component newContentComponent(String id, IModel<DefaultMutableTreeNode
}

private TreeModel createDBTreeModel() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new DisplayDataNode("ComponentRegistry", false));
DefaultMutableTreeNode publicNode = new DefaultMutableTreeNode(new DisplayDataNode("Public", false));
AdminTreeNode rootNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("ComponentRegistry", false));
AdminTreeNode publicNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("Public", false));
rootNode.add(publicNode);
ComponentRegistry publicRegistry = componentRegistryFactory.getPublicRegistry();
add(publicNode, publicRegistry);
Expand All @@ -288,42 +309,42 @@ private TreeModel createDBTreeModel() throws ComponentRegistryException, UserUna
return model;
}

private void addRegistry(DefaultMutableTreeNode rootNode, ComponentRegistry registry, String name) throws UserUnauthorizedException, ItemNotFoundException, ComponentRegistryException {
DefaultMutableTreeNode userNode = new DefaultMutableTreeNode(new DisplayDataNode(name, false));
private void addRegistry(AdminTreeNode rootNode, ComponentRegistry registry, String name) throws UserUnauthorizedException, ItemNotFoundException, ComponentRegistryException {
AdminTreeNode userNode = new AdminTreeNode(DisplayDataNode.newNonItemNode(name, false));
rootNode.add(userNode);
add(userNode, registry);
}

private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
DefaultMutableTreeNode componentsNode = new DefaultMutableTreeNode(new DisplayDataNode("Components", false));
private void add(AdminTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
AdminTreeNode componentsNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("Components", false));
parent.add(componentsNode);
add(componentsNode, registry.getComponentDescriptions(null), false, registry.getRegistrySpace());

DefaultMutableTreeNode profilesNode = new DefaultMutableTreeNode(new DisplayDataNode("Profiles", false));
AdminTreeNode profilesNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("Profiles", false));
parent.add(profilesNode);
add(profilesNode, registry.getProfileDescriptions(null), false, registry.getRegistrySpace());

DefaultMutableTreeNode deletedCompNode = new DefaultMutableTreeNode(new DisplayDataNode("Deleted Components", true));
AdminTreeNode deletedCompNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("Deleted Components", true));
parent.add(deletedCompNode);

List<ComponentDescription> deletedComponentDescriptions = registry.getDeletedComponentDescriptions();
add(deletedCompNode, deletedComponentDescriptions, true, registry.getRegistrySpace());

DefaultMutableTreeNode deletedProfNode = new DefaultMutableTreeNode(new DisplayDataNode("Deleted Profiles", true));
AdminTreeNode deletedProfNode = new AdminTreeNode(DisplayDataNode.newNonItemNode("Deleted Profiles", true));
parent.add(deletedProfNode);
List<ProfileDescription> deletedProfileDescriptions = registry.getDeletedProfileDescriptions();
add(deletedProfNode, deletedProfileDescriptions, true, registry.getRegistrySpace());
}

private void add(DefaultMutableTreeNode parent, List<? extends BaseDescription> descs, boolean isDeleted, RegistrySpace space) {
private void add(AdminTreeNode parent, List<? extends BaseDescription> descs, boolean isDeleted, RegistrySpace space) {
for (BaseDescription desc : descs) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(new DisplayDataNode(desc.getName(), isDeleted, desc, space));
AdminTreeNode child = new AdminTreeNode(new DisplayDataNode(desc.getName(), true, isDeleted, desc, space));
parent.add(child);
}
}

@Override
protected void addLinks() {
protected final void addLinks() {
//no call to super - no home link needed
}

Expand All @@ -345,9 +366,9 @@ public void onConfigure(Component component) {

}

private class AdminTreeItemLeafNode extends Folder<DefaultMutableTreeNode> {
private class AdminTreeItemLeafNode extends Folder<AdminTreeNode> {

public AdminTreeItemLeafNode(String id, AbstractTree tree, IModel<DefaultMutableTreeNode> nodeModel) {
public AdminTreeItemLeafNode(String id, AbstractTree tree, IModel<AdminTreeNode> nodeModel) {
super(id, tree, nodeModel);
}

Expand All @@ -358,7 +379,7 @@ protected boolean isClickable() {

@Override
protected boolean isSelected() {
DisplayDataNode dn = (DisplayDataNode) getModelObject().getUserObject();
final DisplayDataNode dn = getModelObject().getUserObject();
if (info.getId() != null && dn.getDescription() != null) {
return dn.getDescription().getId().equals(info.getId());
}
Expand All @@ -368,7 +389,7 @@ protected boolean isSelected() {
@Override
protected void onClick(Optional<AjaxRequestTarget> target) {
try {
final DisplayDataNode dn = (DisplayDataNode) getModelObject().getUserObject();
final DisplayDataNode dn = getModelObject().getUserObject();
if (dn.getDescription() != null) {
//update description
dn.setDesc(ComponentUtils.toTypeByIdPrefix(componentDao.getDeletedById(dn.getDescription().getDbId())));
Expand All @@ -384,7 +405,7 @@ protected void onClick(Optional<AjaxRequestTarget> target) {
LOG.error("Error getting node data", ex);
getSession().error("Could not get data for node. See Tomcat log for details.");
}

target.ifPresent(t -> {
t.add(infoView);
t.add(tree);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 CLARIN ERIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package clarin.cmdi.componentregistry.frontend;

import javax.swing.tree.DefaultMutableTreeNode;

/**
*
* @author Twan Goosen <[email protected]>
*/
public class AdminTreeNode extends DefaultMutableTreeNode {

public AdminTreeNode() {
}

public AdminTreeNode(DisplayDataNode userObject) {
super(userObject);
}

public AdminTreeNode(DisplayDataNode userObject, boolean allowsChildren) {
super(userObject, allowsChildren);
}

@Override
public DisplayDataNode getUserObject() {
return (DisplayDataNode) userObject;
}

@Override
public boolean equals(Object obj) {
if (userObject instanceof DisplayDataNode && obj instanceof AdminTreeNode) {
return userObject.equals(((AdminTreeNode)obj).userObject);
} else {
return super.equals(obj);
}
}

@Override
public int hashCode() {
if (userObject instanceof DisplayDataNode) {
return userObject.hashCode();
} else {
return super.hashCode();
}
}

}
Loading

0 comments on commit 47df138

Please sign in to comment.