001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.awt.*;
044: import java.util.Vector;
045:
046: public class IEChoice extends Panel {
047: private int wx = 0, wy = 0;
048: private int x = 0, y = 0;
049: private Choice ch;
050: private boolean fakeAdded = false;
051:
052: private Vector ids;
053: private Vector items;
054: private String lastval = null;
055:
056: public Vector getI1() {
057: return items;
058: }
059:
060: public Vector getI2() {
061: return ids;
062: }
063:
064: public void setItems(Vector items, Vector ids) {
065: int i, k = ids.size();
066: this .ids = ids;
067: this .items = items;
068:
069: // System.err.println("+++ " + ids);
070:
071: Choice c = new Choice();
072: for (i = 0; i < k; ++i)
073: c.addItem((String) items.elementAt(i));
074: for (; i < 2; ++i)
075: c.addItem(" ");
076: setChoice(c);
077: }
078:
079: public void select(String value) {
080: // System.err.println("+++ set " + value + " " + ids);
081: if (value == null)
082: value = lastval;
083: if (value == null)
084: return;
085: select(ids.indexOf(lastval = value));
086: }
087:
088: public String getValue() {
089: if (lastval == null) {
090: try {
091: int x = ch.getSelectedIndex();
092: String s = (String) ids.elementAt(x);
093: return lastval = s;
094: } catch (Exception e) {
095: return null;
096: }
097: }
098: // System.err.println("+++ get " + lastval);
099: return lastval;
100: }
101:
102: public IEChoice(Choice c) {
103: super ();
104: ch = c;
105: setLayout(new FitLayout(0, 0, 0, 0));
106: add(this .ch);
107: }
108:
109: public void resize(int wx, int wy) {
110: Dimension d = ch.preferredSize();
111: if (d.height == 0) {
112: Font font = getFont();
113: if (font == null)
114: font = getParent().getFont();
115: if (font != null) {
116: FontMetrics fm = getFontMetrics(font);
117: if (fm == null)
118: fm = Toolkit.getDefaultToolkit().getFontMetrics(
119: font);
120: d.height = fm.getHeight() + 10;
121: } else
122: d.height = wy;
123: }
124: super .resize(wx, d.height);
125: }
126:
127: public Choice getChoice() {
128: return ch;
129: }
130:
131: private void setChoice(Choice x) {
132: String osName = System.getProperty("os.name");
133: boolean solaris = (osName != null && (osName
134: .equalsIgnoreCase("Solaris") || osName
135: .equalsIgnoreCase("Linux")));
136:
137: Color c1 = ch.getBackground();
138: Color c2 = ch.getForeground();
139: Font f = ch.getFont();
140:
141: Container parent = getParent();
142: if (solaris && parent != null)
143: parent.remove(this );
144: removeAll();
145: add(x);
146:
147: x.setBackground(c1);
148: x.setForeground(c2);
149: x.setFont(f);
150: if (isEnabled())
151: x.enable();
152: else
153: x.disable();
154:
155: this .ch = x;
156: if (solaris && parent != null) {
157: parent.add(this );
158: addNotify();
159: }
160:
161: ((Component) this ).invalidate();
162: ((Component) this ).validate();
163: this .ch.requestFocus();
164: }
165:
166: public void enable() {
167: ch.enable();
168: super .enable();
169: }
170:
171: public void disable() {
172: ch.disable();
173: super .disable();
174: }
175:
176: private final static int acceptE[] = { Event.LIST_SELECT,
177: Event.ACTION_EVENT };
178:
179: public boolean handleEvent(Event e) {
180: // if(e.id==e.GOT_FOCUS) return true;
181: // if(e.id==e.LOST_FOCUS && e.target!=ch) return false;
182: if (e.target != this && e.target != ch)
183: return super .handleEvent(e);
184: // if (e.id!=e.MOUSE_MOVE && e.id!=e.MOUSE_ENTER && e.id!=e.MOUSE_EXIT)
185: // System.err.println(e);
186: if (isSelectionEvent(e)) {
187: try {
188: String s = (String) ids
189: .elementAt(ch.getSelectedIndex());
190: sendSelectionEvent(s);
191: } catch (Exception eflt) {
192: }
193: return true;
194: }
195: // if (e.target == ch) e.target = this;
196: return super .handleEvent(e);
197: }
198:
199: public boolean keyDown(Event e, int key) {
200: char c = (char) key;
201: if (Character.isLetter(c)) {
202: c = Character.toLowerCase(c);
203: int size = ch.countItems();
204: for (int i = 0; i < size; i++) {
205: StringBuffer item = new StringBuffer(ch.getItem(i));
206: if (Character.toLowerCase(item.charAt(0)) == c) {
207: if (ch.getSelectedIndex() != i)
208: ch.select(i);
209: return true;
210: }
211: }
212: }
213:
214: if (key == (char) 0x1B) {
215: try {
216: ch.select(ids.indexOf(lastval));
217: } catch (Exception eee) {
218: }
219: return false;
220: }
221:
222: if (key == '\n' || key == '\t') {
223: try {
224: String s = (String) ids
225: .elementAt(ch.getSelectedIndex());
226: if (s.equals(lastval))
227: return false;
228: sendSelectionEvent(s);
229: } catch (Exception eee) {
230: }
231: return false;
232: }
233:
234: if (key == Event.UP) {
235: int z = ch.getSelectedIndex() - 1;
236: if (z < 0)
237: return true;
238: ch.select(z);
239: return true;
240: }
241:
242: if (key == Event.DOWN) {
243: int z = ch.getSelectedIndex() + 1;
244: if (z >= ch.countItems())
245: return true;
246: ch.select(z);
247: return true;
248: }
249:
250: return super .keyDown(e, key);
251: }
252:
253: public void requestFocus() {
254: ch.requestFocus();
255: }
256:
257: public boolean lostFocus(Event e, Object o) {
258: if (e.target == this .ch && ids != null && lastval != null) {
259: try {
260: String s = (String) ids
261: .elementAt(ch.getSelectedIndex());
262: if (lastval.equals(s))
263: return true;
264: sendSelectionEvent(s);
265: } catch (Exception eee) {
266: }
267: return false;
268: } else if (e.target == this )
269: return true;
270: // return true;
271: return false;
272: }
273:
274: public boolean gotFocus(Event e, Object o) {
275: if (ids != null)
276: return false;
277: return true;
278: }
279:
280: private boolean isSelectionEvent(Event e) {
281: if (e.target == ch) // && e.arg instanceof String)
282: for (int i = 0; i < acceptE.length; ++i)
283: if (acceptE[i] == e.id)
284: return true;
285: return false;
286: }
287:
288: private void sendSelectionEvent(String s) {
289: if (!s.equals(lastval)) {
290: // System.err.println("+++ sse " + s + "," + lastval);
291: lastval = s;
292: getParent().postEvent(
293: new Event(this , Event.ACTION_EVENT, s));
294: }
295: // fakeFix();
296: }
297:
298: private void fakeFix() {
299: if (fakeAdded) {
300: Choice x = new Choice();
301: Choice v = ch;
302: int j, k = v.countItems() - 1;
303: for (j = 0; j < k; ++j) {
304: String s = v.getItem(j);
305: if (j == k - 1 && s.trim().length() == 0)
306: break;
307: x.addItem(s);
308: }
309: fakeAdded = false;
310: setChoice(x);
311: select(lastval);
312: }
313: }
314:
315: public void addNotify() {
316: try {
317: super .addNotify();
318: } catch (Exception efck) {
319: // System.err.println("INI Error: " + efck + " for " + getChoice());
320: // efck.printStackTrace();
321: }
322: }
323:
324: public void select(int i) {
325: // System.err.println("+++ set " + i);
326: if (i < 0 && !checkIeHack()) {
327: Choice x = getChoice();
328: int j = 0, k = x.countItems();
329: for (j = 0; j < k; ++j)
330: if (x.getItem(j).trim().length() == 0) {
331: x.select(j);
332: return;
333: }
334: x.addItem(" ");
335: x.select(" ");
336: fakeAdded = true;
337: return;
338: }
339: fakeFix();
340: try {
341: getChoice().select(i);
342: } catch (Exception efck) {
343: System.err.println("SEL Error: " + efck + " for "
344: + getChoice());
345: efck.printStackTrace();
346: }
347: }
348:
349: private static boolean checkIeHack() {
350: String jver = "1.0.2.";
351: String jven = "Sun";
352: try {
353: jver = System.getProperty("java.version");
354: } catch (Throwable t) {
355: }
356: try {
357: jven = System.getProperty("java.vendor");
358: } catch (Throwable t) {
359: }
360:
361: if (!jver.startsWith("1.0") && jven.startsWith("Microsoft"))
362: return true;
363: return false;
364: }
365: }
|