1 package com.nexuiz.demorecorder.ui.swinggui;
\r
3 import java.awt.Frame;
\r
4 import java.awt.event.ActionEvent;
\r
5 import java.awt.event.ActionListener;
\r
7 import java.util.HashMap;
\r
8 import java.util.Map;
\r
9 import java.util.Properties;
\r
10 import java.util.Set;
\r
12 import javax.swing.JButton;
\r
13 import javax.swing.JCheckBox;
\r
14 import javax.swing.JComponent;
\r
15 import javax.swing.JDialog;
\r
16 import javax.swing.JFileChooser;
\r
17 import javax.swing.JLabel;
\r
18 import javax.swing.JPanel;
\r
19 import javax.swing.JTextField;
\r
21 import net.miginfocom.swing.MigLayout;
\r
23 import org.jdesktop.swingx.JXTitledSeparator;
\r
25 import com.nexuiz.demorecorder.application.DemoRecorderApplication;
\r
26 import com.nexuiz.demorecorder.application.NDRPreferences;
\r
27 import com.nexuiz.demorecorder.application.plugins.EncoderPlugin;
\r
28 import com.nexuiz.demorecorder.ui.swinggui.utils.SwingGUIUtils;
\r
30 public class PreferencesDialog extends JDialog implements ActionListener {
\r
32 private static final long serialVersionUID = 7328399646538571333L;
\r
33 private Frame parentFrame;
\r
34 private DemoRecorderApplication appLayer;
\r
35 private NDRPreferences preferences;
\r
36 private Map<String, JComponent> dialogSettings;
\r
38 private JButton saveButton = new JButton("Save");
\r
39 private JButton cancelButton = new JButton("Cancel");
\r
41 public PreferencesDialog(Frame owner, DemoRecorderApplication appLayer) {
\r
43 this.parentFrame = owner;
\r
44 this.appLayer = appLayer;
\r
45 this.preferences = appLayer.getPreferences();
\r
46 this.dialogSettings = new HashMap<String, JComponent>();
\r
47 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
\r
49 setTitle("Preferences");
\r
54 private void setupLayout() {
\r
55 setLayout(new MigLayout("wrap 2", "[][::150,fill]"));
\r
58 JXTitledSeparator applicationHeading = new JXTitledSeparator("Application settings");
\r
59 getContentPane().add(applicationHeading, "span 2,grow");
\r
61 for (int i = 0; i < DemoRecorderApplication.Preferences.PREFERENCES_ORDER.length; i++) {
\r
62 String currentSetting = DemoRecorderApplication.Preferences.PREFERENCES_ORDER[i];
\r
63 if (this.preferences.getProperty(NDRPreferences.MAIN_APPLICATION, currentSetting) != null) {
\r
64 this.setupSingleSetting(NDRPreferences.MAIN_APPLICATION, currentSetting);
\r
68 //add plugin settings
\r
69 for (EncoderPlugin plugin : this.appLayer.getEncoderPlugins()) {
\r
70 String pluginName = plugin.getName();
\r
71 //only display settings if the plugin actually has any...
\r
72 Properties pluginPreferences = plugin.getGlobalPreferences();
\r
73 if (pluginPreferences.size() > 0) {
\r
75 JXTitledSeparator pluginHeading = new JXTitledSeparator(pluginName + " plugin settings");
\r
76 getContentPane().add(pluginHeading, "span 2,grow");
\r
78 for (String pluginKey : plugin.getGlobalPreferencesOrder()) {
\r
79 if (this.preferences.getProperty(pluginName, pluginKey) != null) {
\r
80 this.setupSingleSetting(pluginName, pluginKey);
\r
86 JPanel buttonPanel = new JPanel();
\r
87 buttonPanel.add(saveButton);
\r
88 buttonPanel.add(cancelButton);
\r
89 saveButton.addActionListener(this);
\r
90 cancelButton.addActionListener(this);
\r
91 getContentPane().add(buttonPanel, "span 2");
\r
94 private void setupSingleSetting(String category, String setting) {
\r
95 getContentPane().add(new JLabel(setting + ":"));
\r
97 String value = this.preferences.getProperty(category, setting);
\r
98 if (SwingGUIUtils.isBooleanValue(value)) {
\r
99 JCheckBox checkbox = new JCheckBox();
\r
100 this.dialogSettings.put(NDRPreferences.getConcatenatedKey(category, setting), checkbox);
\r
101 getContentPane().add(checkbox);
\r
102 } else if (SwingGUIUtils.isFileChooser(value)) {
\r
103 final JFileChooser fc = new JFileChooser();
\r
104 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
\r
105 JButton fcButton = new JButton("...");
\r
106 fcButton.addActionListener(new ActionListener() {
\r
108 public void actionPerformed(ActionEvent e) {
\r
109 fc.showOpenDialog(PreferencesDialog.this);
\r
112 this.dialogSettings.put(NDRPreferences.getConcatenatedKey(category, setting), fc);
\r
113 getContentPane().add(fcButton);
\r
115 JTextField textField = new JTextField();
\r
116 this.dialogSettings.put(NDRPreferences.getConcatenatedKey(category, setting), textField);
\r
117 getContentPane().add(textField);
\r
123 public void showDialog() {
\r
124 this.loadSettings();
\r
126 this.setLocationRelativeTo(this.parentFrame);
\r
127 setResizable(false);
\r
128 this.setVisible(true);
\r
132 * Loads the settings from the application layer (and global plug-in settings) to the form.
\r
134 private void loadSettings() {
\r
135 Set<Object> keys = this.preferences.keySet();
\r
136 for (Object keyObj : keys) {
\r
137 String concatenatedKey = (String) keyObj;
\r
139 JComponent component = null;
\r
140 if ((value = this.preferences.getProperty(concatenatedKey)) != null) {
\r
141 if (SwingGUIUtils.isBooleanValue(value)) {
\r
142 component = this.dialogSettings.get(concatenatedKey);
\r
143 if (component != null) {
\r
144 ((JCheckBox) component).setSelected(Boolean.valueOf(value));
\r
146 } else if (SwingGUIUtils.isFileChooser(value)) {
\r
147 component = this.dialogSettings.get(concatenatedKey);
\r
149 File selectedFile = new File(value);
\r
150 if (selectedFile.exists() && component != null) {
\r
151 ((JFileChooser) component).setSelectedFile(selectedFile);
\r
153 } catch (Throwable e) {}
\r
156 component = this.dialogSettings.get(concatenatedKey);
\r
157 if (component != null) {
\r
158 ((JTextField) component).setText(value);
\r
166 public void actionPerformed(ActionEvent e) {
\r
167 if (e.getSource() == cancelButton) {
\r
168 this.setVisible(false);
\r
169 } else if (e.getSource() == saveButton) {
\r
170 this.saveSettings();
\r
174 private void saveSettings() {
\r
175 Set<String> keys = this.dialogSettings.keySet();
\r
176 //remember, the keys are concatenated, containing both the category and actual key
\r
177 for (String key : keys) {
\r
178 JComponent component = this.dialogSettings.get(key);
\r
179 if (component instanceof JCheckBox) {
\r
180 JCheckBox checkbox = (JCheckBox) component;
\r
181 this.appLayer.setPreference(NDRPreferences.getCategory(key), NDRPreferences.getKey(key), checkbox.isSelected());
\r
182 } else if (component instanceof JFileChooser) {
\r
183 JFileChooser fileChooser = (JFileChooser) component;
\r
184 if (fileChooser.getSelectedFile() != null) {
\r
185 String path = fileChooser.getSelectedFile().getAbsolutePath();
\r
186 this.appLayer.setPreference(NDRPreferences.getCategory(key), NDRPreferences.getKey(key), path);
\r
188 } else if (component instanceof JTextField) {
\r
189 JTextField textField = (JTextField) component;
\r
190 this.appLayer.setPreference(NDRPreferences.getCategory(key), NDRPreferences.getKey(key), textField.getText());
\r
193 this.setVisible(false);
\r