configeditor.java

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

JAVA
744
字号
    buttonHolder.add(new ActionButton(loadAction));
    buttonHolder.add(new ActionButton(saveAction));
    buttonHolder.add(new ActionButton(closeAction));

    panel.add(buttonHolder);
    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 text to be displayed on the status bar. Setting text will
   * replace any other previously defined text.
   *
   * @param text the new statul bar text.
   */
  private void setStatusText(final String text)
  {
    statusHolder.setText(text);
  }

//  private String getStatusText ()
//  {
//    return statusHolder.getText();
//  }

  /**
   * Loads the report configuration from a user selectable report properties file.
   */
  protected void load()
  {
    setStatusText("Loading: Select file ...");
    if (fileChooser == null)
    {
      fileChooser = new JFileChooser();
      final FilesystemFilter filter = new FilesystemFilter(".properties", "Properties files");
      fileChooser.addChoosableFileFilter(filter);
      fileChooser.setMultiSelectionEnabled(false);
    }

    final int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      final File selFile = fileChooser.getSelectedFile();
      String selFileName = selFile.getAbsolutePath();

      // Test if ends on .properties
      if (StringUtil.endsWithIgnoreCase(selFileName, ".properties") == false)
      {
        selFileName = selFileName + ".properties";
      }
      final Properties prop = new Properties();
      try
      {
        final InputStream in = new BufferedInputStream(new FileInputStream(selFileName));
        prop.load(in);
        in.close();
      }
      catch (IOException ioe)
      {
        Log.debug("Failed to load the properties.", ioe);
        setStatusText("Failed to load the properties." + ioe.getMessage());
        return;
      }

      reset();

      final Enumeration enum = prop.keys();
      while (enum.hasMoreElements())
      {
        final String key = (String) enum.nextElement();
        final String value = prop.getProperty(key);
        currentReportConfiguration.setConfigProperty(key, value);
      }
      try
      {
        treeModel.init(currentReportConfiguration);
        setStatusText("Loading the properties complete.");
      }
      catch (ConfigTreeModelException e)
      {
        Log.debug("Failed to update the model.", e);
        setStatusText("Failed to update the model.");
      }
    }
  }

  /**
   * Resets all values.
   */
  protected void reset()
  {
    // clear all previously set configuration settings ...
    final Enumeration defaults = currentReportConfiguration.getConfigProperties();
    while (defaults.hasMoreElements())
    {
      final String key = (String) defaults.nextElement();
      currentReportConfiguration.setConfigProperty(key, null);
    }
  }

  /**
   * Saves the report configuration to a user selectable report properties file.
   */
  protected void save()
  {
    setStatusText("Saving: Select file ...");
    detailEditorPane.store();

    if (fileChooser == null)
    {
      fileChooser = new JFileChooser();
      final FilesystemFilter filter = new FilesystemFilter(".properties", "Properties files");
      fileChooser.addChoosableFileFilter(filter);
      fileChooser.setMultiSelectionEnabled(false);
    }

    final int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      final File selFile = fileChooser.getSelectedFile();
      String selFileName = selFile.getAbsolutePath();

      // Test if ends on xls
      if (StringUtil.endsWithIgnoreCase(selFileName, ".properties") == false)
      {
        selFileName = selFileName + ".properties";
      }
      write(selFileName);
    }
  }

  /**
   * Writes the configuration into the file specified by the given file name.
   * 
   * @param filename the target file name
   */
  private void write(final String filename)
  {
    final Properties prop = new Properties();
    final ArrayList names = new ArrayList();
    // clear all previously set configuration settings ...
    final Enumeration defaults = currentReportConfiguration.getConfigProperties();
    while (defaults.hasMoreElements())
    {
      final String key = (String) defaults.nextElement();
      names.add(key);
      prop.setProperty(key, currentReportConfiguration.getConfigProperty(key));
    }

    Collections.sort(names);

    try
    {
      final PrintWriter out =
          new PrintWriter(new OutputStreamWriter
              (new BufferedOutputStream(new FileOutputStream(filename))));

      for (int i = 0; i < names.size(); i++)
      {
        final String key = (String) names.get(i);
        final String value = prop.getProperty(key);

        final ConfigDescriptionEntry entry = treeModel.getEntryForKey(key);
        if (entry != null)
        {
          final String description = entry.getDescription();
          writeDescription(description, out);
        }
        saveConvert(key, ESCAPE_KEY, out);
        out.print("=");
        saveConvert(value, ESCAPE_VALUE, out);
        out.println();
      }
      out.close();
      setStatusText("Saving the properties complete.");
    }
    catch (IOException ioe)
    {
      Log.debug("Failed to save the properties.", ioe);
      setStatusText("Failed to savethe properties." + ioe.getMessage());
    }

  }

  /**
   * Writes a descriptive comment into the given print writer.
   * @param text the text to be written. If it contains more than
   * one line, every line will be prepended by the comment character.
   * @param writer the writer that should receive the content.
   */
  private void writeDescription(final String text, final PrintWriter writer)
  {
    // check if empty content ... this case is easy ...
    if (text.length() == 0)
    {
      return;
    }

    writer.println("# ");
    final LineBreakIterator iterator = new LineBreakIterator(text);
    while (iterator.hasNext())
    {
      writer.print("# ");
      saveConvert((String) iterator.next(), ESCAPE_COMMENT, writer);
      writer.println();
    }
  }

  /**
   * Performs the necessary conversion of an java string into a property
   * escaped string. 
   * 
   * @param text the text to be escaped
   * @param escapeMode the mode that should be applied.
   * @param writer the writer that should receive the content.
   */
  private void saveConvert(final String text, final int escapeMode, final PrintWriter writer)
  {
    final char[] string = text.toCharArray();
    final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    for (int x = 0; x < string.length; x++)
    {
      final char aChar = string[x];
      switch (aChar)
      {
        case ' ':
          {
            if ((escapeMode != ESCAPE_COMMENT) &&
                (x == 0 || escapeMode == ESCAPE_KEY))
            {
              writer.print('\\');
            }
            writer.print(' ');
            break;
          }
        case '\\':
          {
            writer.print('\\');
            writer.print('\\');
            break;
          }
        case '\t':
          {
            if (escapeMode == ESCAPE_COMMENT)
            {
              writer.print(aChar);
            }
            else
            {
              writer.print('\\');
              writer.print('t');
            }
            break;
          }
        case '\n':
          {
            writer.print('\\');
            writer.print('n');
            break;
          }
        case '\r':
          {
            writer.print('\\');
            writer.print('r');
            break;
          }
        case '\f':
          {
            if (escapeMode == ESCAPE_COMMENT)
            {
              writer.print(aChar);
            }
            else
            {
              writer.print('\\');
              writer.print('f');
            }
            break;
          }
        case '#':
        case '"':
        case '!':
        case '=':
        case ':':
          {
            if (escapeMode == ESCAPE_COMMENT)
            {
              writer.print(aChar);
            }
            else
            {
              writer.print('\\');
              writer.print(aChar);
            }
            break;
          }
        default:
          if ((aChar < 0x0020) || (aChar > 0x007e))
          {
            writer.print('\\');
            writer.print('u');
            writer.print(hexChars[(aChar >> 12) & 0xF]);
            writer.print(hexChars[(aChar >> 8) & 0xF]);
            writer.print(hexChars[(aChar >> 4) & 0xF]);
            writer.print(hexChars[aChar & 0xF]);
          }
          else
          {
            writer.print(aChar);
          }
      }
    }
  }

  /**
   * Closes this frame and exits the JavaVM.
   *
   */
  protected void attempClose()
  {
    System.exit(0);
  }

  /**
   * Returns the detail editor pane.
   * @return the detail editor.
   */
  protected ConfigEditorPanel getDetailEditorPane()
  {
    return detailEditorPane;
  }

  /**
   * main Method to start the editor.
   * @param args not used.
   */
  public static void main(final String[] args)
  {
    try
    {
      Boot.start();
      final ConfigEditor ed = new ConfigEditor();
      ed.pack();
      ed.setVisible(true);
    }
    catch (Exception e)
    {
      Log.debug("Failed to init", e);
      JOptionPane.showMessageDialog(null, "Failed to initialize.");
    }
  }
}

⌨️ 快捷键说明

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