1 /*
2
3 dsh-piccolo-eventlist-view Piccolo2D views for event lists.
4 Copyright (c) 2010-2013 held jointly by the individual authors.
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
20 > http://www.fsf.org/licensing/licenses/lgpl.html
21 > http://www.opensource.org/licenses/lgpl-license.php
22
23 */
24 package org.dishevelled.piccolo.eventlist.view;
25
26 import ca.odell.glazedlists.EventList;
27 import ca.odell.glazedlists.ListSelection;
28
29 import org.dishevelled.functor.UnaryFunction;
30
31 import org.dishevelled.iconbundle.IconSize;
32
33 import org.dishevelled.iconbundle.tango.TangoProject;
34
35 import org.dishevelled.piccolo.identify.NautilusIdNode;
36
37 import org.piccolo2d.PNode;
38
39 import org.piccolo2d.event.PBasicInputEventHandler;
40 import org.piccolo2d.event.PInputEvent;
41
42 /**
43 * Identifiable elements node.
44 *
45 * @param <E> model element type
46 * @author Michael Heuer
47 * @version $Revision$ $Date$
48 */
49 public final class IdElementsNode<E>
50 extends ElementsNode<E>
51 {
52 /** Icon size. */
53 private IconSize iconSize = DEFAULT_ICON_SIZE;
54
55 /** Default icon size. */
56 public static final IconSize DEFAULT_ICON_SIZE = TangoProject.LARGE;
57
58 /** Model to view mapping. */
59 private final UnaryFunction<E, NautilusIdNode> modelToView = new UnaryFunction<E, NautilusIdNode>()
60 {
61 @Override
62 public NautilusIdNode evaluate(final E element)
63 {
64 final NautilusIdNode idNode = new NautilusIdNode(element, iconSize);
65 idNode.removeInputEventListener(idNode.getDragHandler());
66 // todo: will need to dispose this listener at some point
67 idNode.addInputEventListener(new PBasicInputEventHandler()
68 {
69 @Override
70 public void mousePressed(final PInputEvent event) {
71 if (event.isLeftMouseButton()) {
72 int indexInParent = indexOfChild(idNode);
73 if (getSelectionModel().isSelected(indexInParent))
74 {
75 if (!(event.isShiftDown()))
76 {
77 getSelectionModel().deselect(indexInParent);
78 }
79 }
80 else
81 {
82 if (!(event.isShiftDown()) && !(event.isControlDown()))
83 {
84 getSelectionModel().deselectAll();
85 }
86 getSelectionModel().select(indexInParent);
87 }
88 }
89 }
90 });
91 return idNode;
92 }
93 };
94
95 /** Selection listener. */
96 private final ListSelection.Listener selectionListener = new ListSelection.Listener()
97 {
98 @Override
99 public void selectionChanged(final int changeStart, final int changeEnd)
100 {
101 for (int i = changeStart; i < (changeEnd + 1); i++)
102 {
103 PNode child = getChild(i);
104 if (child instanceof NautilusIdNode)
105 {
106 NautilusIdNode idNode = (NautilusIdNode) child;
107 if (getSelectionModel().isSelected(i))
108 {
109 idNode.select();
110 }
111 else
112 {
113 idNode.deselect();
114 }
115 }
116 }
117 }
118 };
119
120
121 /**
122 * Create a new identifiable elements summary with the specified model.
123 *
124 * @param model model, must not be null
125 */
126 public IdElementsNode(final EventList<E> model)
127 {
128 super(model);
129 setModelToView(modelToView);
130 getSelectionModel().addSelectionListener(selectionListener);
131 }
132
133
134 // todo: allow other id node impls
135 /**
136 * Return the icon size for this identifiable elements node.
137 *
138 * @return the icon size for this identifiable elements node
139 */
140 public IconSize getIconSize()
141 {
142 return iconSize;
143 }
144
145 /**
146 * Set the icon size for this identifiable elements node to <code>iconSize</code>. Defaults
147 * to {@link #DEFAULT_ICON_SIZE}.
148 *
149 * <p>This is a bound property.</p>
150 *
151 * @param iconSize icon size for this identifiable elements node, must not be null
152 */
153 public void setIconSize(final IconSize iconSize)
154 {
155 IconSize oldIconSize = this.iconSize;
156 this.iconSize = iconSize;
157 updateIconSize();
158 firePropertyChange(-1, "iconSize", oldIconSize, this.iconSize);
159 }
160
161 /**
162 * Update icon size.
163 */
164 private void updateIconSize()
165 {
166 for (Object o : getChildrenReference())
167 {
168 if (o instanceof NautilusIdNode)
169 {
170 NautilusIdNode idNode = (NautilusIdNode) o;
171 idNode.setIconSize(iconSize);
172 }
173 }
174 }
175
176 /**
177 * Remove listeners.
178 */
179 private void removeListeners()
180 {
181 getSelectionModel().removeSelectionListener(selectionListener);
182 }
183
184 @Override
185 public void dispose()
186 {
187 super.dispose();
188 removeListeners();
189 }
190 }