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();
}
}
}));
}
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));
}
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();
}
}
}
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);
}
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;
}
Aggregations