MyColorButton() { setMargin(new Insets(0, 0, 0, 0)); setDefaultCapable(false); setFocusable(false); if (SystemInfo.isMac) { putClientProperty("JButton.buttonType", "square"); } new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent e, int clickCount) { if (myCheckbox.isSelected()) { final Color color = ColorChooser.chooseColor(myCheckbox, "Chose color", CheckBoxWithColorChooser.this.myColor); if (color != null) { myColor = color; MyColorButton.this.repaint(); if (myUpdateRunnable != null) { myUpdateRunnable.run(); } } } return true; } }.installOn(this); }
private static void handleUserSelection(InsertionContext context, @NotNull Function<Color, String> colorToStringConverter) { Color myColorAtCaret = null; Editor selectedTextEditor = context.getEditor(); PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset()); if (element instanceof XmlToken) { myColorAtCaret = getColorFromElement(element); } context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); List<ColorPickerListener> listeners = ColorPickerListenerFactory.createListenersFor(element); Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true); if (color != null) { String colorString = colorToStringConverter.fun(color); context.getDocument().insertString(context.getStartOffset(), colorString); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } }
private void replaceColor(JComponent editorComponent, PsiNewExpression expression, Color oldColor) { final Color color = ColorChooser.chooseColor(editorComponent, getText(), oldColor, true); if (color != null) { final PsiManager manager = expression.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final PsiExpression newCall = factory.createExpressionFromText( "new " + JAVA_AWT_COLOR + "(" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + (color.getAlpha() < 255 ? ", " + color.getAlpha() : "") + ")", expression); final PsiElement insertedElement = expression.replace(newCall); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); codeStyleManager.reformat(insertedElement); } }
private static void handleUserSelection(InsertionContext context, @NotNull Function<Color, String> colorToStringConverter) { Color myColorAtCaret = null; Editor selectedTextEditor = context.getEditor(); PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset()); if (element instanceof XmlToken) { myColorAtCaret = getColorFromElement(element); } context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); ColorPickerListener[] listeners = ColorPickerListenerFactory.createListenersFor(element); Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true); if (color != null) { String colorString = colorToStringConverter.fun(color); context.getDocument().insertString(context.getStartOffset(), colorString); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } }
@Override protected void doPerformAction(ActionEvent e) { final Color color = ColorChooser.chooseColor(this, "Choose Color", myColor); if (color != null) { myColor = color; } setSelected(myColor != null); stateChanged(); }
public ColorChooserButton(@Nullable final DialogProvider dialogProvider) { super(); final ColorChooserButton theInstance = this; this.setModel(new DefaultButtonModel() { private static final long serialVersionUID = 3109256773218160485L; @Override protected void fireActionPerformed(ActionEvent e) { final PropertyEditor editor = PropertyEditorManager.findEditor(Color.class); if (editor == null) { LOGGER.error("Can't find registered color editor"); if (dialogProvider != null) dialogProvider.msgError(null, "Can't find color editor! unexpected state! Contact developer!"); return; } editor.setValue(value); final Color selectedColor = ColorChooser.chooseColor(theInstance, String.format(BUNDLE.getString("ColorChoosingButton.dialogTitle"), getText()), getValue()); if (selectedColor != null) { setValue(selectedColor); lastResultOk = true; } else { lastResultOk = false; } super.fireActionPerformed(e); } }); setValue(Color.BLACK); }
private void invokeForMethodParam(JComponent editorComponent, PsiElement element) { final PsiLiteralExpression literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression.class); if (literal == null) return; final String text = StringUtil.unquoteString(literal.getText()); final int radix = text.startsWith("0x") || text.startsWith("0X") || text.startsWith("#") ? 16 : text.startsWith("0") ? 8 : 10; final String hexPrefix = radix == 16 ? text.startsWith("#") ? "#" : text.substring(0, 2) : null; Color oldColor; try { oldColor = Color.decode(text); } catch (NumberFormatException e) { oldColor = JBColor.GRAY; } Color color = ColorChooser.chooseColor(editorComponent, getText(), oldColor, true); if (color == null) return; final int rgb = color.getRGB() - ((255 & 0xFF) << 24); if (color != null && rgb != oldColor.getRGB()) { final String newText = radix == 16 ? hexPrefix + String.format("%6s", Integer.toHexString(rgb)).replace(' ', '0') : radix == 8 ? "0" + Integer.toOctalString(rgb) : Integer.toString(rgb); final PsiManager manager = literal.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final PsiExpression newLiteral = factory.createExpressionFromText("\"" + newText + "\"", literal); literal.replace(newLiteral); } }
@Override protected void doPerformAction(ActionEvent e) { final Color color = ColorChooser.chooseColor(FileColorConfigurationEditDialog.this.getRootPane(), "Choose Color", myColor); if (color != null) { myColor = color; } setSelected(myColor != null); getOKAction().setEnabled(myColor != null); }
private static void handleUserSelection(InsertionContext context) { Color myColorAtCaret = null; Editor selectedTextEditor = context.getEditor(); PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset()); if (element instanceof XmlToken) { myColorAtCaret = getColorFromElement(element); } context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); ColorPickerListener[] listeners = ColorPickerListenerFactory.createListenersFor(element); Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true); if (color != null) { String s = Integer.toHexString(color.getRGB() & 0xFFFFFF); if (s.length() != 6) { StringBuilder buf = new StringBuilder(s); for (int i = 6 - buf.length(); i > 0; --i) { buf.insert(0, '0'); } s = buf.toString(); } s = "#" + s; context.getDocument().insertString(context.getStartOffset(), s); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } }