com.archivas.clienttools.arcmover.gui.properties.FilePropertiesDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.archivas.clienttools.arcmover.gui.properties.FilePropertiesDialog.java

Source

// Copyright 2007 Hitachi Data Systems
// All Rights Reserved.
//
// 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 com.archivas.clienttools.arcmover.gui.properties;

import com.archivas.clienttools.arcmover.gui.HCPDataMigrator;
import com.archivas.clienttools.arcutils.model.ArcMoverFile;
import com.archivas.clienttools.arcutils.utils.StringUtils;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Date;

public class FilePropertiesDialog extends JDialog {
    private JPanel contentPane;
    private JButton closeButton;
    private JTable propertyTable;

    public FilePropertiesDialog() {
        super(HCPDataMigrator.getInstance());
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(closeButton);

        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onClose();
            }
        });

        // call onClose() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onClose();
            }
        });

        // call onClose() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onClose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        this.setMinimumSize(new Dimension(450, 200));
    }

    private void onClose() {
        dispose();
    }

    public void populate(final ArcMoverFile file) {
        TableModel dataModel = new AbstractTableModel() {
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            Date modifiedTime = file.getMetadata().getModTime();
            String modifiedTimeStr = (modifiedTime == null ? null : modifiedTime.toString());

            final String[] columnNameArray;
            {
                if (file.isDirectory()) {
                    columnNameArray = new String[] { "Name", "Path", "Type", "Modified Time" };
                } else if (file.isSymlink()) {
                    columnNameArray = new String[] { "Name", "Path", "Type", "Symlink Target", "Modified Time" };
                } else {
                    columnNameArray = new String[] { "Name", "Path", "Type", "Size", "Modified Time" };
                }
            }
            final String[] valueArray;
            {
                if (file.isDirectory()) {
                    valueArray = new String[] { file.getFileName(), file.getDisplayPath(), "Directory",
                            modifiedTimeStr };
                } else if (file.isSymlink()) {
                    valueArray = new String[] { file.getFileName(), file.getDisplayPath(),
                            file.getFileType().getDisplay(), file.getDisplaySymlinkTarget(), modifiedTimeStr };
                } else {
                    valueArray = new String[] { file.getFileName(), file.getDisplayPath(), "File",
                            StringUtils.bytesToStringWithUnit(file.getSize(), decimalFormat), modifiedTimeStr };
                }
            }

            public int getColumnCount() {
                return 2;
            }

            public int getRowCount() {
                if (file.isDirectory()) {
                    return 4;
                } else {
                    return 5;
                }
            }

            public String getColumnName(int columnIndex) {
                return columnIndex == 0 ? "Property" : "Value";
            }

            public Object getValueAt(int row, int col) {
                return col == 0 ? columnNameArray[row] : valueArray[row];
            }
        };

        propertyTable.setModel(dataModel);
    }

    public static void main(String[] args) {
        FilePropertiesDialog dialog = new FilePropertiesDialog();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR
     * call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new FormLayout(
                "fill:max(d;4dlu):noGrow,left:150dlu:grow,fill:d:noGrow,fill:50dlu:noGrow,fill:max(d;4dlu):noGrow,fill:d:noGrow",
                "top:110px:grow,top:m:noGrow,top:4dlu:noGrow,top:15dlu:noGrow,top:4dlu:noGrow"));
        contentPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:max(d;4px):noGrow", "center:d:noGrow"));
        CellConstraints cc = new CellConstraints();
        contentPane.add(panel1, cc.xyw(2, 2, 5));
        final JScrollPane scrollPane1 = new JScrollPane();
        contentPane.add(scrollPane1, cc.xyw(2, 1, 3, CellConstraints.FILL, CellConstraints.FILL));
        propertyTable = new JTable();
        propertyTable.setCellSelectionEnabled(true);
        propertyTable.setFillsViewportHeight(true);
        propertyTable.setRowSelectionAllowed(true);
        scrollPane1.setViewportView(propertyTable);
        closeButton = new JButton();
        closeButton.setText("Close");
        contentPane.add(closeButton, cc.xy(4, 4, CellConstraints.FILL, CellConstraints.FILL));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}