コード例 #1
0
 private void resetButtonActionPerformed(ActionEvent evt) {
   Map<String, Field> rfields = Configuration.getConfigurationFields();
   for (Entry<String, Field> entry : rfields.entrySet()) {
     String name = entry.getKey();
     Field field = entry.getValue();
     try {
       ConfigurationItem item = (ConfigurationItem) field.get(null);
       item.unset();
     } catch (IllegalArgumentException | IllegalAccessException ex) {
       // Reflection exceptions. This should never happen
       throw new Error(ex.getMessage());
     }
   }
   Configuration.saveConfig();
   setVisible(false);
   showRestartConfirmDialod();
 }
コード例 #2
0
  @SuppressWarnings("unchecked")
  private void okButtonActionPerformed(ActionEvent evt) {
    boolean modified = false;
    Map<String, Field> fields = Configuration.getConfigurationFields();
    Map<String, Object> values = new HashMap<>();
    for (String name : fields.keySet()) {
      Component c = componentsMap.get(name);
      Object value = null;

      ParameterizedType listType = (ParameterizedType) fields.get(name).getGenericType();
      java.lang.reflect.Type itemType2 = listType.getActualTypeArguments()[0];
      if (!(itemType2 instanceof Class<?>)) {
        continue;
      }

      Class itemType = (Class<?>) itemType2;
      if (name.equals("gui.skin")) {
        value = ((SkinSelect) ((JComboBox<SkinSelect>) c).getSelectedItem()).className;
      } else if (itemType == String.class) {
        value = ((JTextField) c).getText();
      }
      if (itemType == Boolean.class) {
        value = ((JCheckBox) c).isSelected();
      }

      if (itemType == Calendar.class) {
        Calendar cal = Calendar.getInstance();
        try {
          cal.setTime(new SimpleDateFormat().parse(((JTextField) c).getText()));
        } catch (ParseException ex) {
          c.requestFocusInWindow();
          return;
        }
        value = cal;
      }

      if (itemType.isEnum()) {
        String stringValue = (String) ((JComboBox<String>) c).getSelectedItem();
        value = Enum.valueOf(itemType, stringValue);
      }

      try {
        if (itemType == Integer.class) {
          value = Integer.parseInt(((JTextField) c).getText());
        }
        if (itemType == Long.class) {
          value = Long.parseLong(((JTextField) c).getText());
        }
        if (itemType == Double.class) {
          value = Double.parseDouble(((JTextField) c).getText());
        }
        if (itemType == Float.class) {
          value = Float.parseFloat(((JTextField) c).getText());
        }
      } catch (NumberFormatException nfe) {
        if (!((JTextField) c).getText().isEmpty()) {
          c.requestFocusInWindow();
          return;
        } // else null
      }
      values.put(name, value);
    }

    for (String name : fields.keySet()) {
      Component c = componentsMap.get(name);
      Object value = values.get(name);

      Field field = fields.get(name);
      ConfigurationItem item = null;
      try {
        item = (ConfigurationItem) field.get(null);
      } catch (IllegalArgumentException | IllegalAccessException ex) {
        // Reflection exceptions. This should never happen
        throw new Error(ex.getMessage());
      }
      if (item.get() == null || !item.get().equals(value)) {
        if (item.hasValue() || value != null) {
          item.set(value);
          modified = true;
        }
      }
    }
    Configuration.saveConfig();
    setVisible(false);
    if (modified) {
      showRestartConfirmDialod();
    }
  }