001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.util;
027:
028: import java.awt.*;
029:
030: import java.util.*;
031:
032: /**
033: *
034: *
035: * @author $author$
036: * @version $Revision: 1.14 $
037: */
038: public class PropertyUtil {
039: /**
040: *
041: *
042: * @param number
043: * @param defaultValue
044: *
045: * @return
046: */
047: public static int stringToInt(String number, int defaultValue) {
048: try {
049: return (number == null) ? defaultValue : Integer
050: .parseInt(number);
051: } catch (NumberFormatException nfe) {
052: return defaultValue;
053: }
054: }
055:
056: /**
057: *
058: *
059: * @param color
060: *
061: * @return
062: */
063: public static String colorToString(Color color) {
064: StringBuffer buf = new StringBuffer();
065: buf.append('#');
066: buf.append(numberToPaddedHexString(color.getRed(), 2));
067: buf.append(numberToPaddedHexString(color.getGreen(), 2));
068: buf.append(numberToPaddedHexString(color.getBlue(), 2));
069:
070: return buf.toString();
071: }
072:
073: /**
074: *
075: *
076: * @param font
077: *
078: * @return
079: */
080: public static String fontToString(Font font) {
081: StringBuffer b = new StringBuffer(font.getName());
082: b.append(",");
083: b.append(font.getStyle());
084: b.append(",");
085: b.append(font.getSize());
086:
087: return b.toString();
088: }
089:
090: /**
091: *
092: *
093: * @param fontString
094: *
095: * @return
096: */
097: public static Font stringToFont(String fontString) {
098: StringTokenizer st = new StringTokenizer(fontString, ",");
099:
100: try {
101: return new Font(st.nextToken(), Integer.parseInt(st
102: .nextToken()), Integer.parseInt(st.nextToken()));
103: } catch (Exception e) {
104: return null;
105: }
106: }
107:
108: /**
109: *
110: *
111: * @param s
112: *
113: * @return
114: *
115: * @throws IllegalArgumentException
116: */
117: public static Color stringToColor(String s) {
118: try {
119: return new Color(Integer.decode("0x" + s.substring(1, 3))
120: .intValue(), Integer.decode(
121: "0x" + s.substring(3, 5)).intValue(), Integer
122: .decode("0x" + s.substring(5, 7)).intValue());
123: } catch (Exception e) {
124: throw new IllegalArgumentException(
125: "Bad color string format. Should be #rrggbb ");
126: }
127: }
128:
129: /**
130: *
131: *
132: * @param number
133: * @param size
134: *
135: * @return
136: *
137: * @throws IllegalArgumentException
138: */
139: public static String numberToPaddedHexString(int number, int size) {
140: String s = Integer.toHexString(number);
141:
142: if (s.length() > size) {
143: throw new IllegalArgumentException(
144: "Number too big for padded hex string");
145: }
146:
147: StringBuffer buf = new StringBuffer();
148:
149: for (int i = 0; i < (size - s.length()); i++) {
150: buf.append('0');
151: }
152:
153: buf.append(s);
154:
155: return buf.toString();
156: }
157: }
|