01: package com.xoetrope.carousel.langed;
02:
03: import javax.swing.AbstractListModel;
04: import javax.swing.ComboBoxModel;
05:
06: /**
07: * A wrapper for the Swing list model
08: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
09: * the GNU Public License (GPL), please see license.txt for more details. If
10: * you make commercial use of this software you must purchase a commercial
11: * license from Xoetrope.</p>
12: * <p> $Revision: 1.5 $</p>
13: */
14: public class LangListModel extends AbstractListModel implements
15: ComboBoxModel {
16: private int selectedItem = 0;
17: private EdLanguage lang;
18: private LangItem emptyLangItem = new LangItem("empty string", -1);
19: private boolean showIds = false;
20: private boolean showKeys = false;
21:
22: public LangListModel(EdLanguage l, boolean showIds, boolean showKeys) {
23: lang = l;
24: this .showIds = showIds;
25: this .showKeys = showKeys;
26: }
27:
28: public int getSize() {
29: return lang == null ? 1 : lang.getSize();
30: }
31:
32: public Object getElementAt(int index) {
33: if (lang == null)
34: return new String((showIds ? "0|" : "")
35: + "No Language Selected");
36: LangItem li = (LangItem) lang.getElementAt(index);
37: String idTxt = Integer.toString(li.id) + "|";
38: if (showIds) {
39: if (showKeys && (li.keyStr != null))
40: return idTxt + li.keyStr;
41: else
42: return idTxt + li.langStr;
43: } else
44: return li.toString();
45: }
46:
47: public Object getSelectedItem() {
48: return lang == null ? emptyLangItem : new LangItem(lang
49: .getString(selectedItem), selectedItem);
50: }
51:
52: public void setSelectedItem(Object anItem) {
53: selectedItem = ((LangItem) anItem).id;
54: }
55:
56: public void setSelectedItem(int anItem) {
57: selectedItem = anItem;
58: }
59:
60: public String getString(int id) {
61: return lang == null ? "empty string" : lang.getString(id);
62: }
63:
64: public String getStringAt(int idx) {
65: return lang == null ? "empty string" : lang.getStringAt(idx);
66: }
67:
68: public int getStringId(int idx) {
69: return lang == null ? 0 : lang.getStringId(idx);
70: }
71:
72: public int addString(String keyString, String newString) {
73: if (lang == null)
74: return -1;
75:
76: int numItems = lang.getSize();
77: int maxId = 0;
78: for (int iItem = 0; iItem < numItems; iItem++) {
79: int srcId = lang.getStringId(iItem);
80: if (srcId > maxId)
81: maxId = srcId;
82: }
83: lang.addString(++maxId, keyString, newString);
84: return maxId;
85: }
86: }
|