Search in sources :

Example 1 with TreeEvent

use of org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent in project scout.rt by eclipse.

the class TreeEventFilter method filter.

/**
 * Computes whether the event should be returned to the GUI. There are three cases:
 * <ul>
 * <li>No filtering happens: The original event is returned. <br>
 * This is the case if the conditions don't contain an event with the same type as the original event.</li>
 * <li>Partial filtering happens (if condition.checkNodes is true): A new event with a subset of tree nodes is
 * returned.<br>
 * This is the case if the conditions contain a relevant event but has different nodes than the original event.
 * <li>Complete filtering happens: Null is returned.<br>
 * This is the case if the event should be filtered for every node in the original event
 */
@Override
public TreeEvent filter(TreeEvent event) {
    for (TreeEventFilterCondition condition : getConditions()) {
        if (condition.getType() == event.getType()) {
            if (condition.checkNodes()) {
                Collection<ITreeNode> nodes = new ArrayList<>(event.getNodes());
                nodes.removeAll(condition.getNodes());
                if (nodes.size() == 0) {
                    // Ignore event if no nodes remain (or if the event contained no nodes at all)
                    return null;
                }
                return new TreeEvent(m_jsonTree.getModel(), event.getType(), event.getCommonParentNode(), nodes);
            }
            if (condition.checkCheckedNodes()) {
                List<ITreeNode> nodes = new ArrayList<>(event.getNodes());
                List<ITreeNode> checkedNodes = new ArrayList<>();
                List<ITreeNode> uncheckedNodes = new ArrayList<>();
                for (ITreeNode node : nodes) {
                    if (node.isChecked()) {
                        checkedNodes.add(node);
                    } else {
                        uncheckedNodes.add(node);
                    }
                }
                if (CollectionUtility.equalsCollection(checkedNodes, condition.getCheckedNodes()) && CollectionUtility.equalsCollection(uncheckedNodes, condition.getUncheckedNodes())) {
                    // Ignore event if the checked and the unchecked nodes have not changes
                    return null;
                }
                // Otherwise, send nodes that have a different checked state than before
                checkedNodes.removeAll(condition.getCheckedNodes());
                uncheckedNodes.removeAll(condition.getUncheckedNodes());
                nodes = CollectionUtility.combine(checkedNodes, uncheckedNodes);
                TreeEvent newEvent = new TreeEvent(m_jsonTree.getModel(), event.getType(), event.getCommonParentNode(), nodes);
                return newEvent;
            }
            // Ignore event if only type should be checked
            return null;
        }
    }
    return event;
}
Also used : TreeEvent(org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent) ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) ArrayList(java.util.ArrayList)

Example 2 with TreeEvent

use of org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent in project scout.rt by eclipse.

the class JsonTreeTest method testWrongEventOrder.

/**
 * Tests that events are ignored when nodes are not yet inserted.
 */
@Test
public void testWrongEventOrder() throws JSONException {
    List<ITreeNode> nodes = new ArrayList<ITreeNode>();
    nodes.add(new TreeNode());
    nodes.add(new TreeNode());
    nodes.add(new TreeNode());
    ITree tree = createTree(nodes);
    JsonTree<ITree> jsonTree = m_uiSession.createJsonAdapter(tree, null);
    TreeNode newNode = new TreeNode();
    jsonTree.handleModelTreeEvent(new TreeEvent(tree, TreeEvent.TYPE_NODE_EXPANDED, newNode));
    jsonTree.handleModelTreeEvent(new TreeEvent(tree, TreeEvent.TYPE_NODES_INSERTED, nodes.get(0), Collections.singletonList(newNode)));
    JsonTestUtility.processBufferedEvents(m_uiSession);
    List<JsonEvent> events = m_uiSession.currentJsonResponse().getEventList();
    assertEquals(1, events.size());
    assertEquals("nodesInserted", events.get(0).getType());
}
Also used : TreeEvent(org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent) ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) TreeNode(org.eclipse.scout.rt.ui.html.json.tree.fixtures.TreeNode) ArrayList(java.util.ArrayList) JsonEvent(org.eclipse.scout.rt.ui.html.json.JsonEvent) ITree(org.eclipse.scout.rt.client.ui.basic.tree.ITree) Test(org.junit.Test)

Example 3 with TreeEvent

use of org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent in project scout.rt by eclipse.

the class JsonTreeTest method testInsertAndDeleteInSameRequest.

