Search in sources :

Example 6 with Sequence

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

the class CheckedListViewTest method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer serializer = new BXMLSerializer();
    mainWindow = (Window) serializer.readObject(CheckedListViewTest.class, "checked_list_view_test.bxml");
    serializer.bind(this);
    allowMixedStateCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setAllowTriStateCheckmarks(button.isSelected());
            // Not sure why, but changing this setting clears all the checks but doesn't
            // trigger the item state listener (it's documented, but ...)
            selectedItemsLabel.setText("");
        }
    });
    showMixedAsSelectedCheckbox.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            listView.setCheckmarksMixedAsChecked(button.isSelected());
        }
    });
    listView.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.DELETE) {
                @SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
                Sequence<Span> selectedRanges = listView.getSelectedRanges();
                for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
                    Span selectedRange = selectedRanges.get(i);
                    listData.remove(selectedRange.start, selectedRange.end - selectedRange.start + 1);
                }
            }
            return false;
        }
    });
    listView.getListViewItemStateListeners().add(new ListViewItemStateListener() {

        private void displayCheckedItems(ListView listView) {
            List<?> listData = listView.getListData();
            StringBuffer buf = new StringBuffer();
            for (Integer i : listView.getCheckedIndexes()) {
                if (buf.length() > 0) {
                    buf.append(",");
                }
                Object item = listData.get(i);
                buf.append(item.toString());
            }
            selectedItemsLabel.setText(buf.toString());
        }

        @Override
        public void itemCheckedChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }

        @Override
        public void itemCheckedStateChanged(ListView listView, int index) {
            displayCheckedItems(listView);
        }
    });
    listView.setItemChecked(0, true);
    listView.setItemChecked(2, true);
    mainWindow.open(display);
}
Also used : ListViewItemStateListener(org.apache.pivot.wtk.ListViewItemStateListener) Keyboard(org.apache.pivot.wtk.Keyboard) Sequence(org.apache.pivot.collections.Sequence) Span(org.apache.pivot.wtk.Span) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) ListView(org.apache.pivot.wtk.ListView) Button(org.apache.pivot.wtk.Button) List(org.apache.pivot.collections.List) Component(org.apache.pivot.wtk.Component) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 7 with Sequence

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

