use of org.apache.pivot.io.FileList in project pivot by apache.
the class XMLViewer method drop.
public DropAction drop(Manifest dragContent) {
DropAction dropAction = null;
try {
FileList fileList = dragContent.getFileList();
if (fileList.getLength() == 1) {
File file = fileList.get(0);
XMLSerializer xmlSerializer = new XMLSerializer();
@SuppressWarnings("resource") FileInputStream fileInputStream = null;
try {
try {
fileInputStream = new FileInputStream(file);
setDocument(xmlSerializer.readObject(fileInputStream));
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
} catch (Exception exception) {
Prompt.prompt(exception.getMessage(), window);
}
window.setTitle(WINDOW_TITLE + " - " + file.getName());
dropAction = DropAction.COPY;
} else {
Prompt.prompt("Drop of multiple files is not supported.", window);
}
} catch (IOException exception) {
Prompt.prompt(exception.getMessage(), window);
}
return dropAction;
}
use of org.apache.pivot.io.FileList in project pivot by apache.
the class DragAndDropDemo method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
// Text
label.setDragSource(new DragSource() {
private LocalManifest content = null;
@Override
public boolean beginDrag(Component component, int x, int y) {
String text = label.getText();
if (text != null) {
content = new LocalManifest();
content.putText(label.getText());
}
return (content != null);
}
@Override
public void endDrag(Component component, DropAction dropAction) {
content = null;
}
@Override
public boolean isNative() {
return true;
}
@Override
public LocalManifest getContent() {
return content;
}
@Override
public Visual getRepresentation() {
return null;
}
@Override
public Point getOffset() {
return null;
}
@Override
public int getSupportedDropActions() {
return DropAction.COPY.getMask();
}
});
label.setDropTarget(new DropTarget() {
@Override
public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsText() && 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.containsText() ? DropAction.COPY : null);
}
@Override
public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsText() ? DropAction.COPY : null);
}
@Override
public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsText()) {
try {
label.setText(dragContent.getText());
dropAction = DropAction.COPY;
} catch (IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
});
copyTextButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
String text = label.getText();
LocalManifest clipboardContent = new LocalManifest();
clipboardContent.putText(text);
Clipboard.setContent(clipboardContent);
}
});
pasteTextButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null && clipboardContent.containsText()) {
try {
label.setText(clipboardContent.getText());
} catch (IOException exception) {
System.err.println(exception);
}
}
}
});
// Images
imageView.setDragSource(new DragSource() {
private LocalManifest content = null;
@Override
public boolean beginDrag(Component component, int x, int y) {
Image image = imageView.getImage();
if (image != null) {
content = new LocalManifest();
content.putImage(image);
}
return (content != null);
}
@Override
public void endDrag(Component component, DropAction dropAction) {
content = null;
}
@Override
public boolean isNative() {
return true;
}
@Override
public LocalManifest getContent() {
return content;
}
@Override
public Visual getRepresentation() {
return null;
}
@Override
public Point getOffset() {
return null;
}
@Override
public int getSupportedDropActions() {
return DropAction.COPY.getMask();
}
});
imageView.setDropTarget(new DropTarget() {
@Override
public DropAction dragEnter(Component component, Manifest dragContent, int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsImage() && 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.containsImage() ? DropAction.COPY : null);
}
@Override
public DropAction userDropActionChange(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsImage() ? DropAction.COPY : null);
}
@Override
public DropAction drop(Component component, Manifest dragContent, int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsImage()) {
try {
imageView.setImage(dragContent.getImage());
dropAction = DropAction.COPY;
} catch (IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
});
copyImageButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Image image = imageView.getImage();
if (image != null) {
LocalManifest clipboardContent = new LocalManifest();
clipboardContent.putImage(image);
Clipboard.setContent(clipboardContent);
}
}
});
pasteImageButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null && clipboardContent.containsImage()) {
try {
imageView.setImage(clipboardContent.getImage());
} catch (IOException exception) {
System.err.println(exception);
}
}
}
});
// Files
listView.setListData(new FileList());
listView.setDragSource(new DragSource() {
private LocalManifest content = null;
@Override
public boolean beginDrag(Component component, int x, int y) {
ListView listViewLocal = (ListView) component;
FileList fileList = (FileList) listViewLocal.getListData();
if (fileList.getLength() > 0) {
content = new LocalManifest();
content.putFileList(fileList);
}
return (content != null);
}
@Override
public void endDrag(Component component, DropAction dropAction) {
content = null;
}
@Override
public boolean isNative() {
return true;
}
@Override
public LocalManifest getContent() {
return content;
}
@Override
public Visual getRepresentation() {
return null;
}
@Override
public Point getOffset() {
return null;
}
@Override
public int getSupportedDropActions() {
return DropAction.COPY.getMask();
}
});
listView.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 {
listView.setListData(dragContent.getFileList());
dropAction = DropAction.COPY;
} catch (IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
});
copyFilesButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
// TODO
}
});
pasteFilesButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
// TODO
}
});
}
use of org.apache.pivot.io.FileList 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);
}
});
}
use of org.apache.pivot.io.FileList in project pivot by apache.
the class FileBrowser method setSelectedFiles.
/**
* Sets the selected files.
*
* @param selectedFiles The files to select.
* @return The files that were selected, with duplicates eliminated.
* @throws IllegalArgumentException if the selected files sequence is {@code null}
* or if the sequence is longer than one file and multi-select is not enabled, or
* if any entry is the sequence is {@code null} or whose parent is not the
* current root directory.
*/
public Sequence<File> setSelectedFiles(Sequence<File> selectedFiles) {
Utils.checkNull(selectedFiles, "selectedFiles");
if (!multiSelect && selectedFiles.getLength() > 1) {
throw new IllegalArgumentException("Multi-select is not enabled.");
}
// Update the selection
Sequence<File> previousSelectedFiles = getSelectedFiles();
FileList fileList = new FileList();
for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
File file = selectedFiles.get(i);
Utils.checkNull(file, "file");
if (!file.isAbsolute()) {
file = new File(rootDirectory, file.getPath());
}
if (!file.getParentFile().equals(rootDirectory)) {
throw new IllegalArgumentException(file.getPath() + " is not a child of the root directory.");
}
fileList.add(file);
}
this.selectedFiles = fileList;
// Notify listeners
fileBrowserListeners.selectedFilesChanged(this, previousSelectedFiles);
return getSelectedFiles();
}
use of org.apache.pivot.io.FileList in project pivot by apache.
the class JSONViewer method drop.
public DropAction drop(Manifest dragContent) {
DropAction dropAction = null;
try {
FileList fileList = dragContent.getFileList();
if (fileList.getLength() == 1) {
File file = fileList.get(0);
JSONSerializer jsonSerializer = new JSONSerializer();
@SuppressWarnings("resource") FileInputStream fileInputStream = null;
try {
try {
fileInputStream = new FileInputStream(file);
setValue(jsonSerializer.readObject(fileInputStream));
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
} catch (Exception exception) {
Prompt.prompt(exception.getMessage(), window);
}
window.setTitle(WINDOW_TITLE + " - " + file.getName());
dropAction = DropAction.COPY;
} else {
Prompt.prompt("Multiple files not supported.", window);
}
} catch (IOException exception) {
Prompt.prompt(exception.getMessage(), window);
}
return dropAction;
}
Aggregations