private JPanel getRuleOptionsPanel()

in languagetool-gui-commons/src/main/java/org/languagetool/gui/ConfigurationDialog.java [1924:2245]


  private JPanel getRuleOptionsPanel(int num) {
    category = "";
    rule = null;
    JPanel ruleOptionsPanel = new JPanel();
    ruleOptionsPanel.setLayout(new GridBagLayout());
    GridBagConstraints cons0 = new GridBagConstraints();
    cons0.gridx = 0;
    cons0.gridy = 0;
    cons0.fill = GridBagConstraints.NONE;
    cons0.anchor = GridBagConstraints.NORTHWEST;
    cons0.weightx = 2.0f;
    cons0.weighty = 0.0f;
    cons0.insets = new Insets(3, 8, 3, 0);
    ruleOptionsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    
    //  Color Panel
    JPanel colorPanel = new JPanel();
    colorPanel.setLayout(null);
    colorPanel.setBounds(0, 0, 120, 10);

    colorPanel.setLayout(new GridBagLayout());
    GridBagConstraints cons1 = new GridBagConstraints();
    cons1.insets = new Insets(0, 0, 0, 0);
    cons1.gridx = 0;
    cons1.gridy = 0;
    cons1.weightx = 0.0f;
    cons1.fill = GridBagConstraints.NONE;
    cons1.anchor = GridBagConstraints.NORTHWEST;

    JLabel underlineStyle = new JLabel(messages.getString("guiUColorStyleLabel") + " ");
    colorPanel.add(underlineStyle);

    JLabel underlineLabel = new JLabel(" \u2588\u2588\u2588 ");  // \u2587 is smaller

    JComboBox<String> underlineType = new JComboBox<>(getUnderlineTypes());
    if(insideOffice) {
      underlineType.setSelectedIndex(getUnderlineType(category, (rule == null ? null : rule.getId())));
      underlineType.addItemListener(e -> {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          setUnderlineType(underlineType.getSelectedIndex(), category, (rule == null ? null : rule.getId()));
        }
      });
      cons1.gridx++;
      colorPanel.add(underlineType);
    }
    cons1.gridx++;
    colorPanel.add(underlineLabel);

    JButton changeButton = new JButton(messages.getString("guiUColorChange"));
    changeButton.addActionListener(e -> {
      Color oldColor = underlineLabel.getForeground();
      if(insideOffice) {
        dialog.setAlwaysOnTop(false);
      }
      JColorChooser colorChooser = new JColorChooser(oldColor);
      ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
          Color newColor = colorChooser.getColor();
          if(newColor != null && newColor != oldColor) {
            underlineLabel.setForeground(newColor);
            if (rule == null) {
              config.setUnderlineColor(category, newColor);
            } else {
              config.setUnderlineRuleColor(rule.getId(), newColor);
            }
          }
          if(insideOffice) {
            dialog.setAlwaysOnTop(true);
          }
        }
      };
      // For cancel selection, change button background to red
      ActionListener cancelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
          if(insideOffice) {
            dialog.setAlwaysOnTop(true);
          }
        }
      };
      JDialog colorDialog = JColorChooser.createDialog(dialog, messages.getString("guiUColorDialogHeader"), true,
          colorChooser, okActionListener, cancelActionListener);
      if(insideOffice) {
        colorDialog.setAlwaysOnTop(true);
      }
      colorDialog.toFront();
      colorDialog.setVisible(true);
