Changeset 71
- Timestamp:
- 05/26/07 02:37:45 (1 year ago)
- Files:
-
- trunk/src/main/org/lastpod/LastPod.java (modified) (4 diffs)
- trunk/src/main/org/lastpod/Model.java (added)
- trunk/src/main/org/lastpod/ModelImpl.java (added)
- trunk/src/main/org/lastpod/PreferencesEditor.java (modified) (4 diffs)
- trunk/src/main/org/lastpod/RecentPanel.java (modified) (6 diffs)
- trunk/src/main/org/lastpod/UI.java (modified) (4 diffs)
- trunk/src/main/org/lastpod/action/DeletePlayCounts.java (modified) (5 diffs)
- trunk/src/main/org/lastpod/action/OpenPreferencesEditor.java (modified) (4 diffs)
- trunk/src/main/org/lastpod/action/SubmitTracks.java (modified) (5 diffs)
- trunk/src/main/org/lastpod/action/UnselectAll.java (modified) (5 diffs)
- trunk/src/test/org/lastpod/MockModel.java (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/org/lastpod/LastPod.java
r70 r71 19 19 package org.lastpod; 20 20 21 import org.lastpod.util.MiscUtilities;22 23 import java.io.IOException;24 25 21 import java.util.ArrayList; 26 22 import java.util.List; 27 23 import java.util.logging.Level; 28 24 import java.util.logging.Logger; 29 import java.util.prefs.Preferences;30 31 import javax.swing.JOptionPane;32 25 33 26 /** … … 38 31 */ 39 32 public class LastPod { 40 private static UI UI; 41 public static List recentplayed; //parsed using DbReader class 42 private static Scrobbler scrobbler; 43 private static Logger logger; 44 private final static String NO_PREFS_ERROR = 33 public final static String NO_PREFS_ERROR = 45 34 "You have not setup your preferences.\n" 46 35 + "Please click Preferences below to configure the location of " 47 36 + "your iTunesDB (it's on your iPod's drive) and your AudioScrobbler " 48 37 + "username and password."; 38 private UI userInterface; 39 private List recentplayed; //parsed using DbReader class 49 40 50 41 /** … … 52 43 * GUI to display it. 53 44 */ 54 private staticvoid load() {45 private void load() { 55 46 recentplayed = new ArrayList(); 56 UI = new UI();57 UI.buildUI();58 47 59 logger = Logger.getLogger(LastPod.class.getPackage().getName()); 48 Model model = new ModelImpl(); 49 model.setRecentlyPlayed(recentplayed); 50 userInterface = new UI(model); 51 userInterface.buildUI(); 52 53 Logger logger = Logger.getLogger(LastPod.class.getPackage().getName()); 60 54 logger.setLevel(Level.ALL); 61 logger.addHandler(new LogHandler( UI));55 logger.addHandler(new LogHandler(userInterface)); 62 56 63 parsePlayCounts();57 model.parsePlayCounts(userInterface); 64 58 65 UI.makeVisable(); 66 } 67 68 /** 69 * Parses the play counts and track information from the iPod. 70 */ 71 public static void parsePlayCounts() { 72 Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); 73 String iTunesPath = fPrefs.get("iTunes Path", "default"); 74 String parseVariousArtistsStr = fPrefs.get("parseVariousArtists", "1"); 75 boolean parseVariousArtists = parseVariousArtistsStr.equals("1") ? true : false; 76 77 if (iTunesPath.equals("default")) { 78 logger.log(Level.INFO, NO_PREFS_ERROR); 79 80 return; 81 } 82 83 DbReader reader = new DbReader(iTunesPath, parseVariousArtists); 84 85 try { 86 reader.parse(); 87 recentplayed = reader.getRecentplays(); 88 UI.newTrackListAvailable(); 89 } catch (IOException e) { 90 StackTraceElement[] trace = e.getStackTrace(); 91 92 for (int i = 0; i < trace.length; i++) { 93 if (trace[i].getClassName().startsWith("org.lastpod")) { 94 logger.log(Level.SEVERE, trace[i].toString()); 95 } 96 } 97 98 logger.log(Level.SEVERE, e.toString()); 99 } 100 } 101 102 /** 103 * Submits the tracks to Last.fm 104 * @return A status message upon completion. 105 */ 106 public static Object submitTracks() { 107 Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); 108 String username = fPrefs.get("Username", "default"); 109 String password = fPrefs.get("Password", "default"); 110 String encryptedPassword = fPrefs.get("encryptedPassword", "default"); 111 112 if (!password.equals("default")) { 113 encryptedPassword = MiscUtilities.md5DigestPassword(password); 114 fPrefs.put("encryptedPassword", encryptedPassword); 115 fPrefs.remove("Password"); 116 117 String message = 118 "Your password was stored unencrypted on your system." 119 + " This version of LastPod has encrypted this password for future usage."; 120 JOptionPane.showMessageDialog(UI.getFrame(), message); 121 122 logger = Logger.getLogger(LastPod.class.getPackage().getName()); 123 logger.log(Level.WARNING, message); 124 } 125 126 String encryptedDefault = MiscUtilities.md5DigestPassword("default"); 127 128 if (username.equals("default") && encryptedPassword.equals(encryptedDefault)) { 129 logger.log(Level.INFO, NO_PREFS_ERROR); 130 131 return NO_PREFS_ERROR; 132 } 133 134 String backupUrl = fPrefs.get("backupUrl", ""); 135 136 try { 137 scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); 138 139 List activeRecentPlayed = onlyActiveTrackItems(recentplayed); 140 List inactiveRecentPlayed = onlyInactiveTrackItems(recentplayed); 141 142 scrobbler.setChunkProgress(UI); 143 scrobbler.setTracksToSubmit(activeRecentPlayed); 144 scrobbler.handshake(); 145 scrobbler.submitTracks(); 146 scrobbler.addHistories(activeRecentPlayed, inactiveRecentPlayed); 147 148 /* Refresh track list. */ 149 UI.newTrackListAvailable(); 150 } catch (Exception e) { 151 StackTraceElement[] trace = e.getStackTrace(); 152 153 for (int i = 0; i < trace.length; i++) { 154 if (trace[i].getClassName().startsWith("org.lastpod")) { 155 logger.log(Level.SEVERE, trace[i].toString()); 156 } 157 } 158 159 logger.log(Level.SEVERE, e.toString()); 160 JOptionPane.showMessageDialog(null, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE); 161 } 162 163 return "Success"; 164 } 165 166 private static List onlyActiveTrackItems(List recentPlayed) { 167 return filterTrackItems(recentPlayed, true); 168 } 169 170 private static List onlyInactiveTrackItems(List recentPlayed) { 171 return filterTrackItems(recentPlayed, false); 172 } 173 174 private static List filterTrackItems(List recentPlayed, boolean filterActive) { 175 List filteredRecentPlayed = new ArrayList(); 176 177 for (int i = 0; i < recentPlayed.size(); i++) { 178 TrackItem trackItem = (TrackItem) recentPlayed.get(i); 179 180 boolean trackActive = trackItem.isActive().booleanValue(); 181 182 if (trackActive && filterActive) { 183 filteredRecentPlayed.add(trackItem); 184 } else if (!trackActive && !filterActive) { 185 filteredRecentPlayed.add(trackItem); 186 } 187 } 188 189 return filteredRecentPlayed; 190 } 191 192 public static void selectAll() { 193 setupSelections(true); 194 } 195 196 public static void unselectAll() { 197 setupSelections(false); 198 } 199 200 private static void setupSelections(boolean select) { 201 for (int i = 0; i < recentplayed.size(); i++) { 202 TrackItem trackItem = (TrackItem) recentplayed.get(i); 203 trackItem.setActive(new Boolean(select)); 204 } 59 userInterface.makeVisable(); 205 60 } 206 61 … … 208 63 javax.swing.SwingUtilities.invokeLater(new Runnable() { 209 64 public void run() { 210 load(); 65 LastPod lastPod = new LastPod(); 66 lastPod.load(); 211 67 } 212 68 }); trunk/src/main/org/lastpod/PreferencesEditor.java
r70 r71 32 32 import java.io.File; 33 33 34 import java.util.ArrayList;35 34 import java.util.logging.Level; 36 35 import java.util.logging.Logger; … … 64 63 */ 65 64 private UI userInterface = null; 65 66 /** 67 * The application's model. 68 */ 69 private Model model = null; 66 70 private JDialog frame; 67 71 private SpringLayout layout; … … 79 83 * Constructs this object. 80 84 * @param userInterface The application's user interface. 85 * @param model The application's model. 81 86 */ 82 public PreferencesEditor(UI userInterface ) {87 public PreferencesEditor(UI userInterface, Model model) { 83 88 this.userInterface = userInterface; 89 this.model = model; 84 90 } 85 91 … … 403 409 */ 404 410 if (!oldItunesPath.equals(newItunesPath)) { 405 LastPod.recentplayed = new ArrayList();406 LastPod.parsePlayCounts();407 userInterface.newTrackListAvailable( );411 model.clearRecentlyPlayed(); 412 model.parsePlayCounts(userInterface); 413 userInterface.newTrackListAvailable(model.getRecentlyPlayed()); 408 414 } 409 415 } trunk/src/main/org/lastpod/RecentPanel.java
r52 r71 24 24 25 25 import java.util.Date; 26 import java.util.List; 26 27 27 28 import javax.swing.JPanel; … … 31 32 32 33 /** 34 * @author Chris Tilden 33 35 * @author muti 34 36 * @version $Id$ 35 37 */ 36 38 public class RecentPanel extends JPanel { 39 /** 40 * Required for serializable classes. 41 */ 42 public static final long serialVersionUID = 200705252321L; 37 43 private JTable table; 38 44 private RecentModel model; 39 45 46 /** 47 * Constructs the panel for recently played tracks. 48 */ 40 49 public RecentPanel() { 41 50 super(new GridLayout(1, 1)); … … 55 64 } 56 65 57 public void newTrackListAvailable() { 58 this.model.fireTableDataChanged(); 66 public void newTrackListAvailable(List recentlyPlayed) { 67 model.setRecentlyPlayed(recentlyPlayed); 68 model.fireTableDataChanged(); 59 69 } 60 70 61 71 private class RecentModel extends AbstractTableModel { 72 /** 73 * Required for serializable classes. 74 */ 75 public static final long serialVersionUID = 200705252320L; 76 77 /** 78 * A list of recently played tracks. 79 */ 80 private List recentlyPlayed = null; 62 81 private String[] columnData = 63 82 new String[] { "#", "Submit", "Artist", "Album", "Track", "Length", "Play Time" }; 83 84 /** 85 * Sets the list of recenlty played tracks. 86 * @param recentlyPlayed The list of recently played tracks. 87 */ 88 public void setRecentlyPlayed(List recentlyPlayed) { 89 this.recentlyPlayed = recentlyPlayed; 90 } 64 91 65 92 public int getColumnCount() { … … 68 95 69 96 public int getRowCount() { 70 if ( LastPod.recentplayed != null) {71 return LastPod.recentplayed.size();97 if (recentlyPlayed != null) { 98 return recentlyPlayed.size(); 72 99 } 73 100 … … 87 114 TrackItem track; 88 115 89 if ( LastPod.recentplayed != null) {90 track = (TrackItem) LastPod.recentplayed.get(row);116 if (recentlyPlayed != null) { 117 track = (TrackItem) recentlyPlayed.get(row); 91 118 } else { 92 119 return new Object(); … … 131 158 } 132 159 133 if ( LastPod.recentplayed == null) {160 if (recentlyPlayed == null) { 134 161 throw new RuntimeException("Recent Played list is NULL!"); 135 162 } 136 163 137 track = (TrackItem) LastPod.recentplayed.get(row);164 track = (TrackItem) recentlyPlayed.get(row); 138 165 track.setActive(((Boolean) value)); 139 166 fireTableCellUpdated(row, col); trunk/src/main/org/lastpod/UI.java
r70 r71 36 36 import java.awt.event.WindowEvent; 37 37 38 import java.util.List; 39 38 40 import javax.swing.Action; 39 41 import javax.swing.ImageIcon; … … 101 103 * Constructs the user interface and some icon elements. 102 104 */ 103 public UI( ) {105 public UI(Model model) { 104 106 frame = new JFrame("LastPod"); 105 107 … … 117 119 ImageIcon iconExit = SwingUtils.createImageIcon(UI.class, "images/application-exit.png"); 118 120 119 actionOpenPreferences = new OpenPreferencesEditor(this, "Preferences", iconOpenPreferences,120 "Opens Preferences Editor", KeyEvent.VK_P);121 actionUnselectAll = new UnselectAll(frame, "Unselect All", iconUnselectAll,121 actionOpenPreferences = new OpenPreferencesEditor(this, model, "Preferences", 122 iconOpenPreferences, "Opens Preferences Editor", KeyEvent.VK_P); 123 actionUnselectAll = new UnselectAll(frame, model, "Unselect All", iconUnselectAll, 122 124 "Unselects All Tracks", KeyEvent.VK_A); 123 actionSubmitTracks = new SubmitTracks( statusAnimationLabel, "Submit Tracks",125 actionSubmitTracks = new SubmitTracks(this, model, statusAnimationLabel, "Submit Tracks", 124 126 iconSubmitTracks, "Submits the selected tracks to Last.fm", KeyEvent.VK_S); 125 actionDeletePlayCounts = new DeletePlayCounts(this, "Delete Play Counts",127 actionDeletePlayCounts = new DeletePlayCounts(this, model, "Delete Play Counts", 126 128 iconDeletePlayCounts, "Removes the play counts file from the iPod.", KeyEvent.VK_D); 127 129 actionExit = new ExitApplication(frame, "Exit", iconExit, … … 272 274 } 273 275 274 public void newTrackListAvailable( ) {275 this.recentpanel.newTrackListAvailable();276 public void newTrackListAvailable(List recentlyPlayed) { 277 recentpanel.newTrackListAvailable(recentlyPlayed); 276 278 } 277 279 trunk/src/main/org/lastpod/action/DeletePlayCounts.java
r70 r71 19 19 package org.lastpod.action; 20 20 21 import org.lastpod. LastPod;21 import org.lastpod.Model; 22 22 import org.lastpod.UI; 23 23 … … 26 26 import java.io.File; 27 27 28 import java.util.ArrayList;29 28 import java.util.prefs.Preferences; 30 29 … … 50 49 51 50 /** 51 * The application's model. 52 */ 53 private Model model = null; 54 55 /** 52 56 * Constructs this action. 53 57 * @param userInterface The application's user interface. 58 * @param model The application's model. 54 59 * @param text The action's text. 55 60 * @param icon The action's icon. … … 57 62 * @param mnemonic The action's mnemonic. 58 63 */ 59 public DeletePlayCounts(UI userInterface, String text, ImageIcon icon, String desc, int mnemonic) { 64 public DeletePlayCounts(UI userInterface, Model model, String text, ImageIcon icon, 65 String desc, int mnemonic) { 60 66 super(text, icon); 61 67 this.userInterface = userInterface; 68 this.model = model; 62 69 putValue(SHORT_DESCRIPTION, desc); 63 70 putValue(MNEMONIC_KEY, new Integer(mnemonic)); … … 101 108 if (succuss) { 102 109 /* Clear recent track list. */ 103 LastPod.recentplayed = new ArrayList(); 110 model.clearRecentlyPlayed(); 111 104 112 /* Refresh track list. */ 105 userInterface.newTrackListAvailable( );113 userInterface.newTrackListAvailable(model.getRecentlyPlayed()); 106 114 } else { 107 115 String message = "The play counts file was not deleted."; trunk/src/main/org/lastpod/action/OpenPreferencesEditor.java
r70 r71 19 19 package org.lastpod.action; 20 20 21 import org.lastpod.Model; 21 22 import org.lastpod.PreferencesEditor; 22 23 import org.lastpod.UI; … … 44 45 45 46 /** 47 * The application's model. 48 */ 49 private Model model = null; 50 51 /** 46 52 * Constructs this action. 47 53 * @param userInterface The application's user interface. 54 * @param model The application's model. 48 55 * @param text The action's text. 49 56 * @param icon The action's icon. … … 51 58 * @param mnemonic The action's mnemonic. 52 59 */ 53 public OpenPreferencesEditor(UI userInterface, String text, ImageIcon icon, String desc,54 int mnemonic) {60 public OpenPreferencesEditor(UI userInterface, Model model, String text, ImageIcon icon, 61 String desc, int mnemonic) { 55 62 super(text, icon); 56 63 putValue(SHORT_DESCRIPTION, desc); 57 64 putValue(MNEMONIC_KEY, new Integer(mnemonic)); 58 65 this.userInterface = userInterface; 66 this.model = model; 59 67 } 60 68 … … 64 72 */ 65 73 public void actionPerformed(ActionEvent e) { 66 PreferencesEditor prefeditor = new PreferencesEditor(userInterface );74 PreferencesEditor prefeditor = new PreferencesEditor(userInterface, model); 67 75 prefeditor.buildUI(); 68 76 } trunk/src/main/org/lastpod/action/SubmitTracks.java
r65 r71 19 19 package org.lastpod.action; 20 20 21 import org.lastpod. LastPod;21 import org.lastpod.Model; 22 22 import org.lastpod.UI; 23 23 … … 44 44 */ 45 45 public static final long serialVersionUID = 200705180016L; 46 47 /** 48 * The application's user interface. 49 */ 50 private UI userInterface = null; 51 52 /** 53 * The application's model. 54 */ 55 private Model model = null; 46 56 47 57 /** … … 78 88 /** 79 89 * Constructs this action. 90 * @param userInterface The application's user interface. 91 * @param model The application's model. 80 92 * @param text The action's text. 81 93 * @param icon The action's icon. … … 83 95 * @param mnemonic The action's mnemonic. 84 96 */ 85 public SubmitTracks( JLabel statusAnimationLabel, String text, ImageIcon icon, String desc,86 int mnemonic) {97 public SubmitTracks(UI userInterface, Model model, JLabel statusAnimationLabel, String text, 98 ImageIcon icon, String desc, int mnemonic) { 87 99 super(text, icon); 88 100 putValue(SHORT_DESCRIPTION, desc); 89 101 putValue(MNEMONIC_KEY, new Integer(mnemonic)); 102 this.userInterface = userInterface; 103 this.model = model; 90 104 this.statusAnimationLabel = statusAnimationLabel; 91 105 … … 132 146 busyIconTimer.start(); 133 147 134 return LastPod.submitTracks();148 return model.submitTracks(userInterface); 135 149 } 136 150 trunk/src/main/org/lastpod/action/UnselectAll.java
r65 r71 19 19 package org.lastpod.action; 20 20 21 import org.lastpod. LastPod;21 import org.lastpod.Model; 22 22 23 23 import java.awt.event.ActionEvent; … … 43 43 44 44 /** 45 * The application's model. 46 */ 47 private Model model = null; 48 49 /** 45 50 * If <code>true</code> this action selects all tracks. If 46 51 * <code>false</code> this action unselects all tracks. … … 51 56 * Constructs this action. 52 57 * @param mainAppFrame The main frame for this application. 58 * @param model The application's model. 53 59 * @param text The action's text. 54 60 * @param icon The action's icon. … … 56 62 * @param mnemonic The action's mnemonic. 57 63 */ 58 public UnselectAll(JFrame mainAppFrame, String text, ImageIcon icon, String desc, int mnemonic) { 64 public UnselectAll(JFrame mainAppFrame, Model model, String text, ImageIcon icon, String desc, 65 int mnemonic) { 59 66 super(text, icon); 60 67 putValue(SHORT_DESCRIPTION, desc); 61 68 putValue(MNEMONIC_KEY, new Integer(mnemonic)); 62 69 this.mainAppFrame = mainAppFrame; 70 this.model = model; 63 71 } 64 72 … … 85 93 public void actionPerformed(ActionEvent e) { 86 94 if (selectAllType.booleanValue()) { 87 LastPod.selectAll();95 model.selectAll(); 88 96 setSelectAllType(Boolean.FALSE); 89 97 putValue(NAME, "Unselect All"); 90 98 putValue(SHORT_DESCRIPTION, "Unselects All Tracks"); 91 99 } else { 92 LastPod.unselectAll();100 model.unselectAll(); 93 101 setSelectAllType(Boolean.TRUE); 94 102 putValue(NAME, "Select All");
