@Override protected void setup() { HorizontalLayout h = new HorizontalLayout(); addComponent(h); Table table = new Table(); table.addContainerProperty("Column 1", String.class, "Row"); table.setDragMode(TableDragMode.ROW); table.addItem("Row 1"); table.addItem("Row 2"); table.addItem("Row 3"); table.addItem("Row 4"); table.addItem("Row 5"); table.addItem("Row 6"); table.addItem("Row 7"); h.addComponent(table); final Tree tree = new Tree(); tree.setDropHandler( new DropHandler() { public AcceptCriterion getAcceptCriterion() { return TargetItemAllowsChildren.get(); } public void drop(DragAndDropEvent dropEvent) { // criteria verify that this is safe DataBoundTransferable t = (DataBoundTransferable) dropEvent.getTransferable(); Container sourceContainer = t.getSourceContainer(); Object sourceItemId = t.getItemId(); System.out.println(sourceItemId); AbstractSelectTargetDetails dropData = ((AbstractSelectTargetDetails) dropEvent.getTargetDetails()); Object targetItemId = dropData.getItemIdOver(); // move item from table to category' tree.addItem(sourceItemId); tree.setParent(sourceItemId, targetItemId); tree.setChildrenAllowed(sourceItemId, false); sourceContainer.removeItem(sourceItemId); } }); tree.addItem("Item 1"); tree.addItem("Item 11"); tree.setChildrenAllowed("Item 11", false); tree.setParent("Item 11", "Item 1"); tree.addItem("Item 12"); tree.setChildrenAllowed("Item 12", false); tree.setParent("Item 12", "Item 1"); tree.addItem("Item 13"); tree.setChildrenAllowed("Item 13", false); tree.setParent("Item 13", "Item 1"); tree.addItem("Item 2"); tree.addItem("Item 21"); tree.setChildrenAllowed("Item 21", false); tree.setParent("Item 21", "Item 2"); tree.addItem("Item 22"); tree.setChildrenAllowed("Item 22", false); tree.setParent("Item 22", "Item 2"); tree.addItem("Item 23"); tree.setChildrenAllowed("Item 23", false); tree.setParent("Item 23", "Item 2"); tree.addItem("Item 3"); tree.addItem("Item 31"); tree.setChildrenAllowed("Item 31", false); tree.setParent("Item 31", "Item 3"); tree.addItem("Item 32"); tree.setChildrenAllowed("Item 32", false); tree.setParent("Item 32", "Item 3"); tree.addItem("Item 33"); tree.setChildrenAllowed("Item 33", false); tree.setParent("Item 33", "Item 3"); tree.expandItemsRecursively("Item 1"); tree.expandItemsRecursively("Item 2"); tree.expandItemsRecursively("Item 3"); h.addComponent(tree); }
@Override protected Component createRightColumnContent() { VerticalLayout layout = new VerticalLayout(); layout.setMargin(true); layout.setSpacing(true); VerticalLayout usersLayout = new VerticalLayout(); layout.addComponent(usersLayout); usersLayout.addComponent(new Label("<h2>Správa kategorií</h2>", Label.CONTENT_XHTML)); usersLayout.addComponent(tree); tree.setImmediate(true); tree.setDragMode(TreeDragMode.NODE); tree.setDropHandler( new DropHandler() { private static final long serialVersionUID = -5607799513535550687L; public AcceptCriterion getAcceptCriterion() { return AcceptAll.get(); } public void drop(DragAndDropEvent dropEvent) { Transferable data = dropEvent.getTransferable(); TreeTargetDetails dropData = ((TreeTargetDetails) dropEvent.getTargetDetails()); Object sourceItemId = ((DataBoundTransferable) data).getItemId(); Object targetItemId = dropData.getItemIdOver(); VerticalDropLocation location = dropData.getDropLocation(); moveNode(sourceItemId, targetItemId, location); } /** viz. http://demo.vaadin.com/sampler#DragDropTreeSorting */ /** * Move a node within a tree onto, above or below another node depending on the drop * location. * * @param sourceItemId id of the item to move * @param targetItemId id of the item onto which the source node should be moved * @param location VerticalDropLocation indicating where the source node was dropped * relative to the target node */ private void moveNode( final Object sourceItemId, final Object targetItemId, VerticalDropLocation location) { final HierarchicalContainer container = (HierarchicalContainer) tree.getContainerDataSource(); // Sorting goes as // - If dropped ON a node, we append it as a child // - If dropped on the TOP part of a node, we move/add it before // the node // - If dropped on the BOTTOM part of a node, we move/add it // after the node final Object parentItemId = container.getParent(targetItemId); final NodeDTO parent = parentItemId == null ? null : (NodeDTO) parentItemId; final NodeDTO source = (NodeDTO) sourceItemId; final NodeDTO target = (NodeDTO) targetItemId; switch (location) { case MIDDLE: // Přesunutí znamená rovnou přesun kategorie - v tom případě // je potřeba vyhodit potvrzovací okno addWindow( new ConfirmSubwindow( "Opravdu přesunout '" + source.getName() + "' do '" + target.getName() + "' ?") { private static final long serialVersionUID = 414272650677665672L; @Override protected void onConfirm(ClickEvent event) { if (nodeFacade.moveNode(source, target)) { if (container.setParent(sourceItemId, targetItemId) && container.hasChildren(targetItemId)) { // move first in the container container.moveAfterSibling(sourceItemId, null); } CategoriesSettingsWindow.this.showInfo( "Přesun kategorie proběhl úspěšně"); } else { CategoriesSettingsWindow.this.showWarning( "Nezdařilo se přesunout kategorii do vybraného místa"); } } }); break; case TOP: // Přesunutí znamená rovnou přesun kategorie - v tom případě // je potřeba vyhodit potvrzovací okno addWindow( new ConfirmSubwindow( "Opravdu přesunout '" + source.getName() + "' do " + (parentItemId == null ? "kořene sekce ?" : ("'" + parent.getName() + "' ?"))) { private static final long serialVersionUID = 414272650677665672L; @Override protected void onConfirm(ClickEvent event) { if (nodeFacade.moveNode(source, parent)) { if (container.setParent(sourceItemId, parentItemId)) { // reorder only the two items, moving source // above target container.moveAfterSibling(sourceItemId, targetItemId); container.moveAfterSibling(targetItemId, sourceItemId); } CategoriesSettingsWindow.this.showInfo( "Přesun kategorie proběhl úspěšně"); } else { CategoriesSettingsWindow.this.showWarning( "Nezdařilo se přesunout kategorii do vybraného místa"); } } }); break; case BOTTOM: // Přesunutí znamená rovnou přesun kategorie - v tom případě // je potřeba vyhodit potvrzovací okno addWindow( new ConfirmSubwindow( "Opravdu přesunout '" + source.getName() + "' do " + (parentItemId == null ? "kořene sekce ?" : ("'" + parent.getName() + "' ?"))) { private static final long serialVersionUID = 414272650677665672L; @Override protected void onConfirm(ClickEvent event) { if (nodeFacade.moveNode(source, parent)) { if (container.setParent(sourceItemId, parentItemId)) { container.moveAfterSibling(sourceItemId, targetItemId); } CategoriesSettingsWindow.this.showInfo( "Přesun kategorie proběhl úspěšně"); } else { CategoriesSettingsWindow.this.showWarning( "Nezdařilo se přesunout kategorii do vybraného místa"); } } }); break; } } }); tree.addListener( new Property.ValueChangeListener() { private static final long serialVersionUID = 191011037696709486L; public void valueChange(ValueChangeEvent event) { if (event.getProperty().getValue() != null) { // If something is selected from the tree, get it's 'name' // and // insert it into the textfield selectedNode.setValue((NodeDTO) event.getProperty().getValue()); panel.setCaption(panelCaptionPrefix + selectedNode.getValue().getName()); } else { selectedNode.setValue(null); panel.setCaption(panelCaptionPrefix + sectionRootCaption); } } }); tree.addActionHandler( new Action.Handler() { private static final long serialVersionUID = -4835306347998186964L; public void handleAction(Action action, Object sender, final Object target) { final NodeDTO node = (NodeDTO) target; if (action == ACTION_DELETE) { addWindow( new ConfirmSubwindow("Opravdu smazat kategorii '" + node.getName() + "' ?") { private static final long serialVersionUID = 9193745051559434697L; @Override protected void onConfirm(ClickEvent event) { if (!node.getContentNodes().isEmpty() || !node.getSubNodeIDs().isEmpty()) { CategoriesSettingsWindow.this.showWarning("Kategorie musí být prázdná"); } else { if (nodeFacade.deleteNode(node)) { tree.removeItem(target); CategoriesSettingsWindow.this.showInfo("Kategorie byla úspěšně smazána"); } else { CategoriesSettingsWindow.this.showWarning( "Nezdařilo se smazat vybranou kategorii"); } } } }); } else if (action == ACTION_RENAME) { final Window subwindow = new GrassSubWindow("Přejmenovat"); subwindow.center(); addWindow(subwindow); GridLayout subWindowlayout = new GridLayout(2, 2); subwindow.setContent(subWindowlayout); subWindowlayout.setMargin(true); subWindowlayout.setSpacing(true); final TextField newNameField = new TextField("Nový název:"); newNameField.setValue(node.getName()); newNameField.setRequired(true); newNameField.setRequiredError("Název kategorie nesmí být prázdný"); subWindowlayout.addComponent(newNameField, 0, 0, 1, 0); Button confirm = new Button( "Přejmenovat", new Button.ClickListener() { private static final long serialVersionUID = 8490964871266821307L; public void buttonClick(ClickEvent event) { if (newNameField.isValid() == false) return; if (nodeFacade.rename(node, (String) newNameField.getValue())) { showInfo("Kategorie byla úspěšně přejmenována"); tree.getItem(node) .getItemProperty(TreePropertyID.NÁZEV) .setValue(newNameField.getValue()); node.setName((String) newNameField.getValue()); } else { showWarning("Přejmenování se nezdařilo."); } (subwindow.getParent()).removeWindow(subwindow); } }); subWindowlayout.addComponent(confirm, 0, 1); subWindowlayout.setComponentAlignment(confirm, Alignment.MIDDLE_CENTER); Button close = new Button( "Storno", new Button.ClickListener() { private static final long serialVersionUID = 8490964871266821307L; public void buttonClick(ClickEvent event) { (subwindow.getParent()).removeWindow(subwindow); } }); subWindowlayout.addComponent(close, 1, 1); subWindowlayout.setComponentAlignment(close, Alignment.MIDDLE_CENTER); // Zaměř se na nové okno subwindow.focus(); } } public Action[] getActions(Object target, Object sender) { return ACTIONS; } }); createNewNodePanel(layout); return layout; }
@Override protected void setup() { instance = this; // Note, test only works with single app per server if // get() // not converted to thread local sp = new HorizontalSplitPanel(); sp.setSplitPosition(20); CssLayout l = new CssLayout(); sp.setFirstComponent(l); tree1 = new Tree("Volume 1"); tree1.setImmediate(true); fs1 = new BeanItemContainer<File>(File.class); tree1.setContainerDataSource(fs1); for (int i = 0; i < files.length; i++) { fs1.addBean(files[i]); if (files[i] instanceof Folder) { tree1.setChildrenAllowed(files[i], true); } else { tree1.setChildrenAllowed(files[i], false); } if (i >= files.length / 2) { tree1.setParent(files[i], files[i - files.length / 2]); } } tree1.setItemCaptionPropertyId("name"); tree1.setItemIconPropertyId("icon"); tree1.setDragMode(TreeDragMode.NODE); DropHandler dropHandler = new DropHandler() { @Override public AcceptCriterion getAcceptCriterion() { return AcceptAll.get(); } @Override public void drop(DragAndDropEvent dropEvent) { File file = null; Folder folder = null; TreeTargetDetails dropTargetData = (TreeTargetDetails) dropEvent.getTargetDetails(); folder = (Folder) dropTargetData.getItemIdInto(); if (dropEvent.getTransferable() instanceof DataBoundTransferable) { DataBoundTransferable transferable = (DataBoundTransferable) dropEvent.getTransferable(); file = (File) transferable.getItemId(); } else if (dropEvent.getTransferable().getSourceComponent() instanceof FileIcon) { FileIcon draggedIcon = (FileIcon) dropEvent.getTransferable().getSourceComponent(); file = draggedIcon.file; } setParent(file, folder); } }; tree1.setDropHandler(dropHandler); Handler actionHandler = new Handler() { private Action[] actions = new Action[] {new Action("Remove")}; @Override public void handleAction(Action action, Object sender, Object target) { ContainerHierarchicalWrapper containerDataSource = (ContainerHierarchicalWrapper) tree1.getContainerDataSource(); containerDataSource.removeItemRecursively(target); } @Override public Action[] getActions(Object target, Object sender) { return actions; } }; tree1.addActionHandler(actionHandler); tree1.addListener( new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { Object value = event.getProperty().getValue(); if (value != null && !(value instanceof Folder)) { value = tree1.getParent(value); } FolderView folderView = FolderView.get((Folder) value); sp.setSecondComponent(folderView); folderView.reload(); } }); l.addComponent(tree1); sp.setSecondComponent(FolderView.get(null)); getLayout().setSizeFull(); getLayout().addComponent(sp); TestUtils.injectCSS( getLayout().getUI(), "" + ".v-tree .v-icon {height:16px;} " + ".v-tree-node-caption-drag-top {/*border-top: none;*/} " + ".v-tree-node-caption-drag-bottom {border-bottom: none ;} " + ".v-tree-node-caption-drag-center {background-color: transparent;}" + ".v-tree-node-caption-dragfolder { background-color: cyan;} "); }