1 package com.explosion.utilities.preferences.editandrender.form;
2
3 import java.awt.Component;
4 import java.awt.GridBagConstraints;
5 import java.awt.GridBagLayout;
6 import java.awt.Insets;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.File;
10
11 import javax.swing.JButton;
12 import javax.swing.JFileChooser;
13 import javax.swing.JLabel;
14 import javax.swing.JTextField;
15
16 import org.apache.log4j.LogManager;
17 import org.apache.log4j.Logger;
18
19 import com.explosion.utilities.FileSystemUtils;
20 import com.explosion.utilities.exception.ExceptionManagerFactory;
21 import com.explosion.utilities.preferences.Preference;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 /***
45 * This class provides a way to edit and rebder text based preferences
46 * @author Stephen Cowx
47 * @version
48 */
49 public class FilePreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
50
51 private JTextField fileTextField = new JTextField();
52 private JButton button = new JButton("...");
53 private JLabel label = new JLabel();
54 private Component owner;
55 private static Logger log = LogManager.getLogger(FilePreferenceEditorAndRenderer.class);
56 private int type = -1;
57 private String description;
58
59 /***
60 * Constructs an instance of this class
61 * @param preference
62 */
63 public FilePreferenceEditorAndRenderer(Preference preference, Component owner, int type, String description)
64 {
65 super(preference);
66 this.owner = owner;
67 this.type = type;
68 this.description = description;
69 init();
70 }
71
72 /***
73 * This method initialises this renderer editor combination
74 */
75 public void init()
76 {
77 this.setLayout(new GridBagLayout());
78
79
80 label.setText(getPreference().getLongName());
81 fileTextField.setText(((File)getPreference().getValue()).getAbsolutePath());
82
83
84 this.add(label , new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
85 this.add(fileTextField , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
86 this.add(button , new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
87
88
89 button.addActionListener(new FileButtonActionListener(this, owner));
90 }
91
92 /***
93 * @throws Exception
94 */
95 protected void setFile(File file) {
96 log.debug("setFile called");
97 try {
98 getPreference().setValue(file);
99 fileTextField.setText(((File)getPreference().getValue()).getAbsolutePath());
100
101 } catch (Exception e) {
102 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating color preference");
103 }
104 }
105
106 /***
107 * Returns the File for the preference
108 * @return
109 */
110 protected File getFile()
111 {
112 return (File) getPreference().getValue();
113 }
114
115 public String getDescription()
116 {
117 return description;
118 }
119
120 public int getType()
121 {
122 return type;
123 }
124
125 }
126
127
128 /***
129 * Opens up a fileChooserDialog
130 */
131
132 class FileButtonActionListener implements ActionListener
133 {
134
135 private JFileChooser filerChooser = null;
136
137 FilePreferenceEditorAndRenderer fileEditor = null;
138
139 private Component owner;
140
141 public FileButtonActionListener(FilePreferenceEditorAndRenderer fileEditor, Component owner)
142 {
143 this.fileEditor = fileEditor;
144 filerChooser = new JFileChooser();
145 filerChooser.setMultiSelectionEnabled(false);
146 this.owner = owner;
147 }
148
149 public void actionPerformed(ActionEvent e)
150 {
151 try
152 {
153 File[] file = null;
154 if (fileEditor.getType() == Preference.PROPERTY_TYPE_FILE)
155 file = FileSystemUtils.chooseFiles(owner, FileSystemUtils.OPENTYPE, false, fileEditor.getFile(), FileSystemUtils.FILES_ONLY, "Select '"
156 + fileEditor.getDescription() + "'");
157 else if (fileEditor.getType() == Preference.PROPERTY_TYPE_DIRECTORY)
158 file = FileSystemUtils.chooseFiles(owner, FileSystemUtils.OPENTYPE, false, fileEditor.getFile(), FileSystemUtils.DIRECTORIES_ONLY, "Select '"
159 + fileEditor.getDescription() + "'");
160
161 if (file != null && file[0] != null) fileEditor.setFile(file[0]);
162
163 } catch (Exception e1)
164 {
165 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e1, null);
166 }
167 }
168 }