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.controls;
22 
23 import java.awt.Component;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.io.File;
30 
31 import javax.swing.JButton;
32 import javax.swing.JFileChooser;
33 import javax.swing.JPanel;
34 import javax.swing.JTextField;
35 import javax.swing.event.DocumentListener;
36 
37 import net.sourceforge.atunes.model.IBeanFactory;
38 import net.sourceforge.atunes.model.IControlsBuilder;
39 import net.sourceforge.atunes.model.IDialogFactory;
40 import net.sourceforge.atunes.model.IFileSelectorDialog;
41 import net.sourceforge.atunes.model.IFolderSelectorDialog;
42 import net.sourceforge.atunes.model.IOSManager;
43 
44 /**
45  * A custom file chooser with text field and button that opens the chooser.
46  */
47 public final class CustomFileChooser extends JPanel {
48 
49 	private static final long serialVersionUID = 4713483251093570020L;
50 
51 	private final JTextField textField;
52 	private String result;
53 
54 	/**
55 	 * Instantiates a new custom file chooser.
56 	 *
57 	 * @param title
58 	 * @param parent
59 	 * @param length
60 	 * @param type
61 	 * @param osManager
62 	 * @param beanFactory
63 	 * @param controlsBuilder
64 	 */
CustomFileChooser(final String title, final Component parent, final int length, final int type, final IOSManager osManager, final IBeanFactory beanFactory, IControlsBuilder controlsBuilder)65 	public CustomFileChooser(final String title, final Component parent,
66 			final int length, final int type, final IOSManager osManager,
67 			final IBeanFactory beanFactory, IControlsBuilder controlsBuilder) {
68 		super(new GridBagLayout());
69 		// Use user home by default
70 		final File defaultFolder = new File(osManager.getUserHome());
71 		textField = controlsBuilder.createTextField();
72 		textField.setColumns(length);
73 		textField.setText(net.sourceforge.atunes.utils.FileUtils
74 				.getPath(defaultFolder));
75 		JButton button = new JButton("...");
76 
77 		button.addActionListener(new ActionListener() {
78 			@Override
79 			public void actionPerformed(final ActionEvent arg0) {
80 				File selected = null;
81 				if (type == JFileChooser.DIRECTORIES_ONLY) {
82 					IFolderSelectorDialog dialog = beanFactory.getBean(
83 							IDialogFactory.class).newDialog(
84 							IFolderSelectorDialog.class);
85 					dialog.setTitle(title);
86 					selected = dialog.selectFolder(defaultFolder);
87 				} else {
88 					IFileSelectorDialog dialog = beanFactory.getBean(
89 							IDialogFactory.class).newDialog(
90 							IFileSelectorDialog.class);
91 					dialog.setTitle(title);
92 					selected = dialog.loadFile(defaultFolder);
93 				}
94 				if (selected != null) {
95 					result = net.sourceforge.atunes.utils.FileUtils
96 							.getPath(selected);
97 				} else {
98 					result = null;
99 				}
100 				textField.setText(result);
101 			}
102 		});
103 
104 		GridBagConstraints c = new GridBagConstraints();
105 		c.gridx = 0;
106 		c.gridy = 0;
107 		c.weightx = 1;
108 		c.fill = GridBagConstraints.HORIZONTAL;
109 		add(textField, c);
110 		c.gridx = 1;
111 		c.weightx = 0;
112 		c.fill = GridBagConstraints.NONE;
113 		c.insets = new Insets(0, 2, 0, 0);
114 		add(button, c);
115 	}
116 
117 	/**
118 	 * Gets the result.
119 	 *
120 	 * @return the result
121 	 */
getResult()122 	public String getResult() {
123 		result = textField.getText();
124 		return result;
125 	}
126 
127 	/**
128 	 * Sets the text.
129 	 *
130 	 * @param text
131 	 *            the new text
132 	 */
setText(final String text)133 	public void setText(final String text) {
134 		textField.setText(text);
135 		result = text;
136 	}
137 
138 	/**
139 	 * Adds a document listener to text field
140 	 *
141 	 * @param listener
142 	 */
addDocumentListener(final DocumentListener listener)143 	public void addDocumentListener(final DocumentListener listener) {
144 		this.textField.getDocument().addDocumentListener(listener);
145 	}
146 
147 }
148