001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.logistics.ui.inventory.data;
028:
029: import org.cougaar.logistics.plugin.inventory.LogisticsInventoryFormatter;
030: import org.cougaar.logistics.ui.inventory.InventoryColorTable;
031:
032: import java.io.Writer;
033:
034: import java.util.*;
035:
036: /**
037: * <pre>
038: *
039: * The InventoryPreferenceData holds all the information necessary to
040: *
041: **/
042:
043: public class InventoryPreferenceData implements Cloneable {
044: public static final String LITERS_LABEL = "Liters";
045: public static final String CASES_LABEL = LogisticsInventoryFormatter.WATER_UNIT;
046: public static final String BOTTLES_LABEL = "Bottles";
047: public static final String TONS_LABEL = LogisticsInventoryFormatter.AMMUNITION_UNIT;
048: public static final String ROUNDS_LABEL = "Rounds";
049:
050: public static final String[] units = { CASES_LABEL, LITERS_LABEL,
051: BOTTLES_LABEL, TONS_LABEL, ROUNDS_LABEL };
052:
053: public static final String PREF_DATA_HEADER_XML = "INVENTORY_PREFERENCE_DATA";
054: public static final String CDAY_XML = "STARTUP_IN_CDAY_MODE";
055: public static final String SHORTFALL_XML = "DISPLAY_SHORTFALL_WHEN_DETECTED";
056: public static final String SHOW_INVENTORY_XML = "SHOW_INVENTORY_CHART";
057: public static final String SHOW_REFILL_XML = "SHOW_REFILL_CHART";
058: public static final String SHOW_DEMAND_XML = "SHOW_DEMAND_CHART";
059: public static final String ROUND_XML = "ROUND_TO_WHOLES";
060: public static final String AMMO_UNIT_XML = "AMMO_UNIT";
061: public static final String WATER_UNIT_XML = "WATER_UNIT";
062: public static final String COLOR_SCHEME_XML = "COLOR_SCHEME";
063:
064: public boolean startupWCDay;
065: public boolean displayShortfall;
066:
067: public boolean showInventoryChart;
068: public boolean showRefillChart;
069: public boolean showDemandChart;
070:
071: public boolean roundToWholes;
072: public int ammoUnit;
073: public int waterUnit;
074:
075: public static final String COLOR_SCHEME_NORMAL = "Normal";
076: // public static final String COLOR_SCHEME_RG_COLORBLIND = "Red Green Color Blind"; // red-green color blind
077: public static final String COLOR_SCHEME_BW = "Black and White"; // black and white
078:
079: public static final String[] colorSchemes = { COLOR_SCHEME_NORMAL,
080: // COLOR_SCHEME_RG_COLORBLIND,
081: COLOR_SCHEME_BW };
082:
083: public String colorScheme;
084:
085: private InventoryColorTable colorTable;
086:
087: public InventoryPreferenceData() {
088: colorTable = new InventoryColorTable();
089: setDefaultValues();
090: }
091:
092: public void setDefaultValues() {
093: startupWCDay = true;
094: displayShortfall = true;
095: showInventoryChart = true;
096: showRefillChart = true;
097: showDemandChart = true;
098: roundToWholes = false;
099: ammoUnit = getUnit(TONS_LABEL);
100: waterUnit = getUnit(CASES_LABEL);
101: colorScheme = COLOR_SCHEME_NORMAL;
102: }
103:
104: public String getAmmoUnitLabel() {
105: return getUnitLabel(ammoUnit);
106: }
107:
108: public String getWaterUnitLabel() {
109: return getUnitLabel(waterUnit);
110: }
111:
112: public String getPreferredUnit(InventoryData data) {
113: String dataUnit = data.getUnit();
114: String item = data.getItem();
115: String supplyType = data.getSupplyType();
116: if (supplyType
117: .equals(LogisticsInventoryFormatter.AMMUNITION_SUPPLY_TYPE)) {
118: return getAmmoUnitLabel();
119: } else if (item
120: .equals(LogisticsInventoryFormatter.WATER_ITEM_ID)) {
121: return getWaterUnitLabel();
122: }
123: return dataUnit;
124: }
125:
126: public Object clone() {
127: Object copy = null;
128: try {
129: copy = super .clone();
130: } catch (CloneNotSupportedException e) {
131: throw new RuntimeException(
132: "Yow! InventoryPreferenceData is not cloneable", e);
133: }
134: return copy;
135: }
136:
137: public static int getUnit(String unitString) {
138: for (int i = 0; i < units.length; i++) {
139: if (units[i].equals(unitString)) {
140: return i;
141: }
142: }
143: throw new RuntimeException("Unknown Unit Type String:"
144: + unitString);
145: }
146:
147: public static String getUnitLabel(int unit) {
148: if ((unit < 0) || (unit >= units.length)) {
149: throw new RuntimeException("Unknown unit type:" + unit);
150: }
151: return units[unit];
152: }
153:
154: public String getColorScheme() {
155: return colorScheme;
156: }
157:
158: public InventoryColorTable getColorTable() {
159: return colorTable;
160: }
161:
162: public String toXMLString() {
163: String xmlString = "<" + PREF_DATA_HEADER_XML + ">\n";
164: xmlString = xmlString + "<" + CDAY_XML + ">" + startupWCDay
165: + "</" + CDAY_XML + ">\n";
166: xmlString = xmlString + "<" + SHORTFALL_XML + ">"
167: + displayShortfall + "</" + SHORTFALL_XML + ">\n";
168: xmlString = xmlString + "<" + SHOW_INVENTORY_XML + ">"
169: + showInventoryChart + "</" + SHOW_INVENTORY_XML
170: + ">\n";
171: xmlString = xmlString + "<" + SHOW_REFILL_XML + ">"
172: + showRefillChart + "</" + SHOW_REFILL_XML + ">\n";
173: xmlString = xmlString + "<" + SHOW_DEMAND_XML + ">"
174: + showDemandChart + "</" + SHOW_DEMAND_XML + ">\n";
175: xmlString = xmlString + "<" + ROUND_XML + ">" + roundToWholes
176: + "</" + ROUND_XML + ">\n";
177: xmlString = xmlString + "<" + AMMO_UNIT_XML + ">"
178: + getUnitLabel(ammoUnit) + "</" + AMMO_UNIT_XML + ">\n";
179: xmlString = xmlString + "<" + WATER_UNIT_XML + ">"
180: + getUnitLabel(waterUnit) + "</" + WATER_UNIT_XML
181: + ">\n";
182: xmlString = xmlString + "<" + COLOR_SCHEME_XML + ">"
183: + colorScheme + "</" + COLOR_SCHEME_XML + ">\n";
184: xmlString = xmlString + "<" + PREF_DATA_HEADER_XML + ">\n";
185: return xmlString;
186: }
187:
188: public void parseValuesFromXMLString(String xmlString) {
189: String[] lines = xmlString.split("\\n");
190: for (int i = 0; i < lines.length; i++) {
191: String currLine = lines[i];
192: String tag = getTagName(currLine);
193: if (!(tag.equals(PREF_DATA_HEADER_XML))) {
194: String[] tagValueTag = currLine.split(">");
195: String[] valueTag = tagValueTag[1].split("<");
196: String value = valueTag[0];
197: setTagValue(tag, value);
198: }
199: }
200: }
201:
202: public void setTagValue(String tag, String value) {
203: if (tag.equals(COLOR_SCHEME_XML)) {
204: colorScheme = value;
205: } else if (tag.equals(AMMO_UNIT_XML)) {
206: ammoUnit = getUnit(value);
207: } else if (tag.equals(WATER_UNIT_XML)) {
208: waterUnit = getUnit(value);
209: } else if (tag.equals(CDAY_XML)) {
210: startupWCDay = parseBoolean(value);
211: } else if (tag.equals(SHORTFALL_XML)) {
212: displayShortfall = parseBoolean(value);
213: } else if (tag.equals(SHOW_INVENTORY_XML)) {
214: showInventoryChart = parseBoolean(value);
215: } else if (tag.equals(SHOW_REFILL_XML)) {
216: showRefillChart = parseBoolean(value);
217: } else if (tag.equals(SHOW_DEMAND_XML)) {
218: showDemandChart = parseBoolean(value);
219: } else if (tag.equals(ROUND_XML)) {
220: roundToWholes = parseBoolean(value);
221: } else {
222: throw new RuntimeException("Unknown tag:" + tag
223: + " with value:" + value);
224: }
225:
226: }
227:
228: protected static String stripTag(String line) {
229: int start = 0;
230: String tag = line.split(">")[0];
231: int end = tag.length();
232: if (tag.startsWith("</")) {
233: start = 2;
234: } else if (tag.startsWith("<")) {
235: start = 1;
236: }
237: if (tag.endsWith(">")) {
238: end--;
239: }
240: return tag.substring(start, end);
241: }
242:
243: protected static String getTagName(String line) {
244: String tag = stripTag(line);
245: String words[] = tag.split("\\s");
246: return words[0];
247: }
248:
249: protected static boolean parseBoolean(String aBoolStr) {
250: if (aBoolStr.trim().toLowerCase().equals("true")) {
251: return true;
252: } else {
253: return false;
254: }
255: }
256: }
|