Example #1
0
  private boolean confirmAndSave() {
    boolean continueWizard = true;

    if (confirmSave()) {
      if (validateSave()) {
        save();
        continueWizard = false;
      }
    } else {
      // 用户选择了“退出不保存”,所以要恢复原始的数据
      propSet.reloadUserProperties();
      continueWizard = false;
    }

    return continueWizard;
  }
Example #2
0
  private void save() {
    Map modifiedProperties = propSet.getModifiedProperties();

    println();
    println("╭───────────────────────┈┈┈┈");
    println("│ 保存文件 " + getURI() + "...");
    println("│┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈");

    try {
      OutputStream os = propSet.getUserPropertiesFile().getResource().getOutputStream();
      String charset = propSet.getUserPropertiesFile().getCharset();

      fileWriter = new PrintWriter(new OutputStreamWriter(os, charset), true);
    } catch (IOException e) {
      throw new ConfigWizardException(e);
    }

    try {
      List[] keyGroups = getSortedKeys(modifiedProperties, 2);

      for (int i = 0; i < keyGroups.length; i++) {
        List keys = keyGroups[i];

        // 找出最长的名称
        int maxLength = -1;

        for (Iterator j = keys.iterator(); j.hasNext(); ) {
          String key = (String) j.next();
          int length = key.length();

          if (length > maxLength && length < MAX_ALIGN) {
            maxLength = length;
          }
        }

        // 输出property项
        for (Iterator j = keys.iterator(); j.hasNext(); ) {
          String key = (String) j.next();
          String value = (String) modifiedProperties.get(key);

          if (value == null) {
            value = "";
          }

          value = value.replaceAll("\\\\", "\\\\\\\\");

          StringBuffer buffer = new StringBuffer();

          buffer.append(key);

          for (int k = 0; k < maxLength - key.length(); k++) {
            buffer.append(' ');
          }

          buffer.append("  = ").append(value);

          println(buffer);
        }

        if (i < keyGroups.length - 1) {
          println();
        }
      }
    } finally {
      fileWriter.close();
      fileWriter = null;
    }

    println("└───────┈┈┈┈┈┈┈┈┈┈┈");
    println(" 已保存至文件: " + getURI());

    propSet.reloadUserProperties();
  }