use of org.apache.pivot.wtk.ButtonPressListener in project pivot by apache.
the class TerraVFSBrowserSkin method install.
@Override
public void install(Component component) {
super.install(component);
final VFSBrowser fileBrowser = (VFSBrowser) component;
BXMLSerializer bxmlSerializer = new BXMLSerializer();
try {
content = (Component) bxmlSerializer.readObject(TerraVFSBrowserSkin.class, "terra_vfs_browser_skin.bxml", true);
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
fileBrowser.add(content);
bxmlSerializer.bind(this, TerraVFSBrowserSkin.class);
// Notify all the renderers of which component they are dealing with
((FileRenderer) pathListButton.getDataRenderer()).setFileBrowser(fileBrowser);
((FileRenderer) pathListButton.getItemRenderer()).setFileBrowser(fileBrowser);
for (TableView.Column col : fileTableView.getColumns()) {
((FileRenderer) col.getCellRenderer()).setFileBrowser(fileBrowser);
}
homeDirectory = fileBrowser.getHomeDirectory();
driveListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {
@Override
public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
if (previousSelectedItem != null) {
FileObject drive = (FileObject) listButton.getSelectedItem();
try {
if (drive.isReadable()) {
fileBrowser.setRootDirectory(drive);
} else {
refreshRoots = true;
listButton.setSelectedItem(previousSelectedItem);
}
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
}
}
});
pathListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {
@Override
public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
FileObject ancestorDirectory = (FileObject) listButton.getSelectedItem();
if (ancestorDirectory != null) {
try {
fileBrowser.setRootDirectory(ancestorDirectory);
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
}
}
});
goUpButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
try {
FileObject rootDirectory = fileBrowser.getRootDirectory();
FileObject parentDirectory = rootDirectory.getParent();
fileBrowser.setRootDirectory(parentDirectory);
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
}
});
newFolderButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
// TODO
}
});
goHomeButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
try {
fileBrowser.setRootDirectory(fileBrowser.getHomeDirectory());
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
}
});
/**
* {@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;
try {
for (int i = rangeStart; i <= rangeEnd; i++) {
@SuppressWarnings("unchecked") List<FileObject> files = (List<FileObject>) fileTableView.getTableData();
FileObject file = files.get(i);
fileBrowser.addSelectedFile(file);
}
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
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<FileObject> files = (List<FileObject>) fileTableView.getTableData();
FileObject 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<FileObject> files = (Sequence<FileObject>) tableView.getSelectedRows();
for (int i = 0, n = files.getLength(); i < n; i++) {
FileObject file = files.get(i);
files.update(i, file);
}
try {
fileBrowser.setSelectedFiles(files);
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
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<FileObject> files = (List<FileObject>) 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)) {
FileObject file = (FileObject) fileTableView.getTableData().get(indexLocal);
try {
if (file.getName().getType() == FileType.FOLDER) {
fileBrowser.setRootDirectory(file);
consumed = true;
}
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
}
}
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;
}
FileObject file = (FileObject) fileTableView.getTableData().get(row);
// Construct and show the tooltip.
final Tooltip tooltip = new Tooltip();
String text = null;
if (file != null) {
text = file.getName().getBaseName();
}
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);
}
use of org.apache.pivot.wtk.ButtonPressListener in project pivot by apache.
the class RadioButtons method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
selectButton = (PushButton) namespace.get("selectButton");
// Get a reference to the button group
final ButtonGroup numbersGroup = (ButtonGroup) namespace.get("numbers");
// Add a button press listener
selectButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
String message = "You selected \"" + numbersGroup.getSelection().getButtonData() + "\".";
Alert.alert(MessageType.INFO, message, RadioButtons.this);
}
});
}
use of org.apache.pivot.wtk.ButtonPressListener 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());
}
}
});
}
use of org.apache.pivot.wtk.ButtonPressListener in project pivot by apache.
the class DataBinding method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
form = (Form) namespace.get("form");
loadJavaButton = (PushButton) namespace.get("loadJavaButton");
loadJSONButton = (PushButton) namespace.get("loadJSONButton");
clearButton = (PushButton) namespace.get("clearButton");
sourceLabel = (Label) namespace.get("sourceLabel");
loadJavaButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
form.load(new BeanAdapter(CONTACT));
sourceLabel.setText("Java");
}
});
loadJSONButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
JSONSerializer serializer = new JSONSerializer();
try (InputStream inputStream = getClass().getResourceAsStream("contact.json")) {
form.load(serializer.readObject(inputStream));
sourceLabel.setText("JSON");
} catch (Exception exception) {
System.err.println(exception);
}
button.setEnabled(true);
}
});
clearButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
form.clear();
sourceLabel.setText("");
}
});
}
use of org.apache.pivot.wtk.ButtonPressListener in project pivot by apache.
the class BXMLExplorerWindow method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
// hide until we support editing and saving BXML files
if (!BXMLExplorer.ENABLE_EDITING) {
paletteTabPane.getTabs().remove(paletteTreeViewScrollPane);
splitPane.setSplitRatio(0);
fileMenuSection.remove(fileNewMenuItem);
}
fileBrowserSheet.setDisabledFileFilter(new Filter<File>() {
@Override
public boolean include(File item) {
return !(item.isDirectory() || item.getName().endsWith(".bxml"));
}
});
closeButton.setEnabled(false);
closeButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
int x = tabPane.getSelectedIndex();
tabPane.getTabs().remove(x, 1);
if (tabPane.getTabs().getLength() > 0) {
x = Math.max(x - 1, 0);
tabPane.setSelectedIndex(x);
}
closeButton.setEnabled(tabPane.getTabs().getLength() > 0);
}
});
}
Aggregations