1 22 23 package org.gjt.sp.jedit.browser; 24 25 import javax.swing.border.EmptyBorder ; 27 import javax.swing.*; 28 import java.awt.event.*; 29 import java.awt.BorderLayout ; 30 import java.awt.Cursor ; 31 import java.awt.Dimension ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.util.*; 35 import org.gjt.sp.jedit.gui.EnhancedDialog; 36 import org.gjt.sp.jedit.io.*; 37 import org.gjt.sp.jedit.*; 38 import org.gjt.sp.util.*; 39 41 46 public class VFSFileChooserDialog extends EnhancedDialog 47 { 48 49 public VFSFileChooserDialog(View view, String path, 51 int mode, boolean multipleSelection) 52 { 53 this(view,path,mode,multipleSelection,true); 54 } 56 64 public VFSFileChooserDialog(View view, String path, 65 int mode, boolean multipleSelection, boolean autoshow) 66 { 67 super(view,jEdit.getProperty("vfs.browser.title"),true); 68 69 JPanel content = new JPanel(new BorderLayout ()); 70 content.setBorder(new EmptyBorder (12,12,12,12)); 71 setContentPane(content); 72 73 String name; 74 if(mode == VFSBrowser.CHOOSE_DIRECTORY_DIALOG) 75 name = null; 76 else if(path == null || path.endsWith(File.separator) 77 || path.endsWith("/")) 78 { 79 name = null; 80 } 81 else 82 { 83 VFS vfs = VFSManager.getVFSForPath(path); 84 name = vfs.getFileName(path); 85 path = vfs.getParentOfPath(path); 86 } 87 88 browser = new VFSBrowser(view,path,mode,multipleSelection,null); 89 browser.getBrowserView().getTable().setRequestFocusEnabled(false); 90 browser.getBrowserView().getParentDirectoryList() 91 .setRequestFocusEnabled(false); 92 93 browser.addBrowserListener(new BrowserHandler()); 94 content.add(BorderLayout.CENTER,browser); 95 96 JPanel panel = new JPanel(); 97 panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS)); 98 panel.setBorder(new EmptyBorder (12,0,0,0)); 99 100 filenameField = new VFSFileNameField(browser,null); 101 filenameField.setText(name); 102 filenameField.selectAll(); 103 Box box = new Box(BoxLayout.Y_AXIS); 104 box.add(Box.createGlue()); 105 box.add(filenameField); 106 box.add(Box.createGlue()); 107 108 JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename")); 109 label.setDisplayedMnemonic(jEdit.getProperty( 110 "vfs.browser.dialog.filename.mnemonic").charAt(0)); 111 label.setLabelFor(filenameField); 112 panel.add(label); 113 panel.add(Box.createHorizontalStrut(12)); 114 115 panel.add(box); 116 117 panel.add(Box.createHorizontalStrut(12)); 118 119 ok = new JButton(); 120 getRootPane().setDefaultButton(ok); 121 122 switch(mode) 123 { 124 case VFSBrowser.OPEN_DIALOG: 125 case VFSBrowser.BROWSER_DIALOG: 126 ok.setText(jEdit.getProperty("vfs.browser.dialog.open")); 127 break; 128 case VFSBrowser.CHOOSE_DIRECTORY_DIALOG: 129 ok.setText(jEdit.getProperty("vfs.browser.dialog.choose-dir")); 130 Dimension dim = ok.getPreferredSize(); 132 ok.setPreferredSize(dim); 133 break; 134 case VFSBrowser.SAVE_DIALOG: 135 ok.setText(jEdit.getProperty("vfs.browser.dialog.save")); 136 break; 137 } 138 139 ok.addActionListener(new ActionHandler()); 140 panel.add(ok); 141 panel.add(Box.createHorizontalStrut(6)); 142 cancel = new JButton(jEdit.getProperty("common.cancel")); 143 cancel.addActionListener(new ActionHandler()); 144 panel.add(cancel); 145 146 content.add(BorderLayout.SOUTH,panel); 147 148 VFSManager.getIOThreadPool().addProgressListener( 149 workThreadHandler = new WorkThreadHandler()); 150 151 pack(); 152 GUIUtilities.loadGeometry(this,"vfs.browser.dialog"); 153 GUIUtilities.requestFocus(this,filenameField); 154 if (autoshow) 155 setVisible(true); 156 } 158 164 public VFSBrowser getBrowser() 165 { 166 return browser; 167 } 169 public void dispose() 171 { 172 GUIUtilities.saveGeometry(this,"vfs.browser.dialog"); 173 VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler); 174 super.dispose(); 175 } 177 public void ok() 179 { 180 VFSFile[] files = browser.getSelectedFiles(); 181 filename = filenameField.getText(); 182 boolean choosingDir = (browser.getMode() == 183 VFSBrowser.CHOOSE_DIRECTORY_DIALOG); 184 185 if(files.length != 0) 186 { 187 if(files.length > 1 && choosingDir) 188 { 189 isOK = true; 190 dispose(); 191 } 192 else 193 browser.filesActivated(VFSBrowser.M_OPEN,false); 194 return; 195 } 196 else if(choosingDir && (filename == null || filename.length() == 0)) 197 { 198 isOK = true; 199 dispose(); 200 return; 201 } 202 else if(filename == null || filename.length() == 0) 203 { 204 getToolkit().beep(); 205 return; 206 } 207 208 String bufferDir = browser.getView().getBuffer() 209 .getDirectory(); 210 if(filename.equals("-")) 211 filename = bufferDir; 212 else if(filename.startsWith("-/") 213 || filename.startsWith("-" + File.separator)) 214 { 215 filename = MiscUtilities.constructPath( 216 bufferDir,filename.substring(2)); 217 } 218 219 final int[] type = { -1 }; 220 filename = MiscUtilities.expandVariables(filename); 221 final String path = MiscUtilities.constructPath( 222 browser.getDirectory(),filename); 223 final VFS vfs = VFSManager.getVFSForPath(path); 224 Object session = vfs.createVFSSession(path,this); 225 if(session == null) 226 return; 227 228 VFSManager.runInWorkThread(new GetFileTypeRequest( 229 vfs,session,path,type)); 230 VFSManager.runInAWTThread(new Runnable () 231 { 232 public void run() 233 { 234 switch(type[0]) 235 { 236 case VFSFile.FILE: 237 if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG) 238 break; 239 240 if(vfs instanceof FileVFS) 241 { 242 if(doFileExistsWarning(path)) 243 break; 244 } 245 isOK = true; 246 if(browser.getMode() == VFSBrowser.BROWSER_DIALOG) 247 { 248 Hashtable props = new Hashtable(); 249 props.put(Buffer.ENCODING,browser.currentEncoding); 250 jEdit.openFile(browser.getView(), 251 browser.getDirectory(), 252 path,false,props); 253 } 254 dispose(); 255 break; 256 case VFSFile.DIRECTORY: 257 case VFSFile.FILESYSTEM: 258 browser.setDirectory(path); 259 break; 260 } 261 } 262 }); 263 } 265 public void cancel() 267 { 268 dispose(); 269 } 271 public String [] getSelectedFiles() 273 { 274 if(!isOK) 275 return null; 276 277 if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG) 278 { 279 if(browser.getSelectedFiles().length > 0) 280 { 281 return getSelectedFiles(VFSFile.DIRECTORY, 282 VFSFile.FILESYSTEM); 283 } 284 else 285 return new String [] { browser.getDirectory() }; 286 } 287 else if(filename != null && filename.length() != 0) 288 { 289 String path = browser.getDirectory(); 290 return new String [] { MiscUtilities.constructPath( 291 path,filename) }; 292 } 293 else 294 return getSelectedFiles(VFSFile.FILE,VFSFile.FILE); 295 } 297 299 private VFSBrowser browser; 301 private VFSFileNameField filenameField; 302 private String filename; 303 private JButton ok; 304 private JButton cancel; 305 private boolean isOK; 306 private WorkThreadHandler workThreadHandler; 307 309 private boolean doFileExistsWarning(String filename) 311 { 312 if(browser.getMode() == VFSBrowser.SAVE_DIALOG 313 && new File (filename).exists()) 314 { 315 String [] args = { MiscUtilities.getFileName(filename) }; 316 int result = GUIUtilities.confirm(browser, 317 "fileexists",args, 318 JOptionPane.YES_NO_OPTION, 319 JOptionPane.WARNING_MESSAGE); 320 if(result != JOptionPane.YES_OPTION) 321 return true; 322 } 323 324 return false; 325 } 327 private String [] getSelectedFiles(int type1, int type2) 329 { 330 List l = new ArrayList(); 331 VFSFile[] selectedFiles = browser.getSelectedFiles(); 332 for(int i = 0; i < selectedFiles.length; i++) 333 { 334 VFSFile file = selectedFiles[i]; 335 if(file.getType() == type1 || file.getType() == type2) 336 l.add(file.getPath()); 337 } 338 return (String [])l.toArray(new String [l.size()]); 339 } 341 343 345 class ActionHandler implements ActionListener 347 { 348 public void actionPerformed(ActionEvent evt) 349 { 350 if(evt.getSource() == ok) 351 { 352 if(!browser.getDirectory().equals( 353 browser.getDirectoryField().getText())) 354 { 355 browser.setDirectory(browser.getDirectoryField().getText()); 356 } 357 else 358 ok(); 359 } 360 else if(evt.getSource() == cancel) 361 cancel(); 362 } 363 } 365 class BrowserHandler implements BrowserListener 367 { 368 public void filesSelected(VFSBrowser browser, VFSFile[] files) 370 { 371 boolean choosingDir = (browser.getMode() 372 == VFSBrowser.CHOOSE_DIRECTORY_DIALOG); 373 374 if(files.length == 0) 375 { 376 if(choosingDir) 377 { 378 ok.setText(jEdit.getProperty( 379 "vfs.browser.dialog.choose-dir")); 380 } 381 return; 382 } 383 else if(files.length == 1) 384 { 385 if(choosingDir) 386 { 387 ok.setText(jEdit.getProperty( 388 "vfs.browser.dialog.open")); 389 } 390 391 VFSFile file = files[0]; 392 if(file.getType() == VFSFile.FILE) 393 { 394 String path = file.getPath(); 395 String directory = browser.getDirectory(); 396 String parent = MiscUtilities 397 .getParentOfPath(path); 398 if(MiscUtilities.pathsEqual(parent,directory)) 399 path = file.getName(); 400 401 filenameField.setText(path); 402 filenameField.selectAll(); 403 } 404 } 405 else 406 { 407 if(choosingDir) 408 { 409 ok.setText(jEdit.getProperty( 410 "vfs.browser.dialog.choose-dir")); 411 } 412 413 filenameField.setText(null); 414 } 415 } 417 public void filesActivated(VFSBrowser browser, VFSFile[] files) 419 { 420 filenameField.selectAll(); 421 422 if(files.length == 0) 423 { 424 ok(); 428 return; 429 } 430 431 for(int i = 0; i < files.length; i++) 432 { 433 if(files[i].getType() == VFSFile.FILE) 434 { 435 String path = files[i].getPath(); 436 VFS vfs = VFSManager.getVFSForPath(path); 437 if(browser.getMode() == VFSBrowser.SAVE_DIALOG 438 && vfs instanceof FileVFS) 439 { 440 if(doFileExistsWarning(path)) 441 return; 442 } 443 444 isOK = true; 445 filenameField.setText(null); 446 if(browser.getMode() != VFSBrowser.CHOOSE_DIRECTORY_DIALOG) 447 { 448 dispose(); 449 } 450 return; 451 } 452 else 453 return; 454 } 455 } } 458 class KeyHandler extends KeyAdapter 460 { 461 public void keyTyped(KeyEvent evt) 462 { 463 switch(evt.getKeyChar()) 464 { 465 case '/': 466 case '-': 467 case '~': 468 filenameField.processKeyEvent(evt); 469 filenameField.requestFocus(); 470 break; 471 } 472 } 473 } 475 class WorkThreadHandler implements WorkThreadProgressListener 477 { 478 public void statusUpdate(final WorkThreadPool threadPool, 480 final int threadIndex) 481 { 482 SwingUtilities.invokeLater(new Runnable () 483 { 484 public void run() 485 { 486 int requestCount = threadPool.getRequestCount(); 487 if(requestCount == 0) 488 { 489 getContentPane().setCursor( 490 Cursor.getDefaultCursor()); 491 } 492 else if(requestCount >= 1) 493 { 494 getContentPane().setCursor( 495 Cursor.getPredefinedCursor( 496 Cursor.WAIT_CURSOR)); 497 } 498 } 499 }); 500 } 502 public void progressUpdate(WorkThreadPool threadPool, int threadIndex) 504 { 505 } } 508 class GetFileTypeRequest implements Runnable 510 { 511 VFS vfs; 512 Object session; 513 String path; 514 int[] type; 515 516 GetFileTypeRequest(VFS vfs, Object session, 517 String path, int[] type) 518 { 519 this.vfs = vfs; 520 this.session = session; 521 this.path = path; 522 this.type = type; 523 } 524 525 public void run() 526 { 527 try 528 { 529 VFSFile entry = vfs._getFile( 530 session, 531 path, 532 browser); 533 if(entry == null) 534 { 535 type[0] = VFSFile.FILE; 537 } 538 else 539 type[0] = entry.getType(); 540 } 541 catch(IOException e) 542 { 543 VFSManager.error(e,path,browser); 544 return; 545 } 546 finally 547 { 548 try 549 { 550 vfs._endVFSSession( 551 session, 552 browser); 553 } 554 catch(IOException e) 555 { 556 VFSManager.error(e,path,browser); 557 return; 558 } 559 } 560 } 561 } 563 } 565 | Popular Tags |