⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jrargui.java

📁 效率较高的文件压缩算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2003-2004 Andrew Smith
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.InputStreamReader;
import java.net.URL;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class JrarGUI extends JFrame implements ActionListener, ItemListener {

    Jrar jrar;

    JFrame f;

    //JButtons used by the main window
    // viewArchiveButton,
    JButton addToArchiveButton, extractToButton, testArchiveButton,
            deleteFileButton, protectButton, sfxButton, openArchiveButton;

    private JPanel buttonPanel, statusBar, tablePanel;

    // Other random JButtons
    private JButton cancelOperation;
    
    private static final Object[][] emptyData = {};

    private String currentFile;

    private String oldFile;

    private String currentDir;

    private int currentState = 0;

    private JTable infoTable;

    private JLabel jl;

    private FileTableModel ftm;

    private char overwriteAnswer;

    private JDialog overwriteDialog;

    // JButtons used by the JDialog box to confirm overwritting responce
    private JButton quitOverwrite, noOverwrite, yesOverwrite, neverOverwrite,
            allOverwrite;

    //  private List runningThreads;
    private JScrollPane scrollPane;

    private AddingFilePanel afp;

    public JrarGUI(Jrar j) {
        currentFile = null;
        oldFile = null;
        currentDir = null;
        jrar = j;
        gui();
    }

    public JrarGUI(Jrar j, String s1, String s2) {
        jrar = j;
        oldFile = s1;
        currentFile = s1;
        currentDir = s2;
        gui();
    }

    private void gui() {
        f = new JFrame();
        jrar.addToDebugWindow("Creating main frame");
        f.setTitle(JrarConstants.PROGRAMTITLE);
        jrar.addToDebugWindow("Setting default close operation");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        currentState = 0;
        jrar.addToDebugWindow("Calling Jrar.createAndPlaceButtons()");
        createAndPlaceButtons();
        jrar.addToDebugWindow("Calling Jrar.createOutputArea()");
        createOutputArea();
        jrar.addToDebugWindow("Calling Jrar.createStatusBar()");
        createStatusBar();
        jrar
                .addToDebugWindow("Adding rigid area to east and west of main frame");
        f.getContentPane()
                .add("East", Box.createRigidArea(new Dimension(8, 0)));
        f.getContentPane()
                .add("West", Box.createRigidArea(new Dimension(8, 0)));
        jrar.addToDebugWindow("setting main frame resizable to "
                + JrarConstants.MAINFRAMEISRESIZABLE);
        f.setResizable(JrarConstants.MAINFRAMEISRESIZABLE);
        jrar.addToDebugWindow("Setting default size of main frame to width="
                + JrarConstants.MAINFRAMESIZE.getWidth() + " height="
                + JrarConstants.MAINFRAMESIZE.getHeight());
        f.setSize(JrarConstants.MAINFRAMESIZE);
        jrar.addToDebugWindow("Packing main frame");
        f.pack();
        jrar.addToDebugWindow("Centering the main frame");
        f.setLocation((int) ((Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth()) - f.getSize().getWidth()) / 2, (int) ((Toolkit
                .getDefaultToolkit().getScreenSize().getHeight()) - f.getSize()
                .getHeight()) / 2);
        jrar.addToDebugWindow("Setting main frame visiable to "
                + JrarConstants.MAINFRAMEISVISIABLE);
        f.setVisible(JrarConstants.MAINFRAMEISVISIABLE);
        
        if (currentFile != null) {
            getFileList();
        }
        
    }

    private void createAndPlaceButtons() {
        URL imageURL;
        
        jrar.addToDebugWindow("Loading in all the images or the buttons");
        imageURL = Jrar.class.getResource("images/add.png");
        ImageIcon addFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/delete.png");
        ImageIcon deleteFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/extract.png");
        ImageIcon extractFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/open.png");
        ImageIcon openFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/test.png");
        ImageIcon testFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/protect.png");
        ImageIcon protectFileIcon = new ImageIcon(imageURL);
        imageURL = Jrar.class.getResource("images/sfx.png");
        ImageIcon sfxIcon = new ImageIcon(imageURL);

        
        jrar.addToDebugWindow("Constructing button panel");
        buttonPanel = new JPanel();
        
        
        jrar.addToDebugWindow("Setting dimension of buttons to width,height="
                + JrarConstants.BUTTONSQUARESIZE);
        Dimension d = new Dimension(JrarConstants.BUTTONSQUARESIZE,
                JrarConstants.BUTTONSQUARESIZE);
        
        
        jrar.addToDebugWindow("Creating openArchiveButton");
        openArchiveButton = new JButton(openFileIcon);
        openArchiveButton.addActionListener(this);
        openArchiveButton.setPreferredSize(d);
        
        openArchiveButton
                .setToolTipText("Open and archive to view it's contents");
        
        
        jrar.addToDebugWindow("Creating addToArchiveButton");
        addToArchiveButton = new JButton(addFileIcon);
        addToArchiveButton.addActionListener(this);
        addToArchiveButton.setPreferredSize(d);
        addToArchiveButton
                .setToolTipText("Add files to currently open archive");
        
        
        jrar.addToDebugWindow("Creating extractTo Button");
        extractToButton = new JButton(extractFileIcon);
        extractToButton.addActionListener(this);
        extractToButton.setPreferredSize(d);
        extractToButton.setToolTipText("Extract selected files");
        
        
        jrar.addToDebugWindow("Creating testArchive Button");
        testArchiveButton = new JButton(testFileIcon);
        testArchiveButton.addActionListener(this);
        testArchiveButton.setPreferredSize(d);
        testArchiveButton.setToolTipText("Test this archives integeraty");
        testArchiveButton.setEnabled(false);
        
        //debugAddition("Creating viewArchive Button");
        //viewArchiveButton = new JButton("View");
        //viewArchiveButton.addActionListener(this);
        //viewArchiveButton.setPreferredSize(d);
        //viewArchiveButton.setToolTipText("View selected file");
        
        
        jrar.addToDebugWindow("Creating deleteFile Button");
        deleteFileButton = new JButton(deleteFileIcon);
        deleteFileButton.addActionListener(this);
        deleteFileButton.setPreferredSize(d);
        deleteFileButton
                .setToolTipText("Delete selected file from the archive");
        deleteFileButton.setEnabled(false);
        
        
        jrar.addToDebugWindow("Creating protect Button");
        protectButton = new JButton(protectFileIcon);
        protectButton.addActionListener(this);
        protectButton.setPreferredSize(d);
        protectButton.setToolTipText("Protect the archive from modifications");
        protectButton.setEnabled(false);
        
        
        jrar.addToDebugWindow("Creating sfx Button");
        sfxButton = new JButton(sfxIcon);
        sfxButton.addActionListener(this);
        sfxButton.setPreferredSize(d);
        sfxButton.setToolTipText("Create an auto-extracting archive");
        sfxButton.setEnabled(false);
        
        jrar.addToDebugWindow("Adding buttons to button panel");
        buttonPanel.add(openArchiveButton);
        buttonPanel.add(addToArchiveButton);
        buttonPanel.add(extractToButton);
        buttonPanel.add(testArchiveButton);
        //buttonPanel.add(viewArchiveButton);
        buttonPanel.add(deleteFileButton);
        buttonPanel.add(protectButton);
        buttonPanel.add(sfxButton);
        
        
        if (jrar.isDebugOn()) {
            jrar.addToDebugWindow("Creating Debug button");
            imageURL = Jrar.class.getResource("images/debug.png");
            ImageIcon debugIcon = new ImageIcon(imageURL);
            JButton showHideDebugFrame = new JButton(debugIcon);
            //        "<html><center>Show/Hide<br>Debug Window</center></html>");
            showHideDebugFrame.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    jrar.switchDWVisibilaty();
                }
            });
            showHideDebugFrame.setPreferredSize(new Dimension(120,
                    JrarConstants.BUTTONSQUARESIZE));
            buttonPanel.add(showHideDebugFrame);
            jrar
                    .addToDebugWindow("Adding button panel to the north of the main frame");
        }
        f.getContentPane().add("North", buttonPanel);
    }

    private void createOutputArea() {
        tablePanel = new JPanel(new CardLayout());
        jrar.addToDebugWindow("Creating a new FileTableModel");
        ftm = new FileTableModel();
        jrar.addToDebugWindow("Setting up a table with the created"
                + "FileTableModel in the constructor");
        infoTable = new JTable(ftm);
        jrar
                .addToDebugWindow("Disabling the auto resize of the tables columns");
        // disables auto sizing of the columns
        infoTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jrar.addToDebugWindow("Creating an array to store the tables columns");
        TableColumn columns[] = new TableColumn[infoTable.getColumnCount()];
        jrar
                .addToDebugWindow("Creating an array to store the tables columns renderers");
        DefaultTableCellRenderer[] columnRenderers = new DefaultTableCellRenderer[infoTable
                .getColumnCount()];
        jrar
                .addToDebugWindow("Setting up each column and column renderer for the table");
        for (int i = 0; i < infoTable.getColumnCount(); i++) {
            columns[i] = infoTable.getColumn(infoTable.getColumnName(i));
            columnRenderers[i] = new DefaultTableCellRenderer();
        }
        jrar.addToDebugWindow("Configuring the name column");
        // Name column
        columnRenderers[0].setHorizontalAlignment(SwingConstants.LEFT);
        columns[0].setCellRenderer(columnRenderers[0]);
        columns[0].setPreferredWidth(196);
        jrar.addToDebugWindow("Configuring the original size column");
        // original size
        columnRenderers[1].setHorizontalAlignment(SwingConstants.RIGHT);
        columns[1].setCellRenderer(columnRenderers[1]);
        columns[1].setPreferredWidth(80);
        jrar.addToDebugWindow("Configuring the packed size column");
        // packed size
        columnRenderers[2].setHorizontalAlignment(SwingConstants.RIGHT);
        columns[2].setCellRenderer(columnRenderers[2]);
        columns[2].setPreferredWidth(80);
        jrar.addToDebugWindow("Configuring the ratio column");
        // ratio
        columnRenderers[3].setHorizontalAlignment(SwingConstants.RIGHT);
        columns[3].setCellRenderer(columnRenderers[3]);
        columns[3].setPreferredWidth(50);
        jrar.addToDebugWindow("Configuring the data column");
        // data
        columnRenderers[4].setHorizontalAlignment(SwingConstants.CENTER);
        columns[4].setCellRenderer(columnRenderers[4]);
        columns[4].setPreferredWidth(60);
        jrar.addToDebugWindow("Configuring the time column");
        // time
        columnRenderers[5].setHorizontalAlignment(SwingConstants.CENTER);
        columns[5].setCellRenderer(columnRenderers[5]);
        columns[5].setPreferredWidth(50);
        jrar.addToDebugWindow("Configuring the attributes column");
        // attributes
        columnRenderers[6].setHorizontalAlignment(SwingConstants.CENTER);
        columns[6].setCellRenderer(columnRenderers[6]);
        columns[6].setPreferredWidth(60);
        jrar.addToDebugWindow("Configuring the crc32 column");
        // crc32
        columnRenderers[7].setHorizontalAlignment(SwingConstants.CENTER);
        columns[7].setCellRenderer(columnRenderers[7]);
        columns[7].setPreferredWidth(70);
        jrar.addToDebugWindow("Creating a scrollpane with the table"
                + "passed into the constructor");
        scrollPane = new JScrollPane(infoTable);
        scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
        scrollPane.setPreferredSize(new Dimension(600, 500));
        scrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jrar
                .addToDebugWindow("Adding the scrollpane to the center of the main frame");
        tablePanel.add(scrollPane, "File List");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -