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

📄 mainframe.java

📁 基于javazip的二进制发表发布版,也就是winzip的java版本
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
              resource.getString("i_warning"), JOptionPane.YES_NO_OPTION);
          if (selection == JOptionPane.YES_OPTION) {
            addedFiles.add(fileName);
          }
        }
        else {
          addedFiles.add(fileName);
        }
      }
      String[] fileNames = new String[addedFiles.size()];
      addedFiles.toArray(fileNames);
      currentZipFile.addFiles(path, fileNames, true);
      loadEntries();
    }
  }

  /**
   * 创建新的压缩文件。
   */
  private void newFile() {
    String fileName = "";
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    chooser.setFileFilter(new ZipFileFilter());
    int r = chooser.showOpenDialog(this);
    if (r == JFileChooser.APPROVE_OPTION) {
      currentDirectory = chooser.getCurrentDirectory();
      fileName = chooser.getSelectedFile().getPath();
      if (!fileName.endsWith(".zip")) {
        fileName = fileName + ".zip";
        logger.info("new file:" + fileName);
      }
      if (new File(fileName).exists()) {
        int selection = JOptionPane.showConfirmDialog(this,
            resource.getString("i_file") + " " + fileName + " " +
            resource.getString("i_exist") + resource.getString("i_overwrite"),
            resource.getString("i_warning"), JOptionPane.YES_NO_OPTION);
        if (selection == JOptionPane.YES_OPTION) {
          File oldFile = new File(fileName);
          oldFile.delete();
          currentZipFile = new JZJFile(fileName);
          removeAllRows();
        }
      }
      else {
        currentZipFile = new JZJFile(fileName);
        removeAllRows();
      }
      enableThem();
    }
  }

  /**
   * 打开已经存在的压缩文件。
   */
  public void open() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(currentDirectory);
    chooser.setFileFilter(new ZipFileFilter());
    int r = chooser.showOpenDialog(MainFrame.this);
    if (r == JFileChooser.APPROVE_OPTION) {
      currentDirectory = chooser.getCurrentDirectory();
      openFile(chooser.getSelectedFile().getPath());
      recentFilesManager.addFile(chooser.getSelectedFile().getPath());
      tree.updateUI();
      table.updateUI();
    }
  }

  /**
   * 将压缩包中的信息加载到表格中并设置状态栏的总计信息部分。
   */
  private void loadEntries() {
    loadEntries(false);
  }

  /**
   * 将压缩包中的信息加载到表格中并设置状态栏的总计信息部分。
   * @param rescan 在已经加载过的情况下是否强制再次进行扫描
   */
  private void loadEntries(boolean rescan) {
    if (rescan) {
      currentZipFile.rescan();
    }
    else {
      currentZipFile.scan();
    }
    removeAllRows();
    tableModel.addRows(currentZipFile.getEntries());
    treeModel.addRecords(currentZipFile.getEntries());
    setTotalStatus(currentZipFile.size(), currentZipFile.getTotalSize());
  }

  /**
   * 删除选中的项。
   * @param entryIndexes 选中项的数组
   */
  private void deleteSelectedEntries(int[] entryIndexes) {
    String[] entryNames = new String[entryIndexes.length];
    for (int i = 0; i < entryIndexes.length; i++) {
      entryNames[i] = table.getEntryName(i);
    }
    currentZipFile.deleteEntries(entryNames);
  }

  /**
   * 设置是否显示工具栏的按钮的文本。
   * @param show true时显示,否则不显示
   */
  public void showButtonText(boolean show) {
    Iterator buttons = toolbarButtons.iterator();
    while (buttons.hasNext()) {
      JDynamicButton button = (JDynamicButton) buttons.next();
      button.showText(false);
    }
  }

  /**
   * 设置是否显示工具栏的按钮的提示信息。
   * @param show true时显示,否则不显示
   */
  public void showButtonToolTipText(boolean show) {
    Iterator buttons = toolbarButtons.iterator();
    while (buttons.hasNext()) {
      JDynamicButton button = (JDynamicButton) buttons.next();
      button.showToolTipText(false);
    }
  }

  /**
   * 创建新压缩文件事件的处理器。
   */
  class NewAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      newFile();
    }
  }

  /**
   * 打开压缩文件事件的处理器。
   */
  class OpenAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      open();
      /*if(currentZipFile.getFullName()!=null) {
       //currentFilenameLabel.setText("Current filename: "+currentZipFile.getFullName());
       //totalFilesLabel.setText("Total compressed files: "+getZipedCount(currentZipFile.getFullName()));
       }*/
    }
  }

  /**
   *关闭压缩文件事件的处理器。
   */
  class CloseAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      currentZipFile = null;
      removeAllRows();
      disableThem();
      tree.updateUI();
      table.updateUI();
    }
  }

  /**
   * 退出事件的处理器。
   */
  class ExitAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }

  /**
   * 向压缩包中添加新文件的事件的处理器。
   */
  class AddAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (currentZipFile.getFullName() != null) {
        addFiles();
      }
    }
  }

  /**
   * 从压缩包中删除文件的事件的处理器。
   */
  class DeleteAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int[] rows = table.getSelectedRows();
      deleteSelectedEntries(rows);
      loadEntries();
    }
  }

  /**
   * 从压缩包中删除文件的事件的处理器。
   */
  class ExtractAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (currentZipFile == null) {
        return;
      }
      if (extractDialog == null) {
        extractDialog = new ExtractDialog(MainFrame.this);
      }
      Toolkit tk1 = Toolkit.getDefaultToolkit();
      extractDialog.setLocation(new Point( (tk1.getScreenSize().width / 2) -
                                          160,
                                          (tk1.getScreenSize().height / 2) -
                                          125));
      extractDialog.show();
    }
  }

  /**
   * 查看指定文件的事件的处理器。
   */
  class ViewAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (viewFrame == null) {
        viewFrame = new ViewFrame();
      }
      int[] rows = table.getSelectedRows();
      if (rows[0] == -1) {
        return;
      }
      String[] entryNames = new String[rows.length];
      String[] fileNames = new String[rows.length];
      for (int i = 0; i < rows.length; i++) {
        entryNames[i] = table.getEntryName(rows[i]);
        fileNames[i] = tmpDirectory + entryNames[i];
      }
      currentZipFile.extractEntriesToFiles(entryNames, fileNames, false);
      viewFrame.viewText(fileNames);
    }
  }

  /**
   * 更换界面风格的事件的处理器。
   */
  class LookFeelAction
      implements ActionListener {
    final String lookfeelClassName;
    public LookFeelAction(String name) {
      lookfeelClassName = name;
    }

    public void actionPerformed(ActionEvent e) {
      try {
        // 设置新的外观
        UIManager.setLookAndFeel(lookfeelClassName);
        // 告诉每个组件更新其外观
        SwingUtilities.updateComponentTreeUI(MainFrame.this);
        MainFrame.this.repaint();
        if (extractDialog != null) {
          SwingUtilities.updateComponentTreeUI(extractDialog);
          extractDialog.repaint();
        }
        if (viewFrame != null) {
          SwingUtilities.updateComponentTreeUI(viewFrame);
          viewFrame.repaint();
        }
        if (aboutDialog != null) {
          SwingUtilities.updateComponentTreeUI(aboutDialog);
          aboutDialog.repaint();
        }
        if (optionDialog != null) {
          SwingUtilities.updateComponentTreeUI(optionDialog);
          optionDialog.repaint();
        }
      }
      catch (Exception ex) {
        System.err.println(ex);
      }
    }
  }

  /**
   * 选项事件处理器。
   */
  class OptionAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (optionDialog == null) {
        optionDialog = new OptionDialog(MainFrame.this);
      }
      SwingUtil.centerWindow(optionDialog);
      optionDialog.setVisible(true);
    }
  }

  /**
   * 关于对话框事件处理器。
   */
  class AboutAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (aboutDialog == null) {
        aboutDialog = new AboutDialog(MainFrame.this);
      }
      SwingUtil.centerWindow(aboutDialog);
      aboutDialog.setVisible(true);
    }
  }

  /**
   * 文件导出对话框。
   * <p>根据用户的选择完成选定文件或者全部文件的导出工作。
   */
  class ExtractDialog
      extends JDialog
      implements ActionListener {

    String title;
    final static int SELECTED_FILE = 0;
    final static int ALL_FILE = 1;
    final static int USER_FILE = 2;
    int extractMode = SELECTED_FILE;
    JButton okButton;
    JButton cancelButton;
    JButton browseButton = new JButton("...");
    JLabel extractToLabel = new JLabel(resource.getString("e_extract"));
    CheckboxGroup checkBoxGroup = new CheckboxGroup();
    JRadioButton selectedRadioButton = new JRadioButton(resource.
        getString("e_selected_files"), true);
    JRadioButton allRadioButton = new JRadioButton(resource.getString(
        "e_all_files"), false);
    JRadioButton fileRadioButton = new JRadioButton(resource.
        getString("e_files"), false);
    ButtonGroup radioButtonGroup = new ButtonGroup();
    JPanel radioButtonPanel = new JPanel();

    JCheckBox overwriteCheckBox = new JCheckBox(resource.getString(
        "e_overwrite"));
    JTextField pathTextField = new JTextField(30);
    JTextField fileTextField = new JTextField(20);
    boolean canOverWrite = false;
    /**
     * 将全部组件添加到面板中。
     * @param c 主面板的根日期对象
     */
    private void addComponent(Container c) {
      c.add(okButton);
      c.add(cancelButton);
      c.add(browseButton);
      c.add(extractToLabel);
      c.add(pathTextField);
      c.add(radioButtonPanel);
      radioButtonPanel.setLayout(new GridLayout(4, 1));
      radioButtonPanel.add(selectedRadioButton);
      radioButtonPanel.add(allRadioButton);
      radioButtonPanel.add(fileRadioButton);
      radioButtonPanel.add(fileTextField);
      radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Title"));
      radioButtonGroup.add(selectedRadioButton);
      radioButtonGroup.add(allRadioButton);
      radioButtonGroup.add(fileRadioButton);
      c.add(overwriteCheckBox);
    }

    /**
     * 构造方法,完成组件的加载工作。
     * @param parentFrame 对话框依赖的父框架
     */
    public ExtractDialog(MainFrame parentFrame) {
      super(parentFrame, true);
      title = resource.getString("e_title");
      setTitle(title);
      ButtonProperty buttonProperty;
      buttonProperty = (ButtonProperty) resource.getObject("e_b_ok");
      okButton = new JButton(buttonProperty.text);
      okButton.setToolTipText(buttonProperty.tooltip);
      buttonProperty = (ButtonProperty) resource.getObject(
          "e_b_cancel");
      cancelButton = new JButton(buttonProperty.text);
      cancelButton.setToolTipText(buttonProperty.tooltip);
      Container content = getContentPane();
      addComponent(content);
      GridBagLayout gdb = new GridBagLayout();
      content.setLayout(gdb);
      GridBagConstraints c = new GridBagConstraints();
      fileTextField.setEnabled(false);
      c.gridx = 0;
      c.gridy = 0;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.weightx = 0;
      c.weighty = 0;
      c.gridwidth = 1;
      c.gridheight = 1;
      c.fill = GridBagConstraints.BOTH;
      gdb.setConstraints(extractToLabel, c);

      c.gridx = 0;
      c.gridy = 1;
      gdb.setConstraints(pathTextField, c);

      c.gridx = 0;
      c.gridy = 2;
      c.gridwidth = 1;
      c.gridheight = 5;
      gdb.setConstraints(radioButtonPanel, c);

      c.gridx = 0;
      c.gridy = 7;
      c.gridwidth = 1;
      c.gridheight = 1;
      gdb.setConstraints(overwriteCheckBox, c);

      c.gridx = 1;
      c.gridy = 1;

⌨️ 快捷键说明

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