se.trixon.solos.core.panel.NavigatorPanel.java Source code

Java tutorial

Introduction

Here is the source code for se.trixon.solos.core.panel.NavigatorPanel.java

Source

/*
 * Copyright 2014 Patrik Karlsson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.solos.core.panel;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import se.trixon.almond.dictionary.Dict;
import se.trixon.almond.icon.Pict;

/**
 *
 * @author Patrik Karlsson <patrik@trixon.se>
 */
public class NavigatorPanel extends javax.swing.JPanel {

    private File mPath;
    private NavigatorListener mNavigatorListener;

    /**
     * Creates new form PathPanel
     */
    public NavigatorPanel() {
        initComponents();
        init();
    }

    public File getPath() {
        return mPath;
    }

    public void setNavigatorListener(NavigatorListener navigatorListener) {
        mNavigatorListener = navigatorListener;
    }

    public void setPath(File path) {
        mPath = path;
        textField.setText(path.getAbsolutePath());
    }

    private void directoryChanged() {
        if (mPath.isDirectory()) {
            mNavigatorListener.onDirectoryChanged(mPath);
        }
    }

    private void init() {
        DropTarget dropTarget = new DropTarget() {
            @Override
            public synchronized void drop(DropTargetDropEvent evt) {
                try {
                    evt.acceptDrop(DnDConstants.ACTION_COPY);
                    List<File> droppedFiles = (List<File>) evt.getTransferable()
                            .getTransferData(DataFlavor.javaFileListFlavor);
                    File file = droppedFiles.get(0);

                    if (droppedFiles.size() == 1) {
                        if (file.isDirectory()) {
                            setPath(file);
                        } else if (file.isFile()) {
                            setPath(file.getParentFile());
                        }
                    } else {
                        setPath(file.getParentFile());
                    }

                    directoryChanged();
                } catch (UnsupportedFlavorException | IOException ex) {
                }
            }
        };

        textField.setDropTarget(dropTarget);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        textField = new javax.swing.JTextField();
        toolBar = new javax.swing.JToolBar();
        rootButton = new javax.swing.JButton();
        parentButton = new javax.swing.JButton();
        historyButton = new javax.swing.JButton();
        bookmarksButton = new javax.swing.JButton();
        buttonPanel = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        textField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(textField, gridBagConstraints);

        toolBar.setBorder(null);
        toolBar.setFloatable(false);
        toolBar.setBorderPainted(false);

        org.openide.awt.Mnemonics.setLocalizedText(rootButton, String.valueOf(IOUtils.DIR_SEPARATOR));
        rootButton.setToolTipText(Dict.ROOT.getString());
        rootButton.setFocusable(false);
        rootButton.setPreferredSize(new java.awt.Dimension(30, 30));
        rootButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rootButtonActionPerformed(evt);
            }
        });
        toolBar.add(rootButton);

        org.openide.awt.Mnemonics.setLocalizedText(parentButton, ".."); // NOI18N
        parentButton.setToolTipText(Dict.UP.getString());
        parentButton.setFocusable(false);
        parentButton.setPreferredSize(new java.awt.Dimension(30, 30));
        parentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parentButtonActionPerformed(evt);
            }
        });
        toolBar.add(parentButton);

        historyButton.setIcon(Pict.Actions.CHRONOMETER.get(16));
        historyButton.setToolTipText(Dict.HISTORY.getString());
        historyButton.setEnabled(false);
        historyButton.setFocusable(false);
        historyButton.setMaximumSize(new java.awt.Dimension(20, 30));
        historyButton.setMinimumSize(new java.awt.Dimension(20, 30));
        historyButton.setPreferredSize(new java.awt.Dimension(30, 30));
        toolBar.add(historyButton);

        bookmarksButton.setIcon(Pict.Places.BOOKMARKS.get(16));
        bookmarksButton.setToolTipText(Dict.BOOKMARKS.getString());
        bookmarksButton.setEnabled(false);
        bookmarksButton.setFocusable(false);
        bookmarksButton.setMaximumSize(new java.awt.Dimension(20, 30));
        bookmarksButton.setMinimumSize(new java.awt.Dimension(20, 30));
        bookmarksButton.setPreferredSize(new java.awt.Dimension(30, 30));
        toolBar.add(bookmarksButton);

        add(toolBar, new java.awt.GridBagConstraints());

        buttonPanel.setLayout(new java.awt.GridLayout(1, 0));
        add(buttonPanel, new java.awt.GridBagConstraints());
    }// </editor-fold>//GEN-END:initComponents

    private void rootButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rootButtonActionPerformed
        setPath(mPath.toPath().getRoot().toFile());
        directoryChanged();
    }//GEN-LAST:event_rootButtonActionPerformed

    private void parentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parentButtonActionPerformed
        try {
            setPath(mPath.getParentFile());
            directoryChanged();
        } catch (NullPointerException e) {
            // nvm
        }
    }//GEN-LAST:event_parentButtonActionPerformed

    private void textFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldActionPerformed
        setPath(new File(textField.getText().trim()));
        directoryChanged();
    }//GEN-LAST:event_textFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bookmarksButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton historyButton;
    private javax.swing.JButton parentButton;
    private javax.swing.JButton rootButton;
    private javax.swing.JTextField textField;
    private javax.swing.JToolBar toolBar;
    // End of variables declaration//GEN-END:variables
}