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.gui.views.dialogs;
22 
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 
29 import javax.swing.JButton;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33 
34 import net.sourceforge.atunes.gui.views.controls.AbstractCustomDialog;
35 import net.sourceforge.atunes.model.IControlsBuilder;
36 import net.sourceforge.atunes.model.IFrame;
37 import net.sourceforge.atunes.model.IIconFactory;
38 import net.sourceforge.atunes.model.IRadio;
39 import net.sourceforge.atunes.model.IRadioDialog;
40 import net.sourceforge.atunes.model.IRadioHandler;
41 import net.sourceforge.atunes.utils.I18nUtils;
42 
43 /**
44  * Dialog to add or edit a radio
45  *
46  * @author fleax
47  *
48  */
49 public final class RadioDialog extends AbstractCustomDialog implements
50 		IRadioDialog {
51 
52 	private static final long serialVersionUID = 7295438534550341824L;
53 
54 	private JTextField nameTextField;
55 	private JTextField urlTextField;
56 	private JTextField labelTextField;
57 
58 	private IRadioHandler radioHandler;
59 
60 	/** The radio. */
61 	private IRadio result;
62 
63 	private IIconFactory radioMediumIcon;
64 
65 	/**
66 	 * @param radioMediumIcon
67 	 */
setRadioMediumIcon(final IIconFactory radioMediumIcon)68 	public void setRadioMediumIcon(final IIconFactory radioMediumIcon) {
69 		this.radioMediumIcon = radioMediumIcon;
70 	}
71 
72 	/**
73 	 * Instantiates a new radio dialog for adding a new radio
74 	 *
75 	 * @param frame
76 	 * @param controlsBuilder
77 	 */
RadioDialog(final IFrame frame, final IControlsBuilder controlsBuilder)78 	public RadioDialog(final IFrame frame,
79 			final IControlsBuilder controlsBuilder) {
80 		super(frame, 500, 200, controlsBuilder);
81 	}
82 
83 	@Override
initialize()84 	public void initialize() {
85 		setTitle(I18nUtils.getString("ADD_RADIO"));
86 		setResizable(false);
87 		add(getContent());
88 	}
89 
90 	/**
91 	 * @param radioHandler
92 	 */
setRadioHandler(final IRadioHandler radioHandler)93 	public void setRadioHandler(final IRadioHandler radioHandler) {
94 		this.radioHandler = radioHandler;
95 	}
96 
97 	@Override
setRadio(final IRadio radio)98 	public void setRadio(final IRadio radio) {
99 		setTitle(radio != null ? I18nUtils.getString("EDIT_RADIO") : I18nUtils
100 				.getString("ADD_RADIO"));
101 		this.nameTextField.setText(radio != null ? radio.getName() : null);
102 		this.urlTextField.setText(radio != null ? radio.getUrl() : null);
103 		this.labelTextField.setText(radio != null ? radio.getLabel() : null);
104 	}
105 
106 	/**
107 	 * Gets the content.
108 	 *
109 	 * @return the content
110 	 */
getContent()111 	private JPanel getContent() {
112 		JPanel panel = new JPanel(new GridBagLayout());
113 
114 		JLabel nameLabel = new JLabel(I18nUtils.getString("NAME"));
115 		this.nameTextField = getControlsBuilder().createTextField();
116 		JLabel urlLabel = new JLabel(I18nUtils.getString("URL"));
117 		this.urlTextField = getControlsBuilder().createTextField();
118 		JLabel labelLabel = new JLabel(I18nUtils.getString("LABEL"));
119 		this.labelTextField = getControlsBuilder().createTextField();
120 
121 		JButton okButton = new JButton(I18nUtils.getString("OK"));
122 		okButton.addActionListener(new ActionListener() {
123 			@Override
124 			public void actionPerformed(final ActionEvent e) {
125 				RadioDialog.this.result = RadioDialog.this.radioHandler
126 						.createRadio(RadioDialog.this.nameTextField.getText(),
127 								RadioDialog.this.urlTextField.getText(),
128 								RadioDialog.this.labelTextField.getText());
129 				RadioDialog.this.dispose();
130 			}
131 		});
132 		JButton cancelButton = new JButton(I18nUtils.getString("CANCEL"));
133 		cancelButton.addActionListener(new ActionListener() {
134 			@Override
135 			public void actionPerformed(final ActionEvent e) {
136 				RadioDialog.this.dispose();
137 			}
138 		});
139 
140 		arrangePanel(panel, nameLabel, urlLabel, labelLabel, okButton,
141 				cancelButton);
142 
143 		return panel;
144 	}
145 
146 	/**
147 	 * @param panel
148 	 * @param nameLabel
149 	 * @param urlLabel
150 	 * @param labelLabel
151 	 * @param okButton
152 	 * @param cancelButton
153 	 */
arrangePanel(final JPanel panel, final JLabel nameLabel, final JLabel urlLabel, final JLabel labelLabel, final JButton okButton, final JButton cancelButton)154 	private void arrangePanel(final JPanel panel, final JLabel nameLabel,
155 			final JLabel urlLabel, final JLabel labelLabel,
156 			final JButton okButton, final JButton cancelButton) {
157 		GridBagConstraints c = new GridBagConstraints();
158 		c.gridx = 1;
159 		c.gridy = 0;
160 		c.fill = GridBagConstraints.HORIZONTAL;
161 		c.insets = new Insets(5, 10, 5, 10);
162 		panel.add(nameLabel, c);
163 		c.gridx = 2;
164 		c.weightx = 1;
165 		panel.add(this.nameTextField, c);
166 		c.gridx = 1;
167 		c.gridy = 1;
168 		c.weightx = 0;
169 		panel.add(urlLabel, c);
170 		c.gridx = 2;
171 		c.weightx = 1;
172 		panel.add(this.urlTextField, c);
173 		c.gridx = 1;
174 		c.gridy = 2;
175 		c.weightx = 0;
176 		panel.add(labelLabel, c);
177 		c.gridx = 2;
178 		c.weightx = 1;
179 		panel.add(this.labelTextField, c);
180 
181 		c.gridx = 0;
182 		c.gridy = 0;
183 		c.gridheight = 2;
184 		c.fill = GridBagConstraints.NONE;
185 		c.weightx = -1;
186 		panel.add(
187 				new JLabel(this.radioMediumIcon.getIcon(getLookAndFeel()
188 						.getPaintForSpecialControls())), c);
189 
190 		JPanel auxPanel = new JPanel();
191 		auxPanel.add(okButton);
192 		auxPanel.add(cancelButton);
193 
194 		c.gridx = 0;
195 		c.gridy = 3;
196 		c.gridwidth = 3;
197 		c.insets = new Insets(0, 0, 0, 0);
198 		panel.add(auxPanel, c);
199 	}
200 
201 	@Override
getRadio()202 	public IRadio getRadio() {
203 		return this.result;
204 	}
205 
206 	@Override
showDialog()207 	public void showDialog() {
208 		setVisible(true);
209 	}
210 
211 	@Override
hideDialog()212 	public void hideDialog() {
213 		setVisible(false);
214 	}
215 }
216