001 package net.sourceforge.mp3dings;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003 Moritz Ringler
005 * $Id: PreferencesDialogue.java 99 2007-11-01 15:36:49Z ringler $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 import java.awt.AWTEvent;
023 import java.awt.CardLayout;
024 import java.awt.Component;
025 import java.awt.Dimension;
026 import java.awt.Frame;
027 import java.awt.Graphics;
028 import java.awt.GridBagConstraints;
029 import java.awt.GridBagLayout;
030 import java.awt.BorderLayout;
031 import java.awt.event.ActionEvent;
032 import java.awt.event.ActionListener;
033 import java.awt.event.WindowEvent;
034 import java.util.ArrayList;
035 import java.util.Iterator;
036 import java.util.List;
037 import java.util.logging.Logger;
038 import java.util.prefs.BackingStoreException;
039 import java.util.prefs.Preferences;
040 import javax.swing.JButton;
041 import javax.swing.JComponent;
042 import javax.swing.JDialog;
043 import javax.swing.JPanel;
044 import javax.swing.JCheckBox;
045 import javax.swing.JTree;
046 import javax.swing.UIManager;
047 import javax.swing.border.EmptyBorder;
048 import javax.swing.event.TreeSelectionEvent;
049 import javax.swing.event.TreeSelectionListener;
050 import javax.swing.text.Position.Bias;
051 import javax.swing.tree.DefaultMutableTreeNode;
052 import javax.swing.tree.TreeSelectionModel;
053 import de.mospace.lang.ClassLoaderProvider;
054 import net.sourceforge.mp3dings.filter.FilterSetCustomizer;
055 import net.sourceforge.mp3dings.preferences.*;
056 import net.sourceforge.mp3dings.utils.ChecksumFactory;
057 import net.sourceforge.mp3dings.utils.ExternalApplication;
058
059 /**
060 * The Preferences dialog of the mp3dings application that organizes and
061 * controls the available {@link net.sourceforge.mp3dings.preferences.PrefModule
062 * Preference modules}. It is opened with the Options->Preferences Menu command.
063 * @author Moritz Ringler
064 * @version $Revision: 99 $ ($Date: 2007-11-01 16:36:49 +0100 (Do, 01 Nov 2007) $)
065 */
066
067 public final class PreferencesDialogue extends JDialog
068 implements ActionListener {
069 private static final Logger logger = Logger.getLogger(PreferencesDialogue.class.getPackage().getName());
070 /* MEMBER DECLARATION */
071 /* global */
072 Preferences prefs = Globals.getPrefNode();
073 final DefaultMutableTreeNode prefRoot =
074 new DefaultMutableTreeNode(Globals.getString("PREF_DIALOGUE_TITLE"));
075 JTree prefTree;
076 final JPanel modulePane = new JPanel(new CardLayout());
077
078
079 /* PrefModules */
080 private List myModules = new ArrayList();
081
082 /* Ok & Cancel */
083 JPanel mainButtons;
084 JButton buttonOK;
085 JButton buttonCancel;
086
087
088 /** Constructor */
089 public PreferencesDialogue(Frame parent) {
090 super(parent);
091 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
092 try {
093 init();
094 pack();
095 } catch(BackingStoreException e) {
096 throw new RuntimeException(e);
097 }
098 }
099
100 /**Component initialization*/
101 private void init() throws BackingStoreException {
102 setTitle(Globals.getString("MAIN_MENU_PREFS_TEXT"));
103 setResizable(true);
104
105 //+-----contentPane--------------|
106 //|-------|----modulePane--------|
107 //| | |
108 //| | |
109 //| | |
110 //| | |
111 //| pref | module |
112 //| Tree | |
113 //| | |
114 //| | |
115 //| | |
116 //| | |
117 //|-------+----------------------|
118 //| mainButtons |
119 //----------+--------------------|
120
121 /* modules */
122 /* reusable panel and module handles */
123 PrefPanel newField;
124 AbstractPrefModule module;
125
126 /* field selector */
127 FilterSetCustomizer filterSetCustomizer = new FilterSetCustomizer();
128 AbstractPrefModule fields =
129 new TabbedPrefModule(Globals.getString("PREF_TREE_FIELDS"));
130 fields.addPanel(new FieldSelectorPanel(mainFrame(),prefs,
131 Globals.getString("PREFS_FIELDS_DATATAB"),0,
132 filterSetCustomizer.getBeans()));
133 fields.addPanel(new FieldSelectorPanel(mainFrame(), prefs,
134 Globals.getString("PREFS_FIELDS_NAMESTAB"),1,
135 filterSetCustomizer.getBeans()));
136 fields.addPanel(new FieldSelectorPanel(mainFrame(), prefs,
137 Globals.getString("PREFS_FIELDS_ID3V1TAB"),2,
138 filterSetCustomizer.getBeans()));
139 fields.addPanel(new FieldSelectorPanel(mainFrame(), prefs,
140 Globals.getString("PREFS_FIELDS_ID3V2TAB"),3,
141 filterSetCustomizer.getBeans()));
142 addModule(fields);
143
144 /* filter manager */
145 addModule(filterSetCustomizer);
146
147 /* filename patterns */
148 module = FilenamePatternModule.loadFromUserPrefs(
149 Globals.getString("PREF_FPATTERN_FPATTERNS"), mainFrame());
150 addModule(module);
151
152 /* external application module */
153 addModule(new ExternalApplicationModule(mainFrame().getMiniShell()));
154
155 /* warning level */
156 module = new SinglePanelModule(Globals.getString("PREF_TREE_WARNLEVEL"));
157 module.addPanel(
158 new WarnLevelPanel(mainFrame(), prefs,
159 Globals.getString("PREFS_WARNLEVEL")));
160 addModule(module);
161
162
163 /* appearance */
164 module = new VBoxModule(Globals.getString("PREF_TREE_APPEARANCE"));
165 module.addPanel(
166 new ResizeModePanel(prefs,
167 Globals.getString("PREFS_RESIZE_MODE")));
168 module.addPanel(
169 new ColorChooserPanel(mainFrame(),prefs,
170 Globals.getString("PREFS_COLORS")));
171 addModule(module);
172
173 /* checksums */
174 ChecksumFactory cf = (ChecksumFactory) BeanPrefModule.loadBean(prefs, "checksum",
175 ChecksumFactory.class);
176 if(cf != null){
177 cf.setClassLoaderProvider(getClassLoaderProvider());
178 BeanPrefModule bpm = BeanPrefModule.getInstance(
179 Globals.getString("CHECKSUMS"),
180 prefs, "checksum", cf);
181 if(bpm != null){
182 addModule(bpm);
183 }
184 }
185
186 module = new VBoxModule(Globals.getString("PREF_GENERAL"));
187 final JCheckBox cb = new JCheckBox(Globals.getString("PREF_ANALYZE_VBR"));
188 cb.setSelected(prefs.getBoolean("analyzeVBRFiles", false));
189 PrefPanel analyzeVBR = new PrefPanel(mainFrame(), prefs, "VBR files"){
190 public void save(){
191 getPrefs().putBoolean("analyzeVBRFiles", cb.isSelected());
192 }
193 };
194 analyzeVBR.add(cb, BorderLayout.NORTH);
195 module.addPanel(analyzeVBR);
196 addModule(module);
197
198 /* modulePane */
199
200
201 /* buttons */
202 mainButtons = new JPanel();
203 buttonOK = new JButton();
204 buttonCancel = new JButton();
205 buttonOK.setText(Globals.OK);
206 buttonCancel.setText(Globals.CANCEL);
207 buttonOK.addActionListener(this);
208 buttonCancel.addActionListener(this);
209 mainButtons.add(buttonCancel);
210 mainButtons.add(buttonOK);
211
212 /* pref tree & navigation pane
213 * must be built AFTER modules have been added
214 */
215 prefTree = new JTree(prefRoot);
216 prefTree.getSelectionModel().setSelectionMode
217 (TreeSelectionModel.SINGLE_TREE_SELECTION);
218 prefTree.setBorder(new EmptyBorder(5,5,5,5));
219 prefTree.makeVisible(prefTree.getNextMatch(
220 fields.getTreeNode().toString(), 0, Bias.Forward));
221
222 /* contentPane */
223 final JPanel contentPane = new JPanel() {
224 public void paint(Graphics g){
225 growIfNeeded();
226 super.paint(g);
227 }
228 };
229 contentPane.setLayout(new GridBagLayout());
230 GridBagConstraints gbc = new GridBagConstraints();
231 gbc.fill = GridBagConstraints.BOTH;
232 gbc.gridx = 0;
233 gbc.gridy = 0;
234 gbc.weighty = 1;
235 gbc.weightx = 0;
236 gbc.gridheight = 1;
237 gbc.gridwidth = 1;
238
239 contentPane.add(prefTree, gbc);
240
241 gbc.gridx = 1;
242 gbc.weightx = 1;
243
244 contentPane.add(modulePane, gbc);
245
246 gbc.gridx = 0;
247 gbc.gridy = 1;
248 gbc.weighty = 0;
249 gbc.gridheight = 1;
250 gbc.gridwidth = 2;
251
252 contentPane.add(mainButtons, gbc);
253
254 /* tree listener for navigating modules */
255 prefTree.addTreeSelectionListener(new TreeSelectionListener() {
256 public void valueChanged(TreeSelectionEvent e) {
257 DefaultMutableTreeNode node = (DefaultMutableTreeNode)
258 prefTree.getLastSelectedPathComponent();
259 if (node == null){
260 return;
261 }
262 Object obj = node.getUserObject();
263 if(obj instanceof PrefModule){
264 showModule((PrefModule) obj);
265 }
266 }
267 });
268
269 setContentPane(contentPane);
270 growIfNeeded();
271 }
272
273 private void showModule(PrefModule mod){
274 String newModule = mod.toString();
275 CardLayout layout = (CardLayout) modulePane.getLayout();
276 layout.show(modulePane, newModule);
277 }
278
279 private PrefModule getActiveModule(){
280 int count = modulePane.getComponentCount();
281 if(count == 0){
282 return null;
283 }
284 Component c = modulePane.getComponent(0);
285 return (c instanceof PrefModule)? ((PrefModule) c) : null;
286 }
287
288 private void growIfNeeded(){
289 Dimension nps = new Dimension(modulePane.getPreferredSize());
290 Dimension pts = prefTree.getPreferredSize();
291 nps.width = nps.width + pts.width + 10;
292 nps.height = Math.max(nps.height, pts.height) + 10 + mainButtons.getPreferredSize().height;
293 Dimension curSize = getContentPane().getSize();
294 nps.width = Math.max(curSize.width, nps.width);
295 nps.height = Math.max(curSize.height, nps.height);
296 if(! nps.equals(curSize)){
297 ((JComponent) getContentPane()).setPreferredSize(nps);
298 pack();
299 }
300 }
301
302 private void addModule(PrefModule mod){
303 myModules.add(mod);
304 prefRoot.add(mod.getTreeNode());
305 modulePane.add(mod.getComponent(), mod.toString());
306 }
307
308 private void addModule(PrefModule mod, DefaultMutableTreeNode node){
309 myModules.add(mod);
310 node.add(mod.getTreeNode());
311 modulePane.add(mod.getComponent(), mod.toString());
312 }
313
314
315 /**Overridden so we can exit when window is closed*/
316 protected void processWindowEvent(WindowEvent e){
317 if (e.getID() == WindowEvent.WINDOW_CLOSING){
318 dispose();
319 }
320 super.processWindowEvent(e);
321 }
322
323
324 private boolean isSavable(){
325 Iterator moduleIt = myModules.iterator();
326 PrefModule mod;
327 boolean result = true;
328 while(moduleIt.hasNext()){
329 mod = (PrefModule) moduleIt.next();
330 if(!mod.isSavable()){
331 showModule(mod);
332 mod.showInvalidPanel();
333 result = false;
334 break;
335 }
336 }
337 return result;
338 }
339
340
341 private boolean save(){
342 if(!isSavable()){
343 return false;
344 }
345 for (int tab=0; tab<4; tab++){
346 Preferences p = getModel(tab).getPrefs();
347 //Delete old filters
348 try{
349 p.node("filters").clear();
350 } catch (java.util.prefs.BackingStoreException ex){
351 logger.severe("Error deleting old filters.");
352 logger.severe(ex.toString());
353 continue;
354 }
355 }
356 /* Delete old external applications */
357 try{
358 ExternalApplication.getPrefNode().clear();
359 } catch (java.util.prefs.BackingStoreException ex){
360 logger.severe("Error deleting old external applications.");
361 logger.severe(ex.toString());
362 }
363 try {
364 prefs.flush();
365 } catch (java.util.prefs.BackingStoreException ex){
366 logger.severe("Error flushing preferences.");
367 logger.severe(ex.toString());
368 }
369 Iterator moduleIt = myModules.iterator();
370 while(moduleIt.hasNext()){
371 try{
372 ((PrefModule) moduleIt.next()).save();
373 } catch (java.util.prefs.BackingStoreException ex){
374 logger.severe("Error saving preferences.");
375 logger.severe(ex.toString());
376 }
377 }
378 return true;
379 }
380
381 /**Handle button event*/
382 public void actionPerformed(ActionEvent e) {
383 if (e.getSource() == buttonCancel) {
384 dispose();
385 }
386 if (e.getSource() == buttonOK) {
387 if(save()){
388 dispose();
389 }
390 }
391 }
392
393 private MainFrame mainFrame(){
394 return (MainFrame) getOwner();
395 }
396
397 public ClassLoaderProvider getClassLoaderProvider(){
398 return (mainFrame() == null)? null : mainFrame().app;
399 }
400
401 private Mp3dingsTableModel getModel(int tab){
402 try{
403 return mainFrame().getModel(tab);
404 } catch (ClassCastException ex){
405 return null;
406 }
407 }
408 }