use of org.apache.pivot.wtk.ListView 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.ListView in project pivot by apache.
the class TerraListViewSkin method getPreferredHeight.
@Override
public int getPreferredHeight(int width) {
int preferredHeight = 0;
ListView listView = (ListView) getComponent();
@SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
if (variableItemHeight) {
int clientWidth = width;
if (listView.getCheckmarksEnabled()) {
clientWidth = Math.max(clientWidth - (CHECKBOX.getWidth() + checkboxPadding.getWidth()), 0);
}
int index = 0;
for (Object item : listData) {
itemRenderer.render(item, index++, listView, false, Button.State.UNSELECTED, false, false);
preferredHeight += itemRenderer.getPreferredHeight(clientWidth);
}
} else {
itemRenderer.render(null, -1, listView, false, Button.State.UNSELECTED, false, false);
int fixedItemHeightLocal = itemRenderer.getPreferredHeight(-1);
if (listView.getCheckmarksEnabled()) {
fixedItemHeightLocal = Math.max(CHECKBOX.getHeight() + checkboxPadding.getHeight(), fixedItemHeightLocal);
}
preferredHeight = listData.getLength() * fixedItemHeightLocal;
}
return preferredHeight;
}
use of org.apache.pivot.wtk.ListView in project pivot by apache.
the class TerraListViewSkin method keyTyped.
/**
* Select the next enabled list item where the first character of the
* rendered text matches the typed key (case insensitive).
*/
@Override
public boolean keyTyped(Component component, char character) {
boolean consumed = super.keyTyped(component, character);
ListView listView = (ListView) getComponent();
List<?> listData = listView.getListData();
ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
char characterUpper = Character.toUpperCase(character);
for (int i = listView.getLastSelectedIndex() + 1, n = listData.getLength(); i < n; i++) {
if (!listView.isItemDisabled(i)) {
String string = itemRenderer.toString(listData.get(i));
if (string != null && string.length() > 0) {
char first = Character.toUpperCase(string.charAt(0));
if (first == characterUpper) {
listView.setSelectedIndex(i);
consumed = true;
break;
}
}
}
}
if (!consumed && wrapSelectNext) {
for (int i = 0, n = listData.getLength(); i < n; i++) {
if (!listView.isItemDisabled(i)) {
String string = itemRenderer.toString(listData.get(i));
if (string != null && string.length() > 0) {
char first = Character.toUpperCase(string.charAt(0));
if (first == characterUpper) {
listView.setSelectedIndex(i);
consumed = true;
break;
}
}
}
}
}
return consumed;
}
use of org.apache.pivot.wtk.ListView in project pivot by apache.
the class TerraListViewSkin method mouseClick.
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
boolean consumed = super.mouseClick(component, button, x, y, count);
ListView listView = (ListView) getComponent();
int itemIndex = getItemAt(y);
if (itemIndex != -1 && !listView.isItemDisabled(itemIndex)) {
if (listView.getCheckmarksEnabled() && !listView.isCheckmarkDisabled(itemIndex) && getCheckboxBounds(itemIndex).contains(x, y)) {
if (listView.getAllowTriStateCheckmarks()) {
Button.State currentState = listView.getItemCheckmarkState(itemIndex);
Button.State nextState = currentState;
switch(currentState) {
case UNSELECTED:
if (listView.getCheckmarksMixedAsChecked()) {
nextState = Button.State.SELECTED;
} else {
nextState = Button.State.MIXED;
}
break;
case MIXED:
nextState = Button.State.SELECTED;
break;
case SELECTED:
nextState = Button.State.UNSELECTED;
break;
}
listView.setItemCheckmarkState(itemIndex, nextState);
} else {
listView.setItemChecked(itemIndex, !listView.isItemChecked(itemIndex));
}
} else {
if (selectIndex != -1 && count == 1 && button == Mouse.Button.LEFT) {
ListView.ItemEditor itemEditor = listView.getItemEditor();
if (itemEditor != null) {
if (itemEditor.isEditing()) {
itemEditor.endEdit(true);
}
itemEditor.beginEdit(listView, selectIndex);
}
}
}
}
selectIndex = -1;
return consumed;
}
use of org.apache.pivot.wtk.ListView in project pivot by apache.
the class TerraListViewSkin method getPreferredWidth.
@Override
public int getPreferredWidth(int height) {
int preferredWidth = 0;
ListView listView = (ListView) getComponent();
@SuppressWarnings("unchecked") List<Object> listData = (List<Object>) listView.getListData();
ListView.ItemRenderer itemRenderer = listView.getItemRenderer();
int index = 0;
for (Object item : listData) {
itemRenderer.render(item, index++, listView, false, Button.State.UNSELECTED, false, false);
preferredWidth = Math.max(preferredWidth, itemRenderer.getPreferredWidth(-1));
}
if (listView.getCheckmarksEnabled()) {
preferredWidth += CHECKBOX.getWidth() + checkboxPadding.getWidth();
}
return preferredWidth;
}
Aggregations