@Test
public void testInsertAndDeleteInSameRequest() throws Exception {
    // Note: A test for the same problem (but without a real tree) can be found here:
    // org.eclipse.scout.rt.client.ui.basic.tree.TreeEventBufferTest.testInsertAndRemoveInSameRequest()
    ITree tree = new Tree();
    tree.setRootNode(new TreeNode("Root"));
    final List<TreeEvent> treeEventCollector = new ArrayList<>();
    tree.addTreeListener(new TreeAdapter() {

        @Override
        public void treeChanged(TreeEvent e) {
            treeEventCollector.add(e);
        }
    });
    IJsonAdapter<? super ITree> jsonTree = m_uiSession.createJsonAdapter(tree, null);
    m_uiSession.currentJsonResponse().addAdapter(jsonTree);
    JSONObject response = m_uiSession.currentJsonResponse().toJson();
    System.out.println("Response #1: " + response);
    JsonTestUtility.endRequest(m_uiSession);
    // ----------------
    // (root)
    // +-[A]
    // +-[B]
    ITreeNode nodeA = new TreeNode("A");
    ITreeNode nodeB = new TreeNode("B");
    // Insert A and B in one "tree changing" batch
    // -> TreeEventBuffer should remove the second event (because B is a sub-node of A)
    tree.setTreeChanging(true);
    tree.addChildNode(tree.getRootNode(), nodeA);
    tree.addChildNode(nodeA, nodeB);
    tree.setTreeChanging(false);
    assertEquals(1, treeEventCollector.size());
    treeEventCollector.clear();
    // Remove B, then A (in two separate calls)
    // -> TreeEventBuffer should remove the second event (because B is a sub-node of A), altough
    // only an insertion event for A exists (and A.getChildNodes() returns nothing)
    tree.removeAllChildNodes(nodeA);
    tree.removeAllChildNodes(tree.getRootNode());
    assertEquals(2, treeEventCollector.size());
    treeEventCollector.clear();
    assertEquals(0, nodeA.getChildNodeCount());
    assertEquals(0, tree.getRootNode().getChildNodeCount());
    // Process the buffer
    // -> TreeEventBuffer should remove all events
    JsonTestUtility.processBufferedEvents(m_uiSession);
    List<JsonEvent> events = m_uiSession.currentJsonResponse().getEventList();
    assertEquals(0, events.size());
    response = m_uiSession.currentJsonResponse().toJson();
    System.out.println("Response #2: " + response);
    JsonTestUtility.endRequest(m_uiSession);
}
Also used : ArrayList(java.util.ArrayList) JsonEvent(org.eclipse.scout.rt.ui.html.json.JsonEvent) ITree(org.eclipse.scout.rt.client.ui.basic.tree.ITree) TreeAdapter(org.eclipse.scout.rt.client.ui.basic.tree.TreeAdapter) TreeEvent(org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent) ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) JSONObject(org.json.JSONObject) ITreeNode(org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode) TreeNode(org.eclipse.scout.rt.ui.html.json.tree.fixtures.TreeNode) Tree(org.eclipse.scout.rt.ui.html.json.tree.fixtures.Tree) ITree(org.eclipse.scout.rt.client.ui.basic.tree.ITree) Test(org.junit.Test)

Example 4 with TreeEvent

use of org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent in project scout.rt by eclipse.

the class AbstractComposerField method initConfig.

@Override
protected void initConfig() {
    m_uiFacade = BEANS.get(ModelContextProxy.class).newProxy(new P_UIFacade(), ModelContext.copyCurrent());
    super.initConfig();
    m_dataModel = interceptCreateDataModel();
    // tree
    List<ITree> contributedTrees = m_contributionHolder.getContributionsByClass(ITree.class);
    m_tree = CollectionUtility.firstElement(contributedTrees);
    if (m_tree == null) {
        Class<? extends ITree> configuredTree = getConfiguredTree();
        if (configuredTree != null) {
            m_tree = ConfigurationUtility.newInnerInstance(this, configuredTree);
        }
    }
    if (m_tree != null) {
        RootNode rootNode = interceptCreateRootNode();
        rootNode.getCellForUpdate().setText(getLabel());
        m_tree.setRootNode(rootNode);
        m_tree.setNodeExpanded(rootNode, true);
        m_tree.setEnabled(isEnabled());
        m_tree.addTreeListener(new TreeAdapter() {

            @Override
            public void treeChanged(TreeEvent e) {
                switch(e.getType()) {
                    case TreeEvent.TYPE_NODES_DELETED:
                    case TreeEvent.TYPE_ALL_CHILD_NODES_DELETED:
                    case TreeEvent.TYPE_NODES_INSERTED:
                    case TreeEvent.TYPE_NODES_UPDATED:
                    case TreeEvent.TYPE_NODES_CHECKED:
                        {
                            checkSaveNeeded();
                            checkEmpty();
                            break;
                        }
                }
            }
        });
        // local enabled listener
        addPropertyChangeListener(PROP_ENABLED_COMPUTED, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent e) {
                if (m_tree == null) {
                    return;
                }
                boolean newEnabled = ((Boolean) e.getNewValue()).booleanValue();
                m_tree.setEnabled(newEnabled);
            }
        });
    } else {
        LOG.warn("there is no inner class of type ITree in {}", getClass().getName());
    }
}
Also used : TreeEvent(org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent) RootNode(org.eclipse.scout.rt.client.ui.form.fields.composer.node.RootNode) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ITree(org.eclipse.scout.rt.client.ui.basic.tree.ITree) TreeAdapter(org.eclipse.scout.rt.client.ui.basic.tree.TreeAdapter)

