| 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.action; |
|---|
| 20 |
|
|---|
| 21 |
import org.lastpod.Model; |
|---|
| 22 |
import org.lastpod.UI; |
|---|
| 23 |
|
|---|
| 24 |
import org.lastpod.util.SwingUtils; |
|---|
| 25 |
import org.lastpod.util.SwingWorker; |
|---|
| 26 |
|
|---|
| 27 |
import java.awt.event.ActionEvent; |
|---|
| 28 |
import java.awt.event.ActionListener; |
|---|
| 29 |
|
|---|
| 30 |
import javax.swing.AbstractAction; |
|---|
| 31 |
import javax.swing.Icon; |
|---|
| 32 |
import javax.swing.ImageIcon; |
|---|
| 33 |
import javax.swing.Timer; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* A <code>javax.swing.Action</code> class that is used to submit selected |
|---|
| 37 |
* tracks to Last.fm. |
|---|
| 38 |
* @author Chris Tilden |
|---|
| 39 |
*/ |
|---|
| 40 |
public class SubmitTracks extends AbstractAction { |
|---|
| 41 |
/** |
|---|
| 42 |
* Required for serializable classes. |
|---|
| 43 |
*/ |
|---|
| 44 |
public static final long serialVersionUID = 200705180016L; |
|---|
| 45 |
|
|---|
| 46 |
/** |
|---|
| 47 |
* The application's user interface. |
|---|
| 48 |
*/ |
|---|
| 49 |
private UI userInterface = null; |
|---|
| 50 |
|
|---|
| 51 |
/** |
|---|
| 52 |
* The application's model. |
|---|
| 53 |
*/ |
|---|
| 54 |
private Model model = null; |
|---|
| 55 |
|
|---|
| 56 |
/** |
|---|
| 57 |
* A timer used for the busy icon. |
|---|
| 58 |
*/ |
|---|
| 59 |
private Timer busyIconTimer = null; |
|---|
| 60 |
|
|---|
| 61 |
/** |
|---|
| 62 |
* The idle icon. |
|---|
| 63 |
*/ |
|---|
| 64 |
private Icon idleIcon; |
|---|
| 65 |
|
|---|
| 66 |
/** |
|---|
| 67 |
* A series of 15 busy icons. |
|---|
| 68 |
*/ |
|---|
| 69 |
private Icon[] busyIcons = new Icon[15]; |
|---|
| 70 |
|
|---|
| 71 |
/** |
|---|
| 72 |
* The current busy icon. |
|---|
| 73 |
*/ |
|---|
| 74 |
private int busyIconIndex = 0; |
|---|
| 75 |
|
|---|
| 76 |
/** |
|---|
| 77 |
* This worker is used to perform the submission and is a nice threaded |
|---|
| 78 |
* implementation. |
|---|
| 79 |
*/ |
|---|
| 80 |
private SwingWorker worker; |
|---|
| 81 |
|
|---|
| 82 |
/** |
|---|
| 83 |
* Constructs this action. |
|---|
| 84 |
* @param userInterface The application's user interface. |
|---|
| 85 |
* @param model The application's model. |
|---|
| 86 |
* @param text The action's text. |
|---|
| 87 |
* @param icon The action's icon. |
|---|
| 88 |
* @param desc The action's detailed description. |
|---|
| 89 |
* @param mnemonic The action's mnemonic. |
|---|
| 90 |
*/ |
|---|
| 91 |
public SubmitTracks(UI userInterface, Model model, String text, ImageIcon icon, String desc, |
|---|
| 92 |
int mnemonic) { |
|---|
| 93 |
super(text, icon); |
|---|
| 94 |
putValue(SHORT_DESCRIPTION, desc); |
|---|
| 95 |
putValue(MNEMONIC_KEY, new Integer(mnemonic)); |
|---|
| 96 |
this.userInterface = userInterface; |
|---|
| 97 |
this.model = model; |
|---|
| 98 |
|
|---|
| 99 |
idleIcon = SwingUtils.createImageIcon(UI.class, "images/busyicons/idle-icon.png"); |
|---|
| 100 |
setupBusyIcon(); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
private void setupBusyIcon() { |
|---|
| 104 |
int busyAnimationRate = 30; |
|---|
| 105 |
|
|---|
| 106 |
for (int i = 0; i < busyIcons.length; i++) { |
|---|
| 107 |
busyIcons[i] = SwingUtils.createImageIcon(UI.class, |
|---|
| 108 |
"images/busyicons/busy-icon" + i + ".png"); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
busyIconTimer = |
|---|
| 112 |
new Timer(busyAnimationRate, |
|---|
| 113 |
new ActionListener() { |
|---|
| 114 |
public void actionPerformed(ActionEvent e) { |
|---|
| 115 |
busyIconIndex = (busyIconIndex + 1) % busyIcons.length; |
|---|
| 116 |
userInterface.getStatusAnimationLabel().setIcon(busyIcons[busyIconIndex]); |
|---|
| 117 |
} |
|---|
| 118 |
}); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
/** |
|---|
| 122 |
* Performs some processing when the action is triggered. |
|---|
| 123 |
* @param e The event that triggered the action. |
|---|
| 124 |
*/ |
|---|
| 125 |
public void actionPerformed(ActionEvent e) { |
|---|
| 126 |
setEnabled(false); |
|---|
| 127 |
|
|---|
| 128 |
/* Invoking start() on the SwingWorker causes a new Thread |
|---|
| 129 |
* to be created that will call construct(), and then |
|---|
| 130 |
* finished(). Note that finished() is called even if |
|---|
| 131 |
* the worker is interrupted because we catch the |
|---|
| 132 |
* InterruptedException in doWork(). |
|---|
| 133 |
*/ |
|---|
| 134 |
worker = |
|---|
| 135 |
new SwingWorker() { |
|---|
| 136 |
public Object construct() { |
|---|
| 137 |
userInterface.getStatusAnimationLabel().setIcon(busyIcons[0]); |
|---|
| 138 |
busyIconIndex = 0; |
|---|
| 139 |
busyIconTimer.start(); |
|---|
| 140 |
|
|---|
| 141 |
// TODO |
|---|
| 142 |
return model.submitTracks(userInterface, false); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
public void finished() { |
|---|
| 146 |
busyIconTimer.stop(); |
|---|
| 147 |
userInterface.getStatusAnimationLabel().setIcon(idleIcon); |
|---|
| 148 |
userInterface.getUnselectAll().reset(); |
|---|
| 149 |
setEnabled(true); |
|---|
| 150 |
} |
|---|
| 151 |
}; |
|---|
| 152 |
worker.start(); |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|