correct copy action for x debug frames list

This commit is contained in:
Vladimir Krivosheev
2013-02-12 20:43:25 +04:00
parent 0adbce11d5
commit cb37330146
2 changed files with 105 additions and 1 deletions
@@ -22,8 +22,11 @@ import com.intellij.ui.SimpleTextAttributes;
import com.intellij.xdebugger.XDebuggerBundle;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
/**
* Represents a frame of execution stack. The selected frame is shown in 'Variables' panel of 'Debug' tool window.
* Override {@link XValueContainer#computeChildren} to show local variable, parameters, fields available in the frame
@@ -61,9 +64,18 @@ public abstract class XStackFrame extends XValueContainer {
/**
* Customize presentation of the stack frame in frames list
*
* @param component component
*/
public void customizePresentation(final SimpleColoredComponent component) {
public void customizePresentation(SimpleColoredComponent component) {
customizePresentation(new ColoredTextContainerComponent(component));
}
/**
* Customize presentation of the stack frame in frames list
* @param component component
*/
public void customizePresentation(ColoredTextContainer component) {
XSourcePosition position = getSourcePosition();
if (position != null) {
//FileColorManager.getInstance()
@@ -75,4 +87,27 @@ public abstract class XStackFrame extends XValueContainer {
component.append(XDebuggerBundle.message("invalid.frame"), SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
public interface ColoredTextContainer {
void append(@NotNull final String fragment, @NotNull final SimpleTextAttributes attributes);
void setIcon(@Nullable final Icon icon);
}
static class ColoredTextContainerComponent implements ColoredTextContainer {
private final SimpleColoredComponent component;
ColoredTextContainerComponent(SimpleColoredComponent component) {
this.component = component;
}
@Override
public void append(@NotNull String fragment, @NotNull SimpleTextAttributes attributes) {
component.append(fragment, attributes);
}
@Override
public void setIcon(@Nullable Icon icon) {
component.setIcon(icon);
}
}
}
@@ -22,18 +22,24 @@ import com.intellij.psi.PsiManager;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.FileColorManager;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ui.TextTransferrable;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.XDebuggerBundle;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.frame.XStackFrame;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Transferable;
/**
* @author nik
*/
public class XDebuggerFramesList extends DebuggerFramesList {
private static final TransferHandler defaultTransferHandler = new MyListTransferHandler();
private XStackFrame mySelectedFrame;
public XDebuggerFramesList(Project project) {
@@ -106,4 +112,67 @@ public class XDebuggerFramesList extends DebuggerFramesList {
stackFrame.customizePresentation(this);
}
}
@Override
public TransferHandler getTransferHandler() {
return defaultTransferHandler;
}
private static class MyColoredTextContainer implements XStackFrame.ColoredTextContainer {
private final StringBuilder builder = new StringBuilder();
@Override
public void append(@NotNull String fragment, @NotNull SimpleTextAttributes attributes) {
builder.append(fragment);
}
@Override
public void setIcon(@Nullable Icon icon) {
}
}
private static class MyListTransferHandler extends TransferHandler {
protected Transferable createTransferable(JComponent c) {
if (!(c instanceof XDebuggerFramesList)) {
return null;
}
XDebuggerFramesList list = (XDebuggerFramesList)c;
Object[] values = list.getSelectedValues();
if (values == null || values.length == 0) {
return null;
}
StringBuilder plainBuf = new StringBuilder();
StringBuilder htmlBuf = new StringBuilder();
MyColoredTextContainer coloredTextContainer = new MyColoredTextContainer();
htmlBuf.append("<html>\n<body>\n<ul>\n");
for (Object value : values) {
htmlBuf.append(" <li>");
if (value != null) {
if (value instanceof XStackFrame) {
((XStackFrame)value).customizePresentation(coloredTextContainer);
plainBuf.append(coloredTextContainer.builder);
htmlBuf.append(coloredTextContainer.builder);
coloredTextContainer.builder.setLength(0);
}
else {
String text = value.toString();
plainBuf.append(text);
htmlBuf.append(text);
}
}
plainBuf.append('\n');
htmlBuf.append("</li>\n");
}
// remove the last newline
plainBuf.setLength(plainBuf.length() - 1);
htmlBuf.append("</ul>\n</body>\n</html>");
return new TextTransferrable(htmlBuf.toString(), plainBuf.toString());
}
public int getSourceActions(JComponent c) {
return COPY;
}
}
}