Example 5 with TreeEvent

use of org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent in project scout.rt by eclipse.

the class OutlineTest method testEvents.

@Test
public void testEvents() {
    AbstractOutline o = new AbstractOutline() {
    };
    final SavingTreeListener listener = new SavingTreeListener();
    o.addTreeListener(listener);
    o.setTreeChanging(true);
    // unbuffered
    o.fireBeforeDataLoaded(mock(IPage.class));
    // unbuffered
    o.fireAfterDataLoaded(mock(IPage.class));
    o.firePageChanged(mock(IPage.class));
    o.firePageChanged(mock(IPage.class));
    // unbuffered
    o.fireAfterTableInit(mock(IPage.class));
    o.setTreeChanging(false);
    // fires afterDispose event, is sent as buffered event but since tree isn't changing anymore no buffering is applied
    o.disposeTree();
    // check number and order of unbuffered events fired during the treeChanging period
    Stack<? extends TreeEvent> firedEvents = listener.getUnbufferedEvents();
    assertEquals(6, firedEvents.size());
    assertEquals(OutlineEvent.TYPE_PAGE_AFTER_DISPOSE, firedEvents.pop().getType());
    TreeEvent e1 = firedEvents.pop();
    assertEquals(OutlineEvent.TYPE_PAGE_CHANGED, e1.getType());
    TreeEvent e2 = firedEvents.pop();
    assertEquals(OutlineEvent.TYPE_PAGE_CHANGED, e2.getType());
    assertNotEquals(e1.getNode(), e2.getNode());
    assertEquals(OutlineEvent.TYPE_PAGE_AFTER_TABLE_INIT, listener.getUnbufferedEvents().pop().getType());
    assertEquals(OutlineEvent.TYPE_PAGE_AFTER_DATA_LOADED, listener.getUnbufferedEvents().pop().getType());
    assertEquals(OutlineEvent.TYPE_PAGE_BEFORE_DATA_LOADED, listener.getUnbufferedEvents().pop().getType());
}
Also used : TreeEvent(org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent) IPage(org.eclipse.scout.rt.client.ui.desktop.outline.pages.IPage) Test(org.junit.Test)

Aggregations

TreeEvent (org.eclipse.scout.rt.client.ui.basic.tree.TreeEvent)9 ITreeNode (org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode)5 ArrayList (java.util.ArrayList)4 ITree (org.eclipse.scout.rt.client.ui.basic.tree.ITree)4 Test (org.junit.Test)4 TreeAdapter (org.eclipse.scout.rt.client.ui.basic.tree.TreeAdapter)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 IPage (org.eclipse.scout.rt.client.ui.desktop.outline.pages.IPage)2 JsonEvent (org.eclipse.scout.rt.ui.html.json.JsonEvent)2 TreeNode (org.eclipse.scout.rt.ui.html.json.tree.fixtures.TreeNode)2 AbstractTree (org.eclipse.scout.rt.client.ui.basic.tree.AbstractTree)1 IOutline (org.eclipse.scout.rt.client.ui.desktop.outline.IOutline)1 IFormField (org.eclipse.scout.rt.client.ui.form.fields.IFormField)1 RootNode (org.eclipse.scout.rt.client.ui.form.fields.composer.node.RootNode)1 OrderedComparator (org.eclipse.scout.rt.platform.OrderedComparator)1 ILookupRow (org.eclipse.scout.rt.shared.services.lookup.ILookupRow)1 LookupRow (org.eclipse.scout.rt.shared.services.lookup.LookupRow)1 UiException (org.eclipse.scout.rt.ui.html.UiException)1 JsonOutline (org.eclipse.scout.rt.ui.html.json.desktop.JsonOutline)1