litesoft
@ 906
litesoft / trunk / mobileGWT / mgwt.changetracking / mgwt / src / main / java / com / googlecode / mgwt / ui / client / widget / WidgetList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
/* * Copyright 2010 Daniel Kurka * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.googlecode.mgwt.ui.client.widget; import com.google.gwt.dom.client.Document; import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HasWidgets; import com.google.gwt.user.client.ui.Panel; import com.google.gwt.user.client.ui.Widget; import com.googlecode.mgwt.ui.client.MGWTStyle; import com.googlecode.mgwt.ui.client.theme.base.ListCss; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * A list that can contain widgets * * This class renders widgets into a list. The same thing is done by {@link CellList} much more * efficient. * * <b>Note:</b> Normally you should be using {@link CellList}. Only if you really need Widgets * inside the list (which won't be the case most of the time) you should be using WidgetList. * * The reference for styling can be found in CellList as well. * * * @author Daniel Kurka */ public class WidgetList extends Composite implements HasWidgets { private static class WidgetListEntry extends Composite implements HasWidgets { private static class LIFlowPanel extends ComplexPanel { public LIFlowPanel() { setElement(Document.get().createLIElement()); } @Override public void add(Widget w) { add(w, getElement()); } } private Panel container; public WidgetListEntry(ListCss css) { container = new LIFlowPanel(); initWidget(container); } @Override public void add(Widget w) { container.add(w); } @Override public void clear() { container.clear(); } @Override public Iterator<Widget> iterator() { return container.iterator(); } @Override public boolean remove(Widget w) { return container.remove(w); } } private static class ULFlowPanel extends ComplexPanel { public ULFlowPanel() { setElement(Document.get().createULElement()); } @Override public void add(Widget w) { add(w, getElement()); } } private int childCount; private List<WidgetListEntry> children = new ArrayList<WidgetListEntry>(); private Panel container; private Map<Widget, WidgetListEntry> map; protected final ListCss css; /** * Construct a widget list using the default css */ public WidgetList() { this(MGWTStyle.getTheme().getMGWTClientBundle().getListCss()); } /** * Construct a widget list using a specific css * * @param css the css to use */ public WidgetList(ListCss css) { this.css = css; css.ensureInjected(); container = new ULFlowPanel(); initWidget(container); setStylePrimaryName(css.listCss()); map = new HashMap<Widget, WidgetListEntry>(); } /* * (non-Javadoc) * * @see com.google.gwt.user.client.ui.HasWidgets#add(com.google.gwt.user.client.ui.Widget) */ /** {@inheritDoc} */ @Override public void add(Widget w) { WidgetListEntry widgetListEntry = new WidgetListEntry(css); widgetListEntry.add(w); if (childCount == 0) { widgetListEntry.addStyleName(css.first()); } map.put(w, widgetListEntry); container.add(widgetListEntry); children.add(widgetListEntry); if (childCount > 0) { children.get(childCount - 1).removeStyleName(css.last()); } widgetListEntry.addStyleName(css.last()); childCount++; } /* * (non-Javadoc) * * @see com.google.gwt.user.client.ui.HasWidgets#clear() */ /** {@inheritDoc} */ @Override public void clear() { container.clear(); map.clear(); children.clear(); childCount = 0; } /* * (non-Javadoc) * * @see com.google.gwt.user.client.ui.HasWidgets#iterator() */ /** {@inheritDoc} */ @Override public Iterator<Widget> iterator() { return map.keySet().iterator(); } /* * (non-Javadoc) * * @see com.google.gwt.user.client.ui.HasWidgets#remove(com.google.gwt.user.client.ui.Widget) */ /** {@inheritDoc} */ @Override public boolean remove(Widget w) { WidgetListEntry entry = map.remove(w); if (entry == null) return false; // did we remove the last child? if (children.get(childCount - 1) == entry) { // are there others in the list? if (childCount - 2 >= 0) { children.get(childCount - 2).addStyleName(css.last()); } } // did we remove the first child if (children.get(0) == entry) { if (children.size() > 1) { children.get(1).addStyleName(css.first()); } } childCount--; children.remove(entry); return container.remove(entry); } /** * Should the list be displayed with rounded corners * * @param round true to display with rounded corners */ public void setRound(boolean round) { if (round) { addStyleName(css.round()); } else { removeStyleName(css.round()); } } public int getWidgetCount() { return childCount; } public void setGroup(int index, boolean group) { if (group) { children.get(index).addStyleName(css.group()); } else { children.get(index).removeStyleName(css.group()); } } } |