package jetbrains.mps.ide.actions;
/*Generated by MPS */
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.nodeEditor.EditorComponent;
import jetbrains.mps.ide.editor.MPSEditorDataKeys;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.openapi.editor.EditorContext;
import com.intellij.featureStatistics.FeatureUsageTracker;
import java.awt.Point;
import jetbrains.mps.editor.runtime.style.ParametersInformation;
import jetbrains.mps.editor.runtime.style.StyleAttributes;
import java.awt.Component;
import jetbrains.mps.ide.tooltips.MPSToolTipManager;
import jetbrains.mps.ide.tooltips.ToolTipData;
import org.jetbrains.mps.openapi.model.SNode;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import jetbrains.mps.ide.tooltips.ToolTip;
import javax.swing.border.EmptyBorder;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.editor.runtime.style.StyledTextPrinter;
import javax.swing.JTextPane;
import jetbrains.mps.nodeEditor.EditorSettings;
import com.intellij.ui.JBColor;
import java.awt.Color;
import com.intellij.ui.Gray;
import java.awt.GridBagConstraints;
public class ShowParameters_Action extends BaseAction {
private static final Icon ICON = null;
public ShowParameters_Action() {
super("Show Parameters", "", ICON);
this.setIsAlwaysVisible(false);
this.setExecuteOutsideCommand(false);
}
@Override
public boolean isDumbAware() {
return true;
}
@Override
public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
if (ShowParameters_Action.this.getCellNode(_params) == null) {
return false;
}
return true;
}
@Override
public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
this.setEnabledState(event.getPresentation(), this.isApplicable(event, _params));
}
@Override
protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
if (!(super.collectActionData(event, _params))) {
return false;
}
{
EditorComponent editorComponent = event.getData(MPSEditorDataKeys.EDITOR_COMPONENT);
if (editorComponent != null && editorComponent.isInvalid()) {
editorComponent = null;
}
MapSequence.fromMap(_params).put("editor", editorComponent);
if (editorComponent == null) {
return false;
}
}
{
EditorCell p = event.getData(MPSEditorDataKeys.EDITOR_CELL);
MapSequence.fromMap(_params).put("cell", p);
if (p == null) {
return false;
}
}
{
EditorContext p = event.getData(MPSEditorDataKeys.EDITOR_CONTEXT);
MapSequence.fromMap(_params).put("editorContext", p);
if (p == null) {
return false;
}
}
return true;
}
@Override
public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.showParameters");
Point p = new Point(((EditorCell) MapSequence.fromMap(_params).get("cell")).getX() + ((EditorCell) MapSequence.fromMap(_params).get("cell")).getWidth(), ((EditorCell) MapSequence.fromMap(_params).get("cell")).getY() + ((EditorCell) MapSequence.fromMap(_params).get("cell")).getHeight());
EditorCell currentCell = ((EditorCell) MapSequence.fromMap(_params).get("cell"));
while (currentCell != null) {
ParametersInformation parametersInformation = currentCell.getStyle().get(StyleAttributes.PARAMETERS_INFORMATION);
if (parametersInformation != null) {
Component component = ShowParameters_Action.this.createComponent(parametersInformation, currentCell.getSNode(), _params);
MPSToolTipManager.getInstance().showToolTip(new ToolTipData(component), ((EditorComponent) MapSequence.fromMap(_params).get("editor")), p);
return;
}
currentCell = currentCell.getParent();
}
}
/*package*/ SNode getCellNode(final Map<String, Object> _params) {
return ((EditorCell) MapSequence.fromMap(_params).get("cell")).getSNode();
}
private <T> Component createComponent(ParametersInformation<T> parametersInformation, SNode node, final Map<String, Object> _params) {
// TODO: make IDEA like
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(ToolTip.BACKGROUND_COLOR);
panel.setBorder(new EmptyBorder(0, 4, 0, 4));
Iterable<T> methods = parametersInformation.getMethods(node, ((EditorContext) MapSequence.fromMap(_params).get("editorContext")));
int lineNumber = 0;
for (T method : Sequence.fromIterable(methods)) {
StyledTextPrinterImpl printer = new StyledTextPrinterImpl();
parametersInformation.getStyledMethodPresentation(node, ((EditorContext) MapSequence.fromMap(_params).get("editorContext")), method, (StyledTextPrinter) printer);
JTextPane textPane = new JTextPane(printer.getDocument());
textPane.setBorder(null);
textPane.setFont(EditorSettings.getInstance().getDefaultEditorFont());
textPane.setOpaque(true);
if (Sequence.fromIterable(methods).count() > 1 && parametersInformation.isMethodCurrent(node, ((EditorContext) MapSequence.fromMap(_params).get("editorContext")), method)) {
textPane.setBackground(new JBColor(new Color(231, 254, 234), Gray._100));
} else {
textPane.setBackground(ToolTip.BACKGROUND_COLOR);
}
textPane.setForeground(JBColor.foreground());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridy = lineNumber++;
panel.add(textPane, constraints);
if (Sequence.fromIterable(methods).last() != method) {
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridy = lineNumber++;
panel.add(new Line(), constraints);
}
}
return panel;
}
}