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

Revision 83, 11.0 kB (checked in by chris, 3 years ago)

Completes #39
Adds version number to lastpod

  • 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.action.DeletePlayCounts;
22 import org.lastpod.action.ExitApplication;
23 import org.lastpod.action.OpenPreferencesEditor;
24 import org.lastpod.action.SubmitTracks;
25 import org.lastpod.action.UnselectAll;
26 import org.lastpod.action.ViewLog;
27
28 import org.lastpod.util.SwingUtils;
29
30 import java.awt.Dimension;
31 import java.awt.GridBagConstraints;
32 import java.awt.GridBagLayout;
33 import java.awt.Insets;
34 import java.awt.Toolkit;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.WindowAdapter;
37 import java.awt.event.WindowEvent;
38
39 import java.util.List;
40
41 import javax.swing.Action;
42 import javax.swing.ImageIcon;
43 import javax.swing.JButton;
44 import javax.swing.JFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JMenu;
47 import javax.swing.JMenuBar;
48 import javax.swing.JMenuItem;
49 import javax.swing.JPanel;
50 import javax.swing.JProgressBar;
51 import javax.swing.JTextArea;
52 import javax.swing.JToolBar;
53 import javax.swing.SwingUtilities;
54
55 /**
56  * Contains the LastPod user interface. (UI)  The UI interacts with the LastPod
57  * controller.
58  * @author muti
59  * @author Chris Tilden
60  * @version $Id$
61  */
62 public class UI implements ChunkProgress {
63     private RecentPanel recentpanel;
64
65     /**
66      * A status label for the submission.
67      */
68     private JLabel submitStatus = null;
69
70     /**
71      * Displays the progress of the submit.
72      */
73     private JProgressBar progressBar = null;
74
75     /**
76      * The label used to display the idle and busy icons.
77      */
78     private JLabel statusAnimationLabel;
79     private JFrame frame;
80
81     /**
82      * The action that opens the PreferencesEditor.
83      */
84     private final Action actionOpenPreferences;
85
86     /**
87      * The action that unselects all tracks.
88      */
89     private final Action actionUnselectAll;
90
91     /**
92      * The action that submits tracks to Last.fm.
93      */
94     private final Action actionSubmitTracks;
95
96     /**
97      * The action that views the log.
98      */
99     private final Action actionViewLog;
100
101     /**
102      * The action that deletes the iPod's play counts file.
103      */
104     private final Action actionDeletePlayCounts;
105
106     /**
107      * The action that exits the application.
108      */
109     private final Action actionExit;
110
111     /**
112      * Constructs the user interface and some icon elements.
113      */
114     public UI(Model model) {
115         frame = new JFrame("LastPod (v0.8)");
116
117         submitStatus = new JLabel();
118
119         ImageIcon idleIcon = SwingUtils.createImageIcon(UI.class, "images/busyicons/idle-icon.png");
120         statusAnimationLabel = new JLabel();
121         statusAnimationLabel.setIcon(idleIcon);
122
123         ImageIcon iconOpenPreferences =
124             SwingUtils.createImageIcon(UI.class, "images/preferences-desktop.png");
125         ImageIcon iconUnselectAll = SwingUtils.createImageIcon(UI.class, "images/stock_to-top.png");
126         ImageIcon iconSubmitTracks =
127             SwingUtils.createImageIcon(UI.class, "images/applications-system.png");
128         ImageIcon iconViewLog =
129             SwingUtils.createImageIcon(UI.class, "images/format-justify-fill.png");
130         ImageIcon iconDeletePlayCounts =
131             SwingUtils.createImageIcon(UI.class, "images/stock_data-delete-table.png");
132         ImageIcon iconExit = SwingUtils.createImageIcon(UI.class, "images/application-exit.png");
133
134         actionOpenPreferences = new OpenPreferencesEditor(this, model, "Preferences",
135                 iconOpenPreferences, "Opens Preferences Editor", KeyEvent.VK_P);
136         actionUnselectAll = new UnselectAll(frame, model, "Unselect All", iconUnselectAll,
137                 "Unselects All Tracks", KeyEvent.VK_A);
138         actionSubmitTracks = new SubmitTracks(this, model, "Submit Tracks", iconSubmitTracks,
139                 "Submits the selected tracks to Last.fm", KeyEvent.VK_S);
140         actionViewLog = new ViewLog(this, "View Log", iconViewLog, "Views the Application Log",
141                 KeyEvent.VK_L);
142         actionDeletePlayCounts = new DeletePlayCounts(this, model, "Delete Play Counts",
143                 iconDeletePlayCounts, "Removes the play counts file from the iPod.", KeyEvent.VK_D);
144         actionExit = new ExitApplication(frame, "Exit", iconExit,
145                 "Exits the application.  May launch iTunes", KeyEvent.VK_X);
146     }
147
148     /**
149      * Gets the user interface's JFrame.
150      * @return  The user interface's JFrame.
151      */
152     public JFrame getFrame() {
153         return frame;
154     }
155
156     public void buildUI() {
157         JFrame.setDefaultLookAndFeelDecorated(true);
158
159         /* If enabled launch iTunes after exiting the application. */
160         frame.addWindowListener(new WindowAdapter() {
161                 public void windowClosing(WindowEvent windowEvent) {
162                     actionExit.actionPerformed(null);
163                 }
164             });
165
166         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
167         Dimension frameSize = new Dimension(screenSize);
168
169         frameSize.width *= 0.90;
170         frameSize.height *= 0.85;
171         frame.setSize(frameSize);
172
173         /* Centers the interface on the screen. */
174         int x = (screenSize.width / 2) - (frame.getWidth() / 2);
175         int y = (screenSize.height / 2) - (frame.getHeight() / 2);
176         frame.setLocation(x, y);
177
178         GridBagLayout layout = new GridBagLayout();
179         frame.getContentPane().setLayout(layout);
180
181         GridBagConstraints c = new GridBagConstraints();
182
183         JMenuBar menuBar = new JMenuBar();
184         JMenu optionsMenu = new JMenu("Options");
185         optionsMenu.setMnemonic(KeyEvent.VK_O);
186
187         JMenu editMenu = new JMenu("Edit");
188         editMenu.setMnemonic(KeyEvent.VK_E);
189
190         optionsMenu.add(new JMenuItem(actionOpenPreferences));
191         optionsMenu.addSeparator();
192         optionsMenu.add(new JMenuItem(actionExit));
193
194         editMenu.add(new JMenuItem(actionUnselectAll));
195         editMenu.addSeparator();
196         editMenu.add(new JMenuItem(actionDeletePlayCounts));
197
198         menuBar.add(optionsMenu);
199         menuBar.add(editMenu);
200         frame.setJMenuBar(menuBar);
201
202         JToolBar toolBar = new JToolBar();
203         layout.setConstraints(toolBar, c);
204
205         JButton button;
206         button = new JButton(actionOpenPreferences);
207         layout.setConstraints(button, c);
208         toolBar.add(button);
209
210         toolBar.addSeparator();
211
212         button = new JButton(actionUnselectAll);
213         layout.setConstraints(button, c);
214         toolBar.add(button);
215
216         toolBar.addSeparator();
217
218         button = new JButton(actionSubmitTracks);
219         layout.setConstraints(button, c);
220         toolBar.add(button);
221
222         toolBar.addSeparator();
223
224         button = new JButton(actionViewLog);
225         layout.setConstraints(button, c);
226         toolBar.add(button);
227
228         toolBar.addSeparator();
229
230         button = new JButton(actionDeletePlayCounts);
231         layout.setConstraints(button, c);
232         toolBar.add(button);
233
234         toolBar.addSeparator();
235
236         button = new JButton(actionExit);
237         layout.setConstraints(button, c);
238         toolBar.add(button);
239
240         frame.getContentPane().add(toolBar);
241
242         c.gridy = 1;
243         c.fill = GridBagConstraints.BOTH;
244         c.weightx = 1.0;
245         c.weighty = 1.0;
246         c.gridwidth = GridBagConstraints.REMAINDER;
247
248         this.recentpanel = new RecentPanel();
249         layout.setConstraints(this.recentpanel, c);
250         frame.getContentPane().add(this.recentpanel);
251
252         c.gridwidth = 1;
253         c.weightx = 0.0;
254         c.weighty = 0.0;
255         c.fill = GridBagConstraints.NONE;
256         c.anchor = GridBagConstraints.WEST;
257         c.gridx = 0;
258         c.gridy = 2;
259         c.insets = new Insets(3, 10, 4, 5);
260         layout.setConstraints(submitStatus, c);
261         frame.getContentPane().add(submitStatus);
262
263         c.gridx = 2;
264         c.anchor = GridBagConstraints.LAST_LINE_END;
265
266         JPanel statusBar = new JPanel();
267         statusBar.setLayout(layout);
268         layout.setConstraints(statusBar, c);
269
270         progressBar = new JProgressBar();
271         progressBar.setMinimumSize(new Dimension(150, 0));
272
273         c.gridx = 0;
274         c.gridy = 0;
275         layout.setConstraints(progressBar, c);
276         statusBar.add(progressBar);
277
278         c.gridx = 1;
279         c.insets = new Insets(3, 0, 3, 4);
280         layout.setConstraints(statusAnimationLabel, c);
281         statusBar.add(statusAnimationLabel);
282
283         frame.getContentPane().add(statusBar);
284     }
285
286     public void makeVisable() {
287         frame.setVisible(true);
288     }
289
290     public void newTrackListAvailable(List recentlyPlayed) {
291         recentpanel.newTrackListAvailable(recentlyPlayed);
292     }
293
294     public JTextArea getLogtextarea() {
295         return ((ViewLog) actionViewLog).getLogTextArea();
296     }
297
298     public UnselectAll getUnselectAll() {
299         return (UnselectAll) actionUnselectAll;
300     }
301
302     public JLabel getStatusAnimationLabel() {
303         return statusAnimationLabel;
304     }
305
306     public JLabel getSubmitStatus() {
307         return submitStatus;
308     }
309
310     /**
311      * When the worker needs to update the GUI we do so by queuing
312      * a Runnable for the event dispatching thread with
313      * SwingUtilities.invokeLater().  In this case we're just
314      * changing the progress bars value.
315      * @param currentChunk  The progress bar value.
316      */
317     public void updateCurrentChunk(final int currentChunk) {
318         Runnable doSetProgressBarValue =
319             new Runnable() {
320                 public void run() {
321                     progressBar.setValue(currentChunk);
322                 }
323             };
324
325         SwingUtilities.invokeLater(doSetProgressBarValue);
326     }
327
328     /**
329      * Sets the number of chunks to be submitted.
330      * @param numberOfChunks  The number of chunks to be submitted.
331      */
332     public void setNumberOfChunks(final int numberOfChunks) {
333         progressBar.setMaximum(numberOfChunks);
334     }
335
336     /**
337      * Set to <code>true</code> if the submission was successful.
338      * @param completionStatus  <code>true</code> if the submission was
339      * successful.
340      */
341     public void setCompletionStatus(boolean completionStatus) {
342         ((ExitApplication) actionExit).setSubmissionSuccessful(completionStatus);
343     }
344
345     /**
346      * Sets the message for the submitStatus label.
347      * @param submitStatusMessage  The message for the submitStatus label.
348      */
349     public void setSubmitStatusMessage(String submitStatusMessage) {
350         submitStatus.setText(submitStatusMessage);
351     }
352 }
Note: See TracBrowser for help on using the browser.