/*      
      Color newColor = JColorChooser.showDialog( null, messages.getString("guiUColorDialogHeader"), oldColor);
      if(newColor != null && newColor != oldColor) {
        underlineLabel.setForeground(newColor);
        if (rule == null) {
          config.setUnderlineColor(category, newColor);
        } else {
          config.setUnderlineRuleColor(rule.getId(), newColor);
        }
      }
      if(insideOffice) {
        dialog.setAlwaysOnTop(true);
      }
*/
    });
    cons1.gridx++;
    colorPanel.add(changeButton);
  
    JButton defaultButton = new JButton(messages.getString("guiUColorDefault"));
    defaultButton.addActionListener(e -> {
      String ruleId = (rule == null ? null : rule.getId());
      if (rule == null) {
        config.setDefaultUnderlineColor(category);
      } else {
        config.setDefaultUnderlineRuleColor(ruleId);
      }
      underlineLabel.setForeground(config.getUnderlineColor(category, ruleId));
      if(insideOffice) {
        if ( rule == null) {
          config.setDefaultUnderlineType(category);
        } else {
          config.setDefaultUnderlineRuleType(ruleId);
        }
        underlineType.setSelectedIndex(getUnderlineType(category, ruleId));
      }
      config.removeConfigurableValue(ruleId);
    });
    cons1.gridx++;
    colorPanel.add(defaultButton);
    colorPanel.setVisible(false);
    // End of Color Panel
    
    List<JPanel> specialOptionPanels = new ArrayList<>();
    
    ruleOptionsPanel.add(colorPanel, cons0);
    cons0.gridx = 0;
    cons0.gridy = 1;
    
    configTree[num].addTreeSelectionListener(e -> {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)
          configTree[num].getLastSelectedPathComponent();
      if (node != null) {
        if (specialOptionPanels.size() > 0) {
          for (JPanel optionPanel : specialOptionPanels) {
            optionPanel.setVisible(false);
            ruleOptionsPanel.remove(optionPanel);
          }
          specialOptionPanels.clear();
        }
        ruleOptionsPanel.setVisible(false);
        if (node instanceof RuleNode) {
          RuleNode o = (RuleNode) node;
          rule = o.getRule();
          category = rule.getCategory().getName();
          String ruleId = rule.getId();
          underlineLabel.setForeground(config.getUnderlineColor(category, ruleId));
          underlineLabel.setBackground(config.getUnderlineColor(category, ruleId));
          if(insideOffice) {
            underlineType.setSelectedIndex(getUnderlineType(category, ruleId));
          }
          colorPanel.setVisible(true);
          RuleOption[] ruleOptions = rule.getRuleOptions();
          if (ruleOptions != null && ruleOptions.length > 0) {
            Object[] obj = new Object[ruleOptions.length];
            for (int i = 0; i < ruleOptions.length; i++) {
              // Start of special option panel
              JPanel specialOptionPanel = new JPanel();
              specialOptionPanels.add(specialOptionPanel);
              specialOptionPanel.setLayout(new GridBagLayout());
              GridBagConstraints cons2 = new GridBagConstraints();
              cons2.gridx = 0;
              cons2.gridy = 0;
              cons2.weightx = 2.0f;
              cons2.anchor = GridBagConstraints.WEST;
              RuleOption ruleOption = ruleOptions[i];
              int n = i;

              Object defValue = ruleOption.getDefaultValue();
              
              if (defValue instanceof Boolean) {
                JCheckBox isTrueBox = new JCheckBox(ruleOption.getConfigureText());
                boolean value = config.getConfigValueByID(rule.getId(), i, Boolean.class, (Boolean) defValue);
                isTrueBox.setSelected(value);
                obj[n] = value;
                isTrueBox.addItemListener(e1 -> {
                  obj[n] = isTrueBox.isSelected();
                  config.setConfigurableValue(rule.getId(), obj);
                });
                specialOptionPanel.add(isTrueBox, cons2);
              } else {
                JLabel ruleLabel = new JLabel(ruleOption.getConfigureText() + " ");
                specialOptionPanel.add(ruleLabel, cons2);
    
                cons2.gridx++;
                JTextField ruleValueField = new JTextField("   ", 3);
                ruleValueField.setMinimumSize(new Dimension(50, 28));  // without this the box is just a few pixels small, but why?
                String fieldValue;
                if (defValue instanceof Integer) {
                  obj[n] = (int) config.getConfigValueByID(rule.getId(), i, Integer.class, (Integer) defValue);
                  fieldValue = Integer.toString((int) obj[n]);
                } else if (defValue instanceof Character) {
                  obj[n] = (char) config.getConfigValueByID(rule.getId(), i, Character.class, (Character) defValue);
                  fieldValue = Character.toString((char) obj[n]);
                } else if (defValue instanceof Double) {
                  obj[n] = (double) config.getConfigValueByID(rule.getId(), i, Double.class, (Double) defValue);
                  fieldValue = Double.toString((double) obj[n]);
                } else if (defValue instanceof Float) {
                  obj[n] = (float) config.getConfigValueByID(rule.getId(), i, Float.class, (Float) defValue);
                  fieldValue = Float.toString((float) obj[n]);
                } else {
                  obj[n] = (String) config.getConfigValueByID(rule.getId(), i, String.class, (String) defValue);
                  fieldValue = (String) obj[n];
                }
                ruleValueField.setText(fieldValue);
                specialOptionPanel.add(ruleValueField, cons2);
    
                ruleValueField.getDocument().addDocumentListener(new DocumentListener() {
                  @Override
                  public void insertUpdate(DocumentEvent e) {
                    changedUpdate(e);
                  }
    
                  @Override
                  public void removeUpdate(DocumentEvent e) {
                    changedUpdate(e);
                  }
    
                  @Override
                  public void changedUpdate(DocumentEvent e) {
                    try {
                      if (rule != null) {
                        RuleOption[] ruleOptions = rule.getRuleOptions();
                        if (ruleOptions != null && ruleOptions.length > 0) {
                          boolean isCorrect = false;
                          if (defValue instanceof Integer) {
                            int num = Integer.parseInt(ruleValueField.getText());
                            if (num < (int) ruleOption.getMinConfigurableValue()) {
                              num = (int) ruleOption.getMinConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else if (num > (int) ruleOption.getMaxConfigurableValue()) {
                              num = (int) ruleOption.getMaxConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else {
                              ruleValueField.setForeground(null);
                              isCorrect = true;
                              obj[n] = num;
                            }
                          } else if (defValue instanceof Character) {
                            char num = ruleValueField.getText().charAt(0);
                            if (num < (char) ruleOption.getMinConfigurableValue()) {
                              num = (char) ruleOption.getMinConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else if (num > (char) ruleOption.getMaxConfigurableValue()) {
                              num = (char) ruleOption.getMaxConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else {
                              ruleValueField.setForeground(null);
                              isCorrect = true;
                              obj[n] = num;
                            }
                          } else if (defValue instanceof Double) {
                            double num = Double.parseDouble(ruleValueField.getText());
                            if (num < (double) ruleOption.getMinConfigurableValue()) {
                              num = (double) ruleOption.getMinConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else if (num > (double) ruleOption.getMaxConfigurableValue()) {
                              num = (double) ruleOption.getMaxConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else {
                              ruleValueField.setForeground(null);
                              isCorrect = true;
                              obj[n] = num;
                            }
                          } else if (defValue instanceof Float) {
                            float num = Float.parseFloat(ruleValueField.getText());
                            if (num < (float) ruleOption.getMinConfigurableValue()) {
                              num = (float) ruleOption.getMinConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else if (num > (float) ruleOption.getMaxConfigurableValue()) {
                              num = (float) ruleOption.getMaxConfigurableValue();
                              ruleValueField.setForeground(Color.RED);
                            } else {
                              ruleValueField.setForeground(null);
                              isCorrect = true;
                              obj[n] = num;
                            }
                          } else {
                            String num = ruleValueField.getText();
                            ruleValueField.setForeground(null);
                            isCorrect = true;
                            obj[n] = num;
                          }
                          if (isCorrect) {
                            config.setConfigurableValue(rule.getId(), obj);
                          }
                        }
                      }
                    } catch (Exception ex) {
                      ruleValueField.setForeground(Color.RED);
                    }
                  }
                });
              }
              ruleOptionsPanel.add(specialOptionPanel, cons0);
              ruleOptionsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
              ruleOptionsPanel.setVisible(true);
              cons0.gridy++;
              // End of special option panel
            }
          }
        } else if (node instanceof CategoryNode) {
          CategoryNode o = (CategoryNode) node;
          category = o.getCategory().getName();
          underlineLabel.setForeground(config.getUnderlineColor(category, null));
          underlineLabel.setBackground(config.getUnderlineColor(category, null));
          if(insideOffice) {
            underlineType.setSelectedIndex(getUnderlineType(category, null));
          }
          colorPanel.setVisible(true);
          rule = null;
        }
        ruleOptionsPanel.setVisible(true);
      }
    });
    return ruleOptionsPanel;
  }