class DragDropTableTreeExample(HorizontalLayout): """Demonstrate moving data back and forth between a table and a tree using drag and drop. The tree and the table use different data structures: The category is a separate node in the tree and each item just has a String, whereas the table contains items with both a name and a category. Data conversions between these representations are made during drop processing. """ def __init__(self): super(DragDropTableTreeExample, self).__init__() self.setSpacing(True) # First create the components to be able to refer to them as allowed # drag sources self._tree = Tree('Drag from tree to table') self._table = Table('Drag from table to tree') self._table.setWidth('100%') # Populate the tree and set up drag & drop self.initializeTree(SourceIs(self._table)) # Populate the table and set up drag & drop self.initializeTable(SourceIs(self._tree)) # Add components self.addComponent(self._tree) self.addComponent(self._table) def initializeTree(self, acceptCriterion): self._tree.setContainerDataSource(ExampleUtil.getHardwareContainer()) self._tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME) # Expand all nodes for idd in self._tree.rootItemIds(): self._tree.expandItemsRecursively(idd) self._tree.setDragMode(TreeDragMode.NODE) self._tree.setDropHandler(TreeDropHandler(self)) def initializeTable(self, acceptCriterion): tableContainer = BeanItemContainer(Hardware) tableContainer.addItem(Hardware('Dell OptiPlex 380', 'Desktops')) tableContainer.addItem(Hardware('Benq T900HD', 'Monitors')) tableContainer.addItem(Hardware('Lenovo ThinkPad T500', 'Laptops')) self._table.setContainerDataSource(tableContainer) self._table.setVisibleColumns(['category', 'name']) # Handle drop in table: move hardware item or subtree to the table self._table.setDragMode(TableDragMode.ROW) self._table.setDropHandler(TableDropHandler(self)) @classmethod def getTreeNodeName(cls, source, sourceId): return source.getItem(sourceId).getItemProperty( ExampleUtil.hw_PROPERTY_NAME).getValue()
class SimpleAddressBook(Application): _fields = [ 'First Name', 'Last Name', 'Company', 'Mobile Phone', 'Work Phone', 'Home Phone', 'Work Email', 'Home Email', 'Street', 'Zip', 'City', 'State', 'Country' ] _visibleCols = ['Last Name', 'First Name', 'Company'] def __init__(self): super(SimpleAddressBook, self).__init__() self._contactList = Table() self._contactEditor = Form() self._bottomLeftCorner = HorizontalLayout() self._contactRemovalButton = None self._addressBookData = self.createDummyData() def init(self): self.initLayout() self.initContactAddRemoveButtons() self.initAddressList() self.initFilteringControls() def initLayout(self): splitPanel = HorizontalSplitPanel() self.setMainWindow(Window('Address Book', splitPanel)) left = VerticalLayout() left.setSizeFull() left.addComponent(self._contactList) self._contactList.setSizeFull() left.setExpandRatio(self._contactList, 1) splitPanel.addComponent(left) splitPanel.addComponent(self._contactEditor) self._contactEditor.setSizeFull() self._contactEditor.getLayout().setMargin(True) self._contactEditor.setImmediate(True) self._bottomLeftCorner.setWidth('100%') left.addComponent(self._bottomLeftCorner) def initContactAddRemoveButtons(self): # New item button newItem = Button('+') newItem.addCallback(onNew, ClickEvent, self) self._bottomLeftCorner.addComponent(newItem) # Remove item button self._contactRemovalButton = Button('-') self._contactRemovalButton.addCallback(onRemove, ClickEvent, self) self._contactRemovalButton.setVisible(False) self._bottomLeftCorner.addComponent(self._contactRemovalButton) def initAddressList(self): self._contactList.setContainerDataSource(self._addressBookData) self._contactList.setVisibleColumns(self._visibleCols) self._contactList.setSelectable(True) self._contactList.setImmediate(True) self._contactList.addCallback(onContactChange, field.ValueChangeEvent, self) return self._visibleCols def initFilteringControls(self): for pn in self._visibleCols: sf = TextField() self._bottomLeftCorner.addComponent(sf) sf.setWidth("100%") sf.setValue(pn) sf.setImmediate(True) self._bottomLeftCorner.setExpandRatio(sf, 1) sf.addCallback(onFilterChange, property.ValueChangeEvent, pn, sf, self) @classmethod def createDummyData(cls): fnames = [ 'Peter', 'Alice', 'Joshua', 'Mike', 'Olivia', 'Nina', 'Alex', 'Rita', 'Dan', 'Umberto', 'Henrik', 'Rene', 'Lisa', 'Marge' ] lnames = [ 'Smith', 'Gordon', 'Simpson', 'Brown', 'Clavel', 'Simons', 'Verne', 'Scott', 'Allison', 'Gates', 'Rowling', 'Barks', 'Ross', 'Schneider', 'Tate' ] ic = IndexedContainer() for p in cls._fields: ic.addContainerProperty(p, str, '') for _ in range(1000): idd = ic.addItem() fname = fnames[int(len(fnames) * random())] ic.getContainerProperty(idd, 'First Name').setValue(fname) lname = lnames[int(len(lnames) * random())] ic.getContainerProperty(idd, 'Last Name').setValue(lname) return ic
class DragDropTableTreeExample(HorizontalLayout): """Demonstrate moving data back and forth between a table and a tree using drag and drop. The tree and the table use different data structures: The category is a separate node in the tree and each item just has a String, whereas the table contains items with both a name and a category. Data conversions between these representations are made during drop processing. """ def __init__(self): super(DragDropTableTreeExample, self).__init__() self.setSpacing(True) # First create the components to be able to refer to them as allowed # drag sources self._tree = Tree('Drag from tree to table') self._table = Table('Drag from table to tree') self._table.setWidth('100%') # Populate the tree and set up drag & drop self.initializeTree(SourceIs(self._table)) # Populate the table and set up drag & drop self.initializeTable(SourceIs(self._tree)) # Add components self.addComponent(self._tree) self.addComponent(self._table) def initializeTree(self, acceptCriterion): self._tree.setContainerDataSource(ExampleUtil.getHardwareContainer()) self._tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME) # Expand all nodes for idd in self._tree.rootItemIds(): self._tree.expandItemsRecursively(idd) self._tree.setDragMode(TreeDragMode.NODE) self._tree.setDropHandler( TreeDropHandler(self) ) def initializeTable(self, acceptCriterion): tableContainer = BeanItemContainer(Hardware) tableContainer.addItem(Hardware('Dell OptiPlex 380', 'Desktops')) tableContainer.addItem(Hardware('Benq T900HD', 'Monitors')) tableContainer.addItem(Hardware('Lenovo ThinkPad T500', 'Laptops')) self._table.setContainerDataSource(tableContainer) self._table.setVisibleColumns(['category', 'name']) # Handle drop in table: move hardware item or subtree to the table self._table.setDragMode(TableDragMode.ROW) self._table.setDropHandler( TableDropHandler(self) ) @classmethod def getTreeNodeName(cls, source, sourceId): return source.getItem(sourceId).getItemProperty( ExampleUtil.hw_PROPERTY_NAME).getValue()
class SimpleAddressBook(Application): _fields = ['First Name', 'Last Name', 'Company', 'Mobile Phone', 'Work Phone', 'Home Phone', 'Work Email', 'Home Email', 'Street', 'Zip', 'City', 'State', 'Country'] _visibleCols = ['Last Name', 'First Name', 'Company'] def __init__(self): super(SimpleAddressBook, self).__init__() self._contactList = Table() self._contactEditor = Form() self._bottomLeftCorner = HorizontalLayout() self._contactRemovalButton = None self._addressBookData = self.createDummyData() def init(self): self.initLayout() self.initContactAddRemoveButtons() self.initAddressList() self.initFilteringControls() def initLayout(self): splitPanel = HorizontalSplitPanel() self.setMainWindow(Window('Address Book', splitPanel)) left = VerticalLayout() left.setSizeFull() left.addComponent(self._contactList) self._contactList.setSizeFull() left.setExpandRatio(self._contactList, 1) splitPanel.addComponent(left) splitPanel.addComponent(self._contactEditor) self._contactEditor.setSizeFull() self._contactEditor.getLayout().setMargin(True) self._contactEditor.setImmediate(True) self._bottomLeftCorner.setWidth('100%') left.addComponent(self._bottomLeftCorner) def initContactAddRemoveButtons(self): # New item button newItem = Button('+') newItem.addCallback(onNew, ClickEvent, self) self._bottomLeftCorner.addComponent(newItem) # Remove item button self._contactRemovalButton = Button('-') self._contactRemovalButton.addCallback(onRemove, ClickEvent, self) self._contactRemovalButton.setVisible(False) self._bottomLeftCorner.addComponent(self._contactRemovalButton) def initAddressList(self): self._contactList.setContainerDataSource(self._addressBookData) self._contactList.setVisibleColumns(self._visibleCols) self._contactList.setSelectable(True) self._contactList.setImmediate(True) self._contactList.addCallback(onContactChange, field.ValueChangeEvent, self) return self._visibleCols def initFilteringControls(self): for pn in self._visibleCols: sf = TextField() self._bottomLeftCorner.addComponent(sf) sf.setWidth("100%") sf.setValue(pn) sf.setImmediate(True) self._bottomLeftCorner.setExpandRatio(sf, 1) sf.addCallback(onFilterChange, property.ValueChangeEvent, pn, sf, self) @classmethod def createDummyData(cls): fnames = ['Peter', 'Alice', 'Joshua', 'Mike', 'Olivia', 'Nina', 'Alex', 'Rita', 'Dan', 'Umberto', 'Henrik', 'Rene', 'Lisa', 'Marge'] lnames = ['Smith', 'Gordon', 'Simpson', 'Brown', 'Clavel', 'Simons', 'Verne', 'Scott', 'Allison', 'Gates', 'Rowling', 'Barks', 'Ross', 'Schneider', 'Tate'] ic = IndexedContainer() for p in cls._fields: ic.addContainerProperty(p, str, '') for _ in range(1000): idd = ic.addItem() fname = fnames[int( len(fnames) * random() )] ic.getContainerProperty(idd, 'First Name').setValue(fname) lname = lnames[int( len(lnames) * random() )] ic.getContainerProperty(idd, 'Last Name').setValue(lname) return ic