001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing;
020:
021: import java.awt.Color;
022: import java.awt.Dimension;
023: import java.awt.Font;
024: import java.awt.LayoutManager;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.StringTokenizer;
029:
030: import javax.swing.BoxLayout;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033:
034: /**
035: * Component for displaying labels of warning text of which some items
036: * of text can be highlighted.
037: *
038: * To highlight items of text in a string they should be surrounded with
039: * curly braces {} for example;
040: *
041: * <pre>
042: * "This is a string with two {items of highlightable} text in {it}."
043: * </pre>
044: *
045: * @author Matthew Large
046: * @version $Revision: 1.1 $
047: *
048: */
049: public class WarningsLabel extends JPanel {
050:
051: private ArrayList m_aLabels = new ArrayList(5);
052: private HashMap m_variables = new HashMap(3);
053:
054: /**
055: * Constructs a new warnings label component.
056: *
057: * @param sText Text to display
058: */
059: public WarningsLabel(String sText) {
060: super ();
061: setup(sText);
062: }
063:
064: /**
065: * @param arg0
066: */
067: private WarningsLabel(boolean arg0) {
068: super (arg0);
069: }
070:
071: /**
072: * @param arg0
073: */
074: private WarningsLabel(LayoutManager arg0) {
075: super (arg0);
076: }
077:
078: /**
079: * @param arg0
080: * @param arg1
081: */
082: private WarningsLabel(LayoutManager arg0, boolean arg1) {
083: super (arg0, arg1);
084: }
085:
086: /**
087: * Initialises the component
088: *
089: * @param sText Text to display
090: */
091: private void setup(String sText) {
092:
093: BoxLayout layout = new BoxLayout(this , BoxLayout.LINE_AXIS);
094: this .setLayout(layout);
095:
096: boolean bInVar = false;
097: StringTokenizer sTok = new StringTokenizer(sText, "{}", true);
098: while (sTok.hasMoreElements()) {
099: String sTemp = (String) sTok.nextElement();
100: if (sTemp.equals("{")) {
101: bInVar = true;
102: } else if (sTemp.equals("}")) {
103: bInVar = false;
104: } else {
105: JLabel label = new JLabel(sTemp);
106:
107: String fontName = "Dialog";
108: int fontSize = 11;
109: Font font = new Font(fontName, Font.PLAIN, fontSize);
110: label.setFont(font);
111:
112: this .m_aLabels.add(label);
113: if (bInVar) {
114: this .m_variables.put(sTemp, label);
115: }
116:
117: this .add(label);
118: }
119: }
120: }
121:
122: /**
123: * Sets whether an item of text is highlighted or not. Defaults to red.
124: *
125: * @param sVarText Text to set highlight on
126: * @param bHighLighted true if text is to be highlighted
127: */
128: public void setHighlight(String sVarText, boolean bHighLighted) {
129: this .setHighlight(sVarText, bHighLighted, Color.RED);
130: }
131:
132: /**
133: * Sets whether an item of text is highlighted or not and the color to
134: * highlight with.
135: *
136: * @param sVarText Text to set highlight on
137: * @param bHighLighted true if text is to be highlighted
138: * @param color Color to highlight with
139: */
140: public void setHighlight(String sVarText, boolean bHighLighted,
141: Color color) {
142: JLabel label = (JLabel) this .m_variables.get(sVarText);
143: if (label != null) {
144: if (bHighLighted) {
145: label.setForeground(color);
146: } else {
147: label.setForeground(Color.BLACK);
148: }
149: }
150: }
151:
152: /**
153: * Sets whether all items of highlightable text are highlighted
154: * or not. Defaults to red.
155: *
156: * @param bHighLighted true if all items of highlightable text are to be highlighted
157: */
158: public void setAllHighlights(boolean bHighLighted) {
159: this .setAllHighlights(bHighLighted, Color.RED);
160: }
161:
162: /**
163: * Sets whether all items of highlightable text are highlighted
164: * or not. Defaults to red.
165: *
166: * @param bHighLighted true if all items of highlightable text are to be highlighted
167: * @param color Color to highlight with
168: */
169: public void setAllHighlights(boolean bHighLighted, Color color) {
170: Iterator itor = this .m_variables.values().iterator();
171: while (itor.hasNext()) {
172: JLabel label = (JLabel) itor.next();
173: if (label != null) {
174: if (bHighLighted) {
175: label.setForeground(color);
176: } else {
177: label.setForeground(Color.BLACK);
178: }
179: }
180: }
181: }
182:
183: /* (non-Javadoc)
184: * @see java.awt.Component#getPreferredSize()
185: */
186: public Dimension getPreferredSize() {
187: int nX = 0;
188: int nY = 0;
189:
190: Iterator itor = this .m_aLabels.iterator();
191: while (itor.hasNext()) {
192: JLabel label = (JLabel) itor.next();
193: Dimension dim = label.getPreferredSize();
194: nX = nX + dim.width;
195: nY = nY + dim.height;
196: }
197: return new Dimension(nX, 13);
198: }
199:
200: /* (non-Javadoc)
201: * @see java.awt.Component#setFont(java.awt.Font)
202: */
203: public void setFont(Font font) {
204: super .setFont(font);
205: if (this .m_aLabels != null) {
206: Iterator itor = this .m_aLabels.iterator();
207: while (itor.hasNext()) {
208: JLabel label = (JLabel) itor.next();
209: label.setFont(font);
210: }
211: }
212: }
213:
214: }
|