1 26 27 package org.objectweb.util.explorer.swing.gui.lib; 28 29 import javax.swing.JFileChooser ; 30 import javax.swing.JTextField ; 31 import javax.swing.JLabel ; 32 import javax.swing.JButton ; 33 import javax.swing.AbstractAction ; 34 import javax.swing.Box ; 35 import javax.swing.SwingConstants ; 36 37 import org.objectweb.util.explorer.swing.gui.api.ValidateReport; 38 39 import java.awt.Component ; 40 import java.awt.Dimension ; 41 import java.io.File ; 42 import java.net.MalformedURLException ; 43 import java.net.URL ; 44 45 51 public class FileChooserBox 52 extends AbstractElementBox { 53 54 60 61 protected JFileChooser fileChooser_; 62 63 64 protected File file_; 65 66 67 protected String label_; 68 69 70 protected JTextField urlField_; 71 72 78 83 public FileChooserBox(String label, JFileChooser fileChooser){ 84 this(label,fileChooser,true, true); 85 } 86 87 93 public FileChooserBox(String label, JFileChooser fileChooser, boolean isMandatory){ 94 this(label,fileChooser, isMandatory, true); 95 } 96 97 104 public FileChooserBox(String label, JFileChooser fileChooser, boolean isMandatory, boolean isEditable) { 105 super(isMandatory); 106 fileChooser_ = fileChooser; 107 label_ = label; 108 add(Box.createHorizontalGlue()); 109 JLabel explorerLabel = new JLabel (label + ": ", SwingConstants.RIGHT); 110 explorerLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); 111 explorerLabel.setAlignmentY(Component.CENTER_ALIGNMENT); 112 add(explorerLabel); 113 add(Box.createHorizontalStrut(5)); 114 urlField_ = new JTextField (); 115 urlField_.setEditable(isEditable); 116 urlField_.setPreferredSize(new Dimension (200, 20)); 117 urlField_.setMaximumSize(new Dimension (200, 20)); 118 add(urlField_); 119 add(Box.createHorizontalStrut(5)); 120 JButton browseButton = new JButton (new BrowseAction(this)); 121 browseButton.setPreferredSize(new Dimension (20, 20)); 122 browseButton.setMaximumSize(new Dimension (20, 20)); 123 add(browseButton); 124 } 125 126 132 136 public ValidateReport validateBox(){ 137 if(isMandatory_){ 138 if(urlField_.getText()==null || urlField_.getText().equals("")) 139 return new DefaultValidateReport(false,"The \"" + label_ + "\" value is mandatory"); 140 } 141 return new DefaultValidateReport(); 142 } 143 144 148 public Box getBox(){ 149 return this; 150 } 151 152 158 161 public File getFile() { 162 return file_; 163 } 164 165 169 public URL getURL() { 170 try { 171 return new URL (urlField_.getText()); 172 } catch (MalformedURLException e) { 173 return null; 174 } 175 } 176 177 183 186 protected class BrowseAction extends AbstractAction { 187 188 189 protected Component parent_; 190 191 194 protected BrowseAction(Component parent) { 195 super("...", null); 196 parent_ = parent; 197 } 198 199 public void actionPerformed(java.awt.event.ActionEvent ae) { 200 int returnVal = fileChooser_.showOpenDialog(parent_); 201 if (returnVal == JFileChooser.APPROVE_OPTION) { 202 file_ = fileChooser_.getSelectedFile(); 203 try { 204 urlField_.setText(fileChooser_.getSelectedFile().toURL().toString()); 205 } catch (MalformedURLException e) { 206 System.out.println(getClass().getName() + " Malformed URL Exception !"); 207 } 208 } 209 } 210 211 } 212 213 } 214 | Popular Tags |