the class TerraTreeViewSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Selects the previous enabled node when select mode
 * is not {@link SelectMode#NONE}<br> {@link KeyCode#DOWN DOWN} Selects the
 * next enabled node when select mode is not {@link SelectMode#NONE}<p>
 * {@link Modifier#SHIFT SHIFT} + {@link KeyCode#UP UP} Increases the
 * selection size by including the previous enabled node when select mode is
 * {@link SelectMode#MULTI}<br> {@link Modifier#SHIFT SHIFT} +
 * {@link KeyCode#DOWN DOWN} Increases the selection size by including the
 * next enabled node when select mode is {@link SelectMode#MULTI}
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    TreeView treeView = (TreeView) getComponent();
    TreeView.SelectMode selectMode = treeView.getSelectMode();
    switch(keyCode) {
        case Keyboard.KeyCode.UP:
            {
                if (selectMode != TreeView.SelectMode.NONE) {
                    Path firstSelectedPath = treeView.getFirstSelectedPath();
                    int index;
                    if (firstSelectedPath != null) {
                        NodeInfo previousSelectedNode = getNodeInfoAt(firstSelectedPath);
                        index = visibleNodes.indexOf(previousSelectedNode);
                    } else {
                        // Select the last visible node
                        index = visibleNodes.getLength();
                    }
                    NodeInfo newSelectedNode = null;
                    do {
                        newSelectedNode = (--index >= 0) ? visibleNodes.get(index) : null;
                    } while (newSelectedNode != null && newSelectedNode.isDisabled());
                    if (newSelectedNode != null) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && treeView.getSelectMode() == TreeView.SelectMode.MULTI) {
                            treeView.addSelectedPath(newSelectedNode.getPath());
                        } else {
                            treeView.setSelectedPath(newSelectedNode.getPath());
                        }
                        treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.DOWN:
            {
                if (selectMode != TreeView.SelectMode.NONE) {
                    Path lastSelectedPath = treeView.getLastSelectedPath();
                    int index;
                    if (lastSelectedPath != null) {
                        NodeInfo previousSelectedNode = getNodeInfoAt(lastSelectedPath);
                        index = visibleNodes.indexOf(previousSelectedNode);
                    } else {
                        // Select the first visible node
                        index = -1;
                    }
                    NodeInfo newSelectedNode = null;
                    int n = visibleNodes.getLength();
                    do {
                        newSelectedNode = (++index <= n - 1) ? visibleNodes.get(index) : null;
                    } while (newSelectedNode != null && newSelectedNode.isDisabled());
                    if (newSelectedNode != null) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && treeView.getSelectMode() == TreeView.SelectMode.MULTI) {
                            treeView.addSelectedPath(newSelectedNode.getPath());
                        } else {
                            treeView.setSelectedPath(newSelectedNode.getPath());
                        }
                        treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.LEFT:
            {
                if (showBranchControls) {
                    Sequence<Path> paths = treeView.getSelectedPaths();
                    if (paths != null && paths.getLength() > 0) {
                        Path path = paths.get(paths.getLength() - 1);
                        NodeInfo nodeInfo = getNodeInfoAt(path);
                        if (nodeInfo instanceof BranchInfo) {
                            BranchInfo branchInfo = (BranchInfo) nodeInfo;
                            if (branchInfo.isExpanded()) {
                                treeView.collapseBranch(branchInfo.getPath());
                            }
                        }
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.RIGHT:
            {
                if (showBranchControls) {
                    Sequence<Path> paths = treeView.getSelectedPaths();
                    if (paths != null && paths.getLength() > 0) {
                        Path path = paths.get(paths.getLength() - 1);
                        NodeInfo nodeInfo = getNodeInfoAt(path);
                        if (nodeInfo instanceof BranchInfo) {
                            BranchInfo branchInfo = (BranchInfo) nodeInfo;
                            if (!branchInfo.isExpanded()) {
                                treeView.expandBranch(branchInfo.getPath());
                            }
                        }
                        consumed = true;
                    }
                }
                break;
            }
        default:
            consumed = super.keyPressed(component, keyCode, keyLocation);
            break;
    }
    if (consumed) {
        clearHighlightedNode();
    }
    return consumed;
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) GeneralPath(java.awt.geom.GeneralPath) TreeView(org.apache.pivot.wtk.TreeView) Sequence(org.apache.pivot.collections.Sequence) SelectMode(org.apache.pivot.wtk.TreeView.SelectMode)

Example 8 with Sequence

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

the class TerraFileBrowserSheetSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final FileBrowserSheet fileBrowserSheet = (FileBrowserSheet) component;
    fileBrowserSheet.setMinimumWidth(360);
    fileBrowserSheet.setMinimumHeight(180);
    // Load the sheet content
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    Component content;
    try {
        content = (Component) bxmlSerializer.readObject(TerraFileBrowserSheetSkin.class, "terra_file_browser_sheet_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowserSheet.setContent(content);
    bxmlSerializer.bind(this, TerraFileBrowserSheetSkin.class);
    // set the same rootDirectory to fileBrowser
    fileBrowser.setRootDirectory(fileBrowserSheet.getRootDirectory());
    saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textChanged(TextInput textInput) {
            Form.clearFlag(saveAsBoxPane);
            updateOKButtonState();
        }
    });
    fileBrowser.getFileBrowserListeners().add(new FileBrowserListener() {

        @Override
        public void rootDirectoryChanged(FileBrowser fileBrowserArgument, File previousRootDirectory) {
            updatingSelection = true;
            fileBrowserSheet.setRootDirectory(fileBrowserArgument.getRootDirectory());
            updatingSelection = false;
            selectedDirectoryCount = 0;
            updateOKButtonState();
        }

        @Override
        public void selectedFileAdded(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount++;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFileRemoved(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount--;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFilesChanged(FileBrowser fileBrowserArgument, Sequence<File> previousSelectedFiles) {
            selectedDirectoryCount = 0;
            Sequence<File> selectedFiles = fileBrowserArgument.getSelectedFiles();
            for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
                File selectedFile = selectedFiles.get(i);
                if (selectedFile.isDirectory()) {
                    selectedDirectoryCount++;
                }
            }
            if (!fileBrowserArgument.isMultiSelect()) {
                File selectedFile = fileBrowserArgument.getSelectedFile();
                if (selectedFile != null && !selectedFile.isDirectory()) {
                    saveAsTextInput.setText(selectedFile.getName());
                }
            }
            updateOKButtonState();
        }
    });
    fileBrowser.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private File file = null;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            FileBrowserSheet.Mode mode = fileBrowserSheet.getMode();
            if (count == 1) {
                file = fileBrowser.getFileAt(x, y);
            } else if (count == 2) {
                File fileLocal = fileBrowser.getFileAt(x, y);
                if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
                    if (mode == FileBrowserSheet.Mode.OPEN || mode == FileBrowserSheet.Mode.OPEN_MULTIPLE) {
                        if (!fileLocal.isDirectory()) {
                            fileBrowserSheet.close(true);
                            consumed = true;
                        }
                    }
                }
            }
            return consumed;
        }
    });
    okButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(true);
        }
    });
    cancelButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(false);
        }
    });
    // Add this as a file browser sheet listener
    fileBrowserSheet.getFileBrowserSheetListeners().add(this);
    modeChanged(fileBrowserSheet, null);
    rootDirectoryChanged(fileBrowserSheet, null);
    selectedFilesChanged(fileBrowserSheet, null);
}
Also used : FileBrowserListener(org.apache.pivot.wtk.FileBrowserListener) SerializationException(org.apache.pivot.serialization.SerializationException) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) Mouse(org.apache.pivot.wtk.Mouse) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) FileBrowser(org.apache.pivot.wtk.FileBrowser) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 9 with Sequence

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

