1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.actions;
22 
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 
26 import javax.swing.Timer;
27 
28 import net.sourceforge.atunes.model.IColorMutableImageIcon;
29 import net.sourceforge.atunes.model.IIconFactory;
30 import net.sourceforge.atunes.model.ILookAndFeel;
31 import net.sourceforge.atunes.model.IPlayerHandler;
32 import net.sourceforge.atunes.model.IStatePlayer;
33 import net.sourceforge.atunes.utils.I18nUtils;
34 
35 /**
36  * Enables or disables normalization
37  *
38  * @author alex
39  *
40  */
41 public class NormalizeModeAction extends ActionWithColorMutableIcon {
42 
43 	private final class WarningActionListener implements ActionListener {
44 
45 		private boolean showWarning;
46 
47 		@Override
actionPerformed(final ActionEvent arg0)48 		public void actionPerformed(final ActionEvent arg0) {
49 			if (this.showWarning) {
50 				putValue(SMALL_ICON,
51 						NormalizeModeAction.this.normalizationIcon
52 								.getIcon(getLookAndFeel()
53 										.getPaintForSpecialControls()));
54 			} else {
55 				putValue(SMALL_ICON,
56 						NormalizeModeAction.this.warningIcon
57 								.getIcon(getLookAndFeel()
58 										.getPaintForSpecialControls()));
59 			}
60 			this.showWarning = !this.showWarning;
61 		}
62 	}
63 
64 	private static final long serialVersionUID = 6993968558006979367L;
65 
66 	private Timer timer;
67 
68 	private IPlayerHandler playerHandler;
69 
70 	private IIconFactory normalizationIcon;
71 
72 	private IIconFactory warningIcon;
73 
74 	private IStatePlayer statePlayer;
75 
76 	/**
77 	 * @param statePlayer
78 	 */
setStatePlayer(final IStatePlayer statePlayer)79 	public void setStatePlayer(final IStatePlayer statePlayer) {
80 		this.statePlayer = statePlayer;
81 	}
82 
83 	/**
84 	 * @param warningIcon
85 	 */
setWarningIcon(final IIconFactory warningIcon)86 	public void setWarningIcon(final IIconFactory warningIcon) {
87 		this.warningIcon = warningIcon;
88 	}
89 
90 	/**
91 	 * @param normalizationIcon
92 	 */
setNormalizationIcon(final IIconFactory normalizationIcon)93 	public void setNormalizationIcon(final IIconFactory normalizationIcon) {
94 		this.normalizationIcon = normalizationIcon;
95 	}
96 
97 	/**
98 	 * @param playerHandler
99 	 */
setPlayerHandler(final IPlayerHandler playerHandler)100 	public void setPlayerHandler(final IPlayerHandler playerHandler) {
101 		this.playerHandler = playerHandler;
102 	}
103 
104 	/**
105 	 * Default constructor
106 	 */
NormalizeModeAction()107 	public NormalizeModeAction() {
108 		super(I18nUtils.getString("NORMALIZE"));
109 	}
110 
111 	@Override
initialize()112 	protected void initialize() {
113 		putValue(SELECTED_KEY, this.statePlayer.isUseNormalisation());
114 		super.initialize();
115 		this.timer = new Timer(1000, new WarningActionListener());
116 		if (this.statePlayer.isUseNormalisation()) {
117 			this.timer.start();
118 		}
119 	}
120 
121 	@Override
executeAction()122 	protected void executeAction() {
123 		boolean isNormalized = !this.statePlayer.isUseNormalisation();
124 		this.statePlayer.setUseNormalisation(isNormalized);
125 		this.playerHandler.applyNormalization();
126 		if (this.timer.isRunning()) {
127 			this.timer.stop();
128 			putValue(SMALL_ICON,
129 					this.normalizationIcon.getIcon(getLookAndFeel()
130 							.getPaintForSpecialControls()));
131 		} else {
132 			this.timer.start();
133 		}
134 	}
135 
136 	@Override
getIcon(final ILookAndFeel lookAndFeel)137 	public IColorMutableImageIcon getIcon(final ILookAndFeel lookAndFeel) {
138 		return this.normalizationIcon.getColorMutableIcon();
139 	}
140 
141 	@Override
updateTooltip()142 	protected void updateTooltip() {
143 		if ((Boolean) getValue(SELECTED_KEY)) {
144 			putValue(SHORT_DESCRIPTION,
145 					I18nUtils.getString("NORMALIZE_ENABLED"));
146 		} else {
147 			putValue(SHORT_DESCRIPTION,
148 					I18nUtils.getString("NORMALIZE_DISABLED"));
149 		}
150 	}
151 
152 }
153