View Javadoc

1   package org.abstracthorizon.aequo.gui;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.io.File;
8   import java.io.IOException;
9   
10  import javax.swing.JButton;
11  import javax.swing.JFileChooser;
12  import javax.swing.JLabel;
13  import javax.swing.JPanel;
14  import javax.swing.JTextField;
15  import javax.swing.event.DocumentEvent;
16  import javax.swing.event.DocumentListener;
17  
18  import org.abstracthorizon.aequo.GlobalContext;
19  import org.abstracthorizon.aequo.util.TaskUtilities;
20  
21  /**
22   * File selector component. It adds label, text field and button to invoke file selector
23   */
24  public class FileSelector implements ActionListener {
25      
26      /** File */
27      protected File file;
28      
29      /** Input field */
30      protected JTextField field;
31      
32      /** Global context */
33      protected GlobalContext context;
34      
35      /** Panel */
36      protected JPanel panel;
37      
38      /**
39       * Constructor
40       * @param context global context
41       * @param panel panel components to be added to
42       * @param title label
43       */
44      public FileSelector(GlobalContext context, JPanel panel, String title) {
45          this.context = context;
46          this.panel = panel;
47          JLabel label = new JLabel(title);
48          panel.add(label, BorderLayout.WEST);
49          
50          field = new JTextField();
51          field.setColumns(25);
52          
53          final Color backgroundColour = field.getBackground();
54          field.getDocument().addDocumentListener(new DocumentListener() {
55              public void changedUpdate(DocumentEvent e) {
56                  notifyUpdate();
57              }
58  
59              public void insertUpdate(DocumentEvent e) {
60                  notifyUpdate();
61              }
62  
63              public void removeUpdate(DocumentEvent e) {
64                  notifyUpdate();
65              }
66  
67              public void notifyUpdate() {
68                  TaskUtilities.invokeLater(new Runnable() {
69                      public void run() {
70                          file = new File(field.getText());
71                          if (file.exists()) {
72                              field.setBackground(backgroundColour);
73                          } else {
74                              field.setBackground(Color.RED.brighter());
75                          }
76                      }
77                  });
78              }
79          });
80          panel.add(field, BorderLayout.CENTER);
81          
82          JButton button = new JButton("Open");
83          button.setOpaque(false);
84          panel.add(button, BorderLayout.EAST);
85          button.addActionListener(this);
86          
87      }
88      
89      /**
90       * Returns selected file
91       * @return file
92       */
93      public File getFile() {
94          return file;
95      }
96      
97      /**
98       * Invokes the file selector and sets the file
99       * @param e action event
100      */
101     public void actionPerformed(ActionEvent e) {
102         
103         JFileChooser fileChooser = null;
104         
105         File path = null;
106         String pathString = context.getPreferences().getProperty("path");
107         if (pathString != null) {
108             path = new File(pathString);
109             if (!path.exists()) {
110                 path = null;
111             }
112         }
113         if (path != null) {
114             fileChooser = new JFileChooser(path);
115         } else {
116             fileChooser = new JFileChooser();
117         }
118         fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
119         
120         int res = fileChooser.showOpenDialog(panel);
121         if (res == JFileChooser.APPROVE_OPTION) {
122             file = fileChooser.getSelectedFile();
123             path = file.getParentFile();
124             context.getPreferences().setProperty("path", path.getAbsolutePath());
125             try {
126                 context.getPreferences().save();
127             } catch (IOException ignore) {
128             }
129             field.setText(file.getAbsolutePath());
130         }
131     }
132 }