1 6 21 22 package de.schlichtherle.io.swing; 23 24 import de.schlichtherle.io.ArchiveDetector; 25 import de.schlichtherle.io.File; 26 import de.schlichtherle.key.PromptingKeyManager; 27 import de.schlichtherle.swing.AbstractComboBoxBrowser; 28 29 import java.io.FilenameFilter ; 30 import java.text.Collator ; 31 import java.util.Arrays ; 32 33 import javax.swing.DefaultComboBoxModel ; 34 import javax.swing.JComboBox ; 35 36 53 public class FileComboBoxBrowser extends AbstractComboBoxBrowser { 54 55 private java.io.File directory = new File("."); 56 57 61 public FileComboBoxBrowser() { 62 } 63 64 70 public FileComboBoxBrowser(final JComboBox comboBox) { 71 super(comboBox); 72 if (comboBox != null) { 73 final Object item = comboBox.getSelectedItem(); 74 if (item == null || item instanceof String ) 75 update( (DefaultComboBoxModel ) comboBox.getModel(), 76 directory, (String ) item); 77 } 78 } 79 80 85 public java.io.File getDirectory() { 86 return directory; 87 } 88 89 96 public void setDirectory(final java.io.File directory) { 97 this.directory = directory != null ? directory : new File("."); 98 } 99 100 114 protected boolean update(final String initials) { 115 return update( (DefaultComboBoxModel ) getComboBox().getModel(), 116 getDirectory(), initials); 117 } 118 119 private static boolean update( 120 final DefaultComboBoxModel model, 121 java.io.File dir, 122 String initials) { 123 129 if (initials == null) 130 initials = ""; 131 132 final String prefix, base; 135 if ("".equals(initials)) { 136 prefix = base = ""; 137 } else { 138 final ArchiveDetector detector; 139 if (dir instanceof File) 140 detector = ((File) dir).getArchiveDetector(); 141 else 142 detector = ArchiveDetector.NULL; 143 File node = new File(initials, detector); 144 if (node.isAbsolute()) { 145 final boolean dirPath = node.getPath().length() < initials.length(); 146 if (dirPath) 147 PromptingKeyManager.resetCancelledPrompts(); 148 if (dirPath && node.isDirectory()) { 152 dir = node; 153 prefix = ""; 154 } else { 155 dir = (File) node.getParentFile(); 156 if (dir == null) { 157 dir = node; 158 prefix = ""; 159 } else { 160 prefix = node.getName(); 161 } 162 } 163 if (dir.getPath().endsWith(File.separator)) { 164 base = initials.substring(0, dir.getPath().length()); 166 } else { 167 base = initials.substring(0, dir.getPath().length() + 1); 169 } 170 } else { 171 final java.io.File directory = dir; 172 node = new File(directory, initials); final boolean dirPath = node.getPath().length() 174 < (directory.getPath() + File.separator + initials).length(); 175 if (dirPath) 176 PromptingKeyManager.resetCancelledPrompts(); 177 if (dirPath && node.isDirectory()) { 180 dir = node; 181 prefix = ""; 182 } else { 183 dir = (File) node.getParentFile(); 184 prefix = node.getName(); 185 } 186 base = initials.substring(0, dir.getPath().length() - directory.getPath().length()); 188 } 189 } 190 191 final FilenameFilter filter; 192 200 filter = new FilenameFilter () { 202 final int pl = prefix.length(); 203 204 public boolean accept(java.io.File d, String child) { 205 if (child.length() >= pl) 206 return prefix.equalsIgnoreCase(child.substring(0, pl)); 207 else 208 return false; 209 } 210 }; 211 final String [] children = dir.list(filter); 213 214 try { 219 model.removeAllElements(); 220 final int l = children != null ? children.length : 0; 221 if (l > 0) { 222 Arrays.sort(children, Collator.getInstance()); for (int i = 0; i < l; i++) 224 model.addElement(base + children[i]); 225 return true; } else { 227 model.addElement(initials); 229 return false; } 231 } finally { 232 if (!initials.equals(model.getSelectedItem())) model.setSelectedItem(initials); 234 } 235 } 236 } 237 | Popular Tags |