public static void chooseColor(
      JComponent editorComponent, PsiElement element, String caption, boolean startInWriteAction) {
    final XmlAttributeValue literal =
        PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
    if (literal == null) return;
    final String text = StringUtil.unquoteString(literal.getValue());

    Color oldColor;
    try {
      oldColor = Color.decode(text);
    } catch (NumberFormatException e) {
      oldColor = JBColor.GRAY;
    }
    Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
    if (color == null) return;
    if (!Comparing.equal(color, oldColor)) {
      if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
      final String newText = "#" + ColorUtil.toHex(color);
      final PsiManager manager = literal.getManager();
      final XmlAttribute newAttribute =
          XmlElementFactory.getInstance(manager.getProject()).createXmlAttribute("name", newText);
      final Runnable replaceRunnable =
          new Runnable() {
            @Override
            public void run() {
              final XmlAttributeValue valueElement = newAttribute.getValueElement();
              assert valueElement != null;
              literal.replace(valueElement);
            }
          };
      if (startInWriteAction) {
        new WriteCommandAction(element.getProject(), caption) {
          @Override
          protected void run(@NotNull Result result) throws Throwable {
            replaceRunnable.run();
          }
        }.execute();
      } else {
        replaceRunnable.run();
      }
    }
  }
Ejemplo n.º 2
0
 @Nullable
 public Component getTableCellEditorComponent(
     JTable table, Object value, boolean isSelected, int row, int column) {
   myValue = ((MyTableModel) table.getModel()).getRegistryValue(row);
   if (myValue.asColor(null) != null) {
     final Color color =
         ColorChooser.chooseColor(
             table, "Choose color", ((RegistryValue) value).asColor(Color.WHITE));
     if (color != null) {
       myValue.setValue(color.getRed() + "," + color.getGreen() + "," + color.getBlue());
     }
     return null;
   } else if (myValue.isBoolean()) {
     myCheckBox.setSelected(myValue.asBoolean());
     myCheckBox.setBackground(table.getBackground());
     return myCheckBox;
   } else {
     myField.setText(myValue.asString());
     myField.setBorder(null);
     myField.selectAll();
     return myField;
   }
 }