001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.util.converter;
018:
019: import java.awt.Dimension;
020: import java.awt.Font;
021: import java.awt.Insets;
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.util.StringTokenizer;
025:
026: import javax.swing.plaf.DimensionUIResource;
027: import javax.swing.plaf.FontUIResource;
028: import javax.swing.plaf.InsetsUIResource;
029:
030: /**
031: * AWTConverters. <br>Converter commonly used AWT classes like Point,
032: * Dimension, Rectangle, Insets to/from Strings and between each others when
033: * possible.
034: *
035: * The following convertions are supported:
036: *
037: * <table>
038: * <tr>
039: * <th>From</th>
040: * <th>To</th>
041: * <th>Reverse</th>
042: * </tr>
043: * <tr>
044: * <td>Dimension</td>
045: * <td>String</td>
046: * <td>yes</td>
047: * </tr>
048: * <tr>
049: * <td>Font</td>
050: * <td>String</td>
051: * <td>no</td>
052: * </tr>
053: * <tr>
054: * <td>Insets</td>
055: * <td>String</td>
056: * <td>yes</td>
057: * </tr>
058: * <tr>
059: * <td>Point</td>
060: * <td>String</td>
061: * <td>yes</td>
062: * </tr>
063: * <tr>
064: * <td>Rectangle</td>
065: * <td>String</td>
066: * <td>yes</td>
067: * </tr>
068: * </table>
069: */
070: public class AWTConverters implements Converter {
071:
072: public AWTConverters() {
073: super ();
074: }
075:
076: public void register(ConverterRegistry registry) {
077: registry.addConverter(Dimension.class, String.class, this );
078: registry.addConverter(String.class, Dimension.class, this );
079: registry.addConverter(DimensionUIResource.class, String.class,
080: this );
081:
082: registry.addConverter(Insets.class, String.class, this );
083: registry.addConverter(String.class, Insets.class, this );
084: registry.addConverter(InsetsUIResource.class, String.class,
085: this );
086:
087: registry.addConverter(Point.class, String.class, this );
088: registry.addConverter(String.class, Point.class, this );
089:
090: registry.addConverter(Rectangle.class, String.class, this );
091: registry.addConverter(String.class, Rectangle.class, this );
092:
093: registry.addConverter(Font.class, String.class, this );
094: registry.addConverter(FontUIResource.class, String.class, this );
095: }
096:
097: public Object convert(Class type, Object value) {
098: if (String.class.equals(type)) {
099: if (value instanceof Rectangle) {
100: return ((Rectangle) value).getX() + " "
101: + ((Rectangle) value).getY() + " "
102: + ((Rectangle) value).getWidth() + " "
103: + ((Rectangle) value).getHeight();
104: } else if (value instanceof Insets) {
105: return ((Insets) value).top + " "
106: + ((Insets) value).left + " "
107: + ((Insets) value).bottom + " "
108: + ((Insets) value).right;
109: } else if (value instanceof Dimension) {
110: return ((Dimension) value).getWidth() + " x "
111: + ((Dimension) value).getHeight();
112: } else if (Point.class.equals(value.getClass())) {
113: return ((Point) value).getX() + " "
114: + ((Point) value).getY();
115: } else if (value instanceof Font) {
116: return ((Font) value).getFontName() + ", "
117: + ((Font) value).getStyle() + ", "
118: + ((Font) value).getSize();
119: }
120: }
121:
122: if (value instanceof String) {
123: if (Rectangle.class.equals(type)) {
124: double[] values = convert((String) value, 4, " ");
125: if (values == null) {
126: throw new IllegalArgumentException("Invalid format");
127: }
128: Rectangle rect = new Rectangle();
129: rect.setFrame(values[0], values[1], values[2],
130: values[3]);
131: return rect;
132: } else if (Insets.class.equals(type)) {
133: double[] values = convert((String) value, 4, " ");
134: if (values == null) {
135: throw new IllegalArgumentException("Invalid format");
136: }
137: return new Insets((int) values[0], (int) values[1],
138: (int) values[2], (int) values[3]);
139: } else if (Dimension.class.equals(type)) {
140: double[] values = convert((String) value, 2, "x");
141: if (values == null) {
142: throw new IllegalArgumentException("Invalid format");
143: }
144: Dimension dim = new Dimension();
145: dim.setSize(values[0], values[1]);
146: return dim;
147: } else if (Point.class.equals(type)) {
148: double[] values = convert((String) value, 2, " ");
149: if (values == null) {
150: throw new IllegalArgumentException("Invalid format");
151: }
152: Point p = new Point();
153: p.setLocation(values[0], values[1]);
154: return p;
155: }
156: }
157: return null;
158: }
159:
160: private double[] convert(String text, int tokenCount,
161: String delimiters) {
162: StringTokenizer tokenizer = new StringTokenizer(text,
163: delimiters);
164: if (tokenizer.countTokens() != tokenCount) {
165: return null;
166: }
167:
168: try {
169: double[] values = new double[tokenCount];
170: for (int i = 0; tokenizer.hasMoreTokens(); i++) {
171: values[i] = Double.parseDouble(tokenizer.nextToken());
172: }
173: return values;
174: } catch (Exception e) {
175: return null;
176: }
177: }
178:
179: }
|