/** * Determines whether the image is selected, and if it's the only thing selected. * * @return 0 if not selected, 1 if selected, 2 if exclusively selected. "Exclusive" selection is * only returned when editable. */ protected int getSelectionState() { int p0 = fElement.getStartOffset(); int p1 = fElement.getEndOffset(); if (fContainer instanceof JTextComponent) { JTextComponent textComp = (JTextComponent) fContainer; int start = textComp.getSelectionStart(); int end = textComp.getSelectionEnd(); if (start <= p0 && end >= p1) { if (start == p0 && end == p1 && isEditable()) return 2; else return 1; } } return 0; }
/** Select or grow image when clicked. */ public void mousePressed(MouseEvent e) { Dimension size = fComponent.getSize(); if (e.getX() >= size.width - 7 && e.getY() >= size.height - 7 && getSelectionState() == 2) { // Click in selected grow-box: if (DEBUG) System.out.println("ImageView: grow!!! Size=" + fWidth + "x" + fHeight); Point loc = fComponent.getLocationOnScreen(); fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight); fGrowProportionally = e.isShiftDown(); } else { // Else select image: fGrowBase = null; JTextComponent comp = (JTextComponent) fContainer; int start = fElement.getStartOffset(); int end = fElement.getEndOffset(); int mark = comp.getCaret().getMark(); int dot = comp.getCaret().getDot(); if (e.isShiftDown()) { // extend selection if shift key down: if (mark <= start) comp.moveCaretPosition(end); else comp.moveCaretPosition(start); } else { // just select image, without shift: if (mark != start) comp.setCaretPosition(start); if (dot != end) comp.moveCaretPosition(end); } } }
/** Returns the text editor's highlight color. */ protected Color getHighlightColor() { JTextComponent textComp = (JTextComponent) fContainer; return textComp.getSelectionColor(); }