1 26 27 package org.objectweb.util.browser.gui.lib; 28 29 30 import org.objectweb.util.browser.gui.api.ValidateReport; 31 32 33 import javax.swing.JFileChooser ; 34 import javax.swing.JTextField ; 35 import javax.swing.JLabel ; 36 import javax.swing.JButton ; 37 import javax.swing.AbstractAction ; 38 import javax.swing.Box ; 39 import javax.swing.SwingConstants ; 40 41 import java.awt.Component ; 42 import java.awt.Dimension ; 43 import java.io.File ; 44 import java.net.MalformedURLException ; 45 import java.net.URL ; 46 47 53 public class FileChooserBox 54 extends AbstractElementBox { 55 56 62 63 protected JFileChooser fileChooser_; 64 65 66 protected File file_; 67 68 69 protected String label_; 70 71 72 protected JTextField urlField_; 73 74 80 85 public FileChooserBox(String label, JFileChooser fileChooser){ 86 this(label,fileChooser,true, true); 87 } 88 89 95 public FileChooserBox(String label, JFileChooser fileChooser, boolean isMandatory){ 96 this(label,fileChooser, isMandatory, true); 97 } 98 99 106 public FileChooserBox(String label, JFileChooser fileChooser, boolean isMandatory, boolean isEditable) { 107 super(isMandatory); 108 fileChooser_ = fileChooser; 109 label_ = label; 110 add(Box.createHorizontalGlue()); 111 JLabel browserLabel = new JLabel (label + ": ", SwingConstants.RIGHT); 112 browserLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); 113 browserLabel.setAlignmentY(Component.CENTER_ALIGNMENT); 114 add(browserLabel); 115 add(Box.createHorizontalStrut(5)); 116 urlField_ = new JTextField (); 117 urlField_.setEditable(isEditable); 118 urlField_.setPreferredSize(new Dimension (200, 20)); 119 urlField_.setMaximumSize(new Dimension (200, 20)); 120 add(urlField_); 121 add(Box.createHorizontalStrut(5)); 122 JButton browseButton = new JButton (new BrowseAction(this)); 123 browseButton.setPreferredSize(new Dimension (20, 20)); 124 browseButton.setMaximumSize(new Dimension (20, 20)); 125 add(browseButton); 126 } 127 128 134 138 public ValidateReport validateBox(){ 139 if(isMandatory_){ 140 if(urlField_.getText()==null || urlField_.getText().equals("")) 141 return new DefaultValidateReport(false,"The \"" + label_ + "\" value is mandatory"); 142 } 143 return new DefaultValidateReport(); 144 } 145 146 150 public Box getBox(){ 151 return this; 152 } 153 154 160 163 public File getFile() { 164 return file_; 165 } 166 167 171 public URL getURL() { 172 try { 173 return new URL (urlField_.getText()); 174 } catch (MalformedURLException e) { 175 return null; 176 } 177 } 178 179 185 188 protected class BrowseAction extends AbstractAction { 189 190 191 protected Component parent_; 192 193 196 protected BrowseAction(Component parent) { 197 super("...", null); 198 parent_ = parent; 199 } 200 201 public void actionPerformed(java.awt.event.ActionEvent ae) { 202 int returnVal = fileChooser_.showOpenDialog(parent_); 203 if (returnVal == JFileChooser.APPROVE_OPTION) { 204 file_ = fileChooser_.getSelectedFile(); 205 try { 206 urlField_.setText(fileChooser_.getSelectedFile().toURL().toString()); 207 } catch (MalformedURLException e) { 208 System.out.println(getClass().getName() + " Malformed URL Exception !"); 209 } 210 } 211 } 212 213 } 214 215 } 216 | Popular Tags |