001    /*
002     * Copyright (C) 2008 Adam Cornett This program is free software; you can
003     * redistribute it and/or modify it under the terms of the GNU General Public
004     * License as published by the Free Software Foundation; either version 3 of the
005     * License, or (at your option) any later version. This program is distributed
006     * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
007     * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
008     * See the GNU General Public License for more details. You should have received
009     * a copy of the GNU General Public License along with this program; if not, see
010     * <http://www.gnu.org/licenses>.
011     */
012    package net.codescore.ui.components.dnd;
013    
014    import java.util.Collections;
015    import java.util.LinkedList;
016    import java.util.List;
017    
018    import org.zkoss.zk.ui.Component;
019    import org.zkoss.zk.ui.event.DropEvent;
020    import org.zkoss.zul.Vbox;
021    
022    /**
023     * A Vbox that acts as a column for {@link DnDObject}s
024     * 
025     * @author Adam Cornett
026     */
027    public class DnDVbox extends Vbox implements DnDColumn {
028    
029            private static final long serialVersionUID = -8263473174255597176L;
030            /**
031             * The list of objects that this column has
032             */
033            private final List<DnDObject> objects;
034    
035            protected final int col;
036    
037            /**
038             * @param col
039             *            This column's number. Columns are 1-indexed: the column to the
040             *            far left is 1 the next one to the right is 2, etc.
041             */
042            public DnDVbox(final int col) {
043                    this.col = col;
044                    objects = new LinkedList<DnDObject>();
045                    setDroppable("true");
046            }
047    
048            @Override
049            public void addWindow(final DnDObject win) {
050                    objects.add(win);
051                    win.setDnDParent(this);
052                    final int lastIndex = objects.size() - 1;
053                    int newIndex = 1;
054                    if (lastIndex > 0)
055                            newIndex = objects.get(lastIndex).getRowNumber() + 1;
056                    win.setRowNumber(newIndex);
057            }
058    
059            @Override
060            public int compareTo(final DnDColumn o) {
061                    return getColumnNumber() - o.getColumnNumber();
062            }
063    
064            @Override
065            public void fixRowNums() {
066                    int i = 0;
067                    for (final DnDObject d : objects)
068                            d.setRowNumber(i++);
069            }
070    
071            @Override
072            public int getColumnNumber() {
073                    return col;
074            }
075    
076            @Override
077            public void onDrop(final DropEvent event) {
078                    final Component dragged = event.getDragged();
079                    if (dragged instanceof DnDObject) {
080                            final DnDObject dnd = (DnDObject) dragged;
081                            dnd.leaveColumn();
082                            addWindow(dnd);
083                    } else
084                            throw new RuntimeException("Cannot drop a " + dragged.getClass());
085    
086            }
087    
088            @Override
089            public void reBuildList() {
090                    sortList();
091                    fixRowNums();
092                    getChildren().clear();
093                    for (final DnDObject d : objects)
094                            d.setParent(this);
095            }
096    
097            @Override
098            public void removeWindow(final DnDObject win) {
099                    objects.remove(win);
100                    win.setParent(null);
101            }
102    
103            public void sortList() {
104                    Collections.sort(objects);
105            }
106    
107            @Override
108            public String toString() {
109                    return "{DnDVbox:" + getId() + " Column:" + col + "}";
110            }
111    }