Search in sources :

Example 71 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class FileTableModel method getFileRow.

/**
 * Returns the index of the row where the given file is located, <code>-1<code> if the file is not in the
 * current folder.
 *
 * @param file the file for which to find the row index
 * @return the index of the row where the given file is located, <code>-1<code> if the file is not in the
 * current folder
 */
public synchronized int getFileRow(AbstractFile file) {
    // Handle parent folder file
    if (parent != null && file.equals(parent))
        return 0;
    // Use dichotomic binary search rather than a dumb linear search since file array is sorted,
    // complexity is reduced to O(log n) instead of O(n^2)
    int left = parent == null ? 0 : 1;
    int right = getRowCount() - 1;
    int mid;
    AbstractFile midFile;
    FileComparator fc = getFileComparator(sortInfo);
    while (left <= right) {
        mid = (right - left) / 2 + left;
        midFile = getCachedFileAtRow(mid);
        if (midFile.equals(file))
            return mid;
        if (fc.compare(file, midFile) < 0)
            right = mid - 1;
        else
            left = mid + 1;
    }
    return -1;
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) FileComparator(com.mucommander.commons.file.util.FileComparator)

Example 72 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class FileTableModel method getNameFunc.

public Function<AbstractFile, String> getNameFunc() {
    switch(currentFolder.getURL().getScheme()) {
        case SearchFile.SCHEMA:
            String basePath = currentFolder.getURL().getHost();
            AbstractFile baseFile = FileFactory.getFile(basePath);
            int beginIndex = baseFile.getAbsolutePath(true).length();
            return file -> file.getAbsolutePath(false).substring(beginIndex);
        default:
            return AbstractFile::getName;
    }
}
Also used : FileFactory(com.mucommander.commons.file.FileFactory) Date(java.util.Date) Cursor(java.awt.Cursor) Set(java.util.Set) SizeFormat(com.mucommander.text.SizeFormat) HashMap(java.util.HashMap) CachedFile(com.mucommander.commons.file.CachedFile) CustomDateFormat(com.mucommander.text.CustomDateFormat) MuPreference(com.mucommander.conf.MuPreference) Function(java.util.function.Function) MuConfigurations(com.mucommander.conf.MuConfigurations) MuPreferences(com.mucommander.conf.MuPreferences) HashSet(java.util.HashSet) PathUtils(com.mucommander.commons.file.util.PathUtils) List(java.util.List) FileComparator(com.mucommander.commons.file.util.FileComparator) Map(java.util.Map) SearchFile(com.mucommander.commons.file.protocol.search.SearchFile) FileSet(com.mucommander.commons.file.util.FileSet) AbstractTableModel(javax.swing.table.AbstractTableModel) LinkedList(java.util.LinkedList) AbstractFile(com.mucommander.commons.file.AbstractFile) FileFilter(com.mucommander.commons.file.filter.FileFilter) AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 73 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class FileTableModel method setRowMarked.

/**
 * Marks/Unmarks the given row. If the specified row corresponds to the special '..' parent file, the row won't
 * be marked.
 *
 * @param row the row to mark/unmark
 * @param marked <code>true</code> to mark the row, <code>false</code> to unmark it
 */
public synchronized void setRowMarked(int row, boolean marked) {
    if (row == 0 && parent != null)
        return;
    int rowIndex = parent == null ? row : row - 1;
    // Return if the row is already marked/unmarked
    final int fileIndex = fileArrayIndex[rowIndex];
    if (marked == rowMarked[fileIndex])
        return;
    AbstractFile file = getCachedFileAtRow(row);
    // Do not call getSize() on directories, it's unnecessary and the value is most likely not cached by CachedFile yet
    long fileSize;
    if (file.isDirectory()) {
        markedDirectories.add(file);
        fileSize = 0;
    } else {
        fileSize = file.getSize();
    }
    // - marked files FileSet
    if (marked) {
        // File size can equal -1 if not available, do not count that in total
        if (fileSize > 0)
            markedTotalSize += fileSize;
        nbRowsMarked++;
    } else {
        // File size can equal -1 if not available, do not count that in total
        if (fileSize > 0)
            markedTotalSize -= fileSize;
        nbRowsMarked--;
    }
    rowMarked[fileIndex] = marked;
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 74 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class ConfFileTableTab method getInitialAbstractPaths.

/**
 * Returns a valid initial abstract path for the specified frame.
 * <p>
 * This method does its best to interpret <code>path</code> properly, or to fail
 * politely if it can't. This means that:<br/>
 * - we first try to see whether <code>path</code> is a legal, existing URI.<br/>
 * - if it's not, we check whether it might be a legal local, existing file path.<br/>
 * - if it's not, we'll just use the default initial path for the frame.<br/>
 * - if <code>path</code> is browsable (eg directory, archive, ...), use it as is.<br/>
 * - if it's not, use its parent.<br/>
 * - if it does not have a parent, use the default initial path for the frame.<br/>
 * </p>
 *
 * @param path            path to the folder we want to open in <code>frame</code>.
 * @param folderPanelType identifier of the panel we want to compute the path for (either {@link com.mucommander.ui.main.FolderPanel.FolderPanelType.LEFT} or
 *                        {@link #@link com.mucommander.ui.main.FolderPanel.FolderPanelType.RIGHT}).
 * @return our best shot at what was actually requested.
 */
private FileURL getInitialAbstractPaths(String path) {
    // This is one of those cases where a null value actually has a proper meaning.
    if (path == null)
        return MainFrameBuilder.getHomeFolder().getURL();
    // Tries the specified path as-is.
    AbstractFile file;
    CredentialsMapping newCredentialsMapping;
    while (true) {
        try {
            file = FileFactory.getFile(path, true);
            if (!file.exists())
                file = null;
            break;
        }// If an AuthException occurred, gets login credential from the user.
         catch (Exception e) {
            if (e instanceof AuthException) {
                // Prompts the user for a login and password.
                AuthException authException = (AuthException) e;
                FileURL url = authException.getURL();
                AuthDialog authDialog = new AuthDialog(WindowManager.getCurrentMainFrame(), url, true, authException.getMessage());
                authDialog.showDialog();
                newCredentialsMapping = authDialog.getCredentialsMapping();
                if (newCredentialsMapping != null) {
                    // Use the provided credentials
                    CredentialsManager.authenticate(url, newCredentialsMapping);
                    path = url.toString(true);
                } else // If the user cancels, we fall back to the default path.
                {
                    return MainFrameBuilder.getHomeFolder().getURL();
                }
            } else {
                file = null;
                break;
            }
        }
    }
    // If the specified path does not work out,
    if (file == null)
        // Tries the specified path as a relative path.
        if ((file = FileFactory.getFile(new File(path).getAbsolutePath())) == null || !file.exists())
            // Defaults to home.
            return MainFrameBuilder.getHomeFolder().getURL();
    // If the specified path is a non-browsable, uses its parent.
    if (!file.isBrowsable()) {
        fileToSelect = file;
        // a file without a parent directory.
        if ((file = file.getParent()) == null)
            return MainFrameBuilder.getHomeFolder().getURL();
    }
    return file.getURL();
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile) AuthException(com.mucommander.commons.file.AuthException) AuthDialog(com.mucommander.ui.dialog.auth.AuthDialog) AbstractFile(com.mucommander.commons.file.AbstractFile) File(java.io.File) AuthException(com.mucommander.commons.file.AuthException) CredentialsMapping(com.mucommander.auth.CredentialsMapping)

Example 75 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class MainMenuBar method menuSelected.

// ////////////////////////
// MenuListener methods //
// ////////////////////////
public void menuSelected(MenuEvent e) {
    Object source = e.getSource();
    if (source == viewMenu) {
        FileTable activeTable = mainFrame.getActiveTable();
        // Select the 'sort by' criterion currently in use in the active table
        sortByItems[activeTable.getSortInfo().getCriterion().ordinal()].setSelected(true);
        toggleShowFoldersFirstItem.setSelected(activeTable.getSortInfo().getFoldersFirst());
        toggleShowHiddenFilesItem.setSelected(MuConfigurations.getPreferences().getVariable(MuPreference.SHOW_HIDDEN_FILES, MuPreferences.DEFAULT_SHOW_HIDDEN_FILES));
        toggleTreeItem.setSelected(activeTable.getFolderPanel().isTreeVisible());
        toggleToggleAutoSizeItem.setSelected(mainFrame.isAutoSizeColumnsEnabled());
        toggleUseSinglePanel.setSelected(mainFrame.isSinglePanel());
    /* TODO branch toggleBranchView.setSelected(activeTable.getFolderPanel().isBranchView()); */
    } else if (source == columnsMenu) {
        // Update the selected and enabled state of each column menu item.
        FileTable activeTable = mainFrame.getActiveTable();
        for (Column c : Column.values()) {
            if (// Name column doesn't have a menu item as it cannot be disabled
            c == Column.NAME)
                continue;
            JCheckBoxMenuItem item = toggleColumnItems[c.ordinal()];
            item.setSelected(activeTable.isColumnEnabled(c));
            item.setEnabled(activeTable.isColumnDisplayable(c));
            // Override the action's label to a shorter one
            item.setText(c.getLabel());
        }
    } else if (source == goMenu) {
        // as they might have changed since menu was last selected
        for (int i = goMenu.getItemCount(); i > volumeOffset; i--) goMenu.remove(volumeOffset);
        AbstractFile[] volumes = LocalFile.getVolumes();
        int nbFolders = volumes.length;
        for (int i = 0; i < nbFolders; i++) goMenu.add(new OpenLocationAction(mainFrame, new Hashtable<String, Object>(), volumes[i]));
    } else if (source == bookmarksMenu) {
        // as bookmarks might have changed since menu was last selected
        for (int i = bookmarksMenu.getItemCount(); i > bookmarksOffset; i--) bookmarksMenu.remove(bookmarksOffset);
        // Add bookmarks menu items
        java.util.List<Bookmark> bookmarks = BookmarkManager.getBookmarks();
        int nbBookmarks = bookmarks.size();
        if (nbBookmarks > 0) {
            for (int i = 0; i < nbBookmarks; i++) MenuToolkit.addMenuItem(bookmarksMenu, new OpenLocationAction(mainFrame, new Hashtable<String, Object>(), bookmarks.get(i)), null);
        } else {
            // Show 'No bookmark' as a disabled menu item instead showing nothing
            JMenuItem noBookmarkItem = MenuToolkit.addMenuItem(bookmarksMenu, Translator.get("bookmarks_menu.no_bookmark"), null, null, null);
            noBookmarkItem.setEnabled(false);
        }
    } else if (source == windowMenu) {
        // Select the split orientation currently in use
        if (mainFrame.getSplitPaneOrientation())
            splitVerticallyItem.setSelected(true);
        else
            splitHorizontallyItem.setSelected(true);
        // menu has been deselected.
        for (int i = windowMenu.getItemCount(); i > windowOffset; i--) windowMenu.remove(windowOffset);
        // This WeakHashMap maps menu items to frame instances. It has to be a weakly referenced hash map
        // and not a regular hash map, since it will not (and cannot) be emptied when the menu has been deselected
        // and we really do not want this hash map to prevent the frames to be GCed
        windowMenuFrames = new WeakHashMap<JMenuItem, Frame>();
        // Create a menu item for each of the MainFrame instances, that displays the MainFrame's path
        // and a keyboard accelerator to recall the frame (for the first 10 frames only).
        java.util.List<MainFrame> mainFrames = WindowManager.getMainFrames();
        MainFrame mainFrame;
        JCheckBoxMenuItem checkBoxMenuItem;
        int nbFrames = mainFrames.size();
        for (int i = 0; i < nbFrames; i++) {
            mainFrame = mainFrames.get(i);
            checkBoxMenuItem = new JCheckBoxMenuItem();
            // If frame number is less than 10, use the corresponding action class (accelerator will be displayed in the menu item)
            MuAction recallWindowAction;
            if (i < 10) {
                recallWindowAction = ActionManager.getActionInstance(RECALL_WINDOW_ACTION_IDS[i], this.mainFrame);
            } else // Else use the generic RecallWindowAction
            {
                Hashtable<String, Object> actionProps = new Hashtable<String, Object>();
                // Specify the window number using the dedicated property
                actionProps.put(RecallWindowAction.WINDOW_NUMBER_PROPERTY_KEY, "" + (i + 1));
                recallWindowAction = ActionManager.getActionInstance(new ActionParameters(RecallWindowAction.Descriptor.ACTION_ID, actionProps), this.mainFrame);
            }
            checkBoxMenuItem.setAction(recallWindowAction);
            // Replace the action's label and use the MainFrame's current folder path instead
            checkBoxMenuItem.setText((i + 1) + " " + mainFrame.getActiveTable().getFolderPanel().getCurrentFolder().getAbsolutePath());
            // Use the action's label as a tooltip
            checkBoxMenuItem.setToolTipText(recallWindowAction.getLabel());
            // Check current MainFrame (the one this menu bar belongs to)
            checkBoxMenuItem.setSelected(mainFrame == this.mainFrame);
            windowMenu.add(checkBoxMenuItem);
        }
        // Add 'other' (non-MainFrame) windows : viewer and editor frames, no associated accelerator
        Frame[] frames = Frame.getFrames();
        nbFrames = frames.length;
        Frame frame;
        JMenuItem menuItem;
        boolean firstFrame = true;
        for (int i = 0; i < nbFrames; i++) {
            frame = frames[i];
            // Test if Frame is not hidden (disposed), Frame.getFrames() returns both active and disposed frames
            if (frame.isShowing() && (frame instanceof FileFrame)) {
                // and other frames
                if (firstFrame) {
                    windowMenu.add(new JSeparator());
                    firstFrame = false;
                }
                // Use frame's window title
                menuItem = new JMenuItem(frame.getTitle());
                menuItem.addActionListener(this);
                windowMenu.add(menuItem);
                windowMenuFrames.put(menuItem, frame);
            }
        }
    } else if (source == themesMenu) {
        // Remove all previous theme items, create new ones for each available theme and select the current theme
        themesMenu.removeAll();
        ButtonGroup buttonGroup = new ButtonGroup();
        Iterator<Theme> themes = ThemeManager.availableThemes();
        Theme theme;
        JCheckBoxMenuItem item;
        themesMenu.add(new JMenuItem(new EditCurrentThemeAction()));
        themesMenu.add(new JSeparator());
        while (themes.hasNext()) {
            theme = themes.next();
            item = new JCheckBoxMenuItem(new ChangeCurrentThemeAction(theme));
            buttonGroup.add(item);
            if (ThemeManager.isCurrentTheme(theme))
                item.setSelected(true);
            themesMenu.add(item);
        }
    }
}
Also used : FileFrame(com.mucommander.ui.viewer.FileFrame) MainFrame(com.mucommander.ui.main.MainFrame) Frame(java.awt.Frame) AbstractFile(com.mucommander.commons.file.AbstractFile) FileTable(com.mucommander.ui.main.table.FileTable) MainFrame(com.mucommander.ui.main.MainFrame) JSeparator(javax.swing.JSeparator) Column(com.mucommander.ui.main.table.Column) Iterator(java.util.Iterator) MuAction(com.mucommander.ui.action.MuAction) JMenuItem(javax.swing.JMenuItem) Hashtable(java.util.Hashtable) FileFrame(com.mucommander.ui.viewer.FileFrame) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionParameters(com.mucommander.ui.action.ActionParameters) ButtonGroup(javax.swing.ButtonGroup) Theme(com.mucommander.ui.theme.Theme) OpenLocationAction(com.mucommander.ui.action.impl.OpenLocationAction)

Aggregations

AbstractFile (com.mucommander.commons.file.AbstractFile)150 IOException (java.io.IOException)49 FileURL (com.mucommander.commons.file.FileURL)19 FileSet (com.mucommander.commons.file.util.FileSet)11 FileTable (com.mucommander.ui.main.table.FileTable)11 DialogAction (com.mucommander.ui.dialog.DialogAction)10 File (java.io.File)9 List (java.util.List)8 MainFrame (com.mucommander.ui.main.MainFrame)6 InputStream (java.io.InputStream)6 Vector (java.util.Vector)6 AbstractArchiveEntryFile (com.mucommander.commons.file.archive.AbstractArchiveEntryFile)5 ProtocolFile (com.mucommander.commons.file.protocol.ProtocolFile)5 LocalFile (com.mucommander.commons.file.protocol.local.LocalFile)5 ProgressDialog (com.mucommander.ui.dialog.file.ProgressDialog)5 FolderPanel (com.mucommander.ui.main.FolderPanel)5 FileTableModel (com.mucommander.ui.main.table.FileTableModel)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)4