the class TerraFileBrowserSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final FileBrowser fileBrowser = (FileBrowser) component;
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        content = (Component) bxmlSerializer.readObject(TerraFileBrowserSkin.class, "terra_file_browser_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowser.add(content);
    bxmlSerializer.bind(this, TerraFileBrowserSkin.class);
    driveListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            if (previousSelectedItem != null) {
                File drive = (File) listButton.getSelectedItem();
                if (drive != null && drive.canRead()) {
                    if (!selectingDriveFromRootDirectory) {
                        fileBrowser.setRootDirectory(drive);
                    }
                } else {
                    refreshRoots = true;
                    listButton.setSelectedItem(previousSelectedItem);
                }
            }
        }
    });
    pathListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {

        @Override
        public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
            File ancestorDirectory = (File) listButton.getSelectedItem();
            if (ancestorDirectory != null) {
                fileBrowser.setRootDirectory(ancestorDirectory);
            }
        }
    });
    goUpButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            File rootDirectory = fileBrowser.getRootDirectory();
            File parentDirectory = rootDirectory.getParentFile();
            fileBrowser.setRootDirectory(parentDirectory);
        }
    });
    newFolderButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
        // TODO
        }
    });
    goHomeButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowser.setRootDirectory(HOME_DIRECTORY);
        }
    });
    /**
     * {@link KeyCode#DOWN DOWN} Transfer focus to the file list and select
     * the first item.<br> {@link KeyCode#ESCAPE ESCAPE} Clear the search
     * field.
     */
    searchTextInput.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component componentArgument, int keyCode, Keyboard.KeyLocation keyLocation) {
            boolean consumed = false;
            if (keyCode == Keyboard.KeyCode.ESCAPE) {
                searchTextInput.setText("");
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.DOWN) {
                if (fileTableView.getTableData().getLength() > 0) {
                    fileTableView.setSelectedIndex(0);
                    fileTableView.requestFocus();
                }
            }
            return consumed;
        }
    });
    searchTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textChanged(TextInput textInput) {
            refreshFileList();
        }
    });
    fileTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {

        @Override
        public void selectedRangeAdded(TableView tableView, int rangeStart, int rangeEnd) {
            if (!updatingSelection) {
                updatingSelection = true;
                for (int i = rangeStart; i <= rangeEnd; i++) {
                    @SuppressWarnings("unchecked") List<File> files = (List<File>) fileTableView.getTableData();
                    File file = files.get(i);
                    fileBrowser.addSelectedFile(file);
                }
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRangeRemoved(TableView tableView, int rangeStart, int rangeEnd) {
            if (!updatingSelection) {
                updatingSelection = true;
                for (int i = rangeStart; i <= rangeEnd; i++) {
                    @SuppressWarnings("unchecked") List<File> files = (List<File>) fileTableView.getTableData();
                    File file = files.get(i);
                    fileBrowser.removeSelectedFile(file);
                }
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
            if (!updatingSelection && previousSelectedRanges != null) {
                updatingSelection = true;
                @SuppressWarnings("unchecked") Sequence<File> files = (Sequence<File>) tableView.getSelectedRows();
                for (int i = 0, n = files.getLength(); i < n; i++) {
                    File file = files.get(i);
                    files.update(i, file);
                }
                fileBrowser.setSelectedFiles(files);
                updatingSelection = false;
            }
        }

        @Override
        public void selectedRowChanged(TableView tableView, Object previousSelectedRow) {
        // No-op
        }
    });
    fileTableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableView) {
            TableView.SortDictionary sort = fileTableView.getSort();
            if (!sort.isEmpty()) {
                Dictionary.Pair<String, SortDirection> pair = fileTableView.getSort().get(0);
                @SuppressWarnings("unchecked") List<File> files = (List<File>) fileTableView.getTableData();
                files.setComparator(getFileComparator(pair.key, pair.value));
            }
        }
    });
    fileTableView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private int index = -1;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            if (count == 1) {
                index = fileTableView.getRowAt(y);
            } else if (count == 2) {
                int indexLocal = fileTableView.getRowAt(y);
                if (indexLocal != -1 && indexLocal == this.index && fileTableView.isRowSelected(indexLocal)) {
                    File file = (File) fileTableView.getTableData().get(indexLocal);
                    if (file.isDirectory()) {
                        fileBrowser.setRootDirectory(file);
                        consumed = true;
                    }
                }
            }
            return consumed;
        }
    });
    fileBrowser.setFocusTraversalPolicy(new IndexFocusTraversalPolicy() {

        @Override
        public Component getNextComponent(Container container, Component componentArgument, FocusTraversalDirection direction) {
            Component nextComponent;
            if (componentArgument == null) {
                nextComponent = fileTableView;
            } else {
                nextComponent = super.getNextComponent(container, componentArgument, direction);
            }
            return nextComponent;
        }
    });
    fileTableView.setSort(TableViewFileRenderer.NAME_KEY, SortDirection.ASCENDING);
    fileTableView.getComponentTooltipListeners().add(new ComponentTooltipListener() {

        @Override
        public void tooltipTriggered(Component comp, int x, int y) {
            // Check that we are on the first column.
            if (fileTableView.getColumnAt(x) != 0) {
                return;
            }
            // Gets the underlying file
            int row = fileTableView.getRowAt(y);
            if (row < 0) {
                return;
            }
            File file = (File) fileTableView.getTableData().get(row);
            // Construct and show the tooltip.
            final Tooltip tooltip = new Tooltip();
            String text = null;
            if (file != null) {
                text = file.getName();
            }
            if (text == null || text.isEmpty()) {
                return;
            }
            TextArea toolTipTextArea = new TextArea();
            toolTipTextArea.setText(text);
            toolTipTextArea.getStyles().put(Style.wrapText, true);
            toolTipTextArea.getStyles().put(Style.backgroundColor, null);
            tooltip.setContent(toolTipTextArea);
            Point location = comp.getDisplay().getMouseLocation();
            x = location.x;
            y = location.y;
            // Ensure that the tooltip stays on screen
            Display display = comp.getDisplay();
            int tooltipHeight = tooltip.getPreferredHeight();
            if (y + tooltipHeight > display.getHeight()) {
                y -= tooltipHeight;
            }
            int tooltipXOffset = 16;
            int padding = 15;
            toolTipTextArea.setMaximumWidth(display.getWidth() - (x + tooltipXOffset + padding));
            tooltip.setLocation(x + tooltipXOffset, y);
            tooltip.open(comp.getWindow());
        }
    });
    rootDirectoryChanged(fileBrowser, null);
    selectedFilesChanged(fileBrowser, null);
}
Also used : FocusTraversalDirection(org.apache.pivot.wtk.FocusTraversalDirection) ComponentTooltipListener(org.apache.pivot.wtk.ComponentTooltipListener) TextArea(org.apache.pivot.wtk.TextArea) ListButtonSelectionListener(org.apache.pivot.wtk.ListButtonSelectionListener) TableViewSortListener(org.apache.pivot.wtk.TableViewSortListener) Span(org.apache.pivot.wtk.Span) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) Container(org.apache.pivot.wtk.Container) PushButton(org.apache.pivot.wtk.PushButton) ListButton(org.apache.pivot.wtk.ListButton) Button(org.apache.pivot.wtk.Button) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) TableViewSelectionListener(org.apache.pivot.wtk.TableViewSelectionListener) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) TableView(org.apache.pivot.wtk.TableView) SerializationException(org.apache.pivot.serialization.SerializationException) Keyboard(org.apache.pivot.wtk.Keyboard) Tooltip(org.apache.pivot.wtk.Tooltip) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) Point(org.apache.pivot.wtk.Point) Point(org.apache.pivot.wtk.Point) ListButton(org.apache.pivot.wtk.ListButton) ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) Mouse(org.apache.pivot.wtk.Mouse) FileBrowser(org.apache.pivot.wtk.FileBrowser) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) File(java.io.File) Display(org.apache.pivot.wtk.Display)

Example 10 with Sequence

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

the class JSONSerializerTest method testEquals.

@Test
public void testEquals() throws IOException, SerializationException {
    JSONSerializer jsonSerializer = new JSONSerializer();
    JSONSerializerListener jsonSerializerListener = new JSONSerializerListener() {

        @Override
        public void beginDictionary(JSONSerializer jsonSerializerArgument, Dictionary<String, ?> value) {
            System.out.println("Begin dictionary: " + value);
        }

        @Override
        public void endDictionary(JSONSerializer jsonSerializerArgument) {
            System.out.println("End dictionary");
        }

        @Override
        public void readKey(JSONSerializer jsonSerializerArgument, String key) {
            System.out.println("Read key: " + key);
        }

        @Override
        public void beginSequence(JSONSerializer jsonSerializerArgument, Sequence<?> value) {
            System.out.println("Begin sequence: " + value);
        }

        @Override
        public void endSequence(JSONSerializer jsonSerializerArgument) {
            System.out.println("End sequence");
        }

        @Override
        public void readString(JSONSerializer jsonSerializerArgument, String value) {
            System.out.println("Read string: " + value);
        }

        @Override
        public void readNumber(JSONSerializer jsonSerializerArgument, Number value) {
            System.out.println("Read number: " + value);
        }

        @Override
        public void readBoolean(JSONSerializer jsonSerializerArgument, Boolean value) {
            System.out.println("Read boolean: " + value);
        }

        @Override
        public void readNull(JSONSerializer jsonSerializerArgument) {
            System.out.println("Read null");
        }
    };
    jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
    Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
    assertEquals((Integer) JSON.get(o1, "a"), (Integer) 100);
    assertEquals(JSON.get(o1, "b"), "Hello");
    assertEquals(JSON.get(o1, "c"), false);
    assertEquals((Integer) JSON.get(o1, "e.g"), (Integer) 5);
    assertEquals((Integer) JSON.get(o1, "i.a"), (Integer) 200);
    assertEquals(JSON.get(o1, "i.c"), true);
    assertEquals(JSON.get(o1, "m"), "Hello\r\n\tWorld!");
    jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
    Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
    assertEquals((Integer) JSON.get(o2, "k[1].a"), (Integer) 10);
    assertEquals((Integer) JSON.get(o2, "k[2].a"), (Integer) 100);
    assertEquals((Integer) JSON.get(o2, "k[2].b"), (Integer) 200);
    assertEquals(JSON.get(o2, "k[2].c"), "300");
    assertEquals((Integer) JSON.get(o2, "j"), (Integer) 200);
    assertEquals(JSON.get(o2, "n"), "This is a \"test\" of the 'quoting' in \\JSON\\");
    assertTrue(o1.equals(o2));
    List<?> d = JSON.get(o1, "d");
    d.remove(0, 1);
    assertFalse(o1.equals(o2));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Sequence(org.apache.pivot.collections.Sequence) JSONSerializerListener(org.apache.pivot.json.JSONSerializerListener) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Aggregations

Sequence (org.apache.pivot.collections.Sequence)14 Component (org.apache.pivot.wtk.Component)7 IOException (java.io.IOException)6 Button (org.apache.pivot.wtk.Button)6 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)6 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)5 Dictionary (org.apache.pivot.collections.Dictionary)5 SerializationException (org.apache.pivot.serialization.SerializationException)5 PushButton (org.apache.pivot.wtk.PushButton)5 Span (org.apache.pivot.wtk.Span)5 BeanAdapter (org.apache.pivot.beans.BeanAdapter)4 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)4 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)4 Keyboard (org.apache.pivot.wtk.Keyboard)4 Mouse (org.apache.pivot.wtk.Mouse)4 TextInput (org.apache.pivot.wtk.TextInput)4 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)4 File (java.io.File)3 List (org.apache.pivot.collections.List)3 Map (org.apache.pivot.collections.Map)3