001: /* SimpleCategoryModel.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Aug 14 11:25:51 2006, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.zul.event.ChartDataEvent;
023: import org.zkoss.zul.event.ChartDataListener;
024:
025: import java.util.Set;
026: import java.util.Map;
027: import java.util.List;
028: import java.util.HashSet;
029: import java.util.HashMap;
030: import java.util.LinkedHashMap;
031: import java.util.Iterator;
032: import java.util.ArrayList;
033: import java.util.Collection;
034:
035: /**
036: * A Category data model implementation of {@link CategoryModel}.
037: * A Category model is an N series of (category, value) data objects.
038: *
039: * @author henrichen
040: * @see CategoryModel
041: * @see Chart
042: */
043: public class SimpleCategoryModel extends AbstractChartModel implements
044: CategoryModel {
045: private Map _seriesMap = new HashMap(13); // (series, usecount)
046: private List _seriesList = new ArrayList(13);
047:
048: private Map _categoryMap = new HashMap(13); // (category, usecount)
049: private List _categoryList = new ArrayList(13);
050:
051: private Map _valueMap = new LinkedHashMap(79);
052:
053: //-- CategoryModel --//
054: public Comparable getSeries(int index) {
055: return (Comparable) _seriesList.get(index);
056: }
057:
058: public Comparable getCategory(int index) {
059: return (Comparable) _categoryList.get(index);
060: }
061:
062: public Collection getSeries() {
063: return _seriesList;
064: }
065:
066: public Collection getCategories() {
067: return _categoryList;
068: }
069:
070: public Collection getKeys() {
071: return _valueMap.keySet();
072: }
073:
074: public Number getValue(Comparable series, Comparable category) {
075: List key = new ArrayList(2);
076: key.add(series);
077: key.add(category);
078: Number num = (Number) _valueMap.get(key);
079: return num;
080: }
081:
082: public void setValue(Comparable series, Comparable category,
083: Number value) {
084: List key = new ArrayList(2);
085: key.add(series);
086: key.add(category);
087:
088: if (!_valueMap.containsKey(key)) {
089: if (!_categoryMap.containsKey(category)) {
090: _categoryMap.put(category, new Integer(1));
091: _categoryList.add(category);
092: } else {
093: Integer count = (Integer) _categoryMap.get(category);
094: _categoryMap.put(category, new Integer(
095: count.intValue() + 1));
096: }
097:
098: if (!_seriesMap.containsKey(series)) {
099: _seriesMap.put(series, new Integer(1));
100: _seriesList.add(series);
101: } else {
102: Integer count = (Integer) _seriesMap.get(series);
103: _seriesMap.put(series,
104: new Integer(count.intValue() + 1));
105:
106: }
107: } else {
108: Number ovalue = (Number) _valueMap.get(key);
109: if (Objects.equals(ovalue, value)) {
110: return;
111: }
112: }
113:
114: _valueMap.put(key, value);
115: fireEvent(ChartDataEvent.CHANGED, (String) series, category);
116: }
117:
118: public void removeValue(Comparable series, Comparable category) {
119: List key = new ArrayList(2);
120: key.add(series);
121: key.add(category);
122: if (!_valueMap.containsKey(key)) {
123: return;
124: }
125:
126: _valueMap.remove(key);
127:
128: int ccount = ((Integer) _categoryMap.get(category)).intValue();
129: if (ccount > 1) {
130: _categoryMap.put(category, new Integer(ccount - 1));
131: } else {
132: _categoryMap.remove(category);
133: _categoryList.remove(category);
134: }
135:
136: int scount = ((Integer) _seriesMap.get(series)).intValue();
137: if (scount > 1) {
138: _seriesMap.put(series, new Integer(scount - 1));
139: } else {
140: _seriesMap.remove(series);
141: _seriesList.remove(series);
142: }
143:
144: fireEvent(ChartDataEvent.REMOVED, (String) series, category);
145: }
146:
147: public void clear() {
148: _seriesMap.clear();
149: _seriesList.clear();
150: _categoryMap.clear();
151: _categoryList.clear();
152: _valueMap.clear();
153: fireEvent(ChartDataEvent.REMOVED, null, null);
154: }
155: }
|