1 /* 2 * 3 * Fosstrak LLRP Commander (www.fosstrak.org) 4 * 5 * Copyright (C) 2008 ETH Zurich 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/> 19 * 20 */ 21 22 package org.fosstrak.llrp.commander.editors; 23 24 import java.util.HashMap; 25 import java.util.Iterator; 26 import java.util.Map; 27 28 import org.eclipse.swt.graphics.Color; 29 import org.eclipse.swt.graphics.RGB; 30 import org.eclipse.swt.widgets.Display; 31 32 /** 33 * A helper class for supporting the color table used in XML Editor. 34 * 35 * @author Haoning Zhang 36 * @version 1.0 37 */ 38 public class ColorManager { 39 40 /** 41 * Max different colors used in the editor. 42 */ 43 private final static int MAX_COLORS = 10; 44 45 /** 46 * Color table. 47 */ 48 protected Map<RGB, Color> fColorTable = new HashMap<RGB, Color>(MAX_COLORS); 49 50 /** 51 * Dispose all the <code>Color</code> in the Color Table. 52 */ 53 public void dispose() { 54 Iterator<Color> e = fColorTable.values().iterator(); 55 while (e.hasNext()) 56 e.next().dispose(); 57 } 58 59 /** 60 * Get the <code>Color</code> instance by <code>RGB</code>. 61 * The color will be created when first uses. 62 * 63 * @param aRGB RGB instance 64 * @return Color instance from Color Table 65 */ 66 public Color getColor(RGB aRGB) { 67 Color color = fColorTable.get(aRGB); 68 if (color == null) { 69 color = new Color(Display.getCurrent(), aRGB); 70 fColorTable.put(aRGB, color); 71 } 72 return color; 73 } 74 }