use of org.apache.pivot.collections.Sequence in project pivot by apache.
the class BXMLExplorerDocument method analyseObjectTree.
@SuppressWarnings("unchecked")
private TreeNode analyseObjectTree(Object container) {
// doesn't look neat
if (container instanceof TablePane) {
TreeBranch branch = new TreeBranch(nameForObject(container));
TablePane table = (TablePane) container;
for (TablePane.Row row : table.getRows()) {
TreeNode childBranch = analyseObjectTree(row);
branch.add(childBranch);
}
setComponentIconOnTreeNode(container, branch);
return branch;
}
// We don't want to analyse the components that are added as part of the
// skin, so use similar logic to BXMLSerializer
DefaultProperty defaultProperty = container.getClass().getAnnotation(DefaultProperty.class);
if (defaultProperty != null) {
TreeBranch branch = new TreeBranch(nameForObject(container));
String defaultPropertyName = defaultProperty.value();
BeanAdapter beanAdapter = new BeanAdapter(container);
if (!beanAdapter.containsKey(defaultPropertyName)) {
throw new IllegalStateException("default property " + defaultPropertyName + " not found on " + container);
}
Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);
if (defaultPropertyValue != null) {
if (defaultPropertyValue instanceof Component) {
TreeNode childBranch = analyseObjectTree(defaultPropertyValue);
branch.add(childBranch);
}
}
// so make empty branches into nodes.
if (branch.isEmpty()) {
TreeNode node = new TreeNode(branch.getText());
setComponentIconOnTreeNode(container, node);
return node;
}
setComponentIconOnTreeNode(container, branch);
return branch;
}
if (container instanceof Sequence<?>) {
TreeBranch branch = new TreeBranch(nameForObject(container));
Iterable<Object> sequence = (Iterable<Object>) container;
for (Object child : sequence) {
TreeNode childBranch = analyseObjectTree(child);
branch.add(childBranch);
}
setComponentIconOnTreeNode(container, branch);
return branch;
}
TreeNode node = new TreeNode(nameForObject(container));
setComponentIconOnTreeNode(container, node);
return node;
}
use of org.apache.pivot.collections.Sequence in project pivot by apache.
the class ListViews method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
selectionLabel = (Label) namespace.get("selectionLabel");
listView = (ListView) namespace.get("listView");
listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {
@Override
public void selectedRangeAdded(ListView listViewArgument, int rangeStart, int rangeEnd) {
updateSelection(listViewArgument);
}
@Override
public void selectedRangeRemoved(ListView listViewArgument, int rangeStart, int rangeEnd) {
updateSelection(listViewArgument);
}
@Override
public void selectedRangesChanged(ListView listViewArgument, Sequence<Span> previousSelectedRanges) {
if (previousSelectedRanges != null && previousSelectedRanges != listViewArgument.getSelectedRanges()) {
updateSelection(listViewArgument);
}
}
@Override
public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
// No-op
}
private void updateSelection(ListView listViewArgument) {
// TODO: in future use StringBuffer instead ...
String selectionText = "";
Sequence<Span> selectedRanges = listViewArgument.getSelectedRanges();
for (int i = 0, n = selectedRanges.getLength(); i < n; i++) {
Span selectedRange = selectedRanges.get(i);
for (int j = selectedRange.start; j <= selectedRange.end; j++) {
if (selectionText.length() > 0) {
selectionText += ", ";
}
Object item = listViewArgument.getListData().get(j);
String text;
if (item instanceof ListItem) {
// item is a listItem
// (for example because
// it has an image)
text = ((ListItem) item).getText();
} else {
// item is a standard item for listData
text = item.toString();
}
selectionText += text;
}
}
selectionLabel.setText(selectionText);
}
});
}
use of org.apache.pivot.collections.Sequence in project pivot by apache.
the class JSON method put.
/**
* Sets the value at the given path.
*
* @param <T> The type of value we're dealing with.
* @param root The root object.
* @param path The path to the desired location from the root.
* @param value The new value to set at the given path.
* @return The value previously associated with the path.
*/
@SuppressWarnings("unchecked")
public static <T> T put(Object root, String path, T value) {
Utils.checkNull(root, "root");
Sequence<String> keys = parse(path);
if (keys.getLength() == 0) {
throw new IllegalArgumentException("Path is empty.");
}
String key = keys.remove(keys.getLength() - 1, 1).get(0);
Object parent = get(root, keys);
if (parent == null) {
throw new IllegalArgumentException("Invalid path.");
}
Map<String, T> adapter = (Map<String, T>) (parent instanceof java.util.Map ? new MapAdapter<>((java.util.Map<String, T>) parent) : (parent instanceof Map ? ((Map<String, T>) parent) : new BeanAdapter(parent)));
Object previousValue;
if (adapter.containsKey(key)) {
previousValue = adapter.put(key, value);
} else if (parent instanceof Sequence<?>) {
Sequence<Object> sequence = (Sequence<Object>) parent;
previousValue = sequence.update(Integer.parseInt(key), value);
} else if (parent instanceof Dictionary<?, ?>) {
Dictionary<String, Object> dictionary = (Dictionary<String, Object>) parent;
previousValue = dictionary.put(key, value);
} else {
throw new IllegalArgumentException("Property \"" + key + "\" not found.");
}
return (T) previousValue;
}
use of org.apache.pivot.collections.Sequence in project pivot by apache.
the class JSON method containsKey.
/**
* Tests the existence of a path in a given object.
*
* @param <T> The type of value we're dealing with.
* @param root The root object.
* @param path The path to test (from the root).
* @return <tt>true</tt> if the path exists; <tt>false</tt>, otherwise.
*/
@SuppressWarnings("unchecked")
public static <T> boolean containsKey(Object root, String path) {
Utils.checkNull(root, "root");
Sequence<String> keys = parse(path);
if (keys.getLength() == 0) {
throw new IllegalArgumentException("Path is empty.");
}
String key = keys.remove(keys.getLength() - 1, 1).get(0);
Object parent = get(root, keys);
boolean containsKey;
if (parent == null) {
containsKey = false;
} else {
Map<String, T> adapter = (Map<String, T>) (parent instanceof java.util.Map ? new MapAdapter<>((java.util.Map<String, T>) parent) : (parent instanceof Map ? ((Map<String, T>) parent) : new BeanAdapter(parent)));
containsKey = adapter.containsKey(key);
if (!containsKey) {
if (parent instanceof Sequence<?>) {
Sequence<Object> sequence = (Sequence<Object>) parent;
containsKey = (sequence.getLength() > Integer.parseInt(key));
} else if (parent instanceof Dictionary<?, ?>) {
Dictionary<String, Object> dictionary = (Dictionary<String, Object>) parent;
containsKey = dictionary.containsKey(key);
} else {
throw new IllegalArgumentException("Property \"" + key + "\" not found.");
}
}
}
return containsKey;
}
use of org.apache.pivot.collections.Sequence in project pivot by apache.
the class FileDropTargetDemo method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
fileList = new FileList();
fileTableView.setTableData(fileList);
fileList.getListListeners().add(new ListListener<File>() {
@Override
public void itemInserted(List<File> list, int index) {
uploadButton.setEnabled(list.getLength() > 0);
}
@Override
public void itemsRemoved(List<File> list, int index, Sequence<File> files) {
uploadButton.setEnabled(list.getLength() > 0);
if (fileTableView.isFocused() && index < list.getLength()) {
fileTableView.setSelectedIndex(index);
}
}
});
fileTableView.getComponentKeyListeners().add(new ComponentKeyListener() {
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
if (keyCode == Keyboard.KeyCode.DELETE || keyCode == Keyboard.KeyCode.BACKSPACE) {
Sequence<Span> selectedRanges = fileTableView.getSelectedRanges();
for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
Span range = selectedRanges.get(i);
int index = range.start;
int count = range.end - index + 1;
fileList.remove(index, count);
}
}
return false;
}
});
fileTableView.setDropTarget(new DropTarget() {
@Override
public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsFileList() && DropAction.COPY.isSelected(supportedDropActions)) {
dropAction = DropAction.COPY;
}
return dropAction;
}
@Override
public void dragExit(Component component) {
// empty block
}
@Override
public DropAction dragMove(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsFileList() ? DropAction.COPY : null);
}
@Override
public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsFileList() ? DropAction.COPY : null);
}
@Override
public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsFileList()) {
try {
FileList tableData = (FileList) fileTableView.getTableData();
FileList fileListLocal = dragContent.getFileList();
for (File file : fileListLocal) {
if (file.isDirectory()) {
// TODO Expand recursively
}
tableData.add(file);
}
dropAction = DropAction.COPY;
} catch (IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
});
uploadButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Prompt.prompt(MessageType.INFO, "Pretending to upload...", FileDropTargetDemo.this);
}
});
}
Aggregations