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.FileDialog;
24 import java.io.File;
25 import java.io.FilenameFilter;
26 
27 import javax.swing.JFileChooser;
28 
29 import net.sourceforge.atunes.model.IControlsBuilder;
30 import net.sourceforge.atunes.model.IFolderSelectorDialog;
31 import net.sourceforge.atunes.model.IFrame;
32 import net.sourceforge.atunes.model.IOSManager;
33 import net.sourceforge.atunes.utils.FileUtils;
34 import net.sourceforge.atunes.utils.JVMProperties;
35 
36 /**
37  * Dialog to select folder
38  *
39  * @author alex
40  *
41  */
42 public class FolderSelectorDialog implements IFolderSelectorDialog {
43 
44 	private static final class RejectAllFilesFileFilter implements
45 			FilenameFilter {
46 		@Override
accept(final File dir, final String name)47 		public boolean accept(final File dir, final String name) {
48 			return false;
49 		}
50 	}
51 
52 	private IFrame frame;
53 
54 	private String title;
55 
56 	private IOSManager osManager;
57 
58 	private IControlsBuilder controlsBuilder;
59 
60 	/**
61 	 * @param controlsBuilder
62 	 */
setControlsBuilder(final IControlsBuilder controlsBuilder)63 	public void setControlsBuilder(final IControlsBuilder controlsBuilder) {
64 		this.controlsBuilder = controlsBuilder;
65 	}
66 
67 	/**
68 	 * @param osManager
69 	 */
setOsManager(final IOSManager osManager)70 	public void setOsManager(final IOSManager osManager) {
71 		this.osManager = osManager;
72 	}
73 
74 	/**
75 	 * @param title
76 	 */
77 	@Override
setTitle(final String title)78 	public void setTitle(final String title) {
79 		this.title = title;
80 	}
81 
82 	/**
83 	 * @param frame
84 	 */
setFrame(final IFrame frame)85 	public void setFrame(final IFrame frame) {
86 		this.frame = frame;
87 	}
88 
89 	@Override
showDialog()90 	public void showDialog() {
91 		throw new UnsupportedOperationException();
92 	}
93 
94 	@Override
hideDialog()95 	public void hideDialog() {
96 		throw new UnsupportedOperationException();
97 	}
98 
99 	@Override
initialize()100 	public void initialize() {
101 		// Do nothing
102 	}
103 
104 	@Override
selectFolder(final String path)105 	public File selectFolder(final String path) {
106 		File file = null;
107 		if (this.osManager.isMacOsX() && !new JVMProperties().isJava7OrLater()) {
108 			// TODO: native folder chooser not working with java 7
109 			// http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-May/004295.html
110 			file = selectFolderWithFileChooser(path);
111 		} else {
112 			file = selectFolderWithJFileChooser(path);
113 		}
114 		return file;
115 	}
116 
117 	/**
118 	 * @param path
119 	 * @return
120 	 */
selectFolderWithJFileChooser(final String path)121 	private File selectFolderWithJFileChooser(final String path) {
122 		JFileChooser dialog = this.controlsBuilder.getFileChooser(path, null);
123 		dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
124 		dialog.showOpenDialog(this.frame.getFrame());
125 		return dialog.getSelectedFile();
126 	}
127 
128 	/**
129 	 * @param path
130 	 * @return
131 	 */
selectFolderWithFileChooser(final String path)132 	private File selectFolderWithFileChooser(final String path) {
133 		System.setProperty("apple.awt.fileDialogForDirectories", "true");
134 		FileDialog dialog = new FileDialog(this.frame.getFrame(), this.title,
135 				FileDialog.LOAD);
136 		dialog.setFilenameFilter(new RejectAllFilesFileFilter());
137 		dialog.setDirectory(path);
138 		dialog.setVisible(true);
139 		String parent = dialog.getDirectory();
140 		String folder = dialog.getFile();
141 		System.setProperty("apple.awt.fileDialogForDirectories", "false");
142 		if (parent != null && folder != null) {
143 			return new File(parent + '/' + folder);
144 		}
145 		return null;
146 
147 	}
148 
149 	@Override
selectFolder(final File path)150 	public File selectFolder(final File path) {
151 		return selectFolder(FileUtils.getPath(path));
152 	}
153 }
154