View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.displayers;
21  
22  import java.awt.Color;
23  
24  import com.cosylab.util.BitCondition;
25  
26  /**
27   * DefaultConditionDescriptor take cares of Condition Colors , it defines
28   * default colors for Conditions , sets Conditions other Colors.
29   *
30   * @author tomo
31   */
32  public class DefaultConditionDescriptor implements ConditionDescriptor
33  {
34  	/** Default ERROR color. */
35  	public static final Color DEFAULT_ERROR = Color.RED;
36  
37  	/** Defaut UNUSED color. */
38  	public static final Color DEFAULT_UNUSED = Color.GRAY;
39  
40  	/** Default WARNING color. */
41  	public static final Color DEFAULT_WARNING = Color.YELLOW;
42  
43  	/** Default OK color. */
44  	public static final Color DEFAULT_OK = Color.GREEN;
45  	private static BitCondition[] conditions = null;
46  	private static Color[] colors = null;
47  	private static boolean overRide = false;
48  
49  	/**
50  	 * Simple constructor...
51  	 */
52  	public DefaultConditionDescriptor()
53  	{
54  		conditions=BitCondition.values();
55  
56  		colors = new Color[conditions.length];
57  
58  		for (int i = 0; i < conditions.length; i++) {
59  			colors[i] = Color.BLACK;
60  		}
61  
62  		setDefaultColors();
63  	}
64  
65  	/**
66  	 * Sets the default colors for conditions
67  	 */
68  	public void setDefaultColors()
69  	{
70  		for (int i = 0; i < conditions.length; i++) {
71  			if (conditions[i] == BitCondition.UNUSED) {
72  				colors[i] = DEFAULT_UNUSED;
73  			} else if (conditions[i] == BitCondition.OK) {
74  				colors[i] = DEFAULT_OK;
75  			} else if (conditions[i] == BitCondition.WARNING) {
76  				colors[i] = DEFAULT_WARNING;
77  			} else if (conditions[i] == BitCondition.ERROR) {
78  				colors[i] = DEFAULT_ERROR;
79  			}
80  		}
81  	}
82  
83  	/*
84  	 * (non-Javadoc)
85  	 *
86  	 * @see com.cosylab.gui.displayers.ConditionDescriptor#toColor(com.cosylab.datatypes.Condition)
87  	 */
88  	public Color toColor(BitCondition condition)
89  	{
90  		Color c = null;
91  
92  		for (int i = 0; i < conditions.length; i++) {
93  			if (condition == conditions[i]) {
94  				c = colors[i];
95  			}
96  		}
97  
98  		return c;
99  	}
100 
101 	private void checkIfColorAllreadyUsed(BitCondition condition, Color color)
102 		throws IllegalArgumentException
103 	{
104 		for (int i = 0; i < conditions.length; i++) {
105 			if (colors[i] == color && condition != conditions[i]) {
106 				if (overRide == false) {
107 					throw new IllegalArgumentException();
108 				}
109 			}
110 		}
111 	}
112 
113 	/**
114 	 * Changes Conditions old Color to new Color , if new Color is allready
115 	 * used by other Condition then it throws IllegalArgumentException(). When
116 	 * changing colors , don't forget to update component...
117 	 *
118 	 * @param condition to change color
119 	 * @param color to what color
120 	 */
121 	public void putConditionToColor(BitCondition condition, Color color)
122 	{
123 		checkIfColorAllreadyUsed(condition, color);
124 
125 		for (int i = 0; i < conditions.length; i++) {
126 			if (condition == conditions[i]) {
127 				colors[i] = color;
128 			}
129 		}
130 	}
131 
132 	/**
133 	 * If in override mode , then color can be used on more then one Condition
134 	 * , else olny for one (if Color is allready used by other condition , it
135 	 * will throw IllegalArgumentException).
136 	 *
137 	 * @param value
138 	 */
139 	public void setOverRideOn(boolean value)
140 	{
141 		overRide = value;
142 	}
143 
144 	/**
145 	 * Tells if DefaultConditionDescriptor is in overRide mode
146 	 *
147 	 * @return
148 	 */
149 	public boolean isOverRideOn()
150 	{
151 		return overRide;
152 	}
153 
154 	/*
155 	 * (non-Javadoc)
156 	 *
157 	 * @see com.cosylab.gui.displayers.ConditionDescriptor#toCondition(java.awt.Color)
158 	 */
159 	public BitCondition toCondition(Color color)
160 	{
161 		BitCondition c = null;
162 
163 		for (int i = 0; i < conditions.length; i++) {
164 			if (color == colors[i]) {
165 				c = conditions[i];
166 			}
167 		}
168 
169 		return c;
170 	}
171 
172 	/**
173 	 * Clears all colors
174 	 */
175 	public void clear()
176 	{
177 		colors = null;
178 	}
179 }
180 
181 /* __oOo__ */