root/trunk/src/main/org/lastpod/PreferencesEditor.java

Revision 58, 16.4 kB (checked in by chris, 3 years ago)

minor cleanups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 /*
2  * LastPod is an application used to publish one's iPod play counts to Last.fm.
3  * Copyright (C) 2007  Chris Tilden
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 package org.lastpod;
20
21 import org.lastpod.util.MiscUtilities;
22 import org.lastpod.util.SpringUtilities;
23
24 import java.awt.Dimension;
25 import java.awt.Toolkit;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31
32 import java.io.File;
33
34 import java.util.ArrayList;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import java.util.prefs.Preferences;
38
39 import javax.swing.BorderFactory;
40 import javax.swing.BoxLayout;
41 import javax.swing.JButton;
42 import javax.swing.JCheckBox;
43 import javax.swing.JDialog;
44 import javax.swing.JFileChooser;
45 import javax.swing.JFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JOptionPane;
48 import javax.swing.JPanel;
49 import javax.swing.JPasswordField;
50 import javax.swing.JTextField;
51 import javax.swing.SpringLayout;
52 import javax.swing.border.TitledBorder;
53
54 /**
55  * @author Muti
56  * @version $Id: /lastpod/local/src/main/org/lastpod/PreferencesEditor.java 7692
57  *          2007-05-07T05:45:17.332896Z chris $
58  */
59 public class PreferencesEditor {
60     private Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod");
61     private JFrame mainFrame;
62     private JDialog frame;
63     private SpringLayout layout;
64     private JTextField userfield;
65     private JPasswordField passfield;
66     private JTextField dbfield;
67     private JTextField backupUrlField;
68     private JCheckBox parseVariousArtistsCheck;
69     private JTextField iTunesfield;
70     private JCheckBox iTCheck;
71     private JLabel iTunesStatus;
72     private JButton browsebuttoniTunes;
73
74     public PreferencesEditor(JFrame mainFrame) {
75         this.mainFrame = mainFrame;
76     }
77
78     public void buildUI() {
79         this.frame = new JDialog(mainFrame, "Preferences...", true);
80         this.layout = new SpringLayout();
81         this.frame.getContentPane().setLayout(this.layout);
82
83         this.addElements();
84         this.matchPreferences(); //gets preferences from LastPod and updates UI
85
86         this.frame.pack();
87
88         int x =
89             (int) ((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (frame.getWidth() / 2));
90         int y =
91             (int) ((Toolkit.getDefaultToolkit().getScreenSize().height / 2)
92             - (frame.getHeight() / 2));
93         frame.setLocation(x, y);
94         this.frame.setResizable(false);
95         this.frame.setVisible(true);
96     }
97
98     private void addElements() {
99         JPanel p = new JPanel();
100         p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
101         p.setOpaque(true);
102         this.frame.setContentPane(p);
103
104         //Username and password panel
105         JPanel p2 = new JPanel();
106         p2.setLayout(new SpringLayout());
107
108         TitledBorder b2 = BorderFactory.createTitledBorder("AudioScrobbler/Last.fm:");
109         p2.setBorder(b2);
110         p2.setToolTipText(
111             "<html>This entry is used if you want to <b>Submit Tracks</b> to AudioScrobbler/Last.fm");
112
113         JLabel userlabel = new JLabel("Username:");
114         p2.add(userlabel);
115         this.userfield = new JTextField();
116         p2.add(this.userfield);
117
118         JLabel passlabel = new JLabel("Password:");
119         p2.add(passlabel);
120         this.passfield = new JPasswordField();
121         p2.add(this.passfield);
122
123         SpringUtilities.makeCompactGrid(p2, 2, 2, 5, 4, 3, 4);
124         p.add(p2);
125
126         //iPod Panel
127         JPanel p1 = new JPanel();
128         p1.setLayout(new SpringLayout());
129
130         TitledBorder b1 = BorderFactory.createTitledBorder("iPod:");
131         p1.setBorder(b1);
132         p1.setToolTipText(
133             "<html>Path of the iTunesDB:<br>Located on your iPod, e.g. <b>M</b>:\\iPod_Control\\iTunes,<br>where M is the drive letter of your iPod.");
134
135         JLabel dblabel = new JLabel("Location of iTunesDB:");
136         p1.add(dblabel);
137         this.dbfield = new JTextField();
138         this.dbfield.setPreferredSize(new Dimension(200, 20));
139         p1.add(this.dbfield);
140
141         JButton BrowseButtoniPod = new JButton("Browse..");
142         BrowseButtoniPod.setMnemonic(KeyEvent.VK_B);
143         BrowseButtoniPod.addActionListener(new BrowseButtonListeneriPod());
144         p1.add(BrowseButtoniPod);
145         SpringUtilities.makeCompactGrid(p1, 1, 3, 5, 4, 3, 4);
146         p.add(p1);
147
148         //iTunes Panel
149         JPanel p4 = new JPanel();
150         p4.setLayout(new SpringLayout());
151         p.add(new JLabel());
152
153         TitledBorder b4 = BorderFactory.createTitledBorder("iTunes:");
154         p4.setBorder(b4);
155         p4.setToolTipText("Enable if you want to use audioPod with iTunes");
156
157         JPanel p41 = new JPanel();
158         iTCheck = new JCheckBox();
159         iTCheck.addActionListener(new ActionListener() {
160                 public void actionPerformed(ActionEvent e) {
161                     if (iTCheck.isSelected()) {
162                         iTunesStatus.setText("Enabled");
163                     } else {
164                         iTunesStatus.setText("Disabled");
165                     }
166                 }
167             });
168         p41.add(iTCheck);
169         iTunesStatus = new JLabel();
170         iTunesStatus.addMouseListener(new MouseListener() {
171                 public void mouseClicked(MouseEvent e) {
172                     if (iTunesStatus.getText().equals("Disabled")) {
173                         iTCheck.setSelected(true);
174                         iTunesStatus.setText("Enabled");
175                         iTunesfield.setEditable(true);
176                         browsebuttoniTunes.setEnabled(true);
177                     } else {
178                         iTCheck.setSelected(false);
179                         iTunesStatus.setText("Disabled");
180                         iTunesfield.setEditable(false);
181                         browsebuttoniTunes.setEnabled(false);
182                     }
183                 }
184
185                 public void mouseEntered(MouseEvent e) {
186                 }
187
188                 public void mouseExited(MouseEvent e) {
189                 }
190
191                 public void mouseReleased(MouseEvent e) {
192                 }
193
194                 public void mousePressed(MouseEvent e) {
195                 }
196             });
197         p41.add(iTunesStatus);
198
199         p4.add(p41);
200         p4.add(new JLabel());
201         p4.add(new JLabel());
202
203         JLabel exelabel = new JLabel("Location of iTunes.exe:");
204         p4.add(exelabel);
205         this.iTunesfield = new JTextField();
206         this.iTunesfield.setPreferredSize(new Dimension(200, 20));
207         p4.add(this.iTunesfield);
208
209         browsebuttoniTunes = new JButton("Browse..");
210         browsebuttoniTunes.setMnemonic(KeyEvent.VK_R);
211         browsebuttoniTunes.addActionListener(new BrowseButtonListenerITunes());
212         p4.add(browsebuttoniTunes);
213         SpringUtilities.makeCompactGrid(p4, 2, 3, 5, 4, 3, 4);
214         p.add(p4);
215
216         //Options Panel
217         JPanel p3 = new JPanel();
218         p3.setLayout(new SpringLayout());
219
220         TitledBorder b3 = BorderFactory.createTitledBorder("Options:");
221         p3.setBorder(b3);
222
223         String toolTip =
224             "<html>If a URL is entered the play information will be<br>"
225             + " submitted to both Last.fm and the given URL.  This allows one<br>"
226             + " to perform a backup of the Last.fm data.<br><br>"
227             + " If the parse option is checked then LastPod will parse the track<br>"
228             + " information when the artist is \"Various Artists\".  The<br>"
229             + " parsing consists of spliting the artist and track from the<br>"
230             + "orgininal track String.  (For example, \"Bing Crosby - <br>"
231             + "I'll Be Home for Christmas\" becomes artist=Bing Crosby<br>"
232             + "and track name=I'll Be Home for Christmas.";
233         p3.setToolTipText(toolTip);
234
235         JLabel backupUrlLabel = new JLabel("Backup URL: ");
236         p3.add(backupUrlLabel);
237         backupUrlField = new JTextField();
238         p3.add(backupUrlField);
239
240         JLabel parseVariousArtistsLabel = new JLabel("Parse \"Various Artists\" Tracks: ");
241         p3.add(parseVariousArtistsLabel);
242         parseVariousArtistsCheck = new JCheckBox();
243         parseVariousArtistsCheck.addActionListener(new ActionListener() {
244                 public void actionPerformed(ActionEvent e) {
245                     if (parseVariousArtistsCheck.isSelected()) {
246                         parseVariousArtistsCheck.setText("Enabled");
247                     } else {
248                         parseVariousArtistsCheck.setText("Disabled");
249                     }
250                 }
251             });
252         p3.add(parseVariousArtistsCheck);
253
254         SpringUtilities.makeCompactGrid(p3, 2, 2, 5, 2, 3, 4);
255         p.add(p3);
256
257         JPanel p5 = new JPanel();
258
259         JButton findPathsButton = new JButton("Find Paths");
260         findPathsButton.setMnemonic(KeyEvent.VK_F);
261         findPathsButton.setToolTipText("Will try to find the paths for iTunesDB and iTunes.exe");
262         findPathsButton.addActionListener(new ActionListener() {
263                 public void actionPerformed(ActionEvent e) {
264                     /* Scan for iTunesDB. */
265                     File itdb = new File(dbfield.getText());
266                     File itdbFile = new File(dbfield.getText() + "\\Play Counts");
267                     boolean dbFound = false;
268
269                     if (itdbFile.isFile() || itdb.isFile()
270                             || itdb.getAbsolutePath().endsWith(":\\iPod_Control\\iTunes")) {
271                         iTunesfield.setText(itdb.getAbsolutePath());
272                         dbFound = true;
273                     } else {
274                         for (int i = (int) 'B'; i <= (int) 'Z'; i++) {
275                             itdb = new File((char) i + ":\\iPod_Control\\iTunes\\iTunesDB");
276
277                             if (itdb.isFile()) {
278                                 dbfield.setText(itdb.getParent());
279                                 dbFound = true;
280
281                                 break;
282                             }
283                         }
284
285                         if (!dbFound) {
286                             JOptionPane.showMessageDialog(new JFrame(),
287                                 "iTunesDB location not found. Are you sure your iPod is plugged in?");
288                             dbfield.setText("<iPod iTunes Database Location>");
289                         }
290                     }
291
292                     /* Scan for iTunes. */
293                     File itProgram = new File(iTunesfield.getText());
294                     File itProgramFile = new File(iTunesfield.getText() + "\\iTunes.exe");
295
296                     if (itProgram.isFile()) {
297                         iTunesfield.setText(itProgram.getParent());
298                     } else if (itProgramFile.isFile()) {
299                         iTunesfield.setText(itProgramFile.getParent());
300                     } else {
301                         for (int i = (int) 'B'; i <= (int) 'Z'; i++) {
302                             itProgram = new File((char) i + ":\\Program Files\\iTunes\\iTunes.exe");
303
304                             if (itProgram.isFile()) {
305                                 iTunesfield.setText(itProgram.getParent());
306
307                                 break;
308                             }
309                         }
310                     }
311                 }
312             });
313         p5.add(findPathsButton);
314
315         JButton okbutton = new JButton("OK");
316         okbutton.setMnemonic(KeyEvent.VK_O);
317         okbutton.addActionListener(new ActionListener() {
318                 public void actionPerformed(ActionEvent e) {
319                     savePreferences();
320                     frame.dispose();
321                 }
322             });
323         p5.add(okbutton);
324
325         JButton cancelbutton = new JButton("Cancel");
326         cancelbutton.setMnemonic(KeyEvent.VK_C);
327         cancelbutton.addActionListener(new ActionListener() {
328                 public void actionPerformed(ActionEvent e) {
329                     frame.dispose();
330                 }
331             });
332         p5.add(cancelbutton);
333
334         p.add(p5);
335     }
336
337     private void matchPreferences() {
338         this.userfield.setText(fPrefs.get("Username", "<Username>"));
339         this.dbfield.setText(fPrefs.get("iTunes Path", "<iPod iTunes Database Location>"));
340         this.backupUrlField.setText(fPrefs.get("backupUrl", ""));
341         this.iTunesfield.setText(fPrefs.get("iT Path", ""));
342
343         if (fPrefs.get("iTunes Status", "Enabled").equals("Enabled")) {
344             this.iTunesStatus.setText("Enabled");
345             this.iTCheck.setSelected(true);
346             this.iTunesfield.setEditable(true);
347             this.browsebuttoniTunes.setEnabled(true);
348         } else {
349             this.iTunesStatus.setText("Disabled");
350             this.iTCheck.setSelected(false);
351             this.iTunesfield.setEditable(false);
352             this.browsebuttoniTunes.setEnabled(false);
353         }
354
355         if (fPrefs.get("parseVariousArtists", "1").equals("1")) {
356             parseVariousArtistsCheck.setText("Enabled");
357             parseVariousArtistsCheck.setSelected(true);
358         } else {
359             parseVariousArtistsCheck.setText("Disabled");
360             parseVariousArtistsCheck.setSelected(false);
361         }
362     }
363
364     private void savePreferences() {
365         String oldItunesPath = fPrefs.get("iTunes Path", "<iPod iTunes Database Location>");
366         String newItunesPath = dbfield.getText();
367
368         fPrefs.put("Username", this.userfield.getText());
369
370         String password = new String(passfield.getPassword());
371
372         if (password.length() != 0) {
373             String encryptedPassword = MiscUtilities.md5DigestPassword(password);
374             fPrefs.put("encryptedPassword", encryptedPassword);
375         }
376
377         boolean selected = parseVariousArtistsCheck.isSelected();
378         String parseVariousArtists = selected ? "1" : "0";
379
380         fPrefs.put("iTunes Path", newItunesPath);
381         fPrefs.put("backupUrl", this.backupUrlField.getText());
382         fPrefs.put("iT Path", this.iTunesfield.getText());
383         fPrefs.put("iTunes Status", this.iTunesStatus.getText());
384         fPrefs.put("parseVariousArtists", parseVariousArtists);
385
386         try {
387             fPrefs.flush();
388         } catch (Exception e) {
389             Logger logger = Logger.getLogger(this.getClass().getPackage().getName());
390             logger.log(Level.WARNING, "Unable to save preferences: " + e.toString());
391         }
392
393         /* Only reloads the track list when the iTunes Path has been
394          * changed.
395          */
396         if (!oldItunesPath.equals(newItunesPath)) {
397             LastPod.recentplayed = new ArrayList();
398             LastPod.parsePlayCounts();
399             LastPod.UI.newTrackListAvailable();
400         }
401     }
402
403     private class BrowseButtonListeneriPod implements ActionListener {
404         public void actionPerformed(ActionEvent e) {
405             JFileChooser fc = new JFileChooser();
406             fc.setFileHidingEnabled(false);
407             fc.setFileFilter(new ItunesdbFilter());
408
409             File d = new File(dbfield.getText());
410
411             if (d.isDirectory()) {
412                 fc.setCurrentDirectory(d);
413             }
414
415             int returnVal = fc.showOpenDialog(frame);
416
417             if (returnVal == JFileChooser.APPROVE_OPTION) {
418                 File f = fc.getSelectedFile();
419                 dbfield.setText(f.getParent());
420             }
421         }
422     }
423
424     private class BrowseButtonListenerITunes implements ActionListener {
425         public void actionPerformed(ActionEvent e) {
426             JFileChooser fc = new JFileChooser();
427             fc.setFileHidingEnabled(false);
428
429             File d = new File(iTunesfield.getText());
430
431             if (d.isDirectory()) {
432                 fc.setCurrentDirectory(d);
433             } else if (d.isFile()) {
434                 fc.setCurrentDirectory(d.getParentFile());
435             }
436
437             fc.setFileFilter(new ExeFileFilter());
438
439             int returnVal = fc.showOpenDialog(frame);
440
441             if (returnVal == JFileChooser.APPROVE_OPTION) {
442                 File f = fc.getSelectedFile();
443                 iTunesfield.setText(f.getParent());
444             }
445         }
446     }
447 }
Note: See TracBrowser for help on using the browser.