Search in sources :

Example 6 with Map

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

the class SearchDemo method executeQuery.

/**
 * Executes a search.
 *
 * @param term The search term.
 * @throws IllegalArgumentException If <tt>term</tt> is <tt>null</tt> or
 * empty.
 * @throws IllegalStateException If a query is already executing.
 */
public void executeQuery(String term) {
    Utils.checkNullOrEmpty(term, "search term");
    if (getQuery != null) {
        throw new IllegalStateException("Query is already running!");
    }
    String country = Locale.getDefault().getCountry().toLowerCase();
    getQuery = new GetQuery(QUERY_HOSTNAME, BASE_QUERY_PATH);
    getQuery.getParameters().put("term", term);
    getQuery.getParameters().put("country", country);
    getQuery.getParameters().put("media", MEDIA);
    getQuery.getParameters().put("limit", Integer.toString(LIMIT));
    getQuery.getParameters().put("output", "json");
    System.out.println(getQuery.getLocation());
    statusLabel.setText("Searching...");
    updateActivityState();
    getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            if (task == getQuery) {
                @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) task.getResult();
                @SuppressWarnings("unchecked") List<Object> results = (List<Object>) result.get("results");
                // Preserve any existing sort
                @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) resultsTableView.getTableData();
                Comparator<Object> comparator = tableData.getComparator();
                results.setComparator(comparator);
                // Update the table data
                resultsTableView.setTableData(results);
                statusLabel.setText("Found " + results.getLength() + " matching items.");
                getQuery = null;
                updateActivityState();
                if (results.getLength() > 0) {
                    resultsTableView.setSelectedIndex(0);
                    resultsTableView.requestFocus();
                } else {
                    termTextInput.requestFocus();
                }
            }
        }

        @Override
        public void executeFailed(Task<Object> task) {
            if (task == getQuery) {
                statusLabel.setText(task.getFault().getMessage());
                getQuery = null;
                updateActivityState();
                termTextInput.requestFocus();
            }
        }
    }));
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List) Map(org.apache.pivot.collections.Map)

Example 7 with Map

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

the class JSONViewer method setValue.

private void setValue(Object value) {
    assert (value instanceof Map<?, ?> || value instanceof List<?>);
    // Remove prompt decorator
    if (promptDecorator != null) {
        treeView.getDecorators().remove(promptDecorator);
        promptDecorator = null;
    }
    TreeBranch treeData = new TreeBranch();
    treeData.add(build(value));
    treeView.setTreeData(treeData);
    treeView.expandBranch(new Path(0));
}
Also used : Path(org.apache.pivot.collections.Sequence.Tree.Path) TreeBranch(org.apache.pivot.wtk.content.TreeBranch) List(org.apache.pivot.collections.List) FileList(org.apache.pivot.io.FileList) Map(org.apache.pivot.collections.Map)

Example 8 with Map

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

the class ResultListTest method main.

