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
24
25
26
27
28
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
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
53
54
55
56 public Disc getDisc() {
57 return this.disc;
58 }
59
60
61
62
63
64
65 public Image getImage() {
66 return this.image;
67 }
68
69
70
71
72
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
86
87
88
89 public void setImage(final Image aImage) {
90 this.image = aImage;
91
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
110
111
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
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
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
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 }