Search in sources :

Example 46 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class BXMLExplorerDocument method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    treeView.setSelectMode(SelectMode.SINGLE);
    treeView.setNodeRenderer(new MyTreeViewNodeRenderer());
    treeView.getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {

        private final Decorator focusDecorator = new ShadeDecorator(0.2f, Color.RED);

        private Component previousSelectedComponent = null;

        @Override
        public void selectedNodeChanged(TreeView treeViewArgument, Object previousSelectedNode) {
            TreeNode node = (TreeNode) treeViewArgument.getSelectedNode();
            if (previousSelectedComponent != null && previousSelectedComponent.getDecorators().indexOf(focusDecorator) > -1) {
                previousSelectedComponent.getDecorators().remove(focusDecorator);
                previousSelectedComponent = null;
            }
            if (node == null || !(node.getUserData() instanceof Component)) {
                // TODO make the inspectors able to deal with things like
                // TablePane.Row
                componentPropertyInspector.setSource(null);
                componentStyleInspector.setSource(null);
                return;
            }
            Component selectedComp = (Component) node.getUserData();
            if (selectedComp != null && selectedComp.getDecorators().indexOf(focusDecorator) == -1) {
                selectedComp.getDecorators().add(focusDecorator);
                previousSelectedComponent = selectedComp;
            }
            if (selectedComp instanceof FakeWindow) {
                selectedComp = ((FakeWindow) selectedComp).window;
            }
            componentPropertyInspector.setSource(selectedComp);
            componentStyleInspector.setSource(selectedComp);
        }

        @Override
        public void selectedPathsChanged(TreeView treeViewArgument, Sequence<Path> previousSelectedPaths) {
            // if the selection becomes empty, remove the decorator
            if (treeViewArgument.getSelectedNode() == null && previousSelectedComponent != null && previousSelectedComponent.getDecorators().indexOf(focusDecorator) > -1) {
                previousSelectedComponent.getDecorators().remove(focusDecorator);
            }
        }
    });
    playgroundCardPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        @Override
        public boolean mouseClick(Component component, Button button, int x, int y, int count) {
            if (count == 1) {
                Component comp = playgroundCardPane.getDescendantAt(x, y);
                if (comp != null) {
                    TreeNode treeNode = componentToTreeNode.get(comp);
                    Path path = getPathForNode(treeView, treeNode);
                    if (path != null) {
                        treeView.setSelectedPath(path);
                        return true;
                    }
                }
            }
            return false;
        }
    });
    reloadButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(org.apache.pivot.wtk.Button button) {
            playgroundCardPane.remove(loadedComponent);
            widgetToID = null;
            componentToTreeNode = null;
            loadedComponent = null;
            try {
                load(file);
            } catch (RuntimeException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (IOException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (SerializationException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (ParserConfigurationException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            } catch (SAXException exception) {
                exception.printStackTrace();
                BXMLExplorer.displayLoadException(exception, BXMLExplorerDocument.this.getWindow());
            }
        }
    });
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) ShadeDecorator(org.apache.pivot.wtk.effects.ShadeDecorator) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Decorator(org.apache.pivot.wtk.effects.Decorator) ShadeDecorator(org.apache.pivot.wtk.effects.ShadeDecorator) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) LinkButton(org.apache.pivot.wtk.LinkButton) Button(org.apache.pivot.wtk.Mouse.Button) RadioButton(org.apache.pivot.wtk.RadioButton) PushButton(org.apache.pivot.wtk.PushButton) TreeNode(org.apache.pivot.wtk.content.TreeNode) TreeViewSelectionListener(org.apache.pivot.wtk.TreeViewSelectionListener) TreeView(org.apache.pivot.wtk.TreeView) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Component(org.apache.pivot.wtk.Component)

Example 47 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class Windows method startup.

