Coverage details for net.sourceforge.demetrix.properties.swixml.FilePropertySwixMLRepresentation

LineHitsSource
1 /*******************************************************************************
2  * Demetrix process modelling system
3  *
4  * Copyright (c) 2003, 2004 Dimitri A. Pissarenko
5  *
6  * This file is part of Demetrix.
7  *
8  * Demetrix is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * Demetrix is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Demetrix; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * For further information you may
23  *
24  * - send an e-mail in Russian, German or English to dimitri.pissarenko@gmx.net
25  * - look at http://sourceforge.net/projects/demetrix/
26  * - look at http://demetrix.sourceforge.net/
27  * - look at http://members.inode.at/d.pissarenko/
28  *
29  *****************************************************************************/
30  
31 package net.sourceforge.demetrix.properties.swixml;
32  
33 import java.awt.event.ActionEvent;
34  
35 import java.io.File;
36 import java.io.IOException;
37  
38 import javax.swing.AbstractAction;
39  
40 import javax.swing.Action;
41  
42 import javax.swing.JButton;
43  
44 import javax.swing.JFileChooser;
45  
46 import javax.swing.JLabel;
47  
48 import javax.swing.JPanel;
49  
50 import javax.swing.JTextField;
51  
52 import javax.swing.filechooser.FileFilter;
53  
54 import org.apache.log4j.Logger;
55  
56 import org.swixml.SwingEngine;
57  
58 import net.sourceforge.demetrix.properties.DemetrixProperty;
59  
60 import net.sourceforge.demetrix.properties.FileProperty;
61  
62 /**
63  * @author Dimitri Pissarenko
64  *
65  */
66  
670public class FilePropertySwixMLRepresentation
68     implements SwixMLDemetrixPropertyRepresentation {
69  
70     private Logger logger = Logger.getLogger(getClass());
71  
72     public JPanel panel;
73  
74     public JTextField fileNameTextField;
75  
76     private File selectedFile;
77  
78     public JLabel propertyNameLabel;
79  
80     private boolean mustBeReadable;
81  
82     private boolean mustBeWriteable;
83  
84     public JButton selectFileButton;
85  
86     private FileFilter fileFilter;
87  
88     private FileProperty fileProperty;
89  
909    public Action selectFileButtonAction = new AbstractAction() {
91  
92         public void actionPerformed(ActionEvent event) {
93  
94             selectFileButtonAction();
95  
96         }
97  
98     };
99     public final static String UI_DEF_FILE_NAME="net/sourceforge/demetrix/properties/swixml/FilePropertySwixMLRepresentation.xml";
1009    public FilePropertySwixMLRepresentation() {
101         try {
102  
1039            this.panel =
104                 (JPanel) (new SwingEngine(this)).render(
105             ClassLoader.getSystemClassLoader().getResource(UI_DEF_FILE_NAME));
106  
1070        } catch (Exception exception) {
108  
109             this.logger.error("", exception);
110  
1119        }
112  
1139        this.setMustBeReadable(true);
114  
1159        this.setMustBeWriteable(true);
116  
1179    }
118  
119     /* (non-Javadoc)
120      * @see net.sourceforge.demetrix.properties.swixml.SwixMLDemetrixPropertyRepresentation#getPanel()
121      */
122  
123     public JPanel getPanel() {
124  
1251        return this.panel;
126  
127     }
128  
129     /* (non-Javadoc)
130      * @see net.sourceforge.demetrix.properties.DemetrixPropertyRepresentation#isEnteredDataValid()
131      */
132  
133     public boolean isEnteredDataValid() {
134  
1358        boolean valid = true;
136  
1378        if (this.isMustBeReadable() && (!this.selectedFile.canRead())) {
138  
139             this.logger.info(
140                 "File \""
141                     + this.selectedFile
142                     + "\" must be readable, but it isn't.");
143  
1442            valid = false;
145  
146         }
147  
1488        if (valid
149             && (this.isMustBeWriteable() && (!this.selectedFile.canWrite()))) {
150                 
151                 try
152                 {
1532                    this.selectedFile.createNewFile();
154                     this.logger.info(
155                         "Created file \""
156                             + this.selectedFile
157                             + "\".");
158                     
159                 }
1601                catch (IOException exception)
161                 {
1621                    valid=false;
163                     this.logger.error("File \"" + this.selectedFile + "\" must be writeable, but it isn't. I tried to create it, but failed :(", exception);
164                     
1651                }
166         
167         }
168  
1698        return valid;
170  
171     }
172  
173     /* (non-Javadoc)
174      * @see net.sourceforge.demetrix.properties.DemetrixPropertyRepresentation#updatePropertyData(net.sourceforge.demetrix.properties.DemetrixProperty)
175      */
176  
177     public void updatePropertyData(DemetrixProperty property) {
178  
1791        property.setValue(this.selectedFile);
180  
1811    }
182  
183     /* (non-Javadoc)
184      * @see net.sourceforge.demetrix.properties.DemetrixPropertyRepresentation#updateRepresentation(net.sourceforge.demetrix.properties.DemetrixProperty)
185      */
186  
187     public void updateRepresentation(DemetrixProperty property) {
188  
18918        this.propertyNameLabel.setText(property.getName());
190  
19118        if (property.getValue() != null) {
192  
1939            this.fileNameTextField.setText("" + property.getValue());
194  
195         } else {
196  
1979            this.fileNameTextField.setText("");
198  
199         }
200  
20118        this.selectedFile = (File) property.getValue();
202  
20318        this.fileNameTextField.setEnabled(!property.isReadOnly());
204  
20518        this.selectFileButton.setEnabled(!property.isReadOnly());
206  
20718        this.fileProperty = (FileProperty) property;
208  
20918        this.setMustBeReadable(this.fileProperty.isMustBeReadable());
210  
21118        this.setMustBeWriteable(this.fileProperty.isMustBeWriteable());
212  
21318        this.fileFilter = this.fileProperty.getFileFilter();
214  
21518    }
216  
217     /**
218      * @return
219      */
220  
221     public boolean isMustBeReadable() {
222  
22312        return mustBeReadable;
224  
225     }
226  
227     /**
228      * @return
229      */
230  
231     public boolean isMustBeWriteable() {
232  
23310        return mustBeWriteable;
234  
235     }
236  
237     /**
238      * @param b
239      */
240  
241     public void setMustBeReadable(boolean b) {
242  
24335        mustBeReadable = b;
244  
24535    }
246  
247     /**
248      * @param b
249      */
250  
251     public void setMustBeWriteable(boolean b) {
252  
25335        mustBeWriteable = b;
254  
25535    }
256  
257     /**
258      * @return
259      */
260  
261     public File getSelectedFile() {
262  
2630        return selectedFile;
264  
265     }
266  
267     /**
268      * @param file
269      */
270  
271     private void setSelectedFile(File file) {
272  
2731        selectedFile = file;
274  
2751    }
276  
277     /**
278      * @return
279      */
280  
281     public FileFilter getFileFilter() {
282  
2830        return fileFilter;
284  
285     }
286  
287     /**
288      * @param filter
289      */
290  
291     public void setFileFilter(FileFilter filter) {
292  
2930        fileFilter = filter;
294  
2950    }
296  
297     private void selectFileButtonAction() {
298  
2990        JFileChooser fileChooser = null;
300  
3010        fileChooser = this.getFileChooser();
302  
3030        if (fileChooser.showDialog(this.getPanel(), "Select")
304             == JFileChooser.APPROVE_OPTION) {
305  
3060            this.selectedFile = fileChooser.getSelectedFile();
307         
308         }
3090        this.fileNameTextField.setText(this.selectedFile.toString());
3100    }
311  
312     private JFileChooser getFileChooser() {
313  
3144        JFileChooser returnValue = null;
315  
3164        File file = null;
317  
3184        returnValue = new JFileChooser(System.getProperty("user.dir"));
319  
3204        returnValue.setDialogTitle(this.fileProperty.getName());
321  
3224        returnValue.setDialogType(JFileChooser.CUSTOM_DIALOG);
323  
3244        if (this.fileProperty.getFileFilter() != null) {
325  
3263            returnValue.setFileFilter(this.fileProperty.getFileFilter());
327  
328         } else {
329  
3301            returnValue.setFileFilter(returnValue.getAcceptAllFileFilter());
331  
332         }
333  
3344        file = (File) this.fileProperty.getValue();
335  
3364        if ((file != null) && file.exists()) {
337  
3381            returnValue.setSelectedFile((File) this.fileProperty.getValue());
339  
340         }
341         
3424        return returnValue;
343  
344     }
345  
346 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.