public static void main(String[] args) throws Exception {
    // e.g. jdbc:mysql://localhost/test
    String connectionURL = args[0];
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    ResultList resultList = null;
    try {
        connection = DriverManager.getConnection(connectionURL);
        statement = connection.createStatement();
        resultSet = statement.executeQuery("SELECT * FROM result_list_test");
        resultList = new ResultList(resultSet);
        resultList.setFields(new ResultList.Field("i"), new ResultList.Field("f"), new ResultList.Field("s"), new ResultList.Field("b"));
        out.println(JSONSerializer.toString(resultList));
        resultSet = statement.executeQuery("SELECT * FROM result_list_test");
        resultList = new ResultList(resultSet);
        resultList.setFields(new ResultList.Field("i", "integer"), new ResultList.Field("f", "float"), new ResultList.Field("s", "string"), new ResultList.Field("b", "boolean"));
        out.println(JSONSerializer.toString(resultList));
        resultSet = statement.executeQuery("SELECT * FROM result_list_test");
        resultList = new ResultList(resultSet);
        resultList.setFields(new ResultList.Field("i", "integer", Integer.class), new ResultList.Field("f", "float", Float.class), new ResultList.Field("s", "string", String.class), new ResultList.Field("b", "boolean", Boolean.class));
        out.println(JSONSerializer.toString(resultList));
        // Test forward and backward iteration
        resultSet = statement.executeQuery("SELECT * FROM result_list_test");
        resultList = new ResultList(resultSet);
        resultList.setFields(new ResultList.Field("i"), new ResultList.Field("f"), new ResultList.Field("s"), new ResultList.Field("b"));
        Iterator<Map<String, Object>> iterator = resultList.iterator();
        while (iterator.hasNext()) {
            out.println(JSONSerializer.toString(iterator.next()));
        }
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : ResultList(org.apache.pivot.sql.ResultList) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Map(org.apache.pivot.collections.Map)

Example 9 with Map

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

the class SearchDemo method updateArtwork.

/**
 * Updates the artwork to reflect the current selection.
 */
public void updateArtwork() {
    @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) resultsTableView.getSelectedRow();
    URL artworkURL = null;
    if (result != null) {
        try {
            artworkURL = new URL((String) result.get("artworkUrl100"));
        } catch (MalformedURLException exception) {
        // ignore exception
        }
    }
    if (artworkURL == null) {
        artworkImageView.setImage((Image) null);
    } else {
        Image.load(artworkURL, new TaskAdapter<>(new TaskListener<Image>() {

            @Override
            public void taskExecuted(Task<Image> task) {
                artworkImageView.setImage(task.getResult());
            }

            @Override
            public void executeFailed(Task<Image> task) {
                artworkImageView.setImage((Image) null);
            }
        }));
    }
    previewButton.setEnabled(result != null);
}
Also used : MalformedURLException(java.net.MalformedURLException) Task(org.apache.pivot.util.concurrent.Task) TaskListener(org.apache.pivot.util.concurrent.TaskListener) Map(org.apache.pivot.collections.Map) URL(java.net.URL)

Example 10 with Map

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

the class JSONViewer method build.

@SuppressWarnings("unchecked")
private static TreeNode build(Object value) {
    TreeNode treeNode;
    if (value instanceof Map<?, ?>) {
        TreeBranch treeBranch = new TreeBranch("{}");
        treeBranch.setComparator(new Comparator<TreeNode>() {

            @Override
            public int compare(TreeNode treeNode1, TreeNode treeNode2) {
                return treeNode1.getText().compareTo(treeNode2.getText());
            }
        });
        Map<String, Object> map = (Map<String, Object>) value;
        for (String key : map) {
            TreeNode valueNode = build(map.get(key));
            String text = valueNode.getText();
            if (text == null) {
                valueNode.setText(key);
            } else {
                valueNode.setText(key + " : " + text);
            }
            treeBranch.add(valueNode);
        }
        treeNode = treeBranch;
    } else if (value instanceof List<?>) {
        TreeBranch treeBranch = new TreeBranch("[]");
        List<Object> list = (List<Object>) value;
        for (int i = 0, n = list.getLength(); i < n; i++) {
            TreeNode itemNode = build(list.get(i));
            String text = itemNode.getText();
            if (text == null) {
                itemNode.setText("[" + i + "]");
            } else {
                itemNode.setText("[" + i + "] " + text);
            }
            treeBranch.add(itemNode);
        }
        treeNode = treeBranch;
    } else if (value instanceof String) {
        treeNode = new TreeNode("\"" + value.toString() + "\"");
    } else if (value instanceof Number) {
        treeNode = new TreeNode(value.toString());
    } else if (value instanceof Boolean) {
        treeNode = new TreeNode(value.toString());
    } else {
        treeNode = new TreeNode("null");
    }
    return treeNode;
}
Also used : TreeBranch(org.apache.pivot.wtk.content.TreeBranch) TreeNode(org.apache.pivot.wtk.content.TreeNode) List(org.apache.pivot.collections.List) FileList(org.apache.pivot.io.FileList) Map(org.apache.pivot.collections.Map)

Aggregations

Map (org.apache.pivot.collections.Map)16 HashMap (org.apache.pivot.collections.HashMap)7 List (org.apache.pivot.collections.List)5 BeanAdapter (org.apache.pivot.beans.BeanAdapter)4 Dictionary (org.apache.pivot.collections.Dictionary)3 Sequence (org.apache.pivot.collections.Sequence)3 JSONSerializer (org.apache.pivot.json.JSONSerializer)3 SerializationException (org.apache.pivot.serialization.SerializationException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (org.apache.pivot.collections.ArrayList)2 FileList (org.apache.pivot.io.FileList)2 PropertiesSerializer (org.apache.pivot.serialization.PropertiesSerializer)2 Task (org.apache.pivot.util.concurrent.Task)2 TaskListener (org.apache.pivot.util.concurrent.TaskListener)2 TreeBranch (org.apache.pivot.wtk.content.TreeBranch)2 Test (org.junit.Test)2 Color (java.awt.Color)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1