/****************************************************************************** * Copyright 1998-1999 by Strategic Database Inc., * 1604 Forest Trail, Austin, Texas, 78703, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information of Strategic * Database Inc.(SDI) ("Confidential Information"). You shall not disclose such * Confidential Information and shall use it only in accordance with the terms * of the license agreement you entered into with SDI. *****************************************************************************/ package com.bbn.openmap.layer.location.generic; import java.awt.Color; import com.bbn.openmap.layer.location.db.*; import com.bbn.openmap.util.ColorFactory; import java.util.*; import java.sql.*; import java.awt.*; /** this class allows you to dynamically change treenode colors based on the value of a DB field, the DB.prop file has the following: 'RN_Radio_Number.colorFormat=RN_Condition/41:green/6:blue' -this is the DB attrib to compare, followed by the the values for that attrib and the color to show when the attrib has the given value this class parses up the string and stores the color mappings in a hashtable for quick lookup on the fly. @author Craig McClendon @version 1.0 */ public class LocationColorFormatter // extends LocationColorFormatter { // ------------------------------------------------- public class ColorEntry { public ColorEntry() {color = null; colorName = null;} public ColorEntry(String name, Color col) {color = col; colorName = name;} public ColorEntry(String name) {this(name, null);} // --------------------------------- public Color color; public String colorName; } // class ColorEntry // ------------------------------------------------- // ------------------------------------------------- public class LocColorEntry { String entryName; ColorEntry[] colors; protected void setColor(int idx, ColorEntry cole) { colors[idx] = cole;} // --------------------------------------------- public ColorEntry getEntry(int idx) { ColorEntry retVal = defaultColor; if(0 < colors.length) { if(idx < colors.length) { retVal = colors[idx];} else { retVal = colors[0];} } return retVal; } // --------------------------------------------- public int getColorCount() {return colors.length;} public Color getColor(int idx) {return getEntry(idx).color;} public String getColorName(int idx) {return getEntry(idx).colorName;} // --------------------- public ColorEntry getEntry() {return getEntry(0);} public Color getColor() {return getColor(0);} public String getColorName() {return getColorName(0);} // --------------------------------------------- public LocColorEntry(int i, String ename, ColorEntry e) { entryName = ename; i = (i < 1 ? 1:i); colors = new ColorEntry[i]; if(e != null && i > 0) { setColor(0, e);} } } // class LocColorEntry // ------------------------------------------------- private int colorFieldIndex; protected Hashtable colorEntryHash; protected ColorEntry defaultColor; ///////////////////////////////////////////////////////////////// protected static String majorDelimiter = "/"; protected static String minorDelimiter = ":"; protected static LocationColorFormatter myFactoryInstance; ///////////////////////////////////////////////////////////////// // private static final boolean isFinal = false; static { if (myFactoryInstance == null) { myFactoryInstance = new LocationColorFormatter(Color.BLACK); } } ///////////////////////////////////////////////////////////////// public LocationColorFormatter(Color defcolor) { colorFieldIndex = 0; defaultColor = new ColorEntry("Default", defcolor); colorEntryHash= new Hashtable(); colorEntryHash.put(defaultColor.colorName, new LocColorEntry(1, defaultColor.colorName, defaultColor)); } ///////////////////////////////////////////////////////////////// public static LocationColorFormatter newLocationColorFormatter(String colorCodes, Color defcol) { LocationColorFormatter retVal = new LocationColorFormatter(defcol); retVal.parseCodes(colorCodes); return retVal; } public ColorEntry parseColor(String colorstring) { ColorEntry retVal = new ColorEntry(colorstring); //pull out the lighten/darken factor int openNdx = colorstring.indexOf("("); int factor = 0; if ( openNdx > 0 ) { int closeNdx = colorstring.indexOf(")"); String factorStr = colorstring.substring(openNdx+1, closeNdx); retVal.colorName = colorstring.substring(0, openNdx); try { factor = Integer.parseInt(factorStr); } catch(NumberFormatException nfe) { System.err.println("nfe->" + nfe); factor = 0; } }//if try { retVal.color = ColorFactory.parseColor(retVal.colorName); } catch(NumberFormatException nfe) { } if ( retVal.color == null ) { retVal.color = ColorFactory.getNamedColor(retVal.colorName, null); } if( retVal.color != null && factor != 0) { retVal.color = alterColorByFactor(retVal.color, factor);} return retVal; } ///////////////////////////////////////////////////////////////// public void parseCodes(String colorCodes) { // if (Log.DEBUG && myLog != null) // { myLog.println(3,"LocationColorFormatter.initColorTable(meta, "+colorCodes+")"); } //parse up the colorCodes string into the table //tokenize the string from site.properties //we start with something like this '41:green/6:blue' if(colorCodes != null) { StringTokenizer st = new StringTokenizer(colorCodes, majorDelimiter); String tempString = null; ColorEntry color = null; ColorEntry color2 = null; String code = null; StringTokenizer tempST = null; LocColorEntry locentry = null; while( st.hasMoreTokens() ) { //grab a 'part', separate it into it's 2 pieces tempString = st.nextToken(); //we now have something like this '41:green' tempST = new StringTokenizer(tempString, minorDelimiter); //sepate it into the two pieces, put it in the Hashtable code = tempST.nextToken(); //the '41' part color = parseColor(tempST.nextToken()); //the 'color' part if(tempST.hasMoreTokens()) { color2= parseColor(tempST.nextToken());} else { color2= null;} //all thats left to do is put a Color object in the table if(color != null) { code = code.toUpperCase(); locentry = new LocColorEntry((color2 == null? 1: 2), code, color); if(color2 != null) { locentry.setColor(1, color2);} colorEntryHash.put(code, locentry); } }//for } // if }//method // -------------------------------------------- public LocColorEntry getEntry(Object key) { LocColorEntry retVal = null; //make sure there is a (hash)table for this (DB)table if( colorEntryHash == null || colorFieldIndex < 0 ) { return retVal;} if( key == null ) { return retVal;} String codeVal = key.toString(); if( codeVal != null) { retVal = (LocColorEntry)colorEntryHash.get(codeVal.toUpperCase());} return retVal; }//method ///////////////////////////////////////////////////////////////// public Color getColor(Object val) { Color retVal = null; LocColorEntry e = getEntry(val); //make sure there is a (hash)table for this (DB)table if( e != null) { retVal = e.getColor();} return retVal; }//method ///////////////////////////////////////////////////////////////// public String getColorName(Object val) { String retVal = null; LocColorEntry e = getEntry(val); //make sure there is a (hash)table for this (DB)table if( e != null) { retVal = e.getColorName();} return retVal; }//method ///////////////////////////////////////////////////////////////// /** * This method darkens or brightens the Color passed in * by the factor given. */ public static Color alterColorByFactor(Color color, int factor) { boolean darken = true; if ( factor < 0 ) { factor = -factor; darken = false; }//if for( int i=0; i < factor; i++ ) { if( darken == true ) color = color.darker(); else color = color.brighter(); }//for return color; }//method ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// public static LocationColorFormatter LocationColorFormatterFactory(String colorCodes, Color defcol) { LocationColorFormatter retVal = null; if (myFactoryInstance != null) { retVal = myFactoryInstance.newLocationColorFormatter(colorCodes, defcol); } return retVal; } ///////////////////////////////////////////////////////////////// } // class