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