Java JEditorPane矩形(列)选择模式

Java JEditorPane矩形(列)选择模式,java,swing,selection,jeditorpane,Java,Swing,Selection,Jeditorpane,我想知道如何扩展JEditorPane(或任何其他swing文本编辑组件)来处理矩形(列)选择模式。这是当前文本编辑器中的一个众所周知的功能,您可以选择多行(行),从一个偏移量(列)开始,到一个偏移量(列)结束,看起来像是选择一个文本矩形,然后您键入的内容将同时覆盖每行(行)中的选择 一个想法是覆盖选择并创建假选择,方法是通过跟踪鼠标事件以矩形形式突出显示每一行,并跟踪这些信息以便在键入时使用。但是,我不知道如何覆盖选择并跟踪鼠标,也不知道如何重定向键入以影响每一行 任何形式的帮助都将不胜感激。

我想知道如何扩展JEditorPane(或任何其他swing文本编辑组件)来处理矩形(列)选择模式。这是当前文本编辑器中的一个众所周知的功能,您可以选择多行(行),从一个偏移量(列)开始,到一个偏移量(列)结束,看起来像是选择一个文本矩形,然后您键入的内容将同时覆盖每行(行)中的选择

一个想法是覆盖选择并创建假选择,方法是通过跟踪鼠标事件以矩形形式突出显示每一行,并跟踪这些信息以便在键入时使用。但是,我不知道如何覆盖选择并跟踪鼠标,也不知道如何重定向键入以影响每一行

任何形式的帮助都将不胜感激。

发现,这涉及到一个自定义的插入符号(用于处理碎片选择)和荧光笔(用于显示碎片):

类MyCaret扩展了DefaultCaret{
点lastPoint=新点(0,0);
public void mouseMoved(MouseEvent e){
超级鼠标移动(e);
lastPoint=新点(e.getX(),e.getY());
}
公共无效mouseClicked(MouseEvent e){
超级鼠标点击(e);
getComponent().getHighlighter().removeAllHighlights();
}
受保护的空移动插入符号(MouseEvent e){
点pt=新点(e.getX(),e.getY());
Position.Bias[]biasRet=新的Position.Bias[1];
int pos=getComponent().getUI().viewToModel(getComponent(),pt,biasRet);
if(biasRet[0]==null)
biasRet[0]=位置.Bias.Forward;
如果(位置>=0){
设定点(pos);
点开始=新点(Math.min(lastPoint.x,pt.x),Math.min(lastPoint.y,pt.y));
点端点=新点(Math.max(lastPoint.x,pt.x),Math.max(lastPoint.y,pt.y));
自定义突出显示(开始、结束);
}
}
受保护的空心自定义高亮显示(点起点、点终点){
getComponent().getHighlighter().removeAllHighlights();
int y=start.y;
int firstX=start.x;
int lastX=end.x;
int pos1=getComponent().getUI().viewToModel(getComponent(),新点(firstX,y));
int pos2=getComponent().getUI().viewToModel(getComponent(),新点(lastX,y));
试一试{
getComponent().getHighlighter().addHighlight(位置1,位置2,
((DefaultHighlighter)getComponent().getHighlighter()).DefaultPainter);
}
捕获(例外情况除外){
例如printStackTrace();
}
y++;

虽然(我不知道怎么做(对不起),但我听说这不是一件容易的事。好吧,你应该运行它,即使它是斯坦的;-)这是一个不错的把戏,当然,但失败了(难怪)对于键盘选择。要真正解决这个问题,我们需要一个更丰富的选择模型,它支持垂直/分段选择。这可能不太容易。(虽然从未尝试过)而且,一次在所有选定的行中键入内容也很难(使用带有一些黑客的替换选择)在不破坏默认行为的情况下,最终会出现一个奇怪的/不可用的编辑器(在过去一整天都在尝试)。即使您的伟大方向没有满足我的要求,我也会将其标记为正确答案(部分正确)以表示感谢。10x.显示一些代码,好吗?还有,有没有基于java的编辑器可以做到这一点?
class MyCaret extends DefaultCaret {

Point lastPoint=new Point(0,0);
public void mouseMoved(MouseEvent e) {
    super.mouseMoved(e);
    lastPoint=new Point(e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e) {
    super.mouseClicked(e);
    getComponent().getHighlighter().removeAllHighlights();
}

protected void moveCaret(MouseEvent e) {
    Point pt = new Point(e.getX(), e.getY());
    Position.Bias[] biasRet = new Position.Bias[1];
    int pos = getComponent().getUI().viewToModel(getComponent(), pt, biasRet);
    if(biasRet[0] == null)
        biasRet[0] = Position.Bias.Forward;
    if (pos >= 0) {
        setDot(pos);
        Point start=new Point(Math.min(lastPoint.x,pt.x),Math.min(lastPoint.y,pt.y));
        Point end=new Point(Math.max(lastPoint.x,pt.x),Math.max(lastPoint.y,pt.y));
        customHighlight(start,end);
    }
}

protected void customHighlight(Point start, Point end) {
    getComponent().getHighlighter().removeAllHighlights();
    int y=start.y;
    int firstX=start.x;
    int lastX=end.x;

    int pos1 = getComponent().getUI().viewToModel(getComponent(), new Point(firstX,y));
    int pos2 = getComponent().getUI().viewToModel(getComponent(), new Point(lastX,y));
    try {
        getComponent().getHighlighter().addHighlight(pos1,pos2,
                 ((DefaultHighlighter)getComponent().getHighlighter()).DefaultPainter);
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    y++;
    while (y<end.y) {
        int pos1new = getComponent().getUI().viewToModel(getComponent(), new Point(firstX,y));
        int pos2new = getComponent().getUI().viewToModel(getComponent(), new Point(lastX,y));
        if (pos1!=pos1new)  {
            pos1=pos1new;
            pos2=pos2new;
            try {
                getComponent().getHighlighter().addHighlight(pos1,pos2,
                         ((DefaultHighlighter)getComponent().getHighlighter()).DefaultPainter);
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        y++;
    }
}
}