]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/ui/swinggui/PreferencesDialog.java
Merge branch 'master' of ssh://git.xonotic.org/xonotic
[xonotic/xonotic.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / ui / swinggui / PreferencesDialog.java
1 package com.nexuiz.demorecorder.ui.swinggui;
2
3 import java.awt.Frame;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.File;
7 import java.util.HashMap;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.Set;
11
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;
20
21 import net.miginfocom.swing.MigLayout;
22
23 import org.jdesktop.swingx.JXTitledSeparator;
24
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;
29
30 public class PreferencesDialog extends JDialog implements ActionListener {
31
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;
37         
38         private JButton saveButton = new JButton("Save");
39         private JButton cancelButton = new JButton("Cancel");
40         
41         public PreferencesDialog(Frame owner, DemoRecorderApplication appLayer) {
42                 super(owner, true);
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);
48
49                 setTitle("Preferences");
50
51                 this.setupLayout();
52         }
53
54         private void setupLayout() {
55                 setLayout(new MigLayout("wrap 2", "[][::150,fill]"));
56                 
57                 //add heading
58                 JXTitledSeparator applicationHeading = new JXTitledSeparator("Application settings");
59                 getContentPane().add(applicationHeading, "span 2,grow");
60                 
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);
65                         }
66                 }
67                 
68                 //add plugin settings
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) {
74                                 //add heading
75                                 JXTitledSeparator pluginHeading = new JXTitledSeparator(pluginName + " plugin settings");
76                                 getContentPane().add(pluginHeading, "span 2,grow");
77                                 
78                                 for (String pluginKey : plugin.getGlobalPreferencesOrder()) {
79                                         if (this.preferences.getProperty(pluginName, pluginKey) != null) {
80                                                 this.setupSingleSetting(pluginName, pluginKey);
81                                         }
82                                 }
83                         }
84                 }
85                 
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");
92         }
93         
94         private void setupSingleSetting(String category, String setting) {
95                 getContentPane().add(new JLabel(setting + ":"));
96                 
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() {
107                                 @Override
108                                 public void actionPerformed(ActionEvent e) {
109                                         fc.showOpenDialog(PreferencesDialog.this);
110                                 }
111                         });
112                         this.dialogSettings.put(NDRPreferences.getConcatenatedKey(category, setting), fc);
113                         getContentPane().add(fcButton);
114                 } else {
115                         JTextField textField = new JTextField();
116                         this.dialogSettings.put(NDRPreferences.getConcatenatedKey(category, setting), textField);
117                         getContentPane().add(textField);
118                 }
119         }
120         
121         
122         
123         public void showDialog() {
124                 this.loadSettings();
125                 this.pack();
126                 this.setLocationRelativeTo(this.parentFrame);
127                 setResizable(false);
128                 this.setVisible(true);
129         }
130         
131         /**
132          * Loads the settings from the application layer (and global plug-in settings) to the form.
133          */
134         private void loadSettings() {
135                 Set<Object> keys = this.preferences.keySet();
136                 for (Object keyObj : keys) {
137                         String concatenatedKey = (String) keyObj;
138                         String value;
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));
145                                         }
146                                 } else if (SwingGUIUtils.isFileChooser(value)) {
147                                         component = this.dialogSettings.get(concatenatedKey);
148                                         try {
149                                                 File selectedFile = new File(value);
150                                                 if (selectedFile.exists() && component != null) {
151                                                         ((JFileChooser) component).setSelectedFile(selectedFile);
152                                                 }
153                                         } catch (Throwable e) {}
154                                         
155                                 } else {
156                                         component = this.dialogSettings.get(concatenatedKey);
157                                         if (component != null) {
158                                                 ((JTextField) component).setText(value);
159                                         }
160                                 }
161                         }
162                 }
163         }
164
165         @Override
166         public void actionPerformed(ActionEvent e) {
167                 if (e.getSource() == cancelButton) {
168                         this.setVisible(false);
169                 } else if (e.getSource() == saveButton) {
170                         this.saveSettings();
171                 }
172         }
173
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);
187                                 }
188                         } else if (component instanceof JTextField) {
189                                 JTextField textField = (JTextField) component;
190                                 this.appLayer.setPreference(NDRPreferences.getCategory(key), NDRPreferences.getKey(key), textField.getText());
191                         }
192                 }
193                 this.setVisible(false);
194         }
195 }