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.KeyAdapter; 24 import java.awt.event.KeyEvent; 25 26 import javax.swing.JTextField; 27 import javax.swing.SwingUtilities; 28 29 final class TitleTextFieldKeyAdapter extends KeyAdapter { 30 private final JTextField textField; 31 private final String fileName; 32 private int lenght = 0; 33 TitleTextFieldKeyAdapter(JTextField textField, String fileName)34 TitleTextFieldKeyAdapter(JTextField textField, String fileName) { 35 this.textField = textField; 36 this.fileName = fileName; 37 } 38 39 @Override keyTyped(KeyEvent e)40 public void keyTyped(KeyEvent e) { 41 SwingUtilities.invokeLater(new Runnable() { 42 @Override 43 public void run() { 44 String text = textField.getText(); 45 46 // User added a char 47 if (text.length() > lenght && text.length() >= 3) { 48 int index = fileName.indexOf(text); 49 if (index != -1) { 50 textField.setText(fileName.substring(index)); 51 textField.setSelectionStart(text.length()); 52 textField.setSelectionEnd(textField.getText().length()); 53 } 54 } 55 lenght = text.length(); 56 } 57 }); 58 } 59 }