@Override
public void startup(Display displayArgument, Map<String, String> properties) throws Exception {
    this.display = displayArgument;
    int x = 0;
    int y = 0;
    for (int i = 0; i < 3; i++) {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        bxmlSerializer.getNamespace().put("application", this);
        Frame frame;
        try {
            frame = (Frame) bxmlSerializer.readObject(Windows.class, "frame.bxml");
        } catch (SerializationException exception) {
            throw new RuntimeException(exception);
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
        frame.setTitle("Frame " + (i + 1));
        frame.setLocation(x, y);
        x += 20;
        y += 20;
        frame.open(displayArgument);
    }
}
Also used : Frame(org.apache.pivot.wtk.Frame) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 48 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class QueryServlet method doPost.

@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Path path = getPath(request);
    URL location = null;
    try {
        validate(Query.Method.POST, path);
        Object value = null;
        if (request.getContentLength() > 0) {
            Serializer<?> serializer = createSerializer(Query.Method.POST, path);
            value = serializer.readObject(request.getInputStream());
        }
        location = doPost(path, value);
    } catch (SerializationException exception) {
        throw new ServletException(exception);
    } catch (QueryException exception) {
        response.setStatus(exception.getStatus());
        response.flushBuffer();
    }
    if (!response.isCommitted()) {
        if (location == null) {
            response.setStatus(Query.Status.NO_CONTENT);
        } else {
            response.setStatus(Query.Status.CREATED);
            response.setHeader(LOCATION_HEADER, location.toString());
        }
        setResponseHeaders(response);
        response.setContentLength(0);
    }
}
Also used : ServletException(javax.servlet.ServletException) QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) URL(java.net.URL)

Example 49 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class QueryServlet method doGet.

@Override
@SuppressWarnings("unchecked")
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Path path = getPath(request);
    Object result = null;
    Serializer<Object> serializer = null;
    try {
        validate(Query.Method.GET, path);
        result = doGet(path);
        serializer = (Serializer<Object>) createSerializer(Query.Method.GET, path);
    } catch (QueryException exception) {
        response.setStatus(exception.getStatus());
        response.flushBuffer();
    }
    if (!response.isCommitted() && serializer != null) {
        response.setStatus(Query.Status.OK);
        setResponseHeaders(response);
        response.setContentType(serializer.getMIMEType(result));
        OutputStream responseOutputStream = response.getOutputStream();
        if (determineContentLength) {
            File tempFile = File.createTempFile(getClass().getName(), null);
            // Serialize the result to an intermediary file
            try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
                serializer.writeObject(result, fileOutputStream);
            } catch (SerializationException exception) {
                throw new ServletException(exception);
            }
            // Set the content length header
            response.setHeader(CONTENT_LENGTH_HEADER, String.valueOf(tempFile.length()));
            // Write the contents of the file out to the response
            try (FileInputStream fileInputStream = new FileInputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int nBytes;
                do {
                    nBytes = fileInputStream.read(buffer);
                    if (nBytes > 0) {
                        responseOutputStream.write(buffer, 0, nBytes);
                    }
                } while (nBytes != -1);
            }
        } else {
            try {
                serializer.writeObject(result, responseOutputStream);
            } catch (SerializationException exception) {
                throw new ServletException(exception);
            }
        }
        response.flushBuffer();
    }
}
Also used : ServletException(javax.servlet.ServletException) QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

SerializationException (org.apache.pivot.serialization.SerializationException)49 IOException (java.io.IOException)28 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)14 URL (java.net.URL)9 JSONSerializer (org.apache.pivot.json.JSONSerializer)9 ArrayList (org.apache.pivot.collections.ArrayList)8 Component (org.apache.pivot.wtk.Component)7 File (java.io.File)6 List (org.apache.pivot.collections.List)6 QueryException (org.apache.pivot.web.QueryException)6 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)6 InputStream (java.io.InputStream)5 Sequence (org.apache.pivot.collections.Sequence)5 Button (org.apache.pivot.wtk.Button)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 Mouse (org.apache.pivot.wtk.Mouse)5 PushButton (org.apache.pivot.wtk.PushButton)5 MalformedURLException (java.net.MalformedURLException)4 ScriptException (javax.script.ScriptException)4 TextInput (org.apache.pivot.wtk.TextInput)4