Save This Page
Home » openjdk-7 » net.sourceforge » unufree » [javadoc | source]
    1   package net.sourceforge.unufree;
    2   
    3   import java.awt;
    4   import java.awt.event;
    5   import javax.swing;
    6   import java.io.File;
    7   import javax.swing.filechooser;
    8   
    9   public class ImageChooser extends JDialog implements ActionListener {
   10   
   11   	protected JRadioButton urlButton; 
   12   	protected JRadioButton fileButton; 
   13   	protected JButton okButton; 
   14   	protected JButton cancelButton; 
   15   	protected JPanel radioPanel; 
   16   	protected JPanel buttonsPanel; 
   17   	protected JTextField imageUrl; 
   18   	protected JTextField imagePath;
   19   	protected JFileChooser chooser; 
   20   	protected String returnValue;
   21   	protected JFrame owner;
   22   	
   23   	public ImageChooser(JFrame owner) {
   24   		
   25   		super(owner, true);
   26   		getContentPane().setLayout(new BorderLayout());
   27   		imageUrl = new JTextField();
   28   		imagePath = new JTextField();
   29   		imagePath.setEnabled(false);
   30   		
   31   		chooser = new JFileChooser();
   32   		
   33   		chooser.addChoosableFileFilter(new FileFilter() {
   34   			public boolean accept(File f) {
   35   				return (f.isDirectory()
   36   					|| f.getName().toLowerCase().endsWith(".jpg")
   37   					 || f.getName().toLowerCase().endsWith(".jpeg"));
   38   			}
   39   			public String getDescription() {
   40   				return "JPEG Images";
   41   			}
   42   		});
   43   		
   44   		chooser.addChoosableFileFilter(new FileFilter() {
   45   			public boolean accept(File f) {
   46   				return (f.isDirectory()
   47   					|| f.getName().toLowerCase().endsWith(".gif"));
   48   			}
   49   			
   50   			public String getDescription() {
   51   				return "GIF Images";
   52   			}
   53   		});
   54   		
   55   		chooser.addChoosableFileFilter(new FileFilter() {
   56   			public boolean accept(File f) {
   57   				return (f.isDirectory()
   58   					|| f.getName().toLowerCase().endsWith(".png"));
   59   			}
   60   			
   61   			public String getDescription() {
   62   				return "PNG Images";
   63   			}
   64   		});
   65   		
   66   		chooser.addChoosableFileFilter(new FileFilter() {
   67   			public boolean accept(File f) {
   68   				return (f.isDirectory()
   69   					|| f.getName().toLowerCase().endsWith(".bmp"));
   70   			}
   71   			
   72   			public String getDescription() {
   73   				return "BMP Images";
   74   			}
   75   		}); 
   76   		
   77   		FileFilter allImagesFilter = new FileFilter() {
   78   			public boolean accept(File f) {
   79   				String fName = f.getName().toLowerCase();
   80   				return ( f.isDirectory() 
   81   					|| fName.endsWith(".bmp") 
   82   					|| fName.endsWith(".gif")
   83   					|| fName.endsWith(".jpg")
   84   					|| fName.endsWith(".jpeg")
   85   					|| fName.endsWith(".png"));
   86   			}
   87   			
   88   			public String getDescription() {
   89   				return "All image files";
   90   			}
   91   		};
   92   		
   93   		chooser.addChoosableFileFilter(allImagesFilter); 
   94   		chooser.setFileFilter(allImagesFilter);
   95   	
   96   		radioPanel = new JPanel();
   97   		radioPanel.setLayout(new GridLayout(0,1));
   98   		
   99   		buttonsPanel = new JPanel(new FlowLayout());
  100   		okButton = new JButton("OK");
  101   		okButton.addActionListener(this);
  102   		cancelButton = new JButton("Cancel");
  103   		cancelButton.addActionListener(this);
  104   		buttonsPanel.add(okButton);
  105   		buttonsPanel.add(cancelButton);
  106   		
  107   		urlButton = new JRadioButton("Load image from URL", true);
  108   		fileButton = new JRadioButton("Load image from file ...");
  109   		ButtonGroup group = new ButtonGroup();
  110   		group.add(urlButton);
  111   		group.add(fileButton);
  112   		urlButton.addActionListener(this);
  113   		fileButton.addActionListener(this);
  114   		radioPanel.add(urlButton);
  115   		radioPanel.add(imageUrl);
  116   		radioPanel.add(fileButton);
  117   		radioPanel.add(imagePath);
  118   		
  119   		getContentPane().add(radioPanel, BorderLayout.CENTER);
  120   		getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
  121   		setSize(300,150);
  122   	
  123   	}
  124   	
  125   	public String getSelectedFile() {
  126   		return returnValue;
  127   	}
  128   	
  129   	public void actionPerformed(ActionEvent e) {
  130   		
  131   		if (e.getSource() instanceof JRadioButton) {
  132   			
  133   			if (e.getSource() == urlButton) {
  134   				imageUrl.setEnabled(true);
  135   				imagePath.setEnabled(false);
  136   			} else {
  137   				imageUrl.setEnabled(false);
  138   				imagePath.setEnabled(true);
  139   				
  140   				if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
  141   						imagePath.setText("file:///"+chooser.getSelectedFile().getPath());
  142   				}
  143   			
  144   		}
  145   		
  146   		} else if (e.getSource() instanceof JButton) {
  147   		
  148   			if (e.getSource() == okButton) {
  149   		
  150   				if (urlButton.isSelected()) {
  151   					returnValue = imageUrl.getText();
  152   				} else {
  153   		
  154   					returnValue = imagePath.getText();
  155   				}
  156   		
  157   			} else if (e.getSource() == cancelButton) {
  158   		
  159   				returnValue = null; 
  160   		
  161   			}
  162   		
  163   			dispose();
  164   		}	
  165   	} // actionPefrormed()
  166   
  167   }
  168   		

Save This Page
Home » openjdk-7 » net.sourceforge » unufree » [javadoc | source]