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