#1 block selection

open
nobody
None
5
2014-12-26
2012-08-29
nenopera
No

fully functional block selection from your block selection branch

Despite of implement keyboard selection is completely functional (I use java 1.4 for test)

Cut, copy, paste and edit al block selection at time is working in this example (last post about these topics in your blog says they didn't at that time)

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.plaf.TextUI;
import javax.swing.plaf.basic.BasicTextUI;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
* Resaltado de paréntesis ( [ { y block selection
*/
public class BlockSelectionTest implements CaretListener {

public static void main(String a[]) {
JFrame f = new JFrame();
final JEditorPane p = new JEditorPane() {
public String getSelectedText() {
String text = null;
BlockCaret cc = (BlockCaret) getCaret();

if (cc.isBlockSelectionHighlighted()) {
StringBuffer sb = new StringBuffer();
Highlighter.Highlight[] h = getHighlighter().getHighlights();
try {
for (int i = 0; i < h.length; i++) {
int offs = h[i].getStartOffset();
int len = h[i].getEndOffset() - offs;
String line = getText(offs, len);
sb.append(line);
if (i < h.length - 1) {
sb.append('\n');
}
}
} catch (BadLocationException ble) { // Never happens
ble.printStackTrace();
}
text = sb.toString();
} else {
text = super.getSelectedText();
}

return text;
}
};
p.setDocument(new PlainDocument() {
public void remove(int offs, int len) throws BadLocationException {
BlockCaret cc = (BlockCaret) p.getCaret();

if (cc.isBlockSelectionHighlighted()) {
Highlighter.Highlight[] h = p.getHighlighter().getHighlights();
try {
for (int i = 0; i < h.length; i++) {
int offsTemp = h[i].getStartOffset();
int lenTemp = h[i].getEndOffset() - offsTemp;
super.remove(offsTemp, lenTemp);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
} else {
super.remove(offs, len);
}
}

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
BlockCaret cc = (BlockCaret) p.getCaret();

if (cc.isBlockSelectionHighlighted()) {
Highlighter.Highlight[] h = p.getHighlighter().getHighlights();
try {
for (int i = 0; i < h.length; i++) {
int offsTemp = h[i].getStartOffset();
super.insertString(offsTemp, str, a);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
} else {
super.insertString(offs, str, a);
}
}
});
// NOTE: UndoManager based on RSyntaxTextArea (not implemented here)
// TUndoManager.buildUndoManager(p);
p.setCaret(new BlockCaret(p));
p.addCaretListener(new PruebaHighLight());
f.getContentPane().add(new JScrollPane(p));
p.setFont(new Font("Monospaced", 0, 12));

f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

private static class BlockCaret extends BasicTextUI.BasicCaret {

JTextComponent component;

/**
* Used to help with block selections.
*/
private Point lastPoint = new Point();

/**
* Whether a block selection is currently taking place.
*/
private boolean isBlockSelection;

public boolean isBlockSelectionHighlighted() {
return blockSelectionHighlighted;
}

/**
* Whether a block selection exists.
*/
private boolean blockSelectionHighlighted;

/**
* The dot of the block selection. Only valid if
* {@link #isBlockSelection} is <code>true</code>.
*/
private int blockSelectionDot;

/**
* The mark of the block selection. Only valid if
* {@link #isBlockSelection} is <code>true</code> and
*/
private int blockSelectionMark;

private BlockCaret(JTextComponent component) {
this.component = component;
}

private void removeHighlights() {
if (blockSelectionHighlighted) {
getComponent().getHighlighter().removeAllHighlights();
blockSelectionHighlighted = false;
}
}

public int getDot() {
return blockSelectionDot != blockSelectionMark ? blockSelectionDot : super.getDot();
}

public int getMark() {
return blockSelectionDot != blockSelectionMark ? blockSelectionMark : super.getMark();
}

protected void customHighlight(Point start, Point end) {
blockSelectionHighlighted = true;
Highlighter h = getComponent().getHighlighter();
TextUI ui = getComponent().getUI();
h.removeAllHighlights();
int y = start.y;
int firstX = start.x;
int lastX = end.x;
int pos1 = ui.viewToModel(getComponent(), new Point(firstX, y));
int pos2 = ui.viewToModel(getComponent(), new Point(lastX, y));
try {
h.addHighlight(pos1, pos2, getSelectionPainter());
} catch (Exception ex) {
ex.printStackTrace();
}

for (int i = y + 1; i < end.y; i++) {
int pos1new = ui.viewToModel(getComponent(), new Point(firstX, i));
int pos2new = ui.viewToModel(getComponent(), new Point(lastX, i));
if (pos1 != pos1new) {
pos1 = pos1new;
pos2 = pos2new;
try {
h.addHighlight(pos1, pos2, getSelectionPainter());
} catch (Exception e) {
}
}
}
}

public void mousePressed(MouseEvent e) {
removeHighlights();
isBlockSelection = e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1 && e.isShiftDown();
blockSelectionDot = blockSelectionMark = getComponent().viewToModel(e.getPoint());
super.mousePressed(e);

}

public void mouseMoved(MouseEvent e) {
lastPoint = e.getPoint();
super.mouseMoved(e);
}

public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
isBlockSelection = false;
lastPoint.setLocation(0, 0);
}

protected void moveCaret(MouseEvent e) {
if (isBlockSelection) {
Point p = e.getPoint();
int pos = getComponent().getUI().viewToModel(getComponent(), p);
blockSelectionMark = getComponent().viewToModel(p);
if (pos >= 0) {
setDot(pos);
Point start = new Point(Math.min(lastPoint.x, p.x), Math.min(lastPoint.y, p.y));
Point end = new Point(Math.max(lastPoint.x, p.x), Math.max(lastPoint.y, p.y));
customHighlight(start, end);
}
} else {
removeHighlights();
}
super.moveCaret(e);
}
}
}

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks