001: package com.xoetrope.carousel.langed;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStreamReader;
006: import java.net.URL;
007: import java.sql.Connection;
008: import java.sql.DriverManager;
009: import java.sql.ResultSet;
010: import java.sql.Statement;
011: import java.util.StringTokenizer;
012: import java.util.Vector;
013:
014: import java.awt.Frame;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import javax.swing.JButton;
018: import javax.swing.JList;
019:
020: /**
021: * LangManager
022: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
023: * the GNU Public License (GPL), please see license.txt for more details. If
024: * you make commercial use of this software you must purchase a commercial
025: * license from Xoetrope.</p>
026: * <p> $Revision: 1.7 $</p>
027: */
028: public class LangManager {
029: public LangManager(boolean _bIsStandalone) {
030: bIsStandalone = _bIsStandalone;
031: }
032:
033: /*
034: * @deprecated uses an old file format
035: public void init( URL _url, String defLang ) throws IOException
036: {
037: url = _url;
038: langList = new Vector( 5 );
039: subStrings = new Language();
040:
041: BufferedReader is;
042: is = new BufferedReader( new InputStreamReader( _url.openStream()));
043:
044: String s = is.readLine();
045: StringTokenizer token = new StringTokenizer( s, "|" );
046: int numLangs = Integer.parseInt( token.nextToken());
047:
048: langList.ensureCapacity( numLangs );
049: for ( int i = 0; i < numLangs; i++ ) {
050: s = is.readLine();
051: token = new StringTokenizer( s, "|" );
052: LangName ln = new LangName();
053: ln.id = Integer.parseInt( token.nextToken());
054: ln.code = token.nextToken();
055: ln.name = token.nextToken();
056:
057: langList.addElement( ln );
058: }
059:
060: read( defLang.length() > 0 ? defLang : ((LangName)langList.elementAt( 0 )).code );
061: }
062: */
063:
064: /**
065: * Loads a language and sets it as the default language.
066: * This function is intended primarily for internal use by the
067: * catalogue viewer.
068: * @param langName the code name for the language.
069: */
070: /*
071: * @deprecated uses an old file format
072: public void changeLang( String langName )
073: {
074: int items = langList.size();
075: for ( int i = 0; i < items; i++ ) {
076: if ( ((LangName)langList.elementAt( i )).name.compareTo( langName ) == 0 ) {
077: Language backup = currentLang;
078: try {
079: String langCode = ((LangName)langList.elementAt( i )).code;
080: read( langCode );
081: }
082: catch ( IOException e ) {
083: currentLang = backup;
084: }
085: break;
086: }
087: }
088: }
089: */
090:
091: /**
092: * @deprecated uses an old file format
093: public void read( String code ) throws IOException
094: {
095: currentLang = new Language();
096:
097: String file = url.toString();
098: String fs = System.getProperty( "file.separator" );
099: int index = file.lastIndexOf( fs );
100: if ( index == -1 ) {
101: index = file.lastIndexOf( "/" );
102: if ( index == -1 )
103: index = file.lastIndexOf( "\\" );
104: }
105: file = file.substring( 0, index + 1 );
106: code += ".jln";
107: BufferedReader is;
108: //file += "lang/";
109: file += code;
110: URL url2 = new URL( file );
111: is = new BufferedReader( new InputStreamReader( url2.openStream()));
112:
113: currentLang.read( is, false );
114: is.close();
115:
116: // Read the substrings
117: if ( index == -1 ) {
118: index = file.lastIndexOf( "/" );
119: if ( index == -1 )
120: index = file.lastIndexOf( "\\" );
121: }
122: file = file.substring( 0, index + 1 );
123: file += "sub.jln";
124: URL url3 = new URL( file );
125: is = new BufferedReader( new InputStreamReader( url3.openStream()));
126: subStrings.read( is, true );
127: is.close();
128: }*/
129:
130: public void read(String driver, String url, String userName,
131: String password, String code, String tableName,
132: boolean doSubstitutions) {
133: try {
134: currentLang = new Language();
135:
136: // Try to load the driver.
137: Class.forName(driver.trim());
138:
139: Connection conn = DriverManager.getConnection(url,
140: userName, password);
141: Statement stmt = conn.createStatement();
142: ResultSet rs = stmt.executeQuery("SELECT ID, " + code
143: + " FROM " + tableName);
144:
145: while (rs.next()) {
146: LangItem li = new LangItem(doSubstitutions);
147: li.id = rs.getInt(1);
148: li.langStr = rs.getString(2);
149: currentLang.langStrings.put(li, li);
150: }
151: } catch (Exception e) {
152: e.printStackTrace();
153: }
154: }
155:
156: /**
157: * Gets a string from the language.
158: * @param id the ID of the requird string.
159: * @return the requested string complete with substitutions or else "" if not found.
160: */
161: public String getString(int id) {
162: if (currentLang != null)
163: return currentLang.getString(id);
164: return "";
165: }
166:
167: /**
168: * Gets the number of strings in the current language.
169: */
170: public int getNumStrings() {
171: if (currentLang != null)
172: return currentLang.getNumStrings();
173: return 0;
174: }
175:
176: /**
177: * Gets a string from the language, calls getString.
178: * @param id the ID of the required string.
179: * @return the requested string complete with substitutions or else "" if not found.
180: */
181: public String getSubString(int id) {
182: if (currentLang != null)
183: return currentLang.getString(id);
184: return "";
185: }
186:
187: /**
188: * Gets a string from the language, calls getString.
189: * @param subString the substitution string.
190: * @return the requested string from the current language.
191: */
192: public String getSubString(String substitution) {
193: if (currentLang != null) {
194: int idx = substitution.indexOf('@');
195: int endIdx = 0;
196: int lastIdx = 0;
197: if (idx > -1) {
198: String keyStr;
199: endIdx = substitution.indexOf('@', idx + 1);
200: lastIdx = substitution.length() - 1;
201: if ((idx == 0) && (endIdx == lastIdx))
202: keyStr = substitution;
203: else
204: keyStr = substitution.substring(idx, endIdx + 1);
205:
206: int subStrId = subStrings.findString(keyStr);
207: if (subStrId >= 0) {
208: String retStr = currentLang.getString(subStrId);
209: if (retStr.length() != 0) {
210: if ((idx == 0) && (endIdx == lastIdx))
211: return retStr;
212: else {
213: String x = substitution.substring(0, idx);
214: x += retStr;
215: if (endIdx < lastIdx)
216: x += substitution.substring(endIdx + 1);
217: return x;
218: }
219: }
220: }
221: }
222: /* 04/10/2001 while(
223: int subStrId = subStrings.FindString( subString );
224: if ( subStrId >= 0 ) {
225: String retStr = currentLang.GetString( subStrId );
226: if ( retStr.length() != 0 )
227: return retStr;
228: }*/
229: }
230: return substitution;
231: }
232:
233: /*
234: * @deprecated uses an old file format
235: public void pickLang()
236: {
237: LangPicker lp = new LangPicker( this, langList );
238: lp.init();
239: }
240: */
241:
242: public String getLangName() {
243: if (langName != null)
244: return langName;
245: int items = langList.size();
246: if (items > 0)
247: return ((LangName) langList.elementAt(0)).name;
248:
249: return "";
250: }
251:
252: private Language currentLang;
253: private Language subStrings;
254: private Vector langList;
255: private String langName;
256: private boolean bIsStandalone;
257: private URL url;
258: }
259:
260: class LangName {
261: public int id;
262: public String code;
263: public String name;
264:
265: public LangName() {
266: }
267:
268: public String toString() {
269: return name;
270: }
271: }
272:
273: /* class LangPicker extends Frame
274: * @deprecated uses an old file format
275: {
276: public LangPicker( LangManager _langMgr, Vector v )
277: {
278: setLayout( null );
279: langMgr = _langMgr;
280: elementList = v;
281:
282: okBtn = new JButton( "OK" );
283: okBtn.addActionListener( new ActionListener() {
284: public void actionPerformed( ActionEvent e )
285: {
286: ChangeLang();
287: dispose();
288: }
289: });
290:
291: cancelBtn = new JButton( "Cancel" );
292: cancelBtn.addActionListener( new ActionListener() {
293: public void actionPerformed( ActionEvent e )
294: {
295: dispose();
296: }
297: });
298:
299: langListbox = new JList( elementList );
300:
301: add( okBtn );
302: add( cancelBtn );
303: add( langListbox );
304:
305: langListbox.setSelectedIndex( 0 );
306:
307: okBtn.setBounds( 110, 140, 100, 30 );
308: cancelBtn.setBounds( 10, 140, 100, 30 );
309:
310: setSize( 220, 180 );
311: setVisible( true );
312: }
313:
314:
315: public void init()
316: {
317: langListbox.setBounds( 10, 30, 200, 100 );
318: }
319:
320: public void ChangeLang()
321: {
322: String str = ((LangName)elementList.elementAt( langListbox.getSelectedIndex())).name;
323: langMgr.changeLang( str );
324: }
325:
326: private JButton okBtn;
327: private JButton cancelBtn;
328: private JList langListbox;
329: private Vector elementList;
330: LangManager langMgr;
331: }*/
|