use of org.apache.pivot.wtk.SheetCloseListener in project pivot by apache.
the class FileBrowsing method initialize.
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
openSheetButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Button selection = fileBrowserSheetModeGroup.getSelection();
String mode = (String) selection.getUserData().get("mode");
FileBrowserSheet.Mode fileBrowserSheetMode = FileBrowserSheet.Mode.valueOf(mode.toUpperCase());
final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
if (fileBrowserSheetMode == FileBrowserSheet.Mode.SAVE_AS) {
fileBrowserSheet.setSelectedFile(new File(fileBrowserSheet.getRootDirectory(), "New File"));
}
fileBrowserSheet.setMode(fileBrowserSheetMode);
fileBrowserSheet.open(FileBrowsing.this, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
Sequence<File> selectedFiles = fileBrowserSheet.getSelectedFiles();
ListView listView = new ListView();
listView.setListData(new ArrayList<>(selectedFiles));
listView.setSelectMode(ListView.SelectMode.NONE);
listView.getStyles().put(Style.backgroundColor, null);
Alert.alert(MessageType.INFO, "You selected:", listView, FileBrowsing.this);
} else {
Alert.alert(MessageType.INFO, "You didn't select anything.", FileBrowsing.this);
}
}
});
}
});
}
use of org.apache.pivot.wtk.SheetCloseListener in project pivot by apache.
the class Pivot832 method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(getClass().getResource("pivot_832.bxml"));
bxmlSerializer.bind(this, Pivot832.class);
selectFolderButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();
// to
fileBrowserSheet.setMode(FileBrowserSheet.Mode.SAVE_TO);
// be
// able
// to
// select
// a
// folder
fileBrowserSheet.open(window, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
File loadedFile = fileBrowserSheet.getSelectedFile();
try {
selectedFolder = loadedFile.getCanonicalPath();
System.out.println("Selected folder '" + selectedFolder + "'");
// multiple tests ...
System.out.println("Verify: Root folder (display from getName) was set to '" + fileBrowserSheet.getRootDirectory().getName() + "'");
System.out.println("Verify: Root folder (display from getCanonicalPath) was set to '" + fileBrowserSheet.getRootDirectory().getCanonicalPath() + "'");
System.out.println("Verify: Root folder (display from getCanonicalFile) was set to '" + fileBrowserSheet.getRootDirectory().getCanonicalFile() + "'");
openFileButton.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
openFileButton.setEnabled(false);
}
window.setTitle("Selected folder: " + selectedFolder);
}
}
});
}
});
openFileButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
System.out.println("Now opening a BrowserSheet starting from the previous selected folder: \"" + selectedFolder + "\"");
final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet(FileBrowserSheet.Mode.OPEN, selectedFolder);
try {
// multiple tests ...
System.out.println("Verify: Root folder (display from getName) was set to '" + fileBrowserSheet.getRootDirectory().getName() + "'");
System.out.println("Verify: Root folder (display from getCanonicalPath) was set to '" + fileBrowserSheet.getRootDirectory().getCanonicalPath() + "'");
System.out.println("Verify: Root folder (display from getCanonicalFile) was set to '" + fileBrowserSheet.getRootDirectory().getCanonicalFile() + "'");
} catch (IOException e) {
e.printStackTrace();
}
fileBrowserSheet.open(window, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
// empty block
}
});
}
});
window.open(display);
}
use of org.apache.pivot.wtk.SheetCloseListener 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.SheetCloseListener in project pivot by apache.
the class ExpensesWindow method addExpense.
private void addExpense() {
expenseSheet.clear();
expenseSheet.open(this, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
// Get the expense data from the sheet
final HashMap<String, Object> expense = new HashMap<>();
expenseSheet.store(expense);
// POST expense to server and then add to table
Expenses expensesApplicationLocal = getExpensesApplication();
PostQuery addExpenseQuery = new PostQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses", expensesApplicationLocal.isSecure());
addExpenseQuery.setValue(expense);
activityIndicatorBoxPane.setVisible(true);
activityIndicator.setActive(true);
addExpenseQuery.execute(new TaskAdapter<>(new TaskListener<URL>() {
@Override
public void taskExecuted(Task<URL> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
URL location = task.getResult();
String file = location.getFile();
int id = Integer.parseInt(file.substring(file.lastIndexOf('/') + 1));
expense.put("id", id);
@SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
expenses.add(expense);
}
@Override
public void executeFailed(Task<URL> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
}
}));
}
}
});
}
use of org.apache.pivot.wtk.SheetCloseListener in project pivot by apache.
the class ExpensesWindow method updateSelectedExpense.
private void updateSelectedExpense() {
Object expense = expenseTableView.getSelectedRow();
final int id = JSON.getInt(expense, "id");
expenseSheet.load(expense);
expenseSheet.open(this, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (sheet.getResult()) {
// Get the expense data from the sheet
final HashMap<String, Object> expenseLocal = new HashMap<>();
expenseSheet.store(expenseLocal);
// PUT expense to server and then update table
Expenses expensesApplicationLocal = getExpensesApplication();
PutQuery updateExpenseQuery = new PutQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses/" + JSON.get(expenseLocal, "id"), expensesApplicationLocal.isSecure());
updateExpenseQuery.setValue(expenseLocal);
activityIndicatorBoxPane.setVisible(true);
activityIndicator.setActive(true);
updateExpenseQuery.execute(new TaskAdapter<>(new TaskListener<Boolean>() {
@Override
public void taskExecuted(Task<Boolean> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
// Find matching row and update
@SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
for (int i = 0, n = expenses.getLength(); i < n; i++) {
if (JSON.get(expenses.get(i), "id").equals(id)) {
expenses.update(i, expenseLocal);
break;
}
}
}
@Override
public void executeFailed(Task<Boolean> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
}
}));
}
}
});
}
Aggregations