1 35 36 39 package demo.swingset; 40 41 import swingwtx.swing.*; 42 import swingwtx.swing.event.*; 43 import swingwtx.swing.text.*; 44 import swingwtx.swing.border.*; 45 import swingwtx.swing.colorchooser.*; 46 import swingwtx.swing.filechooser.*; 47 import javax.accessibility.*; 48 49 import swingwt.awt.*; 50 import swingwt.awt.event.*; 51 import java.beans.*; 52 import java.util.*; 53 import java.io.*; 54 import java.net.*; 55 56 62 public class FileChooserDemo extends DemoModule { 63 JLabel theImage; 64 Icon jpgIcon; 65 Icon gifIcon; 66 67 70 public static void main(String [] args) { 71 FileChooserDemo demo = new FileChooserDemo(null); 72 demo.mainImpl(); 73 } 74 75 78 public FileChooserDemo(SwingSet2 swingset) { 79 super(swingset, "FileChooserDemo", "toolbar/JFileChooser.gif"); 82 createFileChooserDemo(); 83 } 84 85 public void createFileChooserDemo() { 86 theImage = new JLabel(""); 87 jpgIcon = createImageIcon("filechooser/jpgIcon.jpg", "jpg image"); 88 gifIcon = createImageIcon("filechooser/gifIcon.gif", "gif image"); 89 90 JPanel demoPanel = getDemoPanel(); 91 demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS)); 92 93 JPanel innerPanel = new JPanel(); 94 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS)); 95 96 demoPanel.add(Box.createRigidArea(VGAP20)); 97 demoPanel.add(innerPanel); 98 demoPanel.add(Box.createRigidArea(VGAP20)); 99 100 innerPanel.add(Box.createRigidArea(HGAP20)); 101 102 JPanel buttonPanel = new JPanel() { 104 public Dimension getMaximumSize() { 105 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 106 } 107 }; 108 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 109 110 buttonPanel.add(Box.createRigidArea(VGAP15)); 111 buttonPanel.add(createPlainFileChooserButton()); 112 buttonPanel.add(Box.createRigidArea(VGAP15)); 113 buttonPanel.add(createPreviewFileChooserButton()); 114 buttonPanel.add(Box.createRigidArea(VGAP15)); 115 buttonPanel.add(createCustomFileChooserButton()); 116 buttonPanel.add(Box.createVerticalGlue()); 117 118 JPanel imagePanel = new JPanel(); 120 imagePanel.setLayout(new BorderLayout()); 121 imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); 122 JScrollPane scroller = new JScrollPane(theImage); 123 scroller.getVerticalScrollBar().setUnitIncrement(10); 124 scroller.getHorizontalScrollBar().setUnitIncrement(10); 125 imagePanel.add(scroller, BorderLayout.CENTER); 126 127 innerPanel.add(buttonPanel); 129 innerPanel.add(Box.createRigidArea(HGAP30)); 130 innerPanel.add(imagePanel); 131 innerPanel.add(Box.createRigidArea(HGAP20)); 132 } 133 134 public JFileChooser createFileChooser() { 135 JFileChooser fc = new JFileChooser(); 137 138 File swingFile = new File("resources/images/About.jpg"); 140 if(swingFile.exists()) { 141 fc.setCurrentDirectory(swingFile); 142 fc.setSelectedFile(swingFile); 143 } 144 145 return fc; 146 } 147 148 149 public JButton createPlainFileChooserButton() { 150 Action a = new AbstractAction(getString("FileChooserDemo.plainbutton")) { 151 public void actionPerformed(ActionEvent e) { 152 JFileChooser fc = createFileChooser(); 153 154 int result = fc.showOpenDialog(getDemoPanel()); 156 if(result == JFileChooser.APPROVE_OPTION) { 158 loadImage(fc.getSelectedFile().getPath()); 159 } 160 161 162 } 163 }; 164 return createButton(a); 165 } 166 167 public JButton createPreviewFileChooserButton() { 168 Action a = new AbstractAction(getString("FileChooserDemo.previewbutton")) { 169 public void actionPerformed(ActionEvent e) { 170 JFileChooser fc = createFileChooser(); 171 172 ExampleFileFilter filter = new ExampleFileFilter( 174 new String [] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription") 175 ); 176 ExampleFileView fileView = new ExampleFileView(); 177 fileView.putIcon("jpg", jpgIcon); 178 fileView.putIcon("gif", gifIcon); 179 fc.addChoosableFileFilter(filter); 181 fc.setFileFilter(filter); 182 183 fc.setAccessory(new FilePreviewer(fc)); 185 186 int result = fc.showOpenDialog(getDemoPanel()); 188 if(result == JFileChooser.APPROVE_OPTION) { 190 loadImage(fc.getSelectedFile().getPath()); 191 } 192 193 194 } 195 }; 196 return createButton(a); 197 } 198 199 JDialog dialog; 200 JFileChooser fc; 201 202 public JButton createCustomFileChooserButton() { 203 Action a = new AbstractAction(getString("FileChooserDemo.custombutton")) { 204 public void actionPerformed(ActionEvent e) { 205 fc = createFileChooser(); 206 207 ExampleFileFilter filter = new ExampleFileFilter( 209 new String [] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription") 210 ); 211 ExampleFileView fileView = new ExampleFileView(); 212 fileView.putIcon("jpg", jpgIcon); 213 fileView.putIcon("gif", gifIcon); 214 fc.addChoosableFileFilter(filter); 216 217 fc.setAccessory(new FilePreviewer(fc)); 219 220 fc.setControlButtonsAreShown(false); 222 223 JPanel custom = new JPanel(); 226 custom.setLayout(new BorderLayout()); 227 JLabel description = new JLabel(getString("FileChooserDemo.description")); 228 description.setAlignmentX(JLabel.CENTER_ALIGNMENT); 229 custom.add(description, BorderLayout.NORTH); 230 custom.add(fc, BorderLayout.CENTER); 231 232 Action okAction = createOKAction(); 233 fc.addActionListener(okAction); 234 235 JPanel buttons = new JPanel(); 236 buttons.setLayout(new FlowLayout()); 237 buttons.add(Box.createRigidArea(HGAP10)); 238 buttons.add(createImageButton(createFindAction())); 239 buttons.add(Box.createRigidArea(HGAP10)); 240 buttons.add(createButton(createAboutAction())); 241 buttons.add(Box.createRigidArea(HGAP10)); 242 buttons.add(createButton(okAction)); 243 buttons.add(Box.createRigidArea(HGAP10)); 244 buttons.add(createButton(createCancelAction())); 245 buttons.add(Box.createRigidArea(HGAP10)); 246 buttons.add(createImageButton(createHelpAction())); 247 buttons.add(Box.createRigidArea(HGAP10)); 248 249 custom.add(buttons, BorderLayout.SOUTH); 250 251 Frame parent = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, getDemoPanel()); 252 dialog = new JDialog(parent, getString("FileChooserDemo.dialogtitle"), true); 253 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 254 dialog.getContentPane().add(custom, BorderLayout.CENTER); 255 dialog.setSize(640, 480); 256 dialog.setLocationRelativeTo(getDemoPanel()); 257 dialog.show(); 258 } 259 }; 260 return createButton(a); 261 } 262 263 public Action createAboutAction() { 264 return new AbstractAction(getString("FileChooserDemo.about")) { 265 public void actionPerformed(ActionEvent e) { 266 File file = fc.getSelectedFile(); 267 String text; 268 if(file == null) { 269 text = getString("FileChooserDemo.nofileselected"); 270 } else { 271 text = "<html>" + getString("FileChooserDemo.thefile"); 272 text += "<br><font color=green>" + file.getName() + "</font><br>"; 273 text += getString("FileChooserDemo.isprobably") + "</html>"; 274 } 275 JOptionPane.showMessageDialog(getDemoPanel(), text); 276 } 277 }; 278 } 279 280 public Action createOKAction() { 281 return new AbstractAction(getString("FileChooserDemo.ok")) { 282 public void actionPerformed(ActionEvent e) { 283 dialog.dispose(); 284 if (!e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION) 285 && fc.getSelectedFile() != null) { 286 287 loadImage(fc.getSelectedFile().getPath()); 288 } 289 } 290 }; 291 } 292 293 public Action createCancelAction() { 294 return new AbstractAction(getString("FileChooserDemo.cancel")) { 295 public void actionPerformed(ActionEvent e) { 296 dialog.dispose(); 297 } 298 }; 299 } 300 301 public Action createFindAction() { 302 Icon icon = createImageIcon("filechooser/find.gif", getString("FileChooserDemo.find")); 303 return new AbstractAction("", icon) { 304 public void actionPerformed(ActionEvent e) { 305 String result = JOptionPane.showInputDialog(getDemoPanel(), getString("FileChooserDemo.findquestion")); 306 if (result != null) { 307 JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.findresponse")); 308 } 309 } 310 }; 311 } 312 313 public Action createHelpAction() { 314 Icon icon = createImageIcon("filechooser/help.gif", getString("FileChooserDemo.help")); 315 return new AbstractAction("", icon) { 316 public void actionPerformed(ActionEvent e) { 317 JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.helptext")); 318 } 319 }; 320 } 321 322 323 public void loadImage(String filename) { 324 theImage.setIcon(new ImageIcon(filename)); 325 } 326 327 public JButton createButton(Action a) { 328 JButton b = new JButton(a) { 329 public Dimension getMaximumSize() { 330 int width = Short.MAX_VALUE; 331 int height = super.getMaximumSize().height; 332 return new Dimension(width, height); 333 } 334 }; 335 return b; 336 } 337 338 public JButton createImageButton(Action a) { 339 JButton b = new JButton(a); 340 b.setMargin(new Insets(0,0,0,0)); 341 return b; 342 } 343 } 344 345 class FilePreviewer extends JLabel implements PropertyChangeListener { 346 347 public FilePreviewer(JFileChooser fc) { 348 setPreferredSize(new Dimension(100, 50)); 349 fc.addPropertyChangeListener(this); 350 setBorder(new BevelBorder(BevelBorder.LOWERED)); 351 } 352 353 public void loadImage(File f) { 354 if (f == null) { 355 setIcon(null); 356 } else { 357 ImageIcon tmpIcon = new ImageIcon(f.getPath()); 358 if(tmpIcon.getIconWidth() > 90) { 359 setIcon(new ImageIcon( 360 tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT))); 361 } else { 362 setIcon(tmpIcon); 363 } 364 } 365 } 366 367 public void propertyChange(PropertyChangeEvent e) { 368 String prop = e.getPropertyName(); 369 if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) { 370 if(isShowing()) { 371 loadImage((File) e.getNewValue()); 372 repaint(); 373 } 374 } 375 } 376 } 377 378 | Popular Tags |