SWT StyledText - 如何使用 Tab 或 Shift+Tab 键缩进/取消缩进所选文本

发布于 2024-09-24 09:21:04 字数 73 浏览 0 评论 0原文

使用 Eclipse SWT StyledText 小部件,如何使用 Tab 或 Shift+Tab 键缩进/取消缩进选定的文本块?

Using the Eclipse SWT StyledText widget, how can you indent/un-indent a selected block of text with the tab or shift+tab keys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-10-01 09:21:04

这似乎可以解决问题...

protected Control createContents(Composite parent){
    ......
    //add the listeners...
    text_code_impl.addVerifyKeyListener(new VerifyKeyListener() { 
        public void verifyKey(VerifyEvent e) { 
            if (e.keyCode == SWT.TAB) {
                if ((e.stateMask & SWT.SHIFT) != 0){
                    e.doit = text_code_impl_shift_tab_pressed();
                } else {
                    e.doit = text_code_impl_tab_pressed();
                }
            }
        }
    });         
    text_code_impl.addTraverseListener(new TraverseListener() { 
        public void keyTraversed(TraverseEvent e) { 
            if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
                e.doit = false; //allows verifyKey listener to fire
            }
        } 
    });
} 

boolean text_code_impl_tab_pressed()
{
    if (text_code_impl.getSelectionText().equals("")){
        return true;
    }

    Point range = text_code_impl.getSelectionRange(); 
    int start = range.x; 
    int length = text_code_impl.getSelectionCount(); 

    String txt = text_code_impl.getText();
    while (start > 0 && txt.charAt(start-1) != '\n') { 
        start--; 
        length++; 
    } 

    int replace_length = length;
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    String sel_text = text_code_impl.getSelectionText();
    String[] lines = sel_text.split("\n");
    String new_text = "";

    for (int x=0; x < lines.length; x++){
        if (x > 0){
            new_text += '\n';
        }
        new_text += '\t';
        length++;
        new_text += lines[x];
    }

    text_code_impl.replaceTextRange(start, replace_length, new_text); 
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    return false;
}

boolean text_code_impl_shift_tab_pressed()
{
    if (text_code_impl.getSelectionText().equals("")){
        return true;
    }

    Point range = text_code_impl.getSelectionRange(); 
    int start = range.x; 
    int length = text_code_impl.getSelectionCount(); 

    String txt = text_code_impl.getText(); 
    while (start > 0 && txt.charAt(start-1) != '\n') { 
        --start; 
        ++length; 
    } 

    int replace_length = length;        
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    String sel_text = text_code_impl.getSelectionText();
    String[] lines = sel_text.split("\n");
    String new_text = "";

    for (int x=0; x < lines.length; x++){
        if (x > 0){
            new_text += '\n';
        }
        if (lines[x].charAt(0) == '\t'){
            new_text += lines[x].substring(1);
            length--;
        } else if (lines[x].startsWith(" ")){
            new_text += lines[x].substring(1);
            length--;
        } else {
            new_text += lines[x];
        }
    }   

    text_code_impl.replaceTextRange(start, replace_length, new_text); 
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    return false;
}

This seems to do the trick...

protected Control createContents(Composite parent){
    ......
    //add the listeners...
    text_code_impl.addVerifyKeyListener(new VerifyKeyListener() { 
        public void verifyKey(VerifyEvent e) { 
            if (e.keyCode == SWT.TAB) {
                if ((e.stateMask & SWT.SHIFT) != 0){
                    e.doit = text_code_impl_shift_tab_pressed();
                } else {
                    e.doit = text_code_impl_tab_pressed();
                }
            }
        }
    });         
    text_code_impl.addTraverseListener(new TraverseListener() { 
        public void keyTraversed(TraverseEvent e) { 
            if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
                e.doit = false; //allows verifyKey listener to fire
            }
        } 
    });
} 

boolean text_code_impl_tab_pressed()
{
    if (text_code_impl.getSelectionText().equals("")){
        return true;
    }

    Point range = text_code_impl.getSelectionRange(); 
    int start = range.x; 
    int length = text_code_impl.getSelectionCount(); 

    String txt = text_code_impl.getText();
    while (start > 0 && txt.charAt(start-1) != '\n') { 
        start--; 
        length++; 
    } 

    int replace_length = length;
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    String sel_text = text_code_impl.getSelectionText();
    String[] lines = sel_text.split("\n");
    String new_text = "";

    for (int x=0; x < lines.length; x++){
        if (x > 0){
            new_text += '\n';
        }
        new_text += '\t';
        length++;
        new_text += lines[x];
    }

    text_code_impl.replaceTextRange(start, replace_length, new_text); 
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    return false;
}

boolean text_code_impl_shift_tab_pressed()
{
    if (text_code_impl.getSelectionText().equals("")){
        return true;
    }

    Point range = text_code_impl.getSelectionRange(); 
    int start = range.x; 
    int length = text_code_impl.getSelectionCount(); 

    String txt = text_code_impl.getText(); 
    while (start > 0 && txt.charAt(start-1) != '\n') { 
        --start; 
        ++length; 
    } 

    int replace_length = length;        
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    String sel_text = text_code_impl.getSelectionText();
    String[] lines = sel_text.split("\n");
    String new_text = "";

    for (int x=0; x < lines.length; x++){
        if (x > 0){
            new_text += '\n';
        }
        if (lines[x].charAt(0) == '\t'){
            new_text += lines[x].substring(1);
            length--;
        } else if (lines[x].startsWith(" ")){
            new_text += lines[x].substring(1);
            length--;
        } else {
            new_text += lines[x];
        }
    }   

    text_code_impl.replaceTextRange(start, replace_length, new_text); 
    text_code_impl.setSelectionRange(start, length);
    text_code_impl.showSelection();

    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文