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 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()); } }
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()); } }