plaintextexportdialog.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 1,075 行 · 第 1/3 页

JAVA
1,075
字号
    init();
  }

  /**
   * Initialise the dialog.
   */
  private void init()
  {
    setModal(true);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    setTitle(getResources().getString("plain-text-exportdialog.dialogtitle"));

    plainTextCommandSet = new PrinterCommandSet(new NullOutputStream(),
        new PageFormat(), 10, 6);
    plainTextEncodingModel = createEncodingModel(plainTextCommandSet);

    epsonPrinterCommandSet = new EpsonPrinterCommandSet(new NullOutputStream(),
        new PageFormat(), 10, 6);
    epsonPrinterEncodingModel = createEncodingModel(epsonPrinterCommandSet);

    ibmPrinterCommandSet = new IBMPrinterCommandSet(new NullOutputStream(),
        new PageFormat(), 10, 6);
    ibmPrinterEncodingModel = createEncodingModel(ibmPrinterCommandSet);

    selectedEncodingModel = plainTextEncodingModel;
    cbEncoding = new JComboBox(selectedEncodingModel);

    final Integer[] lpiModel = {
      LPI_6,
      LPI_10
    };

    final Integer[] cpiModel = {
      CPI_10,
      CPI_12,
      CPI_15,
      CPI_17,
      CPI_20
    };

    cbLinesPerInch = new JComboBox(new DefaultComboBoxModel(lpiModel));
    cbCharsPerInch = new JComboBox(new DefaultComboBoxModel(cpiModel));

    rbPlainPrinterCommandSet = new ActionRadioButton(new ActionSelectPlainPrinter());
    rbEpsonPrinterCommandSet = new ActionRadioButton(new ActionSelectEpsonPrinter());
    rbIBMPrinterCommandSet = new ActionRadioButton(new ActionSelectIBMPrinter());

    txFilename = new JTextField();

    final ButtonGroup bg = new ButtonGroup();
    bg.add(rbPlainPrinterCommandSet);
    bg.add(rbEpsonPrinterCommandSet);
    bg.add(rbIBMPrinterCommandSet);

    final JComponent contentPane = createContentPane();
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 1;
    gbc.gridx = 0;
    gbc.gridwidth = 4;
    gbc.gridy = 8;
    gbc.insets = new Insets(10, 0, 0, 0);
    contentPane.add(createButtonPanel(), gbc);
    setContentPane(contentPane);

    clear();

    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(final WindowEvent e)
      {
        new ActionCancel().actionPerformed(null);
      }
    }
    );
  }

  /**
   * Creates the content pane for the export dialog.
   *
   * @return the created content pane.
   */
  private JComponent createContentPane()
  {
    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());
    contentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

    final JLabel lblPrinterSelect = new JLabel(
        getResources().getString("plain-text-exportdialog.printer"));
    final JLabel lblFileName
        = new JLabel(getResources().getString("plain-text-exportdialog.filename"));
    final JLabel lblEncoding
        = new JLabel(getResources().getString("plain-text-exportdialog.encoding"));
    final JButton btnSelect = new ActionButton(new ActionSelectFile());

    final JLabel lblCharsPerInch = new JLabel(
        getResources().getString("plain-text-exportdialog.chars-per-inch"));
    final JLabel lblLinesPerInch = new JLabel(
        getResources().getString("plain-text-exportdialog.lines-per-inch"));
    final JLabel lblFontSettings = new JLabel(
        getResources().getString("plain-text-exportdialog.font-settings"));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(3, 1, 1, 1);
    contentPane.add(lblFileName, gbc);

    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(lblPrinterSelect, gbc);

    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(lblEncoding, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.ipadx = 120;
    gbc.gridwidth = 2;
    gbc.insets = new Insets(3, 1, 1, 1);
    contentPane.add(txFilename, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(rbPlainPrinterCommandSet, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(rbEpsonPrinterCommandSet, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 2;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(rbIBMPrinterCommandSet, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.gridwidth = 2;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(cbEncoding, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0;
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(lblFontSettings, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(lblCharsPerInch, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 2;
    gbc.gridy = 6;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(lblLinesPerInch, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 5;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(cbCharsPerInch, gbc);

    gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.gridx = 1;
    gbc.gridy = 6;
    gbc.insets = new Insets(1, 1, 1, 1);
    contentPane.add(cbLinesPerInch, gbc);

    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 3;
    gbc.gridy = 0;
    gbc.gridheight = 2;
    contentPane.add(btnSelect, gbc);

    return contentPane;
  }

  /**
   * Creates the button panel for the export dialog.
   *
   * @return the created button panel
   */
  private JPanel createButtonPanel()
  {
    // button panel
    final JButton btnCancel = new ActionButton(new ActionCancel());
    final JButton btnConfirm = new ActionButton(new ActionConfirm());
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout());
    buttonPanel.add(btnConfirm);
    buttonPanel.add(btnCancel);
    btnConfirm.setDefaultCapable(true);
    buttonPanel.registerKeyboardAction(new ActionConfirm(),
        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    return buttonPanel;
  }

  /**
   * Sets the selected printer.
   *
   * @param type  the type.
   */
  public void setSelectedPrinter(final int type)
  {
    if (type == TYPE_EPSON_OUTPUT)
    {
      rbEpsonPrinterCommandSet.setSelected(true);
      selectedEncodingModel = epsonPrinterEncodingModel;
      cbEncoding.setModel(selectedEncodingModel);
      // transfer the selected encoding ...
      setEncoding(getEncoding());
    }
    else if (type == TYPE_IBM_OUTPUT)
    {
      rbIBMPrinterCommandSet.setSelected(true);
      selectedEncodingModel = ibmPrinterEncodingModel;
      cbEncoding.setModel(selectedEncodingModel);
      // transfer the selected encoding ...
      setEncoding(getEncoding());
    }
    else if (type == TYPE_PLAIN_OUTPUT)
    {
      rbPlainPrinterCommandSet.setSelected(true);
      selectedEncodingModel = plainTextEncodingModel;
      cbEncoding.setModel(selectedEncodingModel);
      // transfer the selected encoding ...
      setEncoding(getEncoding());
    }
    else
    {
      throw new IllegalArgumentException();
    }
  }

  /**
   * Returns the selected printer.
   *
   * @return The printer type.
   */
  public int getSelectedPrinter()
  {
    if (rbPlainPrinterCommandSet.isSelected())
    {
      return TYPE_PLAIN_OUTPUT;
    }
    if (rbEpsonPrinterCommandSet.isSelected())
    {
      return TYPE_EPSON_OUTPUT;
    }
    return TYPE_IBM_OUTPUT;
  }

  /**
   * Returns the filename.
   *
   * @return the name of the file where to save the file.
   */
  public String getFilename()
  {
    return txFilename.getText();
  }

  /**
   * Defines the filename of the file.
   *
   * @param filename the filename of the file
   */
  public void setFilename(final String filename)
  {
    this.txFilename.setText(filename);
  }

  /**
   * Gets the confirmation state of the dialog. A confirmed dialog has no invalid
   * settings and the user confirmed any resource conflicts.
   *
   * @return true, if the dialog has been confirmed and the excel file should be saved,
   * false otherwise.
   */
  public boolean isConfirmed()
  {
    return confirmed;
  }

  /**
   * Defines whether this dialog has been finished using the 'OK' or the 'Cancel' option.
   *
   * @param confirmed set to true, if OK was pressed, false otherwise
   */
  protected void setConfirmed(final boolean confirmed)
  {
    this.confirmed = confirmed;
  }

  /**
   * clears all selections, input fields and set the selected encryption level to none.
   */
  public void clear()
  {
    txFilename.setText("");
    rbPlainPrinterCommandSet.setSelected(true);
    selectedEncodingModel = plainTextEncodingModel;
    int idx = selectedEncodingModel.indexOf(System.getProperty("file.encoding", "Cp1251"));
    if (idx == -1 && selectedEncodingModel.getSize() > 0)
    {
      idx = 0;
    }
    cbEncoding.setSelectedIndex(idx);

    cbCharsPerInch.setSelectedItem(CPI_10);
    cbLinesPerInch.setSelectedItem(LPI_6);
  }

  /**

⌨️ 快捷键说明

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