use of org.apache.pivot.wtk.Button in project pivot by apache.
the class TextPaneDemo method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
System.out.println("startup(...)");
System.out.println("\n" + "In this test application as a sample for setting the display scale on startup, use startup argument \"--scale=n\" property; \n" + "for instance, using \"--scale=2.0\" will set double scale on the whole application.\n" + "\n" + "Anyway, using Ctrl-Shift-MouseWheel will scale the display up and down as well, for the user of your application.\n");
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(TextPaneDemo.class, "text_pane_demo.bxml");
bxmlSerializer.bind(this, TextPaneDemo.class);
window.setTitle("Apache Pivot Rich Text Editor Demo");
// make the text on the "bold" button bold
Font boldButtonFont = boldButton.getStyles().getFont(Style.font);
boldButton.getStyles().put(Style.font, boldButtonFont.deriveFont(Font.BOLD));
// make the text on the "italic" button italic
Font italicButtonFont = italicButton.getStyles().getFont(Style.font);
italicButton.getStyles().put(Style.font, italicButtonFont.deriveFont(Font.ITALIC));
fontFamilyListButton.setListData(new ArrayList<>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()));
fontSizeListButton.setSelectedItem(fontFamilyListButton.getListData().get(0));
fontFamilyListButton.setItemRenderer(new ListViewItemRenderer() {
@Override
public void render(Object item, int index, ListView listView, boolean selected, Button.State state, boolean highlighted, boolean disabled) {
super.render(item, index, listView, selected, state, highlighted, disabled);
if (item != null) {
String fontFamilyName = (String) item;
label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
}
}
});
fontFamilyListButton.setDataRenderer(new ListButtonDataRenderer() {
@Override
public void render(Object data, Button button, boolean highlight) {
super.render(data, button, highlight);
if (data != null) {
String fontFamilyName = (String) data;
label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12"));
}
}
});
fontSizeListButton.setListData(new NumericSpinnerData(12, 30, 1));
fontSizeListButton.setSelectedItem(12);
openFileButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
fileBrowserSheet.setMode(FileBrowserSheet.Mode.OPEN);
fileBrowserSheet.open(window, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
loadedFile = fileBrowserSheet.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(loadedFile));
PlainTextSerializer serializer = new PlainTextSerializer();
textPane.setDocument(serializer.readObject(reader));
reader.close();
window.setTitle(loadedFile.getCanonicalPath());
} catch (IOException ex) {
ex.printStackTrace();
Alert.alert(ex.getMessage(), window);
}
}
}
});
}
});
saveFileButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
if (loadedFile != null) {
fileBrowserSheet.setSelectedFile(loadedFile);
}
fileBrowserSheet.setMode(FileBrowserSheet.Mode.SAVE_AS);
fileBrowserSheet.open(window, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
File selectedFile = fileBrowserSheet.getSelectedFile();
try {
FileWriter writer = new FileWriter(selectedFile);
PlainTextSerializer serializer = new PlainTextSerializer();
serializer.writeObject(textPane.getDocument(), writer);
writer.close();
loadedFile = selectedFile;
window.setTitle(loadedFile.getCanonicalPath());
} catch (IOException ex) {
ex.printStackTrace();
Alert.alert(ex.getMessage(), window);
}
}
}
});
}
});
boldButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
if (span.getFont() != null) {
Font font = span.getFont();
if (font.getStyle() == Font.PLAIN) {
font = font.deriveFont(Font.BOLD);
} else if (font.getStyle() == Font.BOLD) {
font = font.deriveFont(Font.PLAIN);
} else {
// the font is BOLD+ITALIC
font = font.deriveFont(Font.ITALIC);
}
span.setFont(font);
} else {
span.setFont("Arial BOLD 12");
}
}
});
requestTextPaneFocus();
}
});
italicButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
if (span.getFont() != null) {
Font font = span.getFont();
if (font.getStyle() == Font.PLAIN) {
font = font.deriveFont(Font.ITALIC);
} else if (font.getStyle() == Font.ITALIC) {
font = font.deriveFont(Font.PLAIN);
} else {
// the font is BOLD+ITALIC
font = font.deriveFont(Font.BOLD);
}
span.setFont(font);
} else {
span.setFont("Arial ITALIC 12");
}
}
});
requestTextPaneFocus();
}
});
underlineButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
span.setUnderline(!span.isUnderline());
}
});
requestTextPaneFocus();
}
});
strikethroughButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
span.setStrikethrough(!span.isStrikethrough());
}
});
requestTextPaneFocus();
}
});
foregroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {
@Override
public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
span.setForegroundColor(foregroundColorChooserButton.getSelectedColor());
}
});
requestTextPaneFocus();
}
});
backgroundColorChooserButton.getColorChooserButtonSelectionListeners().add(new ColorChooserButtonSelectionListener() {
@Override
public void selectedColorChanged(ColorChooserButton colorChooserButton, Color previousSelectedColor) {
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
span.setBackgroundColor(backgroundColorChooserButton.getSelectedColor());
}
});
requestTextPaneFocus();
}
});
ListButtonSelectionListener fontButtonPressListener = new ListButtonSelectionListener() {
@Override
public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
int selectedFontSize = ((Integer) fontSizeListButton.getSelectedItem()).intValue();
String selectedFontFamily = (String) fontFamilyListButton.getSelectedItem();
final Font derivedFont = Font.decode(selectedFontFamily + " " + selectedFontSize);
applyStyleToSelection(new StyleApplicator() {
@Override
public void apply(TextSpan span) {
span.setFont(derivedFont);
}
});
requestTextPaneFocus();
}
};
fontFamilyListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
fontSizeListButton.getListButtonSelectionListeners().add(fontButtonPressListener);
wrapTextCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
textPane.getStyles().put(Style.wrapText, wrapTextCheckbox.isSelected());
requestTextPaneFocus();
}
});
alignLeftButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyAlignmentStyle(HorizontalAlignment.LEFT);
requestTextPaneFocus();
}
});
alignCentreButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyAlignmentStyle(HorizontalAlignment.CENTER);
requestTextPaneFocus();
}
});
alignRightButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
applyAlignmentStyle(HorizontalAlignment.RIGHT);
requestTextPaneFocus();
}
});
String scaleProperty = properties.get("scale");
if (scaleProperty != null && !scaleProperty.isEmpty()) {
try {
double scaleFactor = Double.parseDouble(scaleProperty);
System.out.println("Got scaling factor \"" + scaleProperty + "\" from command line arguments, now applying to display");
display.getDisplayHost().setScale(scaleFactor);
} catch (NumberFormatException nfe) {
System.err.println("(NumberFormatException: " + nfe.getMessage());
}
}
window.open(display);
requestTextPaneFocus();
}
use of org.apache.pivot.wtk.Button 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.wtk.Button 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.wtk.Button in project pivot by apache.
the class CheckedListViewTest method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
BXMLSerializer serializer = new BXMLSerializer();
mainWindow = (Window) serializer.readObject(CheckedListViewTest.class, "checked_list_view_test.bxml");
serializer.bind(this);
allowMixedStateCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
listView.setAllowTriStateCheckmarks(button.isSelected());
// Not sure why, but changing this setting clears all the checks but doesn't
// trigger the item state listener (it's documented, but ...)
selectedItemsLabel.setText("");
}
});
showMixedAsSelectedCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
listView.setCheckmarksMixedAsChecked(button.isSelected());
}
});
listView.getComponentKeyListeners().add(new ComponentKeyListener() {
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
if (keyCode == Keyboard.KeyCode.DELETE) {
@SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
Sequence<Span> selectedRanges = listView.getSelectedRanges();
for (int i = selectedRanges.getLength() - 1; i >= 0; i--) {
Span selectedRange = selectedRanges.get(i);
listData.remove(selectedRange.start, selectedRange.end - selectedRange.start + 1);
}
}
return false;
}
});
listView.getListViewItemStateListeners().add(new ListViewItemStateListener() {
private void displayCheckedItems(ListView listView) {
List<?> listData = listView.getListData();
StringBuffer buf = new StringBuffer();
for (Integer i : listView.getCheckedIndexes()) {
if (buf.length() > 0) {
buf.append(",");
}
Object item = listData.get(i);
buf.append(item.toString());
}
selectedItemsLabel.setText(buf.toString());
}
@Override
public void itemCheckedChanged(ListView listView, int index) {
displayCheckedItems(listView);
}
@Override
public void itemCheckedStateChanged(ListView listView, int index) {
displayCheckedItems(listView);
}
});
listView.setItemChecked(0, true);
listView.setItemChecked(2, true);
mainWindow.open(display);
}
use of org.apache.pivot.wtk.Button in project pivot by apache.
the class EditGreetingSheet method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resourcesArgument) {
this.resources = resourcesArgument;
cancelButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
close(false);
}
});
okButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
close(true);
}
});
}
Aggregations