Skip to content

Commit

Permalink
#8 - refactored way of iterating component children, other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
activey committed Nov 2, 2016
1 parent d994fd9 commit c201da5
Show file tree
Hide file tree
Showing 21 changed files with 450 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public boolean hasMore() {
return index + 1 < idParts.length;
}

public CompositeId forward() {
public void forward() {
index = index + 1;
return this;
}

public String current() {
Expand Down
14 changes: 10 additions & 4 deletions licket-demo/src/main/java/org/licket/demo/view/ContactsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.licket.demo.model.Contacts;
import org.licket.demo.service.ContactsService;
import org.licket.semantic.component.modal.AbstractSemanticUIModal;
import org.licket.semantic.component.modal.ModalSettings;
import org.licket.semantic.component.modal.ModalSettingsBuilder;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -30,7 +31,6 @@ public class ContactsPanel extends AbstractLicketContainer<Contacts> {
private LicketRemote remoteCommunication;

private LicketComponentModelReloader modelReloader;
private AbstractSemanticUIModal modal;

public ContactsPanel(String id, LicketComponentModelReloader modelReloader) {
super(id, Contacts.class, emptyModel(), fromComponentContainerClass(ContactsPanel.class), modelReloader);
Expand All @@ -39,10 +39,10 @@ public ContactsPanel(String id, LicketComponentModelReloader modelReloader) {

@Override
protected void onInitializeContainer() {
add(modal = new AbstractSemanticUIModal("form-modal", builder().build(), modelReloader) {
add(new AbstractSemanticUIModal("form-modal", modalSettings()) {
@Override
protected void onInitializeContainer() {
add(new AddContactForm("add-contact-form", contactsService, remoteCommunication, modelReloader) {
protected void onInitialize() {
slot(new AddContactForm("add-contact-form", contactsService, remoteCommunication, modelReloader) {
@Override
protected void onAfterSubmit(ComponentActionCallback componentActionCallback) {
reloadList();
Expand All @@ -68,6 +68,12 @@ protected void onAfterClick(ComponentActionCallback componentActionCallback) {
readContacts();
}

private ModalSettings modalSettings() {
return builder()
.showActions()
.build();
}

private void readContacts() {
setComponentModel(ofModelObject(fromIterable(contactsService.getAllContacts())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1 class="ui center aligned header">Contacts list</h1>

<div class="ui container" lick:id="contacts-panel">
<ui-modal lick:id="form-modal">
<div lick:id="form-modal">
<div class="ui form" lick:id="add-contact-form">
<div class="two fields">
<div class="field">
Expand All @@ -26,7 +26,7 @@ <h1 class="ui center aligned header">Contacts list</h1>
Add one
</div>
</div>
</ui-modal>
</div>

<div class="ui relaxed divided items">
<div class="item" lick:id="contact">
Expand Down

This file was deleted.

8 changes: 8 additions & 0 deletions licket-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
</dependency>

<dependency>
<groupId>org.licket</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.licket.core;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Optional.ofNullable;
import static org.licket.core.id.CompositeId.fromStringValue;

import java.io.Serializable;
Expand All @@ -10,6 +9,7 @@
import java.util.function.Predicate;

import org.licket.core.id.CompositeId;
import org.licket.core.view.ComponentChildLocator;
import org.licket.core.view.LicketComponent;
import org.licket.core.view.container.LicketComponentContainer;
import org.licket.core.view.hippo.vue.VuePlugin;
Expand Down Expand Up @@ -43,7 +43,12 @@ public LicketComponentContainer<?> rootComponentContainer() {

@Override
public Optional<LicketComponent<?>> findComponent(CompositeId compositeId) {
return ofNullable(rootContainer.findChild(compositeId));
if (compositeId.current().equals(rootContainer.getId())) {
compositeId.forward();
ComponentChildLocator locator = new ComponentChildLocator(rootContainer);
return locator.findByCompositeId(compositeId);
}
return Optional.empty();
}

@Override
Expand All @@ -57,13 +62,6 @@ public void traverseDown(Predicate<LicketComponent<?>> componentVisitor) {
rootContainer.traverseDown(componentVisitor);
}

@Override
public void traverseDownContainers(Predicate<LicketComponentContainer<?>> containerVisitor) {
if (containerVisitor.test(rootContainer)) {
rootContainer.traverseDownContainers(containerVisitor);
}
}

@Override
public final Iterable<VuePlugin> modules() {
return modules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ public interface LicketApplication {

void traverseDown(Predicate<LicketComponent<?>> componentVisitor);

void traverseDownContainers(Predicate<LicketComponentContainer<?>> containerVisitor);

Iterable<VuePlugin> modules();
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private void doRender(ComponentRenderingContext renderingContext) {
LOGGER.trace("Using non-externalized view for component: [{}]", getId());
return;
}

renderingContext.onSurfaceElement(element -> {
try {
renderingContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.licket.core.view;

import org.licket.core.id.CompositeId;

import java.util.Optional;
import java.util.function.Predicate;

import static java.util.Optional.ofNullable;

/**
* @author activey
*/
public class ComponentChildLocator {

private LicketComponent<?> startingComponent;

public ComponentChildLocator(LicketComponent<?> startingComponent) {
this.startingComponent = startingComponent;
}

public final Optional<LicketComponent<?>> findByCompositeId(CompositeId compositeId) {
ChildrenTraverse traverse = new ChildrenTraverse(compositeId);
startingComponent.traverseDown(traverse);
return traverse.getFound();
}

private class ChildrenTraverse implements Predicate<LicketComponent<?>> {

private CompositeId compositeId;
private LicketComponent<?> found;

public ChildrenTraverse(CompositeId compositeId) {
this.compositeId = compositeId;
}

public Optional<LicketComponent<?>> getFound() {
return ofNullable(found);
}

@Override
public boolean test(LicketComponent<?> component) {
if (compositeId.hasMore() && compositeId.current().equals(component.getId())) {
compositeId.forward();
return true;
}
if (compositeId.current().equals(component.getId())) {
this.found = component;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.List;
import java.util.function.Predicate;

import org.licket.core.id.CompositeId;
import org.licket.core.model.LicketModel;
import org.licket.core.module.application.LicketComponentModelReloader;
import org.licket.core.view.AbstractLicketComponent;
Expand All @@ -37,7 +36,6 @@
public abstract class AbstractLicketContainer<T> extends AbstractLicketComponent<T> implements LicketComponentContainer<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLicketContainer.class);
private List<LicketComponentContainer<?>> branches = newArrayList();
private List<LicketComponent<?>> leaves = newArrayList();

@Name("model")
Expand Down Expand Up @@ -93,27 +91,19 @@ protected final void onRender(ComponentRenderingContext renderingContext) {
onRenderContainer(renderingContext);
}

protected void onRenderContainer(ComponentRenderingContext renderingContext) {
protected void onRenderContainer(ComponentRenderingContext renderingContext) {}

}

protected final void add(LicketComponent<?> licketComponent) {
if (branches.contains(licketComponent)) {
LOGGER.trace("Licket component [{}] already used as a branch!", licketComponent.getId());
public final void add(LicketComponent<?> licketComponent) {
if (leaves.contains(licketComponent)) {
LOGGER.trace("Licket component [{}] already used as a leaf!", licketComponent.getId());
return;
}
licketComponent.setParent(this);
leaves.add(licketComponent);
}

protected final void add(LicketComponentContainer<?> licketComponentContainer) {
licketComponentContainer.setParent(this);
branches.add(licketComponentContainer);
}

@Override
protected final void onInitialize() {
branches.forEach(LicketComponent::initialize);
leaves.forEach(LicketComponent::initialize);

onInitializeContainer();
Expand All @@ -122,62 +112,10 @@ protected final void onInitialize() {
protected void onInitializeContainer() {}

public final void traverseDown(Predicate<LicketComponent<?>> componentVisitor) {
leaves.forEach(componentVisitor::test);
branches.forEach(branch -> {
if (componentVisitor.test(branch)) {
branch.traverseDown(componentVisitor);
}
});
}

@Override
public final void traverseDownContainers(Predicate<LicketComponentContainer<?>> containerVisitor) {
branches.forEach(branch -> {
if (containerVisitor.test(branch)) {
branch.traverseDownContainers(containerVisitor);
leaves.forEach(leaf -> {
if (componentVisitor.test(leaf)) {
leaf.traverseDown(componentVisitor);
}
});
}

@Override
public final LicketComponent<?> findChild(CompositeId compositeId) {
if (!compositeId.hasMore()) {
if (compositeId.current().equals(getId())) {
return this;
}
for (LicketComponent<?> leaf : leaves) {
if (leaf.getId().equals(compositeId.current())) {
return leaf;
}
}
for (LicketComponentContainer<?> branch : branches) {
if (!branch.getId().equals(compositeId.current())) {
continue;
}
LicketComponent<?> childComponent = branch.findChild(compositeId);
if (childComponent != null) {
return childComponent;
}
}
return null;
}

compositeId.forward();

for (LicketComponent<?> leaf : leaves) {
if (leaf.getId().equals(compositeId.current())) {
return leaf;
}
}
for (LicketComponentContainer<?> branch : branches) {
if (!branch.getId().equals(compositeId.current())) {
continue;
}
LicketComponent<?> childComponent = branch.findChild(compositeId);
if (childComponent != null) {
return childComponent;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.licket.core.view.container;

import org.licket.core.id.CompositeId;
import org.licket.core.view.LicketComponent;

import java.util.function.Predicate;

/**
* @author grabslu
*/
public interface LicketComponentContainer<T> extends LicketComponent<T> {

LicketComponent<?> findChild(CompositeId compositeId);

void traverseDownContainers(Predicate<LicketComponentContainer<?>> containerConsumer);
void add(LicketComponent<?> licketComponent);
}
Loading

0 comments on commit c201da5

Please sign in to comment.