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.dialog;
042:
043: import org.zaval.awt.*;
044:
045: import java.io.File;
046: import java.awt.*;
047: import java.util.*;
048:
049: public class EditDialog extends Dialog implements
050: java.awt.event.AWTEventListener {
051: private EmulatedTextField edit;
052: private Button ok, cancel;
053: private boolean isApply;
054: private Component listener;
055: private IELabel label;
056:
057: public EditDialog(Frame f, String s, boolean b, Component l) {
058: super (f, s, b);
059: setLayout(new GridBagLayout());
060:
061: ok = new Button("Ok");
062: cancel = new Button("Cancel");
063: label = new IELabel("Name");
064:
065: Panel p = new Panel();
066: p.setLayout(new GridLayout(1, 2, 2, 0));
067: p.add(ok);
068: p.add(cancel);
069:
070: constrain(this , label, 0, 0, 1, 1, GridBagConstraints.NONE,
071: GridBagConstraints.WEST, 0.0, 0.0, 5, 5, 5, 5);
072: constrain(this , edit = new EmulatedTextField(20), 1, 0, 4, 1,
073: GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST,
074: 1.0, 0.0, 5, 5, 5, 5);
075: constrain(this , p, 0, 10, 2, 1, GridBagConstraints.NONE,
076: GridBagConstraints.EAST, 1.0, 0.0, 5, 5, 5, 5);
077:
078: listener = l;
079: edit.requestFocus();
080: }
081:
082: public void setText(String t) {
083: if (t == null)
084: edit.setText("");
085: else
086: edit.setText(t);
087: }
088:
089: public String getText() {
090: return edit.getText();
091: }
092:
093: public void setButtonsCaption(String o, String c) {
094: ok.setLabel(o);
095: cancel.setLabel(c);
096: }
097:
098: public void setLabelCaption(String l) {
099: label.setText(l);
100: }
101:
102: public boolean handleEvent(Event e) {
103: if (e.id == Event.WINDOW_DESTROY
104: || (e.target == cancel && e.id == Event.ACTION_EVENT)) {
105: isApply = false;
106: dispose();
107: }
108:
109: if (e.target == ok && e.id == Event.ACTION_EVENT) {
110: isApply = true;
111: listener.postEvent(new Event(this , e.ACTION_EVENT, edit
112: .getText()));
113: dispose();
114: }
115:
116: return super .handleEvent(e);
117: }
118:
119: public boolean isApply() {
120: return isApply;
121: }
122:
123: public void toCenter() {
124: Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
125: Dimension d = getSize();
126: move((s.width - d.width) / 2, (s.height - d.height) / 2);
127: }
128:
129: static public void constrain(Container c, Component p, int x,
130: int y, int w, int h, int f, int a, double wx, double wy,
131: int t, int l, int r, int b) {
132: GridBagConstraints cc = new GridBagConstraints();
133:
134: cc.gridx = x;
135: cc.gridy = y;
136: cc.gridwidth = w;
137: cc.gridheight = h;
138:
139: cc.fill = f;
140: cc.anchor = a;
141: cc.weightx = wx;
142: cc.weighty = wy;
143:
144: if (t + b + l + r > 0)
145: cc.insets = new Insets(t, l, b, r);
146: LayoutManager lm = c.getLayout();
147: if (lm instanceof GridBagLayout) {
148: GridBagLayout gbl = (GridBagLayout) lm;
149: gbl.setConstraints(p, cc);
150: } else if (lm instanceof ExGridLayout) {
151: ExGridLayout gbl = (ExGridLayout) lm;
152: gbl.setConstraints(p, cc);
153: }
154: c.add(p);
155: }
156:
157: static public void constrain(Container c, Component p, int x,
158: int y, int w, int h) {
159: constrain(c, p, x, y, w, h, GridBagConstraints.NONE,
160: GridBagConstraints.NORTHWEST, 1.0, 1.0, 0, 0, 5, 3);
161: }
162:
163: static public void constrain(Container c, Component p, int x,
164: int y, int w, int h, int f, int a) {
165: constrain(c, p, x, y, w, h, f, a, 1.0, 1.0, 0, 0, 5, 3);
166: }
167:
168: public boolean keyDown(Event e, int key) {
169: if (key == '\t')
170: return moveFocus();
171: else if ((e.target == ok && key == Event.ENTER)
172: || (e.target == edit && key == Event.ENTER)) {
173: isApply = true;
174: listener.postEvent(new Event(this , e.ACTION_EVENT, edit
175: .getText()));
176: dispose();
177: return true;
178: } else if (e.target == cancel && key == Event.ENTER) {
179: isApply = false;
180: dispose();
181: return true;
182: } else
183: return false;
184: }
185:
186: public boolean doModal() {
187: Toolkit.getDefaultToolkit().addAWTEventListener(this ,
188: AWTEvent.KEY_EVENT_MASK);
189: pack();
190: toCenter();
191: edit.requestFocus();
192: show();
193: Toolkit.getDefaultToolkit().removeAWTEventListener(this );
194: return isApply();
195: }
196:
197: private boolean moveFocus() {
198: Component owner = getFocusOwner();
199: return moveFocus(owner);
200: }
201:
202: private void optimize(Vector v) {
203: int j, k = v.size();
204: for (j = 0; j < k; ++j) {
205: Component c = (Component) v.elementAt(j);
206: if (c instanceof Button) {
207: v.removeElementAt(j);
208: v.addElement(c);
209: --k;
210: --j;
211: }
212: }
213: }
214:
215: private boolean moveFocus(Component c) {
216: int j, k;
217: boolean ask = false;
218: Vector v = new Vector();
219:
220: linearize(this , v);
221: optimize(v);
222: for (k = v.size(), j = 0; j < k; ++j)
223: if (c == v.elementAt(j))
224: break;
225: if (j >= k)
226: j = -1;
227: if (j == k - 1) {
228: ask = true;
229: j = -1; // see below +1
230: }
231: ((Component) v.elementAt(j + 1)).requestFocus();
232: return ask;
233: }
234:
235: private void linearize(Container cc, Vector v) {
236: int j, k = cc.getComponentCount();
237: for (j = 0; j < k; ++j) {
238: Component c = cc.getComponent(j);
239: if (c instanceof Label)
240: continue;
241: else if (c instanceof IELabel)
242: continue;
243: else if (c instanceof Container)
244: linearize((Container) c, v);
245: else
246: v.addElement(c);
247: }
248: }
249:
250: public void eventDispatched(AWTEvent event) {
251: if (event.getID() != java.awt.event.KeyEvent.KEY_TYPED)
252: return;
253: if (!(event instanceof java.awt.event.KeyEvent))
254: return;
255: java.awt.event.KeyEvent ke = (java.awt.event.KeyEvent) event;
256: if (ke.getKeyChar() != '\t')
257: return;
258: moveFocus();
259: }
260: }
|