use of org.apache.pivot.wtk.Span in project pivot by apache.
the class TextPaneDemo method applyStyle.
private void applyStyle(Document document, Span selectionSpan, StyleApplicator styleApplicator) {
// I can't apply the styles while iterating over the tree, because I
// need to update the tree.
// So first collect a list of all the nodes in the tree.
List<Node> nodeList = new ArrayList<>();
collectNodes(document, nodeList);
final int selectionStart = textPane.getSelectionStart();
final int selectionLength = textPane.getSelectionLength();
for (Node node : nodeList) {
if (node instanceof TextSpan) {
TextSpan span = (TextSpan) node;
int documentOffset = node.getDocumentOffset();
int characterCount = node.getCharacterCount();
Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
if (selectionSpan.intersects(textSpan)) {
applyStyleToSpanNode(selectionSpan, styleApplicator, span, characterCount, textSpan);
}
}
if (node instanceof org.apache.pivot.wtk.text.TextNode) {
org.apache.pivot.wtk.text.TextNode textNode = (org.apache.pivot.wtk.text.TextNode) node;
int documentOffset = node.getDocumentOffset();
int characterCount = node.getCharacterCount();
Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
if (selectionSpan.intersects(textSpan)) {
applyStyleToTextNode(selectionSpan, styleApplicator, textNode, characterCount, textSpan);
}
}
}
// maintain the selected range
textPane.setSelection(selectionStart, selectionLength);
}
use of org.apache.pivot.wtk.Span in project pivot by apache.
the class TextPaneDemo method applyStyleToSelection.
private void applyStyleToSelection(StyleApplicator styleApplicator) {
Span span = textPane.getSelection();
if (span == null) {
return;
}
applyStyle(textPane.getDocument(), span, styleApplicator);
}
use of org.apache.pivot.wtk.Span 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.Span 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.Span in project pivot by apache.
the class TerraTableViewSkin method layout.
@Override
public void layout() {
columnWidths = getColumnWidths((TableView) getComponent(), getWidth());
TableView tableView = (TableView) getComponent();
TableView.ColumnSequence columns = tableView.getColumns();
if (variableRowHeight) {
@SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
int n = tableData.getLength();
rowBoundaries = new ArrayList<>(n);
int rowY = 0;
for (int i = 0; i < n; i++) {
Object rowData = tableData.get(i);
int rowHeight = 0;
for (int columnIndex = 0, columnCount = columns.getLength(); columnIndex < columnCount; columnIndex++) {
TableView.Column column = columns.get(columnIndex);
TableView.CellRenderer cellRenderer = column.getCellRenderer();
int columnWidth = columnWidths.get(columnIndex).intValue();
cellRenderer.render(rowData, i, columnIndex, tableView, column.getName(), false, false, false);
rowHeight = Math.max(rowHeight, cellRenderer.getPreferredHeight(columnWidth));
}
rowY += rowHeight;
rowBoundaries.add(Integer.valueOf(rowY));
rowY++;
}
} else {
fixedRowHeight = calculateFixedRowHeight(tableView);
}
if (validateSelection) {
// Ensure that the selection is visible
Sequence<Span> selectedRanges = tableView.getSelectedRanges();
if (selectedRanges.getLength() > 0) {
int rangeStart = selectedRanges.get(0).start;
int rangeEnd = selectedRanges.get(selectedRanges.getLength() - 1).end;
Bounds selectionBounds = getRowBounds(rangeStart);
selectionBounds = selectionBounds.union(getRowBounds(rangeEnd));
Bounds visibleSelectionBounds = tableView.getVisibleArea(selectionBounds);
if (visibleSelectionBounds != null && visibleSelectionBounds.height < selectionBounds.height) {
tableView.scrollAreaToVisible(selectionBounds);
}
}
}
validateSelection = false;
}
Aggregations