| Line | Hits | Source |
|---|---|---|
| 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 | ||
| 67 | 0 | public 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 | ||
| 90 | 9 | 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"; | |
| 100 | 9 | public FilePropertySwixMLRepresentation() { |
| 101 | try { | |
| 102 | ||
| 103 | 9 | this.panel = |
| 104 | (JPanel) (new SwingEngine(this)).render( | |
| 105 | ClassLoader.getSystemClassLoader().getResource(UI_DEF_FILE_NAME)); | |
| 106 | ||
| 107 | 0 | } catch (Exception exception) { |
| 108 | ||
| 109 | this.logger.error("", exception); | |
| 110 | ||
| 111 | 9 | } |
| 112 | ||
| 113 | 9 | this.setMustBeReadable(true); |
| 114 | ||
| 115 | 9 | this.setMustBeWriteable(true); |
| 116 | ||
| 117 | 9 | } |
| 118 | ||
| 119 | /* (non-Javadoc) | |
| 120 | * @see net.sourceforge.demetrix.properties.swixml.SwixMLDemetrixPropertyRepresentation#getPanel() | |
| 121 | */ | |
| 122 | ||
| 123 | public JPanel getPanel() { | |
| 124 | ||
| 125 | 1 | return this.panel; |
| 126 | ||
| 127 | } | |
| 128 | ||
| 129 | /* (non-Javadoc) | |
| 130 | * @see net.sourceforge.demetrix.properties.DemetrixPropertyRepresentation#isEnteredDataValid() | |
| 131 | */ | |
| 132 | ||
| 133 | public boolean isEnteredDataValid() { | |
| 134 | ||
| 135 | 8 | boolean valid = true; |
| 136 | ||
| 137 | 8 | 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 | ||
| 144 | 2 | valid = false; |
| 145 | ||
| 146 | } | |
| 147 | ||
| 148 | 8 | if (valid |
| 149 | && (this.isMustBeWriteable() && (!this.selectedFile.canWrite()))) { | |
| 150 | ||
| 151 | try | |
| 152 | { | |
| 153 | 2 | this.selectedFile.createNewFile(); |
| 154 | this.logger.info( | |
| 155 | "Created file \"" | |
| 156 | + this.selectedFile | |
| 157 | + "\"."); | |
| 158 | ||
| 159 | } | |
| 160 | 1 | catch (IOException exception) |
| 161 | { | |
| 162 | 1 | 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 | ||
| 165 | 1 | } |
| 166 | ||
| 167 | } | |
| 168 | ||
| 169 | 8 | 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 | ||
| 179 | 1 | property.setValue(this.selectedFile); |
| 180 | ||
| 181 | 1 | } |
| 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 | ||
| 189 | 18 | this.propertyNameLabel.setText(property.getName()); |
| 190 | ||
| 191 | 18 | if (property.getValue() != null) { |
| 192 | ||
| 193 | 9 | this.fileNameTextField.setText("" + property.getValue()); |
| 194 | ||
| 195 | } else { | |
| 196 | ||
| 197 | 9 | this.fileNameTextField.setText(""); |
| 198 | ||
| 199 | } | |
| 200 | ||
| 201 | 18 | this.selectedFile = (File) property.getValue(); |
| 202 | ||
| 203 | 18 | this.fileNameTextField.setEnabled(!property.isReadOnly()); |
| 204 | ||
| 205 | 18 | this.selectFileButton.setEnabled(!property.isReadOnly()); |
| 206 | ||
| 207 | 18 | this.fileProperty = (FileProperty) property; |
| 208 | ||
| 209 | 18 | this.setMustBeReadable(this.fileProperty.isMustBeReadable()); |
| 210 | ||
| 211 | 18 | this.setMustBeWriteable(this.fileProperty.isMustBeWriteable()); |
| 212 | ||
| 213 | 18 | this.fileFilter = this.fileProperty.getFileFilter(); |
| 214 | ||
| 215 | 18 | } |
| 216 | ||
| 217 | /** | |
| 218 | * @return | |
| 219 | */ | |
| 220 | ||
| 221 | public boolean isMustBeReadable() { | |
| 222 | ||
| 223 | 12 | return mustBeReadable; |
| 224 | ||
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * @return | |
| 229 | */ | |
| 230 | ||
| 231 | public boolean isMustBeWriteable() { | |
| 232 | ||
| 233 | 10 | return mustBeWriteable; |
| 234 | ||
| 235 | } | |
| 236 | ||
| 237 | /** | |
| 238 | * @param b | |
| 239 | */ | |
| 240 | ||
| 241 | public void setMustBeReadable(boolean b) { | |
| 242 | ||
| 243 | 35 | mustBeReadable = b; |
| 244 | ||
| 245 | 35 | } |
| 246 | ||
| 247 | /** | |
| 248 | * @param b | |
| 249 | */ | |
| 250 | ||
| 251 | public void setMustBeWriteable(boolean b) { | |
| 252 | ||
| 253 | 35 | mustBeWriteable = b; |
| 254 | ||
| 255 | 35 | } |
| 256 | ||
| 257 | /** | |
| 258 | * @return | |
| 259 | */ | |
| 260 | ||
| 261 | public File getSelectedFile() { | |
| 262 | ||
| 263 | 0 | return selectedFile; |
| 264 | ||
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * @param file | |
| 269 | */ | |
| 270 | ||
| 271 | private void setSelectedFile(File file) { | |
| 272 | ||
| 273 | 1 | selectedFile = file; |
| 274 | ||
| 275 | 1 | } |
| 276 | ||
| 277 | /** | |
| 278 | * @return | |
| 279 | */ | |
| 280 | ||
| 281 | public FileFilter getFileFilter() { | |
| 282 | ||
| 283 | 0 | return fileFilter; |
| 284 | ||
| 285 | } | |
| 286 | ||
| 287 | /** | |
| 288 | * @param filter | |
| 289 | */ | |
| 290 | ||
| 291 | public void setFileFilter(FileFilter filter) { | |
| 292 | ||
| 293 | 0 | fileFilter = filter; |
| 294 | ||
| 295 | 0 | } |
| 296 | ||
| 297 | private void selectFileButtonAction() { | |
| 298 | ||
| 299 | 0 | JFileChooser fileChooser = null; |
| 300 | ||
| 301 | 0 | fileChooser = this.getFileChooser(); |
| 302 | ||
| 303 | 0 | if (fileChooser.showDialog(this.getPanel(), "Select") |
| 304 | == JFileChooser.APPROVE_OPTION) { | |
| 305 | ||
| 306 | 0 | this.selectedFile = fileChooser.getSelectedFile(); |
| 307 | ||
| 308 | } | |
| 309 | 0 | this.fileNameTextField.setText(this.selectedFile.toString()); |
| 310 | 0 | } |
| 311 | ||
| 312 | private JFileChooser getFileChooser() { | |
| 313 | ||
| 314 | 4 | JFileChooser returnValue = null; |
| 315 | ||
| 316 | 4 | File file = null; |
| 317 | ||
| 318 | 4 | returnValue = new JFileChooser(System.getProperty("user.dir")); |
| 319 | ||
| 320 | 4 | returnValue.setDialogTitle(this.fileProperty.getName()); |
| 321 | ||
| 322 | 4 | returnValue.setDialogType(JFileChooser.CUSTOM_DIALOG); |
| 323 | ||
| 324 | 4 | if (this.fileProperty.getFileFilter() != null) { |
| 325 | ||
| 326 | 3 | returnValue.setFileFilter(this.fileProperty.getFileFilter()); |
| 327 | ||
| 328 | } else { | |
| 329 | ||
| 330 | 1 | returnValue.setFileFilter(returnValue.getAcceptAllFileFilter()); |
| 331 | ||
| 332 | } | |
| 333 | ||
| 334 | 4 | file = (File) this.fileProperty.getValue(); |
| 335 | ||
| 336 | 4 | if ((file != null) && file.exists()) { |
| 337 | ||
| 338 | 1 | returnValue.setSelectedFile((File) this.fileProperty.getValue()); |
| 339 | ||
| 340 | } | |
| 341 | ||
| 342 | 4 | return returnValue; |
| 343 | ||
| 344 | } | |
| 345 | ||
| 346 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |