001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * JXTranslator.java
021: *
022: * Created on June 8, 2007, 9:58 PM
023: *
024: */
025:
026: package org.openjx.core;
027:
028: import java.awt.Color;
029: import java.awt.Font;
030:
031: import javax.swing.border.TitledBorder;
032: import javax.swing.ListSelectionModel;
033: import javax.swing.SwingConstants;
034:
035: /**
036: * This class simply translates specific values between OpenJX and Java.
037: *
038: * @author Jared Spigner
039: */
040: public class JXTranslator {
041:
042: /**
043: * This the constructor for the JXTranslator class. It creates a new instance
044: * of JXTranslator.
045: */
046: public JXTranslator() {
047: }
048:
049: /**
050: * This method translates boolean values to strings.
051: *
052: * @param value is the boolean value we want to convert.
053: *
054: * @return the string representation of the boolean value.
055: */
056: public String BooleanJava2JX(boolean value) {
057: if (value == true)
058: return "true";
059: else
060: return "false";
061: }
062:
063: /**
064: * This method translates strings to boolean values.
065: *
066: * @param str is the string we want to convert.
067: *
068: * @return the boolean representation of the string value.
069: */
070: public boolean BooleanJX2Java(String str) {
071: if (str.equals("true"))
072: return true;
073: else
074: return false;
075: }
076:
077: /**
078: * This method translates border title alignment from JX to Java.
079: *
080: * @param align is the jx border alignment.
081: *
082: * @return the Java border equivalent alignment.
083: */
084: public int BorderTitleHAlignJX2Java(String align) {
085: if (align.equals("center")) {
086: return TitledBorder.CENTER;
087: } else if (align.equals("left")) {
088: return TitledBorder.LEFT;
089: } else if (align.equals("right")) {
090: return TitledBorder.RIGHT;
091: }
092:
093: return TitledBorder.LEFT;
094: }
095:
096: /**
097: * This method translates border title vertical alignment from JX to Java.
098: *
099: * @param align is the jx border alignment.
100: *
101: * @return the Java border equivalent alignment.
102: */
103: public int BorderTitleVAlignJX2Java(String align) {
104: if (align.equals("center")) {
105: return TitledBorder.CENTER;
106: } else if (align.equals("top")) {
107: return TitledBorder.TOP;
108: } else if (align.equals("bottom")) {
109: return TitledBorder.BOTTOM;
110: }
111:
112: return TitledBorder.TOP;
113: }
114:
115: /**
116: * This method simply translates a Java color to a hex string.
117: *
118: * @param color is the java color.
119: *
120: * @return the hex string equivalent.
121: */
122: public String ColorJava2JX(Color color) {
123: return "#"
124: + String.valueOf(Integer
125: .toHexString(color.getRGB() & 0x00ffffff));
126: }
127:
128: /**
129: * This method simply translates a hex string color into a Java color.
130: *
131: * @param color is a hex color string like #000000
132: *
133: * @return the java equivalent color.
134: */
135: public Color ColorJX2Java(String color) {
136: return Color.decode(color);
137: }
138:
139: /**
140: * This method simply translates a Java font style to a JX string
141: * representation.
142: *
143: * @param font is the font style we want to get the JX equivalent of.
144: *
145: * @return the JX equivalent font style.
146: */
147: public String FontStyleJava2JX(Font font) {
148: if (font.getStyle() == Font.PLAIN)
149: return "normal";
150: else if (font.getStyle() == Font.BOLD)
151: return "bold";
152: else if (font.getStyle() == Font.ITALIC)
153: return "italic";
154: else if (font.getStyle() == Font.BOLD + Font.ITALIC)
155: return "bold-italic";
156:
157: return "normal";
158: }
159:
160: /**
161: * This method simply translates a JX string representation of a font style
162: * into a Java font style in integer.
163: *
164: * @param style is an OpenJX font style.
165: *
166: * @return the Java equivalent font style.
167: */
168: public int FontStyleJX2Java(String style) {
169: int fmt = Font.PLAIN;
170: if (style.equals("normal"))
171: fmt = Font.PLAIN;
172: else if (style.equals("italic"))
173: fmt = Font.ITALIC;
174: else if (style.equals("bold"))
175: fmt = Font.BOLD;
176: else if (style.equals("bold-italic"))
177: fmt = Font.BOLD | Font.ITALIC;
178:
179: return fmt;
180: }
181:
182: /**
183: * This method translates horizontal alignment from Java to JX.
184: *
185: * @param align is the Java alignment.
186: *
187: * @return the JX equivalent alignment.
188: */
189: public String HAlignJava2JX(int align) {
190: if (align == SwingConstants.CENTER)
191: return "center";
192: else if (align == SwingConstants.LEFT)
193: return "left";
194: else if (align == SwingConstants.RIGHT)
195: return "right";
196: else if (align == SwingConstants.LEADING)
197: return "leading";
198: else if (align == SwingConstants.TRAILING)
199: return "trailing";
200:
201: return "center";
202: }
203:
204: /**
205: * This method translates horizontal alignment from JX to Java.
206: *
207: * @param align is the jx alignment.
208: *
209: * @return the Java equivalent alignment.
210: */
211: public int HAlignJX2Java(String align) {
212: if (align.equals("center"))
213: return SwingConstants.CENTER;
214: else if (align.equals("left"))
215: return SwingConstants.LEFT;
216: else if (align.equals("right"))
217: return SwingConstants.RIGHT;
218: else if (align.equals("leading"))
219: return SwingConstants.LEADING;
220: else if (align.equals("trailing"))
221: return SwingConstants.TRAILING;
222:
223: return SwingConstants.CENTER;
224: }
225:
226: /**
227: * This method translates the list selection model from JX to Java.
228: *
229: * @param attr is the JX list selection model.
230: *
231: * @return the Java list selection model.
232: */
233: public int listSelectionModelJX2Java(String attr) {
234: if (attr.equals("single"))
235: return ListSelectionModel.SINGLE_SELECTION;
236: else
237: return ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
238: }
239:
240: /**
241: * This method translates vertical alignment from Java to JX.
242: *
243: * @param align is the Java alignment.
244: *
245: * @return the JX equivalent alignment.
246: */
247: public String VAlignJava2JX(int align) {
248: if (align == SwingConstants.CENTER)
249: return "center";
250: else if (align == SwingConstants.TOP)
251: return "top";
252: else if (align == SwingConstants.BOTTOM)
253: return "bottom";
254:
255: return "center";
256: }
257:
258: /**
259: * This method translates vertical alignment from JX to Java.
260: *
261: * @param align is the jx alignment.
262: *
263: * @return the Java equivalent alignment.
264: */
265: public int VAlignJX2Java(String align) {
266: if (align.equals("center"))
267: return SwingConstants.CENTER;
268: else if (align.equals("top"))
269: return SwingConstants.TOP;
270: else if (align.equals("bottom"))
271: return SwingConstants.BOTTOM;
272:
273: return SwingConstants.CENTER;
274: }
275:
276: }
|