001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.io.File;
030:
031: import javax.swing.JButton;
032: import javax.swing.JPanel;
033: import javax.swing.JTextField;
034:
035: import org.beryl.gui.DialogUtils;
036: import org.beryl.gui.GUIException;
037: import org.beryl.gui.Widget;
038: import org.beryl.gui.model.MapChangeEvent;
039: import org.beryl.gui.model.MapDataModel;
040: import org.beryl.gui.model.ModelChangeEvent;
041: import org.beryl.gui.validators.ValidationException;
042:
043: public class FileField extends Widget {
044: private File file = null;
045: private String key = null;
046: private JTextField textField = null;
047: private JButton button = null;
048: private JPanel panel = null;
049: private boolean save = false;
050: private boolean sendEvents = true;
051: private boolean processEvents = true;
052: private String extension = "xml";
053:
054: public FileField(Widget parent, String name) throws GUIException {
055: super (parent, name);
056: textField = new JTextField();
057: button = new JButton("..");
058: panel = new JPanel();
059:
060: textField.setEditable(false);
061: panel.setLayout(new BorderLayout());
062: panel.add(textField, BorderLayout.CENTER);
063: panel.add(button, BorderLayout.EAST);
064: }
065:
066: public void validate() throws ValidationException, GUIException {
067: if (hasValidators()) {
068: try {
069: super .validate();
070: if (textField.getBackground() != TextField.DEFAULT_COLOR) {
071: textField.setBackground(TextField.DEFAULT_COLOR);
072: }
073: if (getParentWidget() instanceof LabeledWidget) {
074: ((LabeledWidget) getParentWidget()).clearError();
075: }
076: } catch (ValidationException e) {
077: textField.setBackground(TextField.ERROR_COLOR);
078: if (getParentWidget() instanceof LabeledWidget) {
079: ((LabeledWidget) getParentWidget()).setError(e);
080: }
081: throw e;
082: }
083: }
084: }
085:
086: private void reload() throws GUIException {
087: MapDataModel model = getDataModel();
088: if (model != null && key != null) {
089: try {
090: processEvents = false;
091: File value = (File) model.getValue(key);
092: if (value != null) {
093: textField.setText(value.getPath());
094: } else {
095: String text = textField.getText();
096: if (text != null && !text.equals("")) {
097: File file = new File(text);
098: this .file = file;
099: model.setValue(FileField.this , key, file);
100: }
101: }
102: } finally {
103: processEvents = true;
104: }
105: }
106: }
107:
108: public void setProperty(String name, Object value)
109: throws GUIException {
110: if (name.equals("enabled")) {
111: boolean enabled = ((Boolean) value).booleanValue();
112: textField.setEnabled(enabled);
113: button.setEnabled(enabled);
114: } else if ("key".equals(name)) {
115: key = (String) value;
116: } else if ("save".equals(name)) {
117: save = ((Boolean) value).booleanValue();
118: } else if ("extension".equals(name)) {
119: extension = (String) value;
120: } else {
121: super .setProperty(name, value);
122: }
123: }
124:
125: public void modelChanged(ModelChangeEvent e) throws GUIException {
126: if (processEvents) {
127: sendEvents = false;
128: try {
129: if (e.getSource() == this ) {
130: /* New data model */
131: reload();
132: try {
133: validate();
134: } catch (ValidationException ex) {
135: /* Ignore, error status is displayed already */
136: }
137: } else if (e instanceof MapChangeEvent) {
138: MapChangeEvent event = (MapChangeEvent) e;
139: if (event.getKey() == null) {
140: reload();
141: } else if (event.getKey().equals(key)) {
142: File file = (File) event.getNewValue();
143: this .file = file;
144: textField.setText(file.getPath());
145: }
146: try {
147: validate();
148: } catch (ValidationException ex) {
149: /* Ignore, error status is displayed already */
150: }
151: }
152: } finally {
153: sendEvents = true;
154: }
155: }
156: }
157:
158: public void finalizeConstruction() {
159: textField.setPreferredSize(new Dimension(MAX_WIDTH,
160: DEFAULT_HEIGHT));
161: button.setPreferredSize(new Dimension(
162: button.getPreferredSize().width, DEFAULT_HEIGHT));
163: button.addActionListener(new ActionListener() {
164: public void actionPerformed(ActionEvent e) {
165: File file = null;
166: if (save)
167: file = DialogUtils.showOpenFileDialog(
168: FileField.this , extension);
169: else
170: file = DialogUtils.showOpenFileDialog(
171: FileField.this , extension);
172: try {
173: if (file != null) {
174: MapDataModel model = getDataModel();
175: FileField.this .file = file;
176: textField.setText(file.getPath());
177:
178: if (model != null && key != null) {
179: try {
180: model.setValue(FileField.this , key,
181: file);
182: } finally {
183: processEvents = true;
184: }
185: }
186: try {
187: validate();
188: } catch (ValidationException ex) {
189: /* Ignore, error status is displayed already */
190: }
191: }
192: } catch (GUIException ex) {
193: throw new RuntimeException(ex);
194: }
195: }
196: });
197: }
198:
199: public Component getWidget() {
200: return panel;
201: }
202:
203: public Component getRealWidget() {
204: return textField;
205: }
206:
207: public File getFile() {
208: return file;
209: }
210: }
|