use of org.apache.pivot.beans.BXMLSerializer in project pivot by apache.
the class ScriptApplication method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
// Get the location of the source file
String src = properties.get(SRC_KEY);
if (src == null) {
if (sourceDefault == null) {
throw new IllegalArgumentException(SRC_KEY + " argument is required.");
}
src = sourceDefault;
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL location = classLoader.getResource(src.substring(1));
if (location == null) {
// If the source file isn't in the resources, try finding it as a local file
File localFile = new File(src);
if (localFile.exists() && localFile.isFile() && localFile.canRead()) {
try {
location = localFile.toURI().toURL();
} catch (MalformedURLException mue) {
}
}
}
if (location == null) {
throw new IllegalArgumentException("Cannot find source file \"" + src + "\".");
}
Resources resources;
if (properties.containsKey(RESOURCES_KEY)) {
resources = new Resources(properties.get(RESOURCES_KEY));
} else {
resources = null;
}
if (properties.containsKey(STYLESHEET_KEY)) {
String stylesheet = properties.get(STYLESHEET_KEY);
if (!stylesheet.startsWith("/")) {
throw new IllegalArgumentException("Value for " + STYLESHEET_KEY + " argument must start with a slash character.");
}
ApplicationContext.applyStylesheet(stylesheet);
}
// Load the file and open the window
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Component component = (Component) bxmlSerializer.readObject(location, resources);
if (component instanceof Sheet || component instanceof Palette) {
window = new Window();
window.setMaximized(true);
window.open(display);
Window auxWindow = (Window) component;
auxWindow.open(window);
auxWindow.requestFocus();
} else {
if (component instanceof Window) {
window = (Window) component;
} else {
window = new Window();
window.setContent(component);
}
window.open(display);
Component content = window.getContent();
if (content != null) {
content.requestFocus();
}
}
}
use of org.apache.pivot.beans.BXMLSerializer in project pivot by apache.
the class TerraVFSBrowserSheetSkin method install.
@Override
public void install(Component component) {
super.install(component);
final VFSBrowserSheet fileBrowserSheet = (VFSBrowserSheet) component;
fileBrowserSheet.setMinimumWidth(360);
fileBrowserSheet.setMinimumHeight(180);
// Load the sheet content
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Component content;
try {
content = (Component) bxmlSerializer.readObject(TerraVFSBrowserSheetSkin.class, "terra_vfs_browser_sheet_skin.bxml", true);
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
fileBrowserSheet.setContent(content);
bxmlSerializer.bind(this, TerraVFSBrowserSheetSkin.class);
// set the same rootDirectory as the component
try {
FileObject rootDirectory = fileBrowserSheet.getRootDirectory();
fileBrowser.setRootDirectory(rootDirectory);
setHostLabel(rootDirectory);
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
// set the same homeDirectory as the component
try {
fileBrowser.setHomeDirectory(fileBrowserSheet.getHomeDirectory());
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {
@Override
public void textChanged(TextInput textInput) {
Form.clearFlag(saveAsBoxPane);
updateOKButtonState();
}
});
fileBrowser.getFileBrowserListeners().add(new VFSBrowserListener() {
@Override
public void rootDirectoryChanged(VFSBrowser fileBrowserArgument, FileObject previousRootDirectory) {
updatingSelection = true;
try {
FileObject rootDirectory = fileBrowserArgument.getRootDirectory();
fileBrowserSheet.setRootDirectory(rootDirectory);
setHostLabel(rootDirectory);
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
updatingSelection = false;
selectedDirectoryCount = 0;
updateOKButtonState();
}
@Override
public void homeDirectoryChanged(VFSBrowser fileBrowserArgument, FileObject previousHomeDirectory) {
updatingSelection = true;
try {
fileBrowserSheet.setHomeDirectory(fileBrowserArgument.getHomeDirectory());
} catch (FileSystemException fse) {
throw new RuntimeException(fse);
}
updatingSelection = false;
}
@Override
public void selectedFileAdded(VFSBrowser fileBrowserArgument, FileObject file) {
if (file.getName().getType() == FileType.FOLDER) {
selectedDirectoryCount++;
}
updateOKButtonState();
}
@Override
public void selectedFileRemoved(VFSBrowser fileBrowserArgument, FileObject file) {
if (file.getName().getType() == FileType.FOLDER) {
selectedDirectoryCount--;
}
updateOKButtonState();
}
@Override
public void selectedFilesChanged(VFSBrowser fileBrowserArgument, Sequence<FileObject> previousSelectedFiles) {
selectedDirectoryCount = 0;
Sequence<FileObject> selectedFiles = fileBrowserArgument.getSelectedFiles();
for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
FileObject selectedFile = selectedFiles.get(i);
if (selectedFile.getName().getType() == FileType.FOLDER) {
selectedDirectoryCount++;
}
}
if (!fileBrowserArgument.isMultiSelect()) {
FileObject selectedFile = fileBrowserArgument.getSelectedFile();
if (selectedFile != null && selectedFile.getName().getType() != FileType.FOLDER) {
saveAsTextInput.setText(selectedFile.getName().getPath());
}
}
updateOKButtonState();
}
});
fileBrowser.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {
private FileObject file = null;
@Override
public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
boolean consumed = false;
VFSBrowserSheet.Mode mode = fileBrowserSheet.getMode();
if (count == 1) {
file = fileBrowser.getFileAt(x, y);
} else if (count == 2) {
FileObject fileLocal = fileBrowser.getFileAt(x, y);
if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
if (mode == VFSBrowserSheet.Mode.OPEN || mode == VFSBrowserSheet.Mode.OPEN_MULTIPLE) {
if (fileLocal.getName().getType() != FileType.FOLDER) {
fileBrowserSheet.close(true);
consumed = true;
}
}
}
}
return consumed;
}
});
okButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
fileBrowserSheet.close(true);
}
});
cancelButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
fileBrowserSheet.close(false);
}
});
// Add this as a file browser sheet listener
fileBrowserSheet.getFileBrowserSheetListeners().add(this);
modeChanged(fileBrowserSheet, null);
homeDirectoryChanged(fileBrowserSheet, null);
rootDirectoryChanged(fileBrowserSheet, null);
selectedFilesChanged(fileBrowserSheet, null);
}
use of org.apache.pivot.beans.BXMLSerializer 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.beans.BXMLSerializer in project pivot by apache.
the class BXMLExplorerDocument method load.
public void load(File f) throws IOException, SerializationException, ParserConfigurationException, SAXException {
BXMLSerializer serializer = new BXMLSerializer();
serializer.setLocation(f.toURI().toURL());
try (FileInputStream in = new FileInputStream(f)) {
Object obj = serializer.readObject(in);
if (!(obj instanceof Component)) {
throw new IllegalStateException("obj " + obj + " is of class " + obj.getClass() + " which is not a subtype of Component");
}
// create an inverse map so we can IDs for widgets
widgetToID = new HashMap<>();
for (String key : serializer.getNamespace()) {
widgetToID.put(serializer.getNamespace().get(key), key);
}
// we can't add a Window into the component hierarchy, so fake it
if (obj instanceof Window) {
obj = new FakeWindow((Window) obj);
}
// create the explorer tree
componentToTreeNode = new HashMap<>();
// the root node is not visible
final TreeBranch rootbranch = new TreeBranch("");
rootbranch.add(analyseObjectTree(obj));
treeView.setTreeData(rootbranch);
treeView.expandAll();
// add the loaded widget to the display
this.loadedComponent = (Component) obj;
playgroundCardPane.add((Component) obj);
}
try (FileInputStream in2 = new FileInputStream(f)) {
CreateHighlightedXML xml = new CreateHighlightedXML();
bxmlSourceTextPane.setDocument(xml.prettyPrint(in2));
}
this.file = f;
}
use of org.apache.pivot.beans.BXMLSerializer 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);
}
}
Aggregations