configdescriptioneditor.java

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

JAVA
1,342
字号
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    commonEntryEditorPanel.add(keyNameField, gbc);

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

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    gbc.ipady = 120;
    commonEntryEditorPanel.add(new JScrollPane
        (descriptionField,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc);

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

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    commonEntryEditorPanel.add(globalField, gbc);

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

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    commonEntryEditorPanel.add(hiddenField, gbc);

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

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    commonEntryEditorPanel.add(createTypeSelectionPane(), gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.gridwidth = 2;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    commonEntryEditorPanel.add(detailManagerPanel, gbc);

    return commonEntryEditorPanel;
  }

  /**
   * Creates the type selection panel containing some radio buttons to
   * define the detail editor type.
   * 
   * @return the type selection panel.
   */
  private JPanel createTypeSelectionPane ()
  {
    final JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3,1));

    rbText = new ActionRadioButton (new SelectTypeAction
      (resources.getString ("config-description-editor.type-text"), TYPE_TEXT));
    rbClass = new ActionRadioButton (new SelectTypeAction
      (resources.getString ("config-description-editor.type-class"), TYPE_CLASS));
    rbEnum = new ActionRadioButton (new SelectTypeAction
      (resources.getString ("config-description-editor.type-enum"), TYPE_ENUM));

    final ButtonGroup bg = new ButtonGroup();
    bg.add (rbText);
    bg.add (rbClass);
    bg.add (rbEnum);

    panel.add (rbText);
    panel.add (rbClass);
    panel.add (rbEnum);

    return panel;
  }

  /**
   * Creates the statusbar for this frame. Use setStatus() to display text on the status bar.
   *
   * @return the status bar.
   */
  protected JPanel createStatusBar()
  {
    final JPanel statusPane = new JPanel();
    statusPane.setLayout(new BorderLayout());
    statusPane.setBorder(
        BorderFactory.createLineBorder(UIManager.getDefaults().getColor("controlShadow")));
    statusHolder = new JLabel(" ");
    statusPane.setMinimumSize(statusHolder.getPreferredSize());
    statusPane.add(statusHolder, BorderLayout.WEST);

    return statusPane;
  }

  /**
   * Defines the status text for this dialog.
   * 
   * @param text the new status text.
   */
  protected void setStatusText (final String text)
  {
    statusHolder.setText(text);
  }

  /**
   * Returns the currently visible status text of this dialog.
   * @return the status text.
   */
  protected String getStatusText ()
  {
    return statusHolder.getText();
  }

  /**
   * Sets the entry type for the current config description entry. This
   * also selects and activates the correct detail editor for this type.
   * 
   * @param type the type of the currently selected entry.
   */
  protected void setEntryType (final int type)
  {
    this.type = type;
    if (type == TYPE_CLASS)
    {
      detailManager.show(detailManagerPanel, CLASS_DETAIL_EDITOR_NAME);
      rbClass.setSelected(true);
    }
    else if (type == TYPE_ENUM)
    {
      detailManager.show(detailManagerPanel, ENUM_DETAIL_EDITOR_NAME);
      rbEnum.setSelected(true);
    }
    else
    {
      detailManager.show(detailManagerPanel, TEXT_DETAIL_EDITOR_NAME);
      rbText.setSelected(true);
    }
    invalidate();
  }

  /**
   * Returns the current entry type.
   * @return the current entry type.
   */
  protected int getEntryType ()
  {
    return type;
  }

  /**
   * Returns the currently select entry from the entry list model.
   * 
   * @return the currently selected entry.
   */
  protected ConfigDescriptionEntry getSelectedEntry()
  {
    return selectedEntry;
  }

  /**
   * Defines the currently selected entry from the entry list model and
   * updates the detail editor to reflect the data from the entry.
   * 
   * @param selectedEntry the selected entry.
   */
  protected void setSelectedEntry(final ConfigDescriptionEntry selectedEntry)
  {
    this.selectedEntry = selectedEntry;

    enumEntryEditField.setText("");
    enumEntryListModel.clear();
    baseClassField.setText("");

    if (this.selectedEntry == null)
    {
      deepEnable(detailEditorPane, false);
    }
    else
    {
      deepEnable(detailEditorPane, true);
      keyNameField.setText(selectedEntry.getKeyName());
      globalField.setSelected(selectedEntry.isGlobal());
      hiddenField.setSelected(selectedEntry.isHidden());
      descriptionField.setText(selectedEntry.getDescription());
      if (selectedEntry instanceof ClassConfigDescriptionEntry)
      {
        final ClassConfigDescriptionEntry ce = (ClassConfigDescriptionEntry) selectedEntry;
        setEntryType(TYPE_CLASS);
        if (ce.getBaseClass() != null)
        {
          baseClassField.setText(ce.getBaseClass().getName());
        }
      }
      else if (selectedEntry instanceof EnumConfigDescriptionEntry)
      {
        final EnumConfigDescriptionEntry enum = (EnumConfigDescriptionEntry) selectedEntry;
        final String[] enums = enum.getOptions();
        for (int i= 0; i < enums.length; i++)
        {
          enumEntryListModel.addElement(enums[i]);
        }
        setEntryType(TYPE_ENUM);
      }
      else
      {
        setEntryType(TYPE_TEXT);
      }
    }
  }

  /**
   * A utility method to enable or disable a component and all childs.
   * @param comp the component that should be enabled or disabled.
   * @param state the new enable state.
   */
  private void deepEnable (final Component comp, final boolean state)
  {
    comp.setEnabled(state);
    if (comp instanceof Container)
    {
      final Container cont = (Container) comp;
      final Component[] childs = cont.getComponents();
      for (int i = 0; i < childs.length; i++)
      {
        deepEnable(childs[i], state);
      }
    }
  }

  /**
   * Saves the config description model in a xml file.
   */
  protected void save ()
  {
    fileChooser.setVisible(true);

    final int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      try
      {
        final OutputStream out = new BufferedOutputStream
          (new FileOutputStream(fileChooser.getSelectedFile()));
        model.save(out, "ISO-8859-1");
        out.close();
        setStatusText(resources.getString ("config-description-editor.save-complete"));
      }
      catch (Exception ioe)
      {
        Log.debug ("Failed", ioe);
        final String message = MessageFormat.format
          (resources.getString ("config-description-editor.save-failed"),
           new Object[] { ioe.getMessage()}); 
        setStatusText(message);
      }
    }
  }

  /**
   * Loads the config description model from a xml file.
   */
  protected void load ()
  {
    fileChooser.setVisible(true);

    final int option = fileChooser.showOpenDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      try
      {
        final InputStream in = new BufferedInputStream
          (new FileInputStream(fileChooser.getSelectedFile()));
        model.load(in);
        in.close();
        model.sort();
        setStatusText(resources.getString ("config-description-editor.load-complete"));
      }
      catch (Exception ioe)
      {
        Log.debug ("Load Failed", ioe);
        final String message = MessageFormat.format
          (resources.getString ("config-description-editor.load-failed"),
           new Object[] { ioe.getMessage()}); 
        setStatusText(message);
      }
    }
  }

  /**
   * Updates the currently selected entry from the values found in the 
   * detail editor.
   *
   */
  protected void updateSelectedEntry ()
  {
    final ConfigDescriptionEntry entry;
    switch (getEntryType())
    {
      case TYPE_CLASS:
        {
          final ClassConfigDescriptionEntry ce =
              new ClassConfigDescriptionEntry(keyNameField.getText());
          ce.setDescription(descriptionField.getText());
          ce.setGlobal(globalField.isSelected());
          ce.setHidden(hiddenField.isSelected());
          try
          {
            final Class c = this.getClass().getClassLoader().loadClass(baseClassField.getText());
            ce.setBaseClass(c);
          }
          catch (Exception e)
          {
            // invalid
            Log.debug ("Class is invalid; defaulting to Object.class");
            ce.setBaseClass(Object.class);
          }
          entry = ce;
          break;
        }
      case TYPE_ENUM:
        {
          final EnumConfigDescriptionEntry ece =
              new EnumConfigDescriptionEntry(keyNameField.getText());
          ece.setDescription(descriptionField.getText());
          ece.setGlobal(globalField.isSelected());
          ece.setHidden(hiddenField.isSelected());
          final String[] enumEntries = new String[enumEntryListModel.getSize()];
          for (int i = 0; i < enumEntryListModel.getSize(); i++)
          {
            enumEntries[i] = String.valueOf(enumEntryListModel.get(i));
          }
          ece.setOptions(enumEntries);
          entry = ece;
          break;
        }
      default:
        {
          final TextConfigDescriptionEntry te =
              new TextConfigDescriptionEntry(keyNameField.getText());
          te.setDescription(descriptionField.getText());
          te.setGlobal(globalField.isSelected());
          te.setHidden(hiddenField.isSelected());
          entry = te;
          break;
        }
    }
    if (model.contains(entry))
    {
      model.remove(entry);
    }
    model.remove(this.selectedEntry);
    model.add(entry);
    model.sort();
    entryList.setSelectedIndex(model.indexOf(entry));
    setStatusText(resources.getString ("config-description-editor.update-complete"));
  }

  /**
   * Returns the config description model containing all metainformation about
   * the configuration. 
   * @return the config description model.
   */
  protected ConfigDescriptionModel getModel()
  {
    return model;
  }
  
  /**
   * Handles the attemp to quit the program. This method shuts down the VM.
   *
   */
  protected void attempExit ()
  {
    System.exit (0);
  }
  
  /**
   * Returns the resource bundle of this editor for translating strings. 
   * @return the resource bundle.
   */
  protected ResourceBundle getResources ()
  {
    return resources;
  }
  
  /**
   * The main entry point to start the detail editor.
   *  
   * @param args ignored.
   */
  public static void main(final String[] args)
  {
    final ConfigDescriptionEditor ed = new ConfigDescriptionEditor();
    ed.pack();
    ed.setVisible(true);
  }
}

⌨️ 快捷键说明

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