Search in sources :

Example 1 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class EventLoggerSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    EventLogger eventLogger = (EventLogger) component;
    eventLogger.getEventLoggerListeners().add(this);
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        content = (Component) bxmlSerializer.readObject(EventLoggerSkin.class, "event_logger_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    eventLogger.add(content);
    declaredEventsTreeView = (TreeView) bxmlSerializer.getNamespace().get("declaredEventsTreeView");
    firedEventsTableView = (TableView) bxmlSerializer.getNamespace().get("firedEventsTableView");
    // Propagate check state upwards or downwards as necessary
    declaredEventsTreeView.getTreeViewNodeStateListeners().add(new TreeViewNodeStateListener() {

        @Override
        public void nodeCheckStateChanged(TreeView treeView, Path path, TreeView.NodeCheckState previousCheckState) {
            TreeView.NodeCheckState checkState = treeView.getNodeCheckState(path);
            if (!updating) {
                // Set the updating flag for the life of this event loop
                updating = true;
                ApplicationContext.queueCallback(new Runnable() {

                    @Override
                    public void run() {
                        updating = false;
                    }
                });
                EventLogger eventLoggerLocal = (EventLogger) getComponent();
                boolean checked = (checkState == TreeView.NodeCheckState.CHECKED);
                List<?> treeData = treeView.getTreeData();
                TreeNode treeNode = (TreeNode) Sequence.Tree.get(treeData, path);
                if (treeNode instanceof List<?>) {
                    if (previousCheckState == TreeView.NodeCheckState.CHECKED || checkState == TreeView.NodeCheckState.CHECKED) {
                        // Propagate downward
                        List<?> treeBranch = (List<?>) treeNode;
                        Path childPath = new Path(path);
                        int lastIndex = childPath.getLength();
                        childPath.add(0);
                        for (int i = 0, n = treeBranch.getLength(); i < n; i++) {
                            childPath.update(lastIndex, i);
                            treeView.setNodeChecked(childPath, checked);
                            EventNode eventNode = (EventNode) treeBranch.get(i);
                            Method event = eventNode.getEvent();
                            if (checked) {
                                eventLoggerLocal.getIncludeEvents().add(event);
                            } else {
                                eventLoggerLocal.getIncludeEvents().remove(event);
                            }
                        }
                    }
                } else {
                    Path parentPath = new Path(path, path.getLength() - 1);
                    EventNode eventNode = (EventNode) treeNode;
                    Method event = eventNode.getEvent();
                    if (checked) {
                        List<?> treeBranch = (List<?>) Sequence.Tree.get(treeData, parentPath);
                        Path childPath = new Path(path);
                        int lastIndex = parentPath.getLength();
                        int i = 0, n = treeBranch.getLength();
                        while (i < n) {
                            childPath.update(lastIndex, i);
                            if (!treeView.isNodeChecked(childPath)) {
                                break;
                            }
                            i++;
                        }
                        if (i == n) {
                            // Propagate upward
                            treeView.setNodeChecked(parentPath, checked);
                        }
                        eventLoggerLocal.getIncludeEvents().add(event);
                    } else {
                        // Propagate upward
                        treeView.setNodeChecked(parentPath, checked);
                        eventLoggerLocal.getIncludeEvents().remove(event);
                    }
                }
            }
        }
    });
    sourceChanged(eventLogger, null);
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) TreeViewNodeStateListener(org.apache.pivot.wtk.TreeViewNodeStateListener) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) Method(java.lang.reflect.Method) TreeNode(org.apache.pivot.wtk.content.TreeNode) TreeView(org.apache.pivot.wtk.TreeView) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 2 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class ComponentExplorerWindow method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    splitPane = (SplitPane) namespace.get("splitPane");
    treeView = (TreeView) namespace.get("treeView");
    contentScrollPane = (ScrollPane) namespace.get("contentScrollPane");
    contentPane = (Border) namespace.get("contentPane");
    sourceTextArea = (TextArea) namespace.get("sourceTextArea");
    componentPropertyInspector = (ComponentPropertyInspector) namespace.get("componentPropertyInspector");
    componentStyleInspector = (ComponentStyleInspector) namespace.get("componentStyleInspector");
    eventLogger = (EventLogger) namespace.get("eventLogger");
    horizontalScrollBarPolicyGroup = (ButtonGroup) namespace.get("horizontalScrollBarPolicyGroup");
    verticalScrollBarPolicyGroup = (ButtonGroup) namespace.get("verticalScrollBarPolicyGroup");
    horizontalAutoButton = (Button) namespace.get("horizontalAutoButton");
    horizontalFillButton = (Button) namespace.get("horizontalFillButton");
    horizontalFillToCapacityButton = (Button) namespace.get("horizontalFillToCapacityButton");
    horizontalNeverButton = (Button) namespace.get("horizontalNeverButton");
    horizontalAlwaysButton = (Button) namespace.get("horizontalAlwaysButton");
    verticalAutoButton = (Button) namespace.get("verticalAutoButton");
    verticalFillButton = (Button) namespace.get("verticalFillButton");
    verticalFillToCapacityButton = (Button) namespace.get("verticalFillToCapacityButton");
    verticalNeverButton = (Button) namespace.get("verticalNeverButton");
    verticalAlwaysButton = (Button) namespace.get("verticalAlwaysButton");
    treeView.getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {

        @Override
        public void selectedPathsChanged(TreeView treeViewArgument, Sequence<Path> previousSelectedPaths) {
            Component component = null;
            Object node = treeViewArgument.getSelectedNode();
            if (node instanceof ComponentNode) {
                ComponentNode componentNode = (ComponentNode) node;
                URL url = componentNode.getSrc();
                try {
                    sourceTextArea.setText(url);
                } catch (IOException exception) {
                    throw new RuntimeException(exception);
                }
                BXMLSerializer bxmlSerializer = new BXMLSerializer();
                try {
                    component = (Component) bxmlSerializer.readObject(url);
                } catch (IOException exception) {
                    throw new RuntimeException(exception);
                } catch (SerializationException exception) {
                    throw new RuntimeException(exception);
                }
                switch(componentNode.getHorizontalScrollBarPolicy()) {
                    case AUTO:
                        horizontalScrollBarPolicyGroup.setSelection(horizontalAutoButton);
                        break;
                    case FILL:
                        horizontalScrollBarPolicyGroup.setSelection(horizontalFillButton);
                        break;
                    case FILL_TO_CAPACITY:
                        horizontalScrollBarPolicyGroup.setSelection(horizontalFillToCapacityButton);
                        break;
                    case NEVER:
                        horizontalScrollBarPolicyGroup.setSelection(horizontalNeverButton);
                        break;
                    case ALWAYS:
                        horizontalScrollBarPolicyGroup.setSelection(horizontalAlwaysButton);
                        break;
                    default:
                        break;
                }
                switch(componentNode.getVerticalScrollBarPolicy()) {
                    case AUTO:
                        verticalScrollBarPolicyGroup.setSelection(verticalAutoButton);
                        break;
                    case FILL:
                        verticalScrollBarPolicyGroup.setSelection(verticalFillButton);
                        break;
                    case FILL_TO_CAPACITY:
                        verticalScrollBarPolicyGroup.setSelection(verticalFillToCapacityButton);
                        break;
                    case NEVER:
                        verticalScrollBarPolicyGroup.setSelection(verticalNeverButton);
                        break;
                    case ALWAYS:
                        verticalScrollBarPolicyGroup.setSelection(verticalAlwaysButton);
                        break;
                    default:
                        break;
                }
            } else {
                sourceTextArea.setText("");
            }
            contentPane.setContent(component);
            componentPropertyInspector.setSource(component);
            componentStyleInspector.setSource(component);
            eventLogger.setSource(component);
            eventLogger.clearLog();
        }
    });
    treeView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        @Override
        public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
            if (button == Mouse.Button.LEFT && count == 2) {
                Path path = treeView.getNodeAt(y);
                if (path != null) {
                    List<?> treeData = treeView.getTreeData();
                    Object node = Tree.get(treeData, path);
                    if (node instanceof List<?>) {
                        treeView.setBranchExpanded(path, !treeView.isBranchExpanded(path));
                    }
                }
            }
            return false;
        }
    });
    horizontalScrollBarPolicyGroup.getButtonGroupListeners().add(new ButtonGroupListener() {

        @Override
        public void selectionChanged(ButtonGroup buttonGroup, Button previousSelection) {
            Button button = buttonGroup.getSelection();
            ScrollBarPolicy horizontalScrollBarPolicy = null;
            if (button == horizontalAutoButton) {
                horizontalScrollBarPolicy = ScrollBarPolicy.AUTO;
            } else if (button == horizontalFillButton) {
                horizontalScrollBarPolicy = ScrollBarPolicy.FILL;
            } else if (button == horizontalFillToCapacityButton) {
                horizontalScrollBarPolicy = ScrollBarPolicy.FILL_TO_CAPACITY;
            } else if (button == horizontalNeverButton) {
                horizontalScrollBarPolicy = ScrollBarPolicy.NEVER;
            } else if (button == horizontalAlwaysButton) {
                horizontalScrollBarPolicy = ScrollBarPolicy.ALWAYS;
            }
            if (horizontalScrollBarPolicy != null) {
                contentScrollPane.setHorizontalScrollBarPolicy(horizontalScrollBarPolicy);
            }
        }
    });
    verticalScrollBarPolicyGroup.getButtonGroupListeners().add(new ButtonGroupListener() {

        @Override
        public void selectionChanged(ButtonGroup buttonGroup, Button previousSelection) {
            Button button = buttonGroup.getSelection();
            ScrollBarPolicy verticalScrollBarPolicy = null;
            if (button == verticalAutoButton) {
                verticalScrollBarPolicy = ScrollBarPolicy.AUTO;
            } else if (button == verticalFillButton) {
                verticalScrollBarPolicy = ScrollBarPolicy.FILL;
            } else if (button == verticalFillToCapacityButton) {
                verticalScrollBarPolicy = ScrollBarPolicy.FILL_TO_CAPACITY;
            } else if (button == verticalNeverButton) {
                verticalScrollBarPolicy = ScrollBarPolicy.NEVER;
            } else if (button == verticalAlwaysButton) {
                verticalScrollBarPolicy = ScrollBarPolicy.ALWAYS;
            }
            if (verticalScrollBarPolicy != null) {
                contentScrollPane.setVerticalScrollBarPolicy(verticalScrollBarPolicy);
            }
        }
    });
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) URL(java.net.URL) ButtonGroupListener(org.apache.pivot.wtk.ButtonGroupListener) Mouse(org.apache.pivot.wtk.Mouse) ButtonGroup(org.apache.pivot.wtk.ButtonGroup) Button(org.apache.pivot.wtk.Button) ScrollBarPolicy(org.apache.pivot.wtk.ScrollPane.ScrollBarPolicy) TreeViewSelectionListener(org.apache.pivot.wtk.TreeViewSelectionListener) TreeView(org.apache.pivot.wtk.TreeView) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) List(org.apache.pivot.collections.List) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 3 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class JSONSerializer method writeObject.

/**
 * Writes data to a JSON stream.
 *
 * @param object The object to serialize. Must be one of the following
 * types: <ul> <li>pivot.collections.Map</li>
 * <li>pivot.collections.List</li> <li>java.lang.String</li>
 * <li>java.lang.Number</li> <li>java.lang.Boolean</li>
 * <li><tt>null</tt></li> </ul>
 * @param writer The writer to which data will be written.
 * @throws IOException for any errors during the writing process.
 * @throws SerializationException for any formatting errors in the data.
 */
@SuppressWarnings("unchecked")
public void writeObject(Object object, Writer writer) throws IOException, SerializationException {
    Utils.checkNull(writer, "writer");
    if (object == null) {
        writer.append("null");
    } else if (object instanceof String) {
        String string = (String) object;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0, n = string.length(); i < n; i++) {
            char ci = string.charAt(i);
            switch(ci) {
                case '\t':
                    {
                        stringBuilder.append("\\t");
                        break;
                    }
                case '\n':
                    {
                        stringBuilder.append("\\n");
                        break;
                    }
                case '\r':
                    {
                        stringBuilder.append("\\r");
                        break;
                    }
                case '\f':
                    {
                        stringBuilder.append("\\f");
                        break;
                    }
                case '\b':
                    {
                        stringBuilder.append("\\b");
                        break;
                    }
                case '\\':
                case '\"':
                case '\'':
                    {
                        stringBuilder.append("\\" + ci);
                        break;
                    }
                default:
                    {
                        // and for other character sets if the value is an ASCII control character.
                        if ((charset.name().startsWith("UTF") && !Character.isISOControl(ci)) || (ci > 0x1F && ci != 0x7F && ci <= 0xFF)) {
                            stringBuilder.append(ci);
                        } else {
                            stringBuilder.append("\\u");
                            stringBuilder.append(String.format("%04x", (short) ci));
                        }
                    }
            }
        }
        writer.append("\"" + stringBuilder.toString() + "\"");
    } else if (object instanceof Number) {
        Number number = (Number) object;
        if (number instanceof Float) {
            Float f = (Float) number;
            if (f.isNaN() || f.isInfinite()) {
                throw new SerializationException(number + " is not a valid value.");
            }
        } else if (number instanceof Double) {
            Double d = (Double) number;
            if (d.isNaN() || d.isInfinite()) {
                throw new SerializationException(number + " is not a valid value.");
            }
        }
        writer.append(number.toString());
    } else if (object instanceof Boolean) {
        writer.append(object.toString());
    } else if (object instanceof List<?>) {
        List<Object> list = (List<Object>) object;
        writer.append("[");
        int i = 0;
        for (Object item : list) {
            if (i > 0) {
                writer.append(", ");
            }
            writeObject(item, writer);
            i++;
        }
        writer.append("]");
    } else {
        Map<String, Object> map;
        if (object instanceof Map<?, ?>) {
            map = (Map<String, Object>) object;
        } else if (object instanceof java.util.Map<?, ?>) {
            map = new MapAdapter<>((java.util.Map<String, Object>) object);
        } else {
            map = new BeanAdapter(object, true);
        }
        writer.append("{");
        int i = 0;
        for (String key : map) {
            Object value = map.get(key);
            boolean identifier = true;
            StringBuilder keyStringBuilder = new StringBuilder();
            for (int j = 0, n = key.length(); j < n; j++) {
                char cj = key.charAt(j);
                identifier &= Character.isJavaIdentifierPart(cj);
                if (cj == '"') {
                    keyStringBuilder.append('\\');
                }
                keyStringBuilder.append(cj);
            }
            key = keyStringBuilder.toString();
            if (i > 0) {
                writer.append(", ");
            }
            // Write the key
            if (!identifier || alwaysDelimitMapKeys) {
                writer.append('"');
            }
            writer.append(key);
            if (!identifier || alwaysDelimitMapKeys) {
                writer.append('"');
            }
            writer.append(": ");
            // Write the value
            writeObject(value, writer);
            i++;
        }
        writer.append("}");
    }
    writer.flush();
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) ListenerList(org.apache.pivot.util.ListenerList) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) MapAdapter(org.apache.pivot.collections.adapter.MapAdapter)

Example 4 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class BindTest method testUntypedList.

/**
 * Tests returning an untyped list.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testUntypedList() throws IOException, SerializationException {
    JSONSerializer listSerializer = new JSONSerializer(ArrayList.class);
    List<?> list = (List<?>) listSerializer.readObject(new StringReader("[1, 2, 3, 4, 5]"));
    assertEquals(list.get(0), 1);
}
Also used : StringReader(java.io.StringReader) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 5 with List

use of org.apache.pivot.collections.List in project pivot by apache.

the class BindTest method testListSubclass.

/**
 * Tests returning a subclass of a generic
 * {@code org.apache.pivot.collections.List}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testListSubclass() throws IOException, SerializationException {
    JSONSerializer listSerializer = new JSONSerializer();
    @SuppressWarnings("unchecked") List<Object> list = (List<Object>) listSerializer.readObject(getClass().getResourceAsStream("list.json"));
    JSONSerializer typedListSerializer = new JSONSerializer(SampleBean2ListSubclass.class);
    SampleBean2List typedList = (SampleBean2List) typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));
    Object item0 = typedList.get(0);
    assertTrue(item0 instanceof SampleBean2);
    assertEquals((Integer) typedList.get(0).getA(), (Integer) JSON.get(list, "[0].a"));
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Aggregations

List (org.apache.pivot.collections.List)47 ArrayList (org.apache.pivot.collections.ArrayList)37 TableView (org.apache.pivot.wtk.TableView)11 Span (org.apache.pivot.wtk.Span)10 Button (org.apache.pivot.wtk.Button)8 ListView (org.apache.pivot.wtk.ListView)8 Point (org.apache.pivot.wtk.Point)8 IOException (java.io.IOException)7 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)7 SerializationException (org.apache.pivot.serialization.SerializationException)6 Component (org.apache.pivot.wtk.Component)6 File (java.io.File)5 HashMap (org.apache.pivot.collections.HashMap)5 Map (org.apache.pivot.collections.Map)5 JSONSerializer (org.apache.pivot.json.JSONSerializer)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)5 Color (java.awt.Color)4 FileObject (org.apache.commons.vfs2.FileObject)4 Task (org.apache.pivot.util.concurrent.Task)4