| 1 |
/* |
|---|
| 2 |
* LastPod is an application used to publish one's iPod play counts to Last.fm. |
|---|
| 3 |
* Copyright (C) 2007 muti, 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 |
* package org.lastpod; |
|---|
| 19 |
*/ |
|---|
| 20 |
package org.lastpod; |
|---|
| 21 |
|
|---|
| 22 |
import java.awt.Dimension; |
|---|
| 23 |
import java.awt.Toolkit; |
|---|
| 24 |
import java.awt.event.ActionEvent; |
|---|
| 25 |
import java.awt.event.ActionListener; |
|---|
| 26 |
import java.awt.event.KeyEvent; |
|---|
| 27 |
import java.awt.event.MouseEvent; |
|---|
| 28 |
import java.awt.event.MouseListener; |
|---|
| 29 |
|
|---|
| 30 |
import java.io.File; |
|---|
| 31 |
|
|---|
| 32 |
import java.util.ArrayList; |
|---|
| 33 |
import java.util.logging.Level; |
|---|
| 34 |
import java.util.logging.Logger; |
|---|
| 35 |
import java.util.prefs.Preferences; |
|---|
| 36 |
|
|---|
| 37 |
import javax.swing.BorderFactory; |
|---|
| 38 |
import javax.swing.BoxLayout; |
|---|
| 39 |
import javax.swing.JButton; |
|---|
| 40 |
import javax.swing.JCheckBox; |
|---|
| 41 |
import javax.swing.JFileChooser; |
|---|
| 42 |
import javax.swing.JFrame; |
|---|
| 43 |
import javax.swing.JLabel; |
|---|
| 44 |
import javax.swing.JOptionPane; |
|---|
| 45 |
import javax.swing.JPanel; |
|---|
| 46 |
import javax.swing.JPasswordField; |
|---|
| 47 |
import javax.swing.JTextField; |
|---|
| 48 |
import javax.swing.SpringLayout; |
|---|
| 49 |
import javax.swing.border.TitledBorder; |
|---|
| 50 |
|
|---|
| 51 |
/** |
|---|
| 52 |
* @author Muti |
|---|
| 53 |
* @version $Id$ |
|---|
| 54 |
*/ |
|---|
| 55 |
public class PreferencesEditor { |
|---|
| 56 |
private Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); |
|---|
| 57 |
private JFrame frame; |
|---|
| 58 |
private SpringLayout layout; |
|---|
| 59 |
private JTextField userfield; |
|---|
| 60 |
private JPasswordField passfield; |
|---|
| 61 |
private JTextField dbfield; |
|---|
| 62 |
private JTextField iTunesfield; |
|---|
| 63 |
private JCheckBox iTCheck; |
|---|
| 64 |
private JLabel iTunesStatus; |
|---|
| 65 |
private JButton browsebuttoniTunes; |
|---|
| 66 |
|
|---|
| 67 |
public PreferencesEditor() { |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
public void buildUI() { |
|---|
| 71 |
this.frame = new JFrame("Preferences..."); |
|---|
| 72 |
this.layout = new SpringLayout(); |
|---|
| 73 |
this.frame.getContentPane().setLayout(this.layout); |
|---|
| 74 |
|
|---|
| 75 |
this.addElements(); |
|---|
| 76 |
this.matchPreferences(); //gets preferences from LastPod and updates UI |
|---|
| 77 |
|
|---|
| 78 |
this.frame.pack(); |
|---|
| 79 |
|
|---|
| 80 |
int x = |
|---|
| 81 |
(int) ((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (frame.getWidth() / 2)); |
|---|
| 82 |
int y = |
|---|
| 83 |
(int) ((Toolkit.getDefaultToolkit().getScreenSize().height / 2) |
|---|
| 84 |
- (frame.getHeight() / 2)); |
|---|
| 85 |
frame.setLocation(x, y); |
|---|
| 86 |
this.frame.setResizable(false); |
|---|
| 87 |
this.frame.setVisible(true); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
private void addElements() { |
|---|
| 91 |
JPanel p = new JPanel(); |
|---|
| 92 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
|---|
| 93 |
p.setOpaque(true); |
|---|
| 94 |
this.frame.setContentPane(p); |
|---|
| 95 |
|
|---|
| 96 |
//Username and password panel |
|---|
| 97 |
JPanel p2 = new JPanel(); |
|---|
| 98 |
p2.setLayout(new SpringLayout()); |
|---|
| 99 |
|
|---|
| 100 |
TitledBorder b2 = BorderFactory.createTitledBorder("AudioScrobbler/Last.fm:"); |
|---|
| 101 |
p2.setBorder(b2); |
|---|
| 102 |
p2.setToolTipText( |
|---|
| 103 |
"<html>This entry is used if you want to <b>Submit Tracks</b> to AudioScrobbler/Last.fm"); |
|---|
| 104 |
|
|---|
| 105 |
JLabel userlabel = new JLabel("Username:"); |
|---|
| 106 |
p2.add(userlabel); |
|---|
| 107 |
this.userfield = new JTextField(); |
|---|
| 108 |
p2.add(this.userfield); |
|---|
| 109 |
|
|---|
| 110 |
JLabel passlabel = new JLabel("Password:"); |
|---|
| 111 |
p2.add(passlabel); |
|---|
| 112 |
this.passfield = new JPasswordField(); |
|---|
| 113 |
p2.add(this.passfield); |
|---|
| 114 |
|
|---|
| 115 |
SpringUtilities.makeCompactGrid(p2, 2, 2, 5, 4, 3, 4); |
|---|
| 116 |
p.add(p2); |
|---|
| 117 |
|
|---|
| 118 |
//iPod Panel |
|---|
| 119 |
JPanel p1 = new JPanel(); |
|---|
| 120 |
p1.setLayout(new SpringLayout()); |
|---|
| 121 |
|
|---|
| 122 |
TitledBorder b1 = BorderFactory.createTitledBorder("iPod:"); |
|---|
| 123 |
p1.setBorder(b1); |
|---|
| 124 |
p1.setToolTipText( |
|---|
| 125 |
"<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."); |
|---|
| 126 |
|
|---|
| 127 |
JLabel dblabel = new JLabel("Location of iTunesDB:"); |
|---|
| 128 |
p1.add(dblabel); |
|---|
| 129 |
this.dbfield = new JTextField(); |
|---|
| 130 |
this.dbfield.setPreferredSize(new Dimension(200, 20)); |
|---|
| 131 |
p1.add(this.dbfield); |
|---|
| 132 |
|
|---|
| 133 |
JButton BrowseButtoniPod = new JButton("Browse.."); |
|---|
| 134 |
BrowseButtoniPod.setMnemonic(KeyEvent.VK_B); |
|---|
| 135 |
BrowseButtoniPod.addActionListener(new BrowseButtonListeneriPod()); |
|---|
| 136 |
p1.add(BrowseButtoniPod); |
|---|
| 137 |
SpringUtilities.makeCompactGrid(p1, 1, 3, 5, 4, 3, 4); |
|---|
| 138 |
p.add(p1); |
|---|
| 139 |
|
|---|
| 140 |
//iTunes Panel |
|---|
| 141 |
JPanel p4 = new JPanel(); |
|---|
| 142 |
p4.setLayout(new SpringLayout()); |
|---|
| 143 |
|
|---|
| 144 |
TitledBorder b4 = BorderFactory.createTitledBorder("iTunes:"); |
|---|
| 145 |
p4.setBorder(b4); |
|---|
| 146 |
p4.setToolTipText("Enable if you want to use audioPod with iTunes"); |
|---|
| 147 |
|
|---|
| 148 |
JPanel p41 = new JPanel(); |
|---|
| 149 |
iTCheck = new JCheckBox(); |
|---|
| 150 |
iTCheck.addActionListener(new ActionListener() { |
|---|
| 151 |
public void actionPerformed(ActionEvent e) { |
|---|
| 152 |
if (iTCheck.isSelected()) { |
|---|
| 153 |
iTunesStatus.setText("Enabled"); |
|---|
| 154 |
} else { |
|---|
| 155 |
iTunesStatus.setText("Disabled"); |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
}); |
|---|
| 159 |
p41.add(iTCheck); |
|---|
| 160 |
iTunesStatus = new JLabel(); |
|---|
| 161 |
iTunesStatus.addMouseListener(new MouseListener() { |
|---|
| 162 |
public void mouseClicked(MouseEvent e) { |
|---|
| 163 |
if (iTunesStatus.getText().equals("Disabled")) { |
|---|
| 164 |
iTCheck.setSelected(true); |
|---|
| 165 |
iTunesStatus.setText("Enabled"); |
|---|
| 166 |
iTunesfield.setEditable(true); |
|---|
| 167 |
browsebuttoniTunes.setEnabled(true); |
|---|
| 168 |
} else { |
|---|
| 169 |
iTCheck.setSelected(false); |
|---|
| 170 |
iTunesStatus.setText("Disabled"); |
|---|
| 171 |
iTunesfield.setEditable(false); |
|---|
| 172 |
browsebuttoniTunes.setEnabled(false); |
|---|
| 173 |
} |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
public void mouseEntered(MouseEvent e) { |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
public void mouseExited(MouseEvent e) { |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
public void mouseReleased(MouseEvent e) { |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
public void mousePressed(MouseEvent e) { |
|---|
| 186 |
} |
|---|
| 187 |
}); |
|---|
| 188 |
p41.add(iTunesStatus); |
|---|
| 189 |
|
|---|
| 190 |
p4.add(p41); |
|---|
| 191 |
p4.add(new JLabel()); |
|---|
| 192 |
p4.add(new JLabel()); |
|---|
| 193 |
|
|---|
| 194 |
JLabel exelabel = new JLabel("Location of iTunes.exe:"); |
|---|
| 195 |
p4.add(exelabel); |
|---|
| 196 |
this.iTunesfield = new JTextField(); |
|---|
| 197 |
this.iTunesfield.setPreferredSize(new Dimension(200, 20)); |
|---|
| 198 |
p4.add(this.iTunesfield); |
|---|
| 199 |
|
|---|
| 200 |
browsebuttoniTunes = new JButton("Browse.."); |
|---|
| 201 |
browsebuttoniTunes.setMnemonic(KeyEvent.VK_R); |
|---|
| 202 |
browsebuttoniTunes.addActionListener(new BrowseButtonListenerITunes()); |
|---|
| 203 |
p4.add(browsebuttoniTunes); |
|---|
| 204 |
SpringUtilities.makeCompactGrid(p4, 2, 3, 5, 4, 3, 4); |
|---|
| 205 |
p.add(p4); |
|---|
| 206 |
|
|---|
| 207 |
//Options Panel |
|---|
| 208 |
JPanel p3 = new JPanel(); |
|---|
| 209 |
p3.setLayout(new SpringLayout()); |
|---|
| 210 |
|
|---|
| 211 |
TitledBorder b3 = BorderFactory.createTitledBorder("Options:"); |
|---|
| 212 |
p3.setBorder(b3); |
|---|
| 213 |
|
|---|
| 214 |
String toolTip = |
|---|
| 215 |
"<html>If a URL is entered the play information will be" |
|---|
| 216 |
+ " submitted to both Last.fm and the given URL. This allows one" |
|---|
| 217 |
+ " to perform a backup of the Last.fm data."; |
|---|
| 218 |
p3.setToolTipText(toolTip); |
|---|
| 219 |
|
|---|
| 220 |
JLabel j3 = new JLabel("Backup URL: "); |
|---|
| 221 |
p3.add(j3); |
|---|
| 222 |
p3.add(new JTextField()); |
|---|
| 223 |
|
|---|
| 224 |
SpringUtilities.makeCompactGrid(p3, 1, 2, 5, 2, 3, 4); |
|---|
| 225 |
p.add(p3); |
|---|
| 226 |
|
|---|
| 227 |
JPanel p5 = new JPanel(); |
|---|
| 228 |
|
|---|
| 229 |
JButton findPathsButton = new JButton("Find Paths"); |
|---|
| 230 |
findPathsButton.setMnemonic(KeyEvent.VK_F); |
|---|
| 231 |
findPathsButton.setToolTipText("Will try to find the paths for iTunesDB and iTunes.exe"); |
|---|
| 232 |
findPathsButton.addActionListener(new ActionListener() { |
|---|
| 233 |
public void actionPerformed(ActionEvent e) { |
|---|
| 234 |
/* Scan for iTunesDB. */ |
|---|
| 235 |
File itdb = new File(dbfield.getText()); |
|---|
| 236 |
File itdbFile = new File(dbfield.getText() + "\\Play Counts"); |
|---|
| 237 |
boolean dbFound = false; |
|---|
| 238 |
|
|---|
| 239 |
if (itdbFile.isFile() || itdb.isFile() |
|---|
| 240 |
|| itdb.getAbsolutePath().endsWith(":\\iPod_Control\\iTunes")) { |
|---|
| 241 |
iTunesfield.setText(itdb.getAbsolutePath()); |
|---|
| 242 |
dbFound = true; |
|---|
| 243 |
} else { |
|---|
| 244 |
for (int i = (int) 'B'; i <= (int) 'Z'; i++) { |
|---|
| 245 |
itdb = new File((char) i + ":\\iPod_Control\\iTunes\\iTunesDB"); |
|---|
| 246 |
|
|---|
| 247 |
if (itdb.isFile()) { |
|---|
| 248 |
dbfield.setText(itdb.getParent()); |
|---|
| 249 |
dbFound = true; |
|---|
| 250 |
|
|---|
| 251 |
break; |
|---|
| 252 |
} |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
if (!dbFound) { |
|---|
| 256 |
JOptionPane.showMessageDialog(new JFrame(), |
|---|
| 257 |
"iTunesDB location not found. Are you sure your iPod is plugged in?"); |
|---|
| 258 |
dbfield.setText("<iPod iTunes Database Location>"); |
|---|
| 259 |
} |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
/* Scan for iTunes. */ |
|---|
| 263 |
File itProgram = new File(iTunesfield.getText()); |
|---|
| 264 |
File itProgramFile = new File(iTunesfield.getText() + "\\iTunes.exe"); |
|---|
| 265 |
|
|---|
| 266 |
if (itProgram.isFile()) { |
|---|
| 267 |
iTunesfield.setText(itProgram.getParent()); |
|---|
| 268 |
} else if (itProgramFile.isFile()) { |
|---|
| 269 |
iTunesfield.setText(itProgramFile.getParent()); |
|---|
| 270 |
} else { |
|---|
| 271 |
for (int i = (int) 'B'; i <= (int) 'Z'; i++) { |
|---|
| 272 |
itProgram = new File((char) i + ":\\Program Files\\iTunes\\iTunes.exe"); |
|---|
| 273 |
|
|---|
| 274 |
if (itProgram.isFile()) { |
|---|
| 275 |
iTunesfield.setText(itProgram.getParent()); |
|---|
| 276 |
|
|---|
| 277 |
break; |
|---|
| 278 |
} |
|---|
| 279 |
} |
|---|
| 280 |
} |
|---|
| 281 |
} |
|---|
| 282 |
}); |
|---|
| 283 |
p5.add(findPathsButton); |
|---|
| 284 |
|
|---|
| 285 |
JButton okbutton = new JButton("OK"); |
|---|
| 286 |
okbutton.setMnemonic(KeyEvent.VK_O); |
|---|
| 287 |
okbutton.addActionListener(new ActionListener() { |
|---|
| 288 |
public void actionPerformed(ActionEvent e) { |
|---|
| 289 |
savePreferences(); |
|---|
| 290 |
frame.dispose(); |
|---|
| 291 |
} |
|---|
| 292 |
}); |
|---|
| 293 |
p5.add(okbutton); |
|---|
| 294 |
|
|---|
| 295 |
JButton cancelbutton = new JButton("Cancel"); |
|---|
| 296 |
cancelbutton.setMnemonic(KeyEvent.VK_C); |
|---|
| 297 |
cancelbutton.addActionListener(new ActionListener() { |
|---|
| 298 |
public void actionPerformed(ActionEvent e) { |
|---|
| 299 |
frame.dispose(); |
|---|
| 300 |
} |
|---|
| 301 |
}); |
|---|
| 302 |
p5.add(cancelbutton); |
|---|
| 303 |
|
|---|
| 304 |
p.add(p5); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
private void matchPreferences() { |
|---|
| 308 |
this.userfield.setText(fPrefs.get("Username", "<Username>")); |
|---|
| 309 |
this.dbfield.setText(fPrefs.get("iTunes Path", "<iPod iTunes Database Location>")); |
|---|
| 310 |
this.iTunesfield.setText(fPrefs.get("iT Path", " < Enter iTunes path > ")); |
|---|
| 311 |
|
|---|
| 312 |
if (fPrefs.get("iTunes Status", "Enabled").equals("Enabled")) { |
|---|
| 313 |
this.iTunesStatus.setText("Enabled"); |
|---|
| 314 |
this.iTCheck.setSelected(true); |
|---|
| 315 |
this.iTunesfield.setEditable(true); |
|---|
| 316 |
this.browsebuttoniTunes.setEnabled(true); |
|---|
| 317 |
} else { |
|---|
| 318 |
this.iTunesStatus.setText("Disabled"); |
|---|
| 319 |
this.iTCheck.setSelected(false); |
|---|
| 320 |
this.iTunesfield.setEditable(false); |
|---|
| 321 |
this.browsebuttoniTunes.setEnabled(false); |
|---|
| 322 |
} |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
private void savePreferences() { |
|---|
| 326 |
fPrefs.put("Username", this.userfield.getText()); |
|---|
| 327 |
|
|---|
| 328 |
String password = new String(passfield.getPassword()); |
|---|
| 329 |
|
|---|
| 330 |
if (password.length() != 0) { |
|---|
| 331 |
String encryptedPassword = MiscUtilities.md5DigestPassword(password); |
|---|
| 332 |
fPrefs.put("encryptedPassword", encryptedPassword); |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
fPrefs.put("iTunes Path", this.dbfield.getText()); |
|---|
| 336 |
fPrefs.put("iT Path", this.iTunesfield.getText()); |
|---|
| 337 |
fPrefs.put("iTunes Status", this.iTunesStatus.getText()); |
|---|
| 338 |
|
|---|
| 339 |
try { |
|---|
| 340 |
fPrefs.flush(); |
|---|
| 341 |
} catch (Exception e) { |
|---|
| 342 |
Logger logger = Logger.getLogger(this.getClass().getPackage().getName()); |
|---|
| 343 |
logger.log(Level.WARNING, "Unable to save preferences: " + e.toString()); |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
LastPod.recentplayed = new ArrayList(); |
|---|
| 347 |
LastPod.ParsePlayCounts(); |
|---|
| 348 |
LastPod.UI.newTrackListAvailable(); |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
private class BrowseButtonListeneriPod implements ActionListener { |
|---|
| 352 |
public void actionPerformed(ActionEvent e) { |
|---|
| 353 |
JFileChooser fc = new JFileChooser(); |
|---|
| 354 |
fc.setFileHidingEnabled(false); |
|---|
| 355 |
fc.setFileFilter(new ItunesdbFilter()); |
|---|
| 356 |
|
|---|
| 357 |
File d = new File(dbfield.getText()); |
|---|
| 358 |
|
|---|
| 359 |
if (d.isDirectory()) { |
|---|
| 360 |
fc.setCurrentDirectory(d); |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
int returnVal = fc.showOpenDialog(frame); |
|---|
| 364 |
|
|---|
| 365 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
|---|
| 366 |
File f = fc.getSelectedFile(); |
|---|
| 367 |
dbfield.setText(f.getParent()); |
|---|
| 368 |
} |
|---|
| 369 |
} |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|
| 372 |
private class BrowseButtonListenerITunes implements ActionListener { |
|---|
| 373 |
public void actionPerformed(ActionEvent e) { |
|---|
| 374 |
JFileChooser fc = new JFileChooser(); |
|---|
| 375 |
fc.setFileHidingEnabled(false); |
|---|
| 376 |
|
|---|
| 377 |
File d = new File(iTunesfield.getText()); |
|---|
| 378 |
|
|---|
| 379 |
if (d.isDirectory()) { |
|---|
| 380 |
fc.setCurrentDirectory(d); |
|---|
| 381 |
} else if (d.isFile()) { |
|---|
| 382 |
fc.setCurrentDirectory(d.getParentFile()); |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
fc.setFileFilter(new ExeFileFilter()); |
|---|
| 386 |
|
|---|
| 387 |
int returnVal = fc.showOpenDialog(frame); |
|---|
| 388 |
|
|---|
| 389 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
|---|
| 390 |
File f = fc.getSelectedFile(); |
|---|
| 391 |
iTunesfield.setText(f.getParent()); |
|---|
| 392 |
} |
|---|
| 393 |
} |
|---|
| 394 |
} |
|---|
| 395 |
} |
|---|