001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG 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
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020: package net.sf.borg.common;
021:
022: import java.io.InputStream;
023: import java.util.MissingResourceException;
024: import java.util.Properties;
025: import java.util.ResourceBundle;
026:
027: import javax.swing.KeyStroke;
028:
029: public class Resource {
030: private static String version_ = null;
031:
032: public static String getResourceString(String key) {
033: try {
034: String res = ResourceBundle.getBundle(
035: "resource/borg_resource").getString(key);
036:
037: if (res.indexOf("\\n") == -1)
038: return (res);
039:
040: StringBuffer sb = new StringBuffer();
041: for (int i = 0; i < res.length(); i++) {
042:
043: if (res.charAt(i) == '\\' && (i < res.length() - 1)
044: && res.charAt(i + 1) == 'n') {
045: i++;
046: sb.append('\n');
047: } else {
048: sb.append(res.charAt(i));
049: }
050: }
051:
052: //System.out.println(res);
053: //System.out.println( sb.toString());
054: return (sb.toString());
055: } catch (MissingResourceException m) {
056: return ("??" + key + "??");
057: }
058: }
059:
060: public static String getPlainResourceString(String resourceKey) {
061: ComponentParms parms = parseParms(resourceKey);
062: return parms.getText();
063: }
064:
065: public static String getVersion() {
066: if (version_ == null) {
067: try {
068: // get the version and build info from a properties file in the jar file
069: InputStream is = Resource.class.getResource(
070: "/properties").openStream();
071: Properties props = new Properties();
072: props.load(is);
073: is.close();
074: version_ = props.getProperty("borg.version");
075: } catch (Exception e) {
076: Errmsg.errmsg(e);
077: }
078: }
079:
080: return (version_);
081: }
082:
083: public static ComponentParms parseParms(String resourceKey) {
084: String parmsText = getResourceString(resourceKey);
085:
086: if (parmsText.startsWith("Goto"))
087: parmsText = parmsText.substring(0);
088:
089: String text = parmsText;
090: int mnemonic = -1;
091: KeyStroke accel = null;
092: int pos;
093: if ((pos = parmsText.indexOf('|')) != -1) {
094: text = parmsText.substring(0, pos);
095: String parmsTextRem = parmsText.substring(pos + 1);
096: String mnemonicText = parmsTextRem;
097:
098: if ((pos = parmsTextRem.indexOf('|')) != -1) {
099: mnemonicText = parmsTextRem.substring(0, pos);
100: String accelText = parmsTextRem.substring(pos + 1);
101: accel = KeyStroke.getKeyStroke(accelText);
102: }
103:
104: if (mnemonicText.length() > 0)
105: mnemonic = KeyStroke.getKeyStroke(mnemonicText)
106: .getKeyCode();
107: }
108: return new ComponentParms(text, mnemonic, accel);
109: }
110:
111: ////////////////////////////////////////////////////////////////
112: // nested class ComponentParms
113:
114: public static class ComponentParms {
115: public final int getKeyEvent() {
116: return keyEvent;
117: }
118:
119: public final KeyStroke getKeyStroke() {
120: return keyStroke;
121: }
122:
123: public final String getText() {
124: return text;
125: }
126:
127: // "internal" //
128: public ComponentParms(String text, int keyEvent,
129: KeyStroke keyStroke) {
130: this .text = text;
131: this .keyEvent = keyEvent;
132: this .keyStroke = keyStroke;
133: }
134:
135: // private //
136: private String text;
137: private int keyEvent;
138: private KeyStroke keyStroke;
139: }
140:
141: // end nested class ComponentParms
142: ////////////////////////////////////////////////////////////////
143:
144: }
|