View Javadoc

1   package com.melloware.jukes.gui.view.component;
2   
3   import java.awt.AWTEventMulticaster;
4   import java.awt.Dimension;
5   import java.awt.Graphics;
6   import java.awt.Image;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.awt.event.MouseAdapter;
10  import java.awt.event.MouseEvent;
11  
12  import javax.swing.ImageIcon;
13  import javax.swing.JComponent;
14  import javax.swing.JPopupMenu;
15  
16  import com.jgoodies.uif.action.ActionManager;
17  import com.melloware.jukes.db.orm.Disc;
18  import com.melloware.jukes.file.image.ImageFactory;
19  import com.melloware.jukes.gui.tool.Actions;
20  import com.melloware.jukes.gui.tool.Resources;
21  import com.melloware.jukes.gui.tool.MainModule;
22  import com.melloware.jukes.gui.view.MainMenuBuilder;
23  
24  /**
25   * Helper for image album cover images.  If this object contains a disc object
26   * it is clickable to take you to that disc.
27   * <p>
28   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
29   * @author Emil A. Lefkof III <info@melloware.com>
30   * @version 4.0
31   */
32  public final class AlbumImage
33      extends JComponent {
34  
35      ActionListener actionListener = null;
36      Disc disc = null;
37      transient Image image = null;
38      ImageIcon thumbnail = null;
39  
40      /**
41       * Default constructor.
42       */
43      public AlbumImage() {
44          setPreferredSize(new Dimension(91, 91));
45          initComponents();
46      }
47  
48      public AlbumImage(Dimension preferredSize) {
49          setPreferredSize(preferredSize);
50          initComponents();
51      }
52  
53      /**
54       * Gets the disc.
55       * <p>
56       * @return Returns the disc.
57       */
58      public Disc getDisc() {
59          return this.disc;
60      }
61  
62      /**
63       * Gets the image.
64       * <p>
65       * @return Returns the image.
66       */
67      public Image getImage() {
68          return this.image;
69      }
70  
71      /**
72       * Sets the disc.
73       * <p>
74       * @param aDisc The disc to set.
75       */
76      public void setDisc(final Disc aDisc) {
77          this.disc = aDisc;
78          if (disc == null) {
79          	this.setToolTipText(null);
80          	this.setImage(null);
81          } else {
82          	//Set current cover URL as ToolTipText
83          	if (MainModule.SETTINGS.isCopyImagesToDirectory()) {
84          		this.setToolTipText(ImageFactory.standardImageFileName(disc.getArtist().getName(), disc.getName(), disc.getYear()));
85              }
86              else {
87              	this.setToolTipText((disc.getCoverUrl()));
88              }
89          }
90      }
91  
92      /**
93       * Sets the image.
94       * <p>
95       * @param aImage The image to set.
96       */
97      public void setImage(final Image aImage) {
98          this.image = aImage;
99          // Update the preview accordingly.
100         thumbnail = null;
101         if (isShowing()) {
102             loadImage();
103             repaint();
104         }
105     }
106 
107     public void addActionListener(final ActionListener l) {
108         actionListener = AWTEventMulticaster.add(actionListener, l);
109     }
110 
111     public void loadImage() {
112         if (image == null) {
113             thumbnail = null;
114             return;
115         }
116 
117         // Don't use createImageIcon (which is a wrapper for getResource)
118         // because the image we're trying to load is probably not one
119         // of this program's own resources.
120         final ImageIcon tmpIcon = new ImageIcon(image);
121         if (tmpIcon != null) {
122             thumbnail = tmpIcon;
123         }
124     }
125 
126     public void removeActionListener(final ActionListener l) {
127         actionListener = AWTEventMulticaster.remove(actionListener, l);
128     }
129 
130     protected void paintComponent(final Graphics g) {
131         if (thumbnail == null) {
132             loadImage();
133         }
134         if (thumbnail != null) {
135             int x = (getWidth() / 2) - (thumbnail.getIconWidth() / 2);
136             int y = (getHeight() / 2) - (thumbnail.getIconHeight() / 2);
137 
138             if (y < 0) {
139                 y = 0;
140             }
141 
142             if (x < 5) {
143                 x = 5;
144             }
145             thumbnail.paintIcon(this, g, x, y);
146         }
147     }
148 
149     private void initComponents() {
150         final JPopupMenu popup = MainMenuBuilder.buildPlayerPopupMenu(this);
151         addMouseListener(new ClickAdapter(popup));
152     }
153 
154     /**
155      * Handles Actionlistener and popping up the popup menu.
156      */
157     private final class ClickAdapter
158         extends MouseAdapter {
159 
160         final JPopupMenu popup;
161 
162         public ClickAdapter(JPopupMenu popup) {
163             this.popup = popup;
164         }
165 
166         public void mouseClicked(final MouseEvent evt) {
167             maybeShowPopup(evt);
168 
169             // middle mouse button pressed once queue in playlist
170             if (((evt.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) && (evt.getClickCount() == 1)) {
171             	final JComponent source = (JComponent)evt.getSource();
172                 final ActionEvent event = new ActionEvent(source, 1, Actions.PLAYER_QUEUE_ID);
173                 source.putClientProperty(Resources.EDITOR_COMPONENT, source);
174                 ActionManager.get(Actions.PLAYER_QUEUE_ID).actionPerformed(event);
175                 return;
176             }
177 
178             //any other button fire the action event
179             if (actionListener != null) {
180                 actionListener.actionPerformed(new ActionEvent(AlbumImage.this, ActionEvent.ACTION_PERFORMED, ""));
181             }
182         }
183 
184         public void mousePressed(final MouseEvent evt) {
185             maybeShowPopup(evt);
186         }
187 
188         public void mouseReleased(final MouseEvent e) {
189             maybeShowPopup(e);
190         }
191 
192         private void maybeShowPopup(final MouseEvent evt) {
193             if (evt.isPopupTrigger()) {
194                 popup.show(evt.getComponent(), evt.getX(), evt.getY());
195             }
196         }
197 
198     }
199 }