configdescriptioneditor.java

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

JAVA
1,342
字号
   * editor.
   */
  private class AddEnumEntryAction extends AbstractActionDowngrade
  {
    /**
     * Defaultconstructor.
     */
    public AddEnumEntryAction()
    {
      putValue(NAME, getResources().getString("action.add-enum-entry.name"));
    }

    /**
     * Handles the request to add a new enumeration entry to the detail
     * editor.
     * 
     * @param e not used.
     */
    public void actionPerformed(final ActionEvent e)
    {
      getEnumEntryListModel().addElement(getEnumEntryEditField().getText());
    }
  }

  /**
   * Handles the request to remove an enumeration entry to the detail
   * editor.
   */
  private class RemoveEnumEntryAction extends AbstractActionDowngrade
  {
    /**
     * Defaultconstructor.
     */
    public RemoveEnumEntryAction()
    {
      putValue(NAME, getResources().getString("action.remove-enum-entry.name"));
    }

    /**
     * Handles the request to remove an enumeration entry to the detail
     * editor.
     * 
     * @param e not used.
     */
    public void actionPerformed(final ActionEvent e)
    {
      final JList enumEntryList = getEnumEntryList();
      final DefaultListModel enumEntryListModel = getEnumEntryListModel();
      final int[] selectedEntries = enumEntryList.getSelectedIndices();
      for (int i = selectedEntries.length - 1; i >= 0; i--)
      {
        enumEntryListModel.remove(selectedEntries[i]);
      }
      enumEntryList.clearSelection();
    }
  }

  /**
   * Handles the request to update an enumeration entry to the detail
   * editor.
   */
  private class UpdateEnumEntryAction extends AbstractActionDowngrade
  {
    /**
     * Defaultconstructor.
     */
    public UpdateEnumEntryAction()
    {
      putValue(NAME, getResources().getString("action.update-enum-entry.name"));
    }

    /**
     * Handles the request to update an enumeration entry to the detail
     * editor.
     * 
     * @param e not used.
     */
    public void actionPerformed(final ActionEvent e)
    {
      final int idx = getEnumEntryList().getSelectedIndex();
      if (idx == -1)
      {
        getEnumEntryListModel().addElement(getEnumEntryEditField().getText());
      }
      else
      {
        getEnumEntryListModel().setElementAt(getEnumEntryEditField().getText(), idx);
      }
    }
  }

  /** An internal value to mark a text detail editor type. */
  private static final int TYPE_TEXT = 0;
  /** An internal value to mark a class detail editor type. */
  private static final int TYPE_CLASS = 1;
  /** An internal value to mark a enumeration detail editor type. */
  private static final int TYPE_ENUM = 2;
  /** 
   * Contains the name of the resource bundle to be used to translate the 
   * dialogs.
   */
  private static final String RESOURCE_BUNDLE =
      ConfigResources.class.getName();

  /** A radio button to select the text editor type for the current key. */ 
  private ActionRadioButton rbText;
  /** A radio button to select the class editor type for the current key. */ 
  private ActionRadioButton rbClass;
  /** A radio button to select the enumeration editor type for the current key. */ 
  private ActionRadioButton rbEnum;
  /** The list model used to collect and manage all available keys. */
  private ConfigDescriptionModel model;
  /** The name of the currently edited key. */
  private JTextField keyNameField;
  /** The description field contains a short description of the current key. */
  private JTextArea descriptionField;
  /** Allows to check, whether the key is a global (boot-time) key. */ 
  private JCheckBox globalField;
  /** Allows to check, whether the key is hidden. */
  private JCheckBox hiddenField;
  /** The name of the base class for the class detail editor. */
  private JTextField baseClassField;
  /** contains a message after validating the given base class. */ 
  private JLabel baseClassValidateMessage;
  /** contains the currently selected entry of the enumeration detail editor. */
  private JTextField enumEntryEditField;
  /** contains all entries of the enumeration detail editor. */
  private DefaultListModel enumEntryListModel;
  /** The current resource bundle used to translate the strings in this dialog. */
  private final ResourceBundle resources;
  /** This cardlayout is used to display the currently selected detail editor. */
  private CardLayout detailManager;
  /** Contains the detail editor manager. */
  private JPanel detailManagerPanel;
  /** Contains the detail editor for the key. */
  private final JPanel detailEditorPane;
  /** The list is used to manage all available keys. */
  private JList entryList;
  /** This list is used to manage the available entries of the enumeration detail editor. */
  private JList enumEntryList;
  /**the currently selected description entry. */
  private ConfigDescriptionEntry selectedEntry;
  /** The file chooser is used to select the file for the load/save operations. */
  private final JFileChooser fileChooser;
  /** Serves as statusline for the dialog. */
  private JLabel statusHolder;
  /** The currently selected detail editor type. */
  private int type;


  /**
   * Constructs a ConfigDescriptionEditor that is initially invisible.
   */
  public ConfigDescriptionEditor()
  {
    this.resources = ResourceBundle.getBundle(RESOURCE_BUNDLE);

    setTitle(resources.getString("config-description-editor.title"));
    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());

    detailEditorPane = createEditPane();
    final JSplitPane splitPane = new JSplitPane
        (JSplitPane.HORIZONTAL_SPLIT, createEntryList(), detailEditorPane);

    contentPane.add(splitPane, BorderLayout.CENTER);
    contentPane.add(createButtonPane(), BorderLayout.SOUTH);

    final JPanel cPaneStatus = new JPanel();
    cPaneStatus.setLayout(new BorderLayout());
    cPaneStatus.add (contentPane, BorderLayout.CENTER);
    cPaneStatus.add (createStatusBar(), BorderLayout.SOUTH);

    setContentPane(cPaneStatus);
    setEntryType(TYPE_TEXT);
    setSelectedEntry(null);

    fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(
        new ExtensionFileFilter
            (resources.getString("config-description-editor.xml-files"), ".xml"));
    fileChooser.setMultiSelectionEnabled(false);

    setStatusText(resources.getString("config-description-editor.welcome"));

    addWindowListener(new WindowAdapter()
    {
      /**
       * Invoked when a window is in the process of being closed.
       * The close operation can be overridden at this point.
       */
      public void windowClosing(final WindowEvent e)
      {
        attempExit();
      }
    });
  }

  /**
   * Creates and returns the entry list component that will hold all
   * config description entries within a list.
   * 
   * @return the created entry list.
   */
  private JPanel createEntryList ()
  {
    final Action addEntryAction = new AddEntryAction();
    final Action removeEntryAction = new RemoveEntryAction();

    model = new ConfigDescriptionModel();
    entryList = new JList(model);
    entryList.addListSelectionListener(new ConfigListSelectionListener());

    final JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);
    toolbar.add(addEntryAction);
    toolbar.add(removeEntryAction);

    final JPanel panel = new JPanel();
    panel.setMinimumSize(new Dimension(200,0));
    panel.setLayout(new BorderLayout());
    panel.add(toolbar, BorderLayout.NORTH);
    panel.add(new JScrollPane
        (entryList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
    return panel;
  }

  /**
   * Returns the JList component containing all entries of the enumeration
   * detail editor.
   * @return the enumeration entry list.
   */
  protected JList getEnumEntryList()
  {
    return enumEntryList;
  }
  
  /**
   * Returns the text field containing the currently edited enumeration entry.
   * @return the textfield containing the current entry.
   */
  protected JTextField getEnumEntryEditField ()
  {
    return enumEntryEditField;
  }
  
  /**
   * Returns the List Model containing all entries of the current enumeration
   * entry editor.
   * @return the entry list.
   */
  protected DefaultListModel getEnumEntryListModel()
  {
    return enumEntryListModel;
  }
  
  /**
   * Returns the JList component containing all configuration entries.
   * @return the entry list.
   */
  protected JList getEntryList ()
  {
    return entryList;
  }
  
  /**
   * Creates a panel containing all dialog control buttons, like close,
   * load, save and import.
   * 
   * @return the button panel.
   */
  private JPanel createButtonPane ()
  {
    final Action closeAction = new CloseAction();
    final Action saveAction = new SaveAction();
    final Action loadAction = new LoadAction();
    final Action importAction = new ImportAction();

    final JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    panel.setBorder(new EmptyBorder (5,5,5,5));

    final JPanel buttonHolder = new JPanel();
    buttonHolder.setLayout(new GridLayout(1,4));
    buttonHolder.add (new ActionButton(importAction));
    buttonHolder.add (new ActionButton(loadAction));
    buttonHolder.add (new ActionButton(saveAction));
    buttonHolder.add (new ActionButton (closeAction));

    panel.add(buttonHolder);
    return panel;
  }

  /**
   * Creates the detail editor panel. This panel will contain
   * all specific editors for the keys.
   * @return the detail editor panel.
   */
  private JPanel createEditPane()
  {
    final Action updateAction = new UpdateAction();
    final Action cancelAction = new CancelAction();

    final JPanel buttonHolder = new JPanel();
    buttonHolder.setLayout(new GridLayout(1,4));
    buttonHolder.add (new ActionButton(cancelAction));
    buttonHolder.add (new ActionButton(updateAction));

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.setBorder(new EmptyBorder (5,5,5,5));
    buttonPanel.add(buttonHolder);

    final JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(createDetailEditorPanel(), BorderLayout.CENTER);
    panel.add(buttonPanel, BorderLayout.SOUTH);
    return panel;
  }

  /**
   * Creates the enumeration detail editor.
   * @return the enumeration detail editor.
   */
  private JPanel createEnumerationEditor ()
  {
    enumEntryEditField = new JTextField();
    enumEntryListModel = new DefaultListModel();

    enumEntryList = new JList(enumEntryListModel);
    enumEntryList.addListSelectionListener(new EnumerationListSelectionHandler());

    final JPanel listPanel = new JPanel();
    listPanel.setLayout(new BorderLayout());
    listPanel.add(enumEntryEditField, BorderLayout.NORTH);
    listPanel.add(new JScrollPane(enumEntryList), BorderLayout.CENTER);

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(5,1));
    buttonPanel.add(new ActionButton(new AddEnumEntryAction()));
    buttonPanel.add(new ActionButton(new RemoveEnumEntryAction()));
    buttonPanel.add(new ActionButton(new UpdateEnumEntryAction()));
    buttonPanel.add(new JPanel());
    buttonPanel.add(new ActionButton(new SetBooleanEnumEntryAction()));

    final JPanel buttonCarrier = new JPanel();
    buttonCarrier.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    buttonCarrier.add (buttonPanel);

    final JPanel editorPanel = new JPanel();
    editorPanel.setLayout(new BorderLayout());
    editorPanel.add(listPanel, BorderLayout.CENTER);
    editorPanel.add(buttonCarrier, BorderLayout.EAST);
    return editorPanel;
  }

  /**
   * Creates the class detail editor.
   * @return the class detail editor.
   */
  private JPanel createClassEditor ()
  {
    baseClassField = new JTextField();
    baseClassValidateMessage = new JLabel(" ");

    final JLabel textLabel = new JLabel
        (resources.getString ("config-description-editor.baseclass"));
    final JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(textLabel, BorderLayout.WEST);
    panel.add(baseClassField, BorderLayout.CENTER);
    panel.add(baseClassValidateMessage, BorderLayout.SOUTH);

    final JPanel carrier = new JPanel();
    carrier.setLayout(new BorderLayout());
    carrier.add (panel, BorderLayout.NORTH);
    return carrier;
  }

  /**
   * Creates the text detail editor.
   * @return the text detail editor.
   */
  private JPanel createTextEditor ()
  {
    final JLabel textLabel = new JLabel
        (resources.getString ("config-description-editor.text-editor-message"));
    final JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add (textLabel);
    return panel;
  }

  /**
   * Creates the common entry detail editor. This editor contains all
   * shared properties.
   * 
   * @return the common entry editor.
   */
  private JPanel createDetailEditorPanel ()
  {
    final JLabel keyNameLabel = new JLabel
      (resources.getString ("config-description-editor.keyname"));
    final JLabel descriptionLabel = new JLabel
      (resources.getString ("config-description-editor.description"));
    final JLabel typeLabel = new JLabel(resources.getString ("config-description-editor.type"));
    final JLabel globalLabel = new JLabel(resources.getString ("config-description-editor.global"));
    final JLabel hiddenLabel = new JLabel(resources.getString ("config-description-editor.hidden"));

    hiddenField = new JCheckBox();
    globalField = new JCheckBox();
    final String font = ReportConfiguration.getGlobalConfig().getConfigProperty
        ("org.jfree.report.modules.gui.config.EditorFont", "Monospaced");
    final int fontSize = StringUtil.parseInt
        (ReportConfiguration.getGlobalConfig().getConfigProperty
          ("org.jfree.report.modules.gui.config.EditorFontSize"), 12);
    descriptionField = new JTextArea();
    descriptionField.setFont(new Font (font, Font.PLAIN, fontSize));
    descriptionField.setLineWrap(true);
    descriptionField.setWrapStyleWord(true);
    keyNameField = new JTextField();

    final JPanel enumerationEditor = createEnumerationEditor();
    final JPanel textEditor = createTextEditor();
    final JPanel classEditor = createClassEditor();

    detailManagerPanel = new JPanel();
    detailManager = new CardLayout ();
    detailManagerPanel.setLayout(detailManager);
    detailManagerPanel.add (classEditor, CLASS_DETAIL_EDITOR_NAME);
    detailManagerPanel.add (textEditor, TEXT_DETAIL_EDITOR_NAME);
    detailManagerPanel.add (enumerationEditor, ENUM_DETAIL_EDITOR_NAME);

    final JPanel commonEntryEditorPanel = new JPanel();
    commonEntryEditorPanel.setLayout(new GridBagLayout());
    commonEntryEditorPanel.setBorder(BorderFactory.createEmptyBorder(5,5,0,5));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.insets = new Insets(3, 1, 1, 1);
    commonEntryEditorPanel.add(keyNameLabel, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;

⌨️ 快捷键说明

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