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.modules.tags;
22 
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.io.File;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 
29 import javax.swing.tree.TreePath;
30 
31 import net.sourceforge.atunes.kernel.modules.pattern.PatternInputDialog;
32 import net.sourceforge.atunes.kernel.modules.pattern.PatternMatcher;
33 import net.sourceforge.atunes.kernel.modules.pattern.Patterns;
34 import net.sourceforge.atunes.model.IDialogFactory;
35 
36 import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
37 
38 final class FillTagsFromFolderNameActionListener implements ActionListener {
39 
40 	private final ReviewImportDialog dialog;
41 
42 	private final IDialogFactory dialogFactory;
43 
44 	private final Patterns patterns;
45 
46 	private final PatternMatcher patternMatcher;
47 
48 	/**
49 	 * @param dialog
50 	 * @param dialogFactory
51 	 * @param patterns
52 	 * @param patternMatcher
53 	 */
FillTagsFromFolderNameActionListener(final ReviewImportDialog dialog, final IDialogFactory dialogFactory, final Patterns patterns, final PatternMatcher patternMatcher)54 	FillTagsFromFolderNameActionListener(final ReviewImportDialog dialog, final IDialogFactory dialogFactory, final Patterns patterns, final PatternMatcher patternMatcher) {
55 		super();
56 		this.dialog = dialog;
57 		this.dialogFactory = dialogFactory;
58 		this.patterns = patterns;
59 		this.patternMatcher = patternMatcher;
60 	}
61 
62 	@Override
actionPerformed(final ActionEvent e)63 	public void actionPerformed(final ActionEvent e) {
64 		TreePath[] selectedNodes = dialog.getTreeTable().getTreeSelectionModel().getSelectionPaths();
65 		if (selectedNodes.length > 0) {
66 			PatternInputDialog inputDialog = dialogFactory.newDialog("massivePatternInputDialog", PatternInputDialog.class);
67 			Object node = selectedNodes[0].getLastPathComponent();
68 			Object folder = ((DefaultMutableTreeTableNode)node).getUserObject();
69 			inputDialog.show(patterns.getMassiveRecognitionPatterns(), net.sourceforge.atunes.utils.FileUtils.getPath(((File)folder)));
70 			String pattern = inputDialog.getResult();
71 			for (TreePath treePath : selectedNodes) {
72 				node = treePath.getLastPathComponent();
73 				folder = ((DefaultMutableTreeTableNode)node).getUserObject();
74 				Map<String, String> matches = patternMatcher.getPatternMatches(pattern, net.sourceforge.atunes.utils.FileUtils.getPath(((File)folder)), true);
75 				for (Entry<String, String> entry : matches.entrySet()) {
76 					((ReviewImportTreeTableModel) dialog.getTreeTable().getTreeTableModel()).setValueForColumn(dialog.getTreeTable().getRowForPath(treePath), entry.getKey(), entry.getValue());
77 				}
78 			}
79 		}
80 	}
81 }