| 1 |
/* |
| 2 |
* @(#)FileIntelliHints.java 7/24/2005 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2005 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.hints; |
| 7 |
|
| 8 |
import javax.swing.*; |
| 9 |
import javax.swing.text.BadLocationException; |
| 10 |
import javax.swing.text.JTextComponent; |
| 11 |
import java.awt.*; |
| 12 |
import java.io.File; |
| 13 |
import java.io.FilenameFilter; |
| 14 |
|
| 15 |
/** |
| 16 |
* <code>FileIntelliHints</code> is a concrete implementation of {@link com.jidesoft.hints.IntelliHints}. |
| 17 |
* It allows user to type in a file patch quickly by providing them the hints based on what |
| 18 |
* is existed on file system. You can use {@link #setFolderOnly(boolean)} to control if |
| 19 |
* the hints contain only the folders, or folders and files. |
| 20 |
*/ |
| 21 |
public class FileIntelliHints extends AbstractListIntelliHints { |
| 22 |
private boolean _folderOnly = false; |
| 23 |
private boolean _showFullPath = true; |
| 24 |
private FilenameFilter _filter; |
| 25 |
|
| 26 |
public FileIntelliHints(JTextComponent comp) { |
| 27 |
super(comp); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* If the hints contain the folder names only. |
| 32 |
* |
| 33 |
* @return true if the hints contain the folder names only. |
| 34 |
*/ |
| 35 |
public boolean isFolderOnly() { |
| 36 |
return _folderOnly; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Sets the property of folder only. If true, the hints will only show the folder names. |
| 41 |
* Otherwise, both folder and file names will be shown in the hints. |
| 42 |
* |
| 43 |
* @param folderOnly only provide hints for the folders. |
| 44 |
*/ |
| 45 |
public void setFolderOnly(boolean folderOnly) { |
| 46 |
_folderOnly = folderOnly; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* If the hints contain the full path. |
| 51 |
* |
| 52 |
* @return true if the hints contain the full path. |
| 53 |
*/ |
| 54 |
public boolean isShowFullPath() { |
| 55 |
return _showFullPath; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the property of showing full path. If true, the hints will show the full path. |
| 60 |
* Otherwise, it will only show the path after user typed in so far. |
| 61 |
* |
| 62 |
* @param showFullPath whether show the full path. |
| 63 |
*/ |
| 64 |
public void setShowFullPath(boolean showFullPath) { |
| 65 |
_showFullPath = showFullPath; |
| 66 |
} |
| 67 |
|
| 68 |
public boolean updateHints(Object value) { |
| 69 |
if (value == null) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
String s = value.toString(); |
| 73 |
if (s.length() == 0) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
int index1 = s.lastIndexOf('\\'); |
| 77 |
int index2 = s.lastIndexOf('/'); |
| 78 |
int index = Math.max(index1, index2); |
| 79 |
if (index == -1) |
| 80 |
return false; |
| 81 |
String dir = s.substring(0, index + 1); |
| 82 |
final String prefix = index == s.length() - 1 ? null : s.substring(index + 1).toLowerCase(); |
| 83 |
String[] files = new File(dir).list(new FilenameFilter() { |
| 84 |
public boolean accept(File dir, String name) { |
| 85 |
if (isFolderOnly()) { |
| 86 |
if (new File(dir.getAbsolutePath() + File.separator + name).isFile()) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
} |
| 90 |
boolean result = prefix == null || name.toLowerCase().startsWith(prefix); |
| 91 |
if (result && getFilter() != null) { |
| 92 |
return getFilter().accept(dir, name); |
| 93 |
} |
| 94 |
return result; |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
if (files == null || files.length == 0 || (files.length == 1 && files[0].equalsIgnoreCase(prefix))) { |
| 99 |
setListData(new String[0]); |
| 100 |
return false; |
| 101 |
} |
| 102 |
else { |
| 103 |
getList().setCellRenderer(new PrefixListCellRenderer(isShowFullPath() ? dir : "")); |
| 104 |
setListData(files); |
| 105 |
return true; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void acceptHint(Object selected) { |
| 111 |
if (selected == null) |
| 112 |
return; |
| 113 |
|
| 114 |
String selectedValue = "" + selected; |
| 115 |
|
| 116 |
String value = getTextComponent().getText(); |
| 117 |
int caretPosition = getTextComponent().getCaretPosition(); |
| 118 |
int index1 = value.lastIndexOf('\\', caretPosition); |
| 119 |
int index2 = value.lastIndexOf('/', caretPosition); |
| 120 |
int index = Math.max(index1, index2); |
| 121 |
if (index == -1) { |
| 122 |
return; |
| 123 |
} |
| 124 |
int prefixlen = caretPosition - index - 1; |
| 125 |
try { |
| 126 |
getTextComponent().getDocument().insertString(caretPosition, selectedValue.substring(prefixlen), null); |
| 127 |
} |
| 128 |
catch (BadLocationException e) { |
| 129 |
e.printStackTrace(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get FilenameFilter configured to this hints. |
| 135 |
* <p/> |
| 136 |
* By default, it returns null. You could set this field to let the IntelliHints only show the files meet your criteria. |
| 137 |
* |
| 138 |
* @return the FilenameFilter in use. |
| 139 |
*/ |
| 140 |
public FilenameFilter getFilter() { |
| 141 |
return _filter; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Set FilenameFilter to this hints. |
| 146 |
* |
| 147 |
* @see #getFilter() |
| 148 |
* @param filter the FilenameFilter in use. |
| 149 |
*/ |
| 150 |
public void setFilter(FilenameFilter filter) { |
| 151 |
_filter = filter; |
| 152 |
} |
| 153 |
|
| 154 |
private class PrefixListCellRenderer extends DefaultListCellRenderer { |
| 155 |
private String _prefix; |
| 156 |
|
| 157 |
public PrefixListCellRenderer(String prefix) { |
| 158 |
_prefix = prefix; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 163 |
return super.getListCellRendererComponent(list, _prefix + value, index, isSelected, cellHasFocus); |
| 164 |
} |
| 165 |
} |
| 166 |
} |