private static final Object PROPERTY_KIND = "Kind";\r
private static final Object PROPERTY_HIRED = "Hired";\r
\r
+ // "global" components\r
Table source;\r
Table saved;\r
-\r
Button saveSelected;\r
Button hireSelected;\r
Button deleteSelected;\r
\r
public TableExample() {\r
-\r
+ // main layout\r
OrderedLayout main = new OrderedLayout();\r
main.setMargin(true);\r
setCompositionRoot(main);\r
\r
+ // "source" table with bells & whistlesenabled\r
source = new Table("Also try the row context-menu");\r
source.setPageLength(7);\r
source.setWidth(550);\r
source.addActionHandler(this);\r
main.addComponent(source);\r
\r
+ // x-selected button row\r
OrderedLayout horiz = new OrderedLayout(\r
OrderedLayout.ORIENTATION_HORIZONTAL);\r
horiz.setMargin(false, false, true, false);\r
deleteSelected.addListener(this);\r
horiz.addComponent(deleteSelected);\r
\r
+ // "saved" table, minimalistic\r
saved = new Table();\r
saved.setPageLength(5);\r
saved.setWidth(550);\r
main.addComponent(saved);\r
}\r
\r
+ // set up the properties (columns)\r
private void initProperties(Table table) {\r
table.addContainerProperty(PROPERTY_SPECIES, String.class, "");\r
table.addContainerProperty(PROPERTY_TYPE, String.class, "");\r
Boolean.FALSE);\r
}\r
\r
+ // fill the table with some random data\r
private void fillTable(Table table) {\r
initProperties(table);\r
\r
\r
}\r
\r
+ // Called for each item (row), returns valid actions for that item\r
public Action[] getActions(Object target, Object sender) {\r
if (sender == source) {\r
Item item = source.getItem(target);\r
+ // save, delete, and hire if not already hired\r
if (item != null\r
&& item.getItemProperty(PROPERTY_HIRED).getValue() == Boolean.FALSE) {\r
return ACTIONS_HIRE;\r
return ACTIONS_NOHIRE;\r
}\r
} else {\r
+ // "saved" table only has one action\r
return new Action[] { ACTION_DELETE };\r
}\r
}\r
\r
+ // called when an action is invoked on an item (row)\r
public void handleAction(Action action, Object sender, Object target) {\r
if (sender == source) {\r
Item item = source.getItem(target);\r
if (action == ACTION_HIRE) {\r
+ // set HIRED property to true\r
item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);\r
source.requestRepaint();\r
if (saved.containsId(target)) {\r
item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);\r
saved.requestRepaint();\r
}\r
-\r
getWindow().showNotification("Hired", "" + item);\r
+\r
} else if (action == ACTION_SAVE) {\r
if (saved.containsId(target)) {\r
+ // let's not save twice\r
getWindow().showNotification("Already saved", "" + item);\r
return;\r
}\r
+ // "manual" copy of the item properties we want\r
Item added = saved.addItem(target);\r
Property p = added.getItemProperty(PROPERTY_SPECIES);\r
p.setValue(item.getItemProperty(PROPERTY_SPECIES).getValue());\r
getWindow().showNotification("Deleted ", "" + item);\r
source.removeItem(target);\r
}\r
+\r
} else {\r
// sender==saved\r
if (action == ACTION_DELETE) {\r
public void buttonClick(ClickEvent event) {\r
Button b = event.getButton();\r
if (b == saveSelected) {\r
+ // loop each selected and copy to "saved" table\r
Set selected = (Set) source.getValue();\r
- if (selected != null) {\r
- int s = 0;\r
- for (Iterator it = selected.iterator(); it.hasNext();) {\r
- Object id = it.next();\r
- if (!saved.containsId(id)) {\r
- Item item = source.getItem(id);\r
- Item added = saved.addItem(id);\r
- Property p = added.getItemProperty(PROPERTY_SPECIES);\r
- p.setValue(item.getItemProperty(PROPERTY_SPECIES)\r
- .getValue());\r
- p = added.getItemProperty(PROPERTY_TYPE);\r
- p.setValue(item.getItemProperty(PROPERTY_TYPE)\r
- .getValue());\r
- p = added.getItemProperty(PROPERTY_KIND);\r
- p.setValue(item.getItemProperty(PROPERTY_KIND)\r
- .getValue());\r
- p = added.getItemProperty(PROPERTY_HIRED);\r
- p.setValue(item.getItemProperty(PROPERTY_HIRED)\r
- .getValue());\r
- s++;\r
- }\r
+ int s = 0;\r
+ for (Iterator it = selected.iterator(); it.hasNext();) {\r
+ Object id = it.next();\r
+ if (!saved.containsId(id)) {\r
+ Item item = source.getItem(id);\r
+ Item added = saved.addItem(id);\r
+ // "manual" copy of the properties we want\r
+ Property p = added.getItemProperty(PROPERTY_SPECIES);\r
+ p.setValue(item.getItemProperty(PROPERTY_SPECIES)\r
+ .getValue());\r
+ p = added.getItemProperty(PROPERTY_TYPE);\r
+ p.setValue(item.getItemProperty(PROPERTY_TYPE).getValue());\r
+ p = added.getItemProperty(PROPERTY_KIND);\r
+ p.setValue(item.getItemProperty(PROPERTY_KIND).getValue());\r
+ p = added.getItemProperty(PROPERTY_HIRED);\r
+ p.setValue(item.getItemProperty(PROPERTY_HIRED).getValue());\r
+ s++;\r
}\r
- getWindow().showNotification("Saved " + s);\r
- saved.requestRepaint();\r
}\r
+ getWindow().showNotification("Saved " + s);\r
+ saved.requestRepaint();\r
+\r
} else if (b == hireSelected) {\r
+ // loop each selected and set property HIRED to true\r
int s = 0;\r
Set selected = (Set) source.getValue();\r
for (Iterator it = selected.iterator(); it.hasNext();) {\r
s++;\r
}\r
if (saved.containsId(id)) {\r
+ // also update "saved" table\r
item = saved.getItem(id);\r
item.getItemProperty(PROPERTY_HIRED).setValue(Boolean.TRUE);\r
saved.requestRepaint();\r
getWindow().showNotification("Hired " + s);\r
\r
} else {\r
- // delete selected\r
+ // loop trough selected and delete\r
int s = 0;\r
Set selected = (Set) source.getValue();\r
for (Iterator it = selected.iterator(); it.hasNext();) {\r