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
26
27
28
29
30
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
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
55
56
57
58 public Disc getDisc() {
59 return this.disc;
60 }
61
62
63
64
65
66
67 public Image getImage() {
68 return this.image;
69 }
70
71
72
73
74
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
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
94
95
96
97 public void setImage(final Image aImage) {
98 this.image = aImage;
99
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
118
119
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
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
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
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 }