Package com.mucommander.commons.file

Examples of com.mucommander.commons.file.AbstractFile


            // - Thread was created using an AbstractFile instance
            // - Thread was created using a FileURL, corresponding AbstractFile needs to be resolved

            // Thread was created using a FileURL
            if(folder==null) {
              AbstractFile file = FileFactory.getFile(folderURL, true);

              synchronized(KILL_LOCK) {
                if(killed) {
                  LOGGER.debug("this thread has been killed, returning");
                  break;
                }
              }

              // File resolved -> 25% complete
              folderPanel.setProgressValue(25);

              // Popup an error dialog and abort folder change if the file could not be resolved
              // or doesn't exist
              if(file==null || !file.exists()) {
                // Restore default cursor
                mainFrame.setCursor(Cursor.getDefaultCursor());

                showFolderDoesNotExistDialog();
                break;
              }

              // File is a regular directory, all good
              if(file.isDirectory()) {
                // Just continue
              }
              // File is a browsable file (Zip archive for instance) but not a directory : Browse or Download ? => ask the user
              else if(file.isBrowsable()) {
                // If history already contains this file, do not ask the question again and assume
                // the user wants to 'browse' the file. In particular, this prevent the 'Download or browse'
                // dialog from popping up when going back or forward in history.
                // The dialog is also not displayed if the file corresponds to the currently selected file,
                // which is a weak (and not so accurate) way to know if the folder change is the result
                // of the OpenAction (enter pressed on the file). This works well enough in practice.
                if(!globalHistory.historyContains(folderURL) && !file.equals(folderPanel.getFileTable().getSelectedFile())) {
                  // Restore default cursor
                  mainFrame.setCursor(Cursor.getDefaultCursor());

                  // Download or browse file ?
                  QuestionDialog dialog = new QuestionDialog(mainFrame,
                      null,
                      Translator.get("table.download_or_browse"),
                      mainFrame,
                      new String[] {BROWSE_TEXT, DOWNLOAD_TEXT, CANCEL_TEXT},
                      new int[] {BROWSE_ACTION, DOWNLOAD_ACTION, CANCEL_ACTION},
                      0);

                  int ret = dialog.getActionValue();

                  if(ret==-1 || ret==CANCEL_ACTION)
                    break;

                  // Download file
                  if(ret==DOWNLOAD_ACTION) {
                    showDownloadDialog(file);
                    break;
                  }
                  // Continue if BROWSE_ACTION
                  // Set cursor to hourglass/wait
                  mainFrame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                }
                // else just continue and browse file's contents
              }
              // File is a regular file: show download dialog which allows to download (copy) the file
              // to a directory specified by the user
              else {
                showDownloadDialog(file);
                break;
              }

              this.folder = file;
            }
            // Thread was created using an AbstractFile instance, check file existence
            else if(!folder.exists()) {
              // Find a 'workable' folder if the requested folder doesn't exist anymore
              if(findWorkableFolder) {
                AbstractFile newFolder = getWorkableFolder(folder);
                if(newFolder.equals(folder)) {
                  // If we've already tried the returned folder, give up (avoids a potentially endless loop)
                  showFolderDoesNotExistDialog();
                  break;
                }

                // Try again with the new folder
                folder = newFolder;
                folderURL = folder.getURL();
                // Discard the file to select, if any
                fileToSelect = null;

                continue;
              }
              else {
                showFolderDoesNotExistDialog();
                break;
              }
            }

            // Checks if canonical should be followed. If that is the case, the file is invalidated
            // and resolved again. This happens only once at most, to avoid a potential infinite loop
            // in the event that the absolute path still didn't match canonical one after the file is
            // resolved again.
            if(!canonicalPathFollowed && followCanonicalPath(folder)) {
              try {
                // Recreate the FileURL using the file's canonical path
                FileURL newURL = FileURL.getFileURL(folder.getCanonicalPath());
                // Keep the credentials and properties (if any)
                newURL.setCredentials(folderURL.getCredentials());
                newURL.importProperties(folderURL);
                this.folderURL = newURL;
                // Invalidate the AbstractFile instance
                this.folder = null;
                // There won't be any further attempts after this one
                canonicalPathFollowed = true;

                // Loop the resolve the file
                continue;
              }
              catch(MalformedURLException e) {
                // In the unlikely event of the canonical path being malformed, the AbstractFile
                // and FileURL instances are left untouched
              }
            }

            synchronized(KILL_LOCK) {
              if(killed) {
                LOGGER.debug("this thread has been killed, returning");
                break;
              }
            }

            // File tested -> 50% complete
            folderPanel.setProgressValue(50);

            /* TODO branch
            AbstractFile children[] = new AbstractFile[0];
            if (branchView) {
              childrenList = new ArrayList();
              readBranch(folder);
              children = (AbstractFile[]) childrenList.toArray(children);
            } else {
              children = folder.ls(chainedFileFilter);                           
            }*/

            synchronized(KILL_LOCK) {
              if(killed) {
                LOGGER.debug("this thread has been killed, returning");
                break;
              }
              // From now on, thread cannot be killed (would comprise table integrity)
              doNotKill = true;
            }

            // files listed -> 75% complete
            folderPanel.setProgressValue(75);

            LOGGER.trace("calling setCurrentFolder");

            // Change the file table's current folder and select the specified file (if any)
            setCurrentFolder(folder, fileToSelect, changeLockedTab);

            // folder set -> 95% complete
            folderPanel.setProgressValue(95);

            // If new credentials were entered by the user, these can now be considered valid
            // (folder was changed successfully), so we add them to the CredentialsManager.
            // Do not add the credentials if guest credentials were selected by the user.
            if(newCredentialsMapping!=null && !guestCredentialsSelected)
              CredentialsManager.addCredentials(newCredentialsMapping);

            // All good !
            folderChangedSuccessfully = true;

            break;
          }
          catch(Exception e) {
            LOGGER.debug("Caught exception", e);

            if(killed) {
              // If #tryKill() called #interrupt(), the exception we just caught was most likely
              // thrown as a result of the thread being interrupted.
              //
              // The exception can be a java.lang.InterruptedException (Thread throws those),
              // a java.nio.channels.ClosedByInterruptException (InterruptibleChannel throws those)
              // or any other exception thrown by some code that swallowed the original exception
              // and threw a new one.

              LOGGER.debug("Thread was interrupted, ignoring exception");
              break;
            }

            // Restore default cursor
            mainFrame.setCursor(Cursor.getDefaultCursor());

            if(e instanceof AuthException) {
              AuthException authException = (AuthException)e;
              // Retry (loop) if user provided new credentials, if not stop
              AuthDialog authDialog = popAuthDialog(authException.getURL(), true, authException.getMessage());
              newCredentialsMapping = authDialog.getCredentialsMapping();
              guestCredentialsSelected = authDialog.guestCredentialsSelected();

              if(newCredentialsMapping!=null) {
                // Invalidate the existing AbstractFile instance
                folder = null;
                // Use the provided credentials
                CredentialsManager.authenticate(folderURL, newCredentialsMapping);
                continue;
              }
            }
            else {
              // Find a 'workable' folder if the requested folder doesn't exist anymore
              if(findWorkableFolder) {
                AbstractFile newFolder = getWorkableFolder(folder);
                if(newFolder.equals(folder)) {
                  // If we've already tried the returned folder, give up (avoids a potentially endless loop)
                  showFolderDoesNotExistDialog();
                  break;
                }

View Full Code Here


        super(progressDialog, mainFrame, filesToCopy, getTemporaryFolder(filesToCopy), null, COPY_MODE, FileCollisionDialog.OVERWRITE_ACTION);
    }


    protected static AbstractFile getTemporaryFolder(FileSet files) {
        AbstractFile tempFolder;
        try {
            tempFolder = FileFactory.getTemporaryFile(files.getBaseFolder().getName(), true);
            tempFolder.mkdir();
        }
        catch(IOException e) {
            tempFolder = FileFactory.getTemporaryFolder();
        }
View Full Code Here

     * @return the files contained by the specified Transferable as a FileSet, or <code>null</code> if no file
     * was present or if an error occurred
     */
    public static FileSet getTransferFiles(Transferable transferable) {
        FileSet files;
        AbstractFile file;

        try {
            // FileSet DataFlavor
            if(transferable.isDataFlavorSupported(FILE_SET_DATA_FLAVOR)) {
                files = (FileSet)transferable.getTransferData(FILE_SET_DATA_FLAVOR);
View Full Code Here

        // Return files stored in a java.util.Vector instance
        else if(dataFlavor.equals(DataFlavor.javaFileListFlavor) && javaFileListFlavorSupported) {
            List<File> fileList = new Vector<File>(nbFiles);

            for(int i=0; i<nbFiles; i++) {
                AbstractFile file = fileSet.elementAt(i);
                fileList.add(new File(file.getAbsolutePath()));
            }
            return fileList;
        }
//        // Return an InputStream formatted in a specified Unicode charset that contains file paths separated by '\n' characters
//        else if(dataFlavor.equals(DataFlavor.getTextPlainUnicodeFlavor())) {
//            String mimeType = dataFlavor.getMimeType();
//            String charset = mimeType.substring(mimeType.indexOf("charset=")+8, mimeType.length());
//
//            StringBuilder sb = new StringBuilder();
//            for(int i=0; i<nbFiles; i++) {
//                sb.append(fileSet.fileAt(i).getAbsolutePath());
//                if(i!=nbFiles-1)
//                    sb.append('\n');
//            }
//
//            return new ByteArrayInputStream(sb.toString().getBytes(charset));
//        }
        // Return a String with file paths or names
        else if(dataFlavor.equals(DataFlavor.stringFlavor) && stringFlavorSupported) {
            StringBuilder sb = new StringBuilder();
            AbstractFile file;
            for(int i=0; i<nbFiles; i++) {
                file = fileSet.elementAt(i);
                // Check if to return string with filenames
                if (stringFlavourTransfersFilename) {
                  // Append just base name or file name with extension
                  sb.append(stringFlavourTransfersFileBaseName ? file.getBaseName() : file.getName());
                } else {
                  sb.append(file.getAbsolutePath());
                }
                                 
                if(i!=nbFiles-1)
                    sb.append('\n');
            }

            return sb.toString();
        }
        // Return a String with file URLs, as specified by RFC 2483
        else if(dataFlavor.equals(TEXT_URI_FLAVOR) && textUriFlavorSupported) {
            StringBuilder sb = new StringBuilder();
            AbstractFile file;
            for(int i=0; i<nbFiles; i++) {
                file = fileSet.elementAt(i);
                String url = file.getURL().getScheme().equals(FileProtocols.FILE)
                    // Use java.io.File.toURI() for local files (e.g. file:/mnt/music), this is the format expected by
                    // Gnome/Nautilus.
                    ?new File(file.getAbsolutePath()).toURI().toString()
                    // Use standard URL format (e.g. smb://host/share/file) for other file types
                    :file.getAbsolutePath();

                sb.append(url);
                if(i!=nbFiles-1){
                    sb.append("\r\n");
                }
View Full Code Here

      }
    }
   
    private void trySaveAs() {
        JFileChooser fileChooser = new JFileChooser();
    AbstractFile currentFile = getCurrentFile();
        // Sets selected file in JFileChooser to current file
        if(currentFile.getURL().getScheme().equals(FileProtocols.FILE))
            fileChooser.setSelectedFile(new java.io.File(currentFile.getAbsolutePath()));
        fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
        int ret = fileChooser.showSaveDialog(getFrame());
   
        if (ret==JFileChooser.APPROVE_OPTION) {
            AbstractFile destFile;
            try {
                destFile = FileFactory.getFile(fileChooser.getSelectedFile().getAbsolutePath(), true);
            }
            catch(IOException e) {
                InformationDialog.showErrorDialog(getFrame(), Translator.get("write_error"), Translator.get("file_editor.cannot_write"));
View Full Code Here

     *
     * @param  panel in which to change the location.
     * @return <code>true</code> if <code>panel</code> has a parent, <code>false</code> otherwise.
     */
    protected boolean goToParent(FolderPanel panel) {
      AbstractFile parent;

        if((parent = panel.getCurrentFolder().getParent()) != null) {
          panel.tryChangeCurrentFolder(parent, null, true);
            return true;
        }
View Full Code Here

    }

    @Override
    public void performAction() {
        // Changes the current folder to make it the user home folder
        AbstractFile homeFolder = LocalFile.getUserHome();
        if(homeFolder!=null)
            mainFrame.getActivePanel().tryChangeCurrentFolder(homeFolder);
    }
View Full Code Here

    public MkfileAction(MainFrame mainFrame, Map<String,Object> properties) {
        super(mainFrame, properties);
    }

    protected void toggleEnabledState() {
        AbstractFile firstFile = mainFrame.getActiveTable().getFileTableModel().getFileAt(0);

        // If there is no file at all, do not rely on the action being supported by the current folder as this
        // would be incorrect for some filesystems which do not support operations consistently across the
        // filesystem (e.g. S3). In that case, err on the safe side and enable the action, even if the operation
        // end up not being supported.
        setEnabled(firstFile==null || firstFile.isFileOperationSupported(FileOperation.WRITE_FILE));
    }
View Full Code Here

    /**
     * This method is overridden to enable this action when the parent folder is selected.
     */
    @Override
    protected boolean getFileTableCondition(FileTable fileTable) {
        AbstractFile selectedFile = fileTable.getSelectedFile(true, true);

        return selectedFile!=null && selectedFile.isBrowsable();
    }
View Full Code Here

    /**
     * Opens the currently selected file in the inactive folder panel.
     */
    @Override
    public void performAction() {
        AbstractFile file;

        // Retrieves the currently selected file, aborts if none (should not normally happen).
        if((file = mainFrame.getActiveTable().getSelectedFile(true, true)) == null || !file.isBrowsable())
            return;

        // Opens the currently selected file in the inactive panel.
        mainFrame.getInactivePanel().tryChangeCurrentFolder(file);
    }
View Full Code Here

TOP

Related Classes of com.mucommander.commons.file.AbstractFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.