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