1 19 20 package com.sshtools.ui.awt; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Checkbox ; 24 import java.awt.Choice ; 25 import java.awt.Component ; 26 import java.awt.Dimension ; 27 import java.awt.FlowLayout ; 28 import java.awt.GridBagConstraints ; 29 import java.awt.GridBagLayout ; 30 import java.awt.Insets ; 31 import java.awt.Label ; 32 import java.awt.Panel ; 33 import java.awt.TextField ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.awt.event.ItemEvent ; 38 import java.awt.event.ItemListener ; 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.text.MessageFormat ; 42 import java.util.Enumeration ; 43 import java.util.Vector ; 44 45 import com.sshtools.ui.FileFilter; 46 import com.sshtools.ui.FileSelector; 47 import com.sshtools.ui.StockIcons; 48 import com.sshtools.ui.awt.options.Option; 49 import com.sshtools.ui.awt.options.OptionDialog; 50 import com.sshtools.util.StringComparator; 51 import com.sshtools.util.Util; 52 53 56 public class AWTFileSelector extends Panel implements FileSelector, ActionListener , ItemListener { 57 58 59 private java.awt.List files; 60 private File cwd; 61 private TextField path; 62 private Choice lookIn; 64 private ImageButton go; 65 private ImageButton remove; 66 private ImageButton newFolder; 67 private ImageButton home; 68 private ImageButton parent; 69 private int type; 70 private Checkbox showHiddenFiles; 71 private Choice filterSelect; 72 private Vector filters; 73 74 private FileFilter acceptAllFilter = new FileFilter() { 75 76 public String getDescription() { 77 return Messages.getString("AWTFileSelector.allFiles"); } 79 80 public boolean accept(File f) { 81 return true; 82 } 83 84 }; 85 86 public AWTFileSelector() { 87 super(new BorderLayout ()); 88 } 89 90 public void init(int type, File cwd, boolean showButtons, boolean showHiddenFilesSwitch, boolean showButtonImages, boolean showButtonText) { 91 92 this.cwd = cwd; 94 this.type = type; 95 filters = new Vector (); 96 filters.addElement(acceptAllFilter); 97 if (cwd == null) { 98 cwd = new File (System.getProperty("user.dir")); } 100 101 files = new SelectList() { 103 104 public Dimension getPreferredSize() { 105 return new Dimension (400, 260); 106 } 107 108 public void selected() { 109 String item = files.getSelectedItem(); 110 if (item != null) { 111 path.setText(item); 112 selectFile(); 113 } 114 } 115 116 }; 117 files.addItemListener(this); 118 119 lookIn = new Choice (); 121 lookIn.addItemListener(new ItemListener () { 122 123 public void itemStateChanged(ItemEvent e) { 124 AWTFileSelector.this.cwd = new File (lookIn.getSelectedItem()); 125 refresh(); 126 } 127 128 }); 129 rebuildLookIn(); 130 131 Panel z = new Panel (new FlowLayout ()); 133 if (showButtons) { 134 home = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_HOME, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.home") : null, "home"); home.setHoverButton(true); 136 home.addActionListener(this); 137 home.setToolTipText(Messages.getString("AWTFileSelector.navigateToYourHomeDirectory")); z.add(home); 139 parent = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_UP_FOLDER, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.home") : null, "home"); parent.setHoverButton(true); 141 parent.addActionListener(this); 142 parent.setToolTipText(Messages.getString("AWTFileSelector.navigateToParent")); z.add(parent); 144 newFolder = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_NEW_FOLDER, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.new") : null, "newFolder"); newFolder.setHoverButton(true); 146 newFolder.addActionListener(this); 147 newFolder.setToolTipText(Messages.getString("AWTFileSelector.createFolder")); z.add(newFolder); 149 remove = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_DELETE, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.delete") : null, "delete"); remove.setHoverButton(true); 151 remove.addActionListener(this); 152 remove.setToolTipText(Messages.getString("AWTFileSelector.removeSelected")); z.add(remove); 154 } 155 if (showHiddenFilesSwitch) { 156 showHiddenFiles = new Checkbox (Messages.getString("AWTFileSelector.hiddentFiles")); showHiddenFiles.addItemListener(new ItemListener () { 158 159 public void itemStateChanged(ItemEvent e) { 160 refresh(); 161 } 162 }); 163 } 164 165 Panel top = new Panel (new GridBagLayout ()); 167 GridBagConstraints gbc = new GridBagConstraints (); 168 gbc.fill = GridBagConstraints.HORIZONTAL; 169 gbc.anchor = GridBagConstraints.WEST; 170 gbc.weightx = 1.0; 171 UIUtil.gridBagAdd(top, lookIn, gbc, GridBagConstraints.RELATIVE); 172 gbc.weightx = 0.0; 173 UIUtil.gridBagAdd(top, z, gbc, GridBagConstraints.REMAINDER); 174 175 Panel pathPanel = new Panel (new GridBagLayout ()); 177 GridBagConstraints gbc1 = new GridBagConstraints (); 178 path = new TextField (""); path.addActionListener(this); 180 gbc1.fill = GridBagConstraints.HORIZONTAL; 181 gbc1.anchor = GridBagConstraints.WEST; 182 gbc1.weightx = 0.0; 183 gbc1.insets = new Insets (2, 2, 2, 2); 184 UIUtil.gridBagAdd(pathPanel, new Label (Messages.getString("AWTFileSelector.fileName")), gbc1, showButtons ? 1 : GridBagConstraints.RELATIVE); gbc1.weightx = 1.0; 186 UIUtil.gridBagAdd(pathPanel, path, gbc1, showButtons ? GridBagConstraints.RELATIVE : GridBagConstraints.REMAINDER); 187 gbc1.weightx = 0.0; 188 if(showButtons) { 189 go = new ImageButton(null, Messages.getString("AWTFileSelector.go"), "go"); go.setHoverButton(true); 191 go.addActionListener(this); 192 go.setToolTipText(Messages.getString("AWTFileSelector.navigateToSelectedFolder")); UIUtil.gridBagAdd(pathPanel, go, gbc1, GridBagConstraints.REMAINDER); 194 } 195 UIUtil.gridBagAdd(pathPanel, new Label (Messages.getString("AWTFileSelector.filesOfType")), gbc1, showButtons ? 1 : GridBagConstraints.RELATIVE); gbc1.weightx = 1.0; 197 filterSelect = new Choice (); 198 rebuildFilterSelect(); 199 filterSelect.addItemListener(new ItemListener () { 200 public void itemStateChanged(ItemEvent e) { 201 refresh(); 202 } 203 }); 204 UIUtil.gridBagAdd(pathPanel, filterSelect, gbc1, showButtons ? GridBagConstraints.RELATIVE : GridBagConstraints.REMAINDER); 205 gbc1.weightx = 0.0; 206 if(showButtons) { 207 UIUtil.gridBagAdd(pathPanel, new Label (), gbc1, GridBagConstraints.REMAINDER); 208 } 209 210 add(top, "North"); add(files, "Center"); add(pathPanel, "South"); refresh(); 215 } 216 217 public void setUseAcceptAllFilter(boolean useAcceptAllFilter) { 218 if(useAcceptAllFilter && !filters.contains(acceptAllFilter)) { 219 filters.insertElementAt(acceptAllFilter, 0); 220 } 221 else if(!useAcceptAllFilter && filters.contains(acceptAllFilter)) { 222 filters.removeElement(acceptAllFilter); 223 } 224 rebuildFilterSelect(); 225 } 226 227 public void addFileFilter(FileFilter filter) { 228 filters.addElement(filter); 229 rebuildFilterSelect(); 230 } 231 232 private void rebuildFilterSelect() { 233 filterSelect.removeAll(); 234 for(Enumeration e = filters.elements(); e.hasMoreElements(); ) { 235 FileFilter f = (FileFilter)e.nextElement(); 236 filterSelect.add(f.getDescription()); 237 } 238 } 239 240 private void rebuildLookIn() { 241 File dir = cwd; 242 String lastParentPath = null; 243 lookIn.removeAll(); 244 while (dir != null && dir.exists()) { 245 lookIn.add(dir.getAbsolutePath()); 246 String parentPath = dir.getParent(); 247 System.out.println("Parent = " + parentPath); dir = parentPath == null ? null : new File (parentPath); 249 } 250 } 251 252 private void gotoParent() { 253 254 String newPath = cwd.getAbsolutePath(); 255 if (newPath.endsWith(File.separator)) { 256 newPath = newPath.substring(0, newPath.length() - 1); 257 } 258 int idx = newPath.lastIndexOf(File.separator); 259 if (idx != -1) { 260 newPath = newPath.substring(0, idx + 1); 261 cwd = new File (newPath); 262 path.setText(""); refresh(); 264 } 265 } 266 267 private void selectFile() { 268 if (path.getText().equals("..")) { gotoParent(); 270 } else { 271 File f = new File (path.getText()); 272 if (!f.isAbsolute()) 273 f = new File (cwd, path.getText()); 274 if (f.exists()) { 275 if (f.isFile()) { 276 cwd = new File (f.getParent()); 277 path.setText(f.getName()); 278 } else { 279 cwd = f; 280 path.setText(""); } 282 refresh(); 283 } else { 284 Toolkit.getDefaultToolkit().beep(); 285 } 286 } 287 } 288 289 public File [] getSelectedFiles() { 290 int[] sel = files.getSelectedIndexes(); 291 File [] f = new File [sel.length]; 292 for (int i = 0; i < sel.length; i++) { 293 f[i] = new File (cwd, files.getItem(sel[i]).toString()); 294 } 295 return f; 296 } 297 298 public File getSelectedFile() { 299 File f = new File (path.getText()); 300 if (f.isAbsolute()) 301 if (type == 1 && !f.isDirectory()) 302 return cwd; 303 else 304 return f; 305 if (type == 1 && !f.isDirectory()) 306 return cwd; 307 else 308 return new File (cwd, path.getText()); 309 } 310 311 public void refresh() { 312 String l[] = cwd.list(); 313 rebuildLookIn(); 314 files.removeAll(); 315 Vector v = new Vector (); 316 if (cwd.getParent() != null) 317 files.add(".."); for (int i = 0; l != null && i < l.length; i++) { 319 if (showHiddenFiles == null || showHiddenFiles.getState() && l[i].startsWith(".") || !l[i].startsWith(".")) { if(filters.size() == 0) { 321 v.addElement(l[i]); 322 } 323 else { 324 for(Enumeration e = filters.elements(); e.hasMoreElements(); ) { 325 FileFilter filter = (FileFilter)e.nextElement(); 326 if(filter.getDescription().equals(filterSelect.getSelectedItem())) { 327 File f = new File (cwd, l[i]); 328 if(f.isDirectory() || filter.accept(f)) { 329 v.addElement(l[i]); 330 break; 331 } 332 } 333 } 334 } 335 } 336 } 337 Util.sort(v, StringComparator.getDefaultInstance()); 338 for (Enumeration e = v.elements(); e.hasMoreElements();) { 339 files.addItem(e.nextElement().toString()); 340 } 341 files.deselect(files.getSelectedIndex()); 342 doLayout(); 343 } 344 345 public void actionPerformed(ActionEvent e) { 346 if (e.getSource() == home) { 347 cwd = new File (System.getProperty("user.home")); refresh(); 349 } 350 else if (e.getSource() == parent) { 351 gotoParent(); 352 } 353 else if (e.getSource() == remove) { 354 File f = getSelectedFile(); 355 Option choice = OptionDialog.prompt(this, OptionDialog.WARNING, Messages.getString("AWTFileSelector.confirmRemove"), MessageFormat.format(Messages.getString("AWTFileSelector.confirmRemoveText"), new Object [] { f.getPath() } ), OptionDialog.CHOICES_YES_NO); 357 if (choice == OptionDialog.CHOICE_YES) 358 if (!f.delete()) { 359 OptionDialog.error(this, Messages.getString("AWTFileSelector.error"), Messages.getString("AWTFileSelector.failedToRemove")); } else { 361 refresh(); 362 path.setText(""); } 364 } else if (e.getSource() == newFolder) 365 newFolder(); 366 else 367 selectFile(); 368 } 369 370 private void newFolder() { 371 String name = OptionDialog.promptForText(this, Messages.getString("AWTFileSelector.newFolder"), "", null, ' ', Messages.getString("AWTFileSelector.name")); if (name != null) { 373 File f = new File (cwd, name); 374 try { 375 if (!f.mkdir()) 376 throw new IOException (Messages.getString("AWTFileSelector.couldNotCreateDirectory")); refresh(); 378 } catch (IOException ioe) { 379 OptionDialog.error(this, Messages.getString("AWTFileSelector.error"), ioe.getMessage()); } 381 } 382 } 383 384 public void setAllowMultipleSelection(boolean allowMultipleSelection) { 385 files.setMultipleMode(allowMultipleSelection); 386 } 387 388 public void itemStateChanged(ItemEvent e) { 389 String sel = files.getSelectedItem(); 390 if (sel != null && sel.equals("..")) { String parent = getWorkingDirectory().getParent(); 392 path.setText(parent); 393 } 394 path.setText(sel != null ? sel : ""); } 396 397 public Option showDialog(Component parent, String title) { 398 Option choice = OptionDialog.prompt(parent, OptionDialog.UNCATEGORISED, title, this, OptionDialog.CHOICES_OK_CANCEL, null, showHiddenFiles); 399 return choice; 400 } 401 402 public File getWorkingDirectory() { 403 return cwd; 404 } 405 406 409 public void setSelectedFileFilter(FileFilter filter) { 410 int idx = filters.indexOf(filter); 411 if(idx != -1) { 412 filterSelect.select(idx); 413 } 414 } 415 416 419 public void setWorkingDirectory(File cwd) { 420 this.cwd = cwd; 421 refresh(); 422 } 423 424 public static void main(String [] args) { 425 } 426 } | Popular Tags |