/** 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) CampaignData.mwlog.infoLog("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); } } }
public void letterOrDigitTyped() { table.setAllowEditing(true); table.repaint(); // G.Sturr 2009-10-10: cleanup when keypress edit begins // check if cell fixed Object o = model.getValueAt(table.getSelectedRow(), table.getSelectedColumn()); if (o != null && o instanceof GeoElement) { GeoElement geo = (GeoElement) o; if (geo.isFixed()) return; } model.setValueAt(null, table.getSelectedRow(), table.getSelectedColumn()); table.editCellAt(table.getSelectedRow(), table.getSelectedColumn()); // workaround, see // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4192625 final JTextComponent f = (JTextComponent) table.getEditorComponent(); f.requestFocus(); f.getCaret().setVisible(true); // workaround for Mac OS X 10.5 problem (first character typed deleted) if (Application.MAC_OS) SwingUtilities.invokeLater( new Runnable() { public void run() { f.setSelectionStart(1); f.setSelectionEnd(1); } }); table.setAllowEditing(false); }
static void updateStyle(JTextComponent comp, SynthContext context, String prefix) { SynthStyle style = context.getStyle(); Color color = comp.getCaretColor(); if (color == null || color instanceof UIResource) { comp.setCaretColor((Color) style.get(context, prefix + ".caretForeground")); } Color fg = comp.getForeground(); if (fg == null || fg instanceof UIResource) { fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND); if (fg != null) { comp.setForeground(fg); } } Object ar = style.get(context, prefix + ".caretAspectRatio"); if (ar instanceof Number) { comp.putClientProperty("caretAspectRatio", ar); } context.setComponentState(SELECTED | FOCUSED); Color s = comp.getSelectionColor(); if (s == null || s instanceof UIResource) { comp.setSelectionColor(style.getColor(context, ColorType.TEXT_BACKGROUND)); } Color sfg = comp.getSelectedTextColor(); if (sfg == null || sfg instanceof UIResource) { comp.setSelectedTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND)); } context.setComponentState(DISABLED); Color dfg = comp.getDisabledTextColor(); if (dfg == null || dfg instanceof UIResource) { comp.setDisabledTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND)); } Insets margin = comp.getMargin(); if (margin == null || margin instanceof UIResource) { margin = (Insets) style.get(context, prefix + ".margin"); if (margin == null) { // Some places assume margins are non-null. margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; } comp.setMargin(margin); } Caret caret = comp.getCaret(); if (caret instanceof UIResource) { Object o = style.get(context, prefix + ".caretBlinkRate"); if (o != null && o instanceof Integer) { Integer rate = (Integer) o; caret.setBlinkRate(rate.intValue()); } } }
/** Creates new instance initializing final fields. */ public AnnotationBar(JTextComponent target) { this.textComponent = target; this.editorUI = Utilities.getEditorUI(target); this.foldHierarchy = FoldHierarchy.get(editorUI.getComponent()); this.doc = editorUI.getDocument(); this.caret = textComponent.getCaret(); setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); elementAnnotationsSubstitute = ""; // NOI18N }
public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { boolean active; if (mark != null) { active = mark.activateLayer; } else { JTextComponent c = ctx.getEditorUI().getComponent(); active = (c != null) && c.getCaret().isSelectionVisible() && ctx.getFragmentOffset() >= c.getSelectionStart() && ctx.getFragmentOffset() < c.getSelectionEnd(); } return active; }
public static Point getLocationForCaret(JTextComponent pathTextField) { Point point; int position = pathTextField.getCaretPosition(); try { final Rectangle rec = pathTextField.modelToView(position); point = new Point((int) rec.getMaxX(), (int) rec.getMaxY()); } catch (BadLocationException e) { point = pathTextField.getCaret().getMagicCaretPosition(); } SwingUtilities.convertPointToScreen(point, pathTextField); point.y += 2; return point; }
/** * Perform the goto operation. * * @return whether the dialog should be made invisible or not */ protected boolean performGoto() { JTextComponent c = EditorRegistry.lastFocusedComponent(); if (c != null) { try { int line = Integer.parseInt(getGotoValueText()); // issue 188976 if (line == 0) line = 1; // end of issue 188976 BaseDocument doc = Utilities.getDocument(c); if (doc != null) { int rowCount = Utilities.getRowCount(doc); if (line > rowCount) line = rowCount; // Obtain the offset where to jump int pos = Utilities.getRowStartFromLineOffset(doc, line - 1); BaseKit kit = Utilities.getKit(c); if (kit != null) { Action a = kit.getActionByName(ExtKit.gotoAction); if (a instanceof ExtKit.GotoAction) { pos = ((ExtKit.GotoAction) a).getOffsetFromLine(doc, line - 1); } } if (pos != -1) { Caret caret = c.getCaret(); caret.setDot(pos); } else { c.getToolkit().beep(); return false; } } } catch (NumberFormatException e) { c.getToolkit().beep(); return false; } } return true; }
protected boolean changed(JComponent comp, String str, Position.Bias bias) { JTextComponent textComp = (JTextComponent) comp; int offset = bias == Position.Bias.Forward ? textComp.getCaretPosition() : textComp.getCaret().getMark() - 1; int index = getNextMatch(textComp, str, offset, bias); if (index != -1) { textComp.select(index, index + str.length()); return true; } else { offset = bias == null || bias == Position.Bias.Forward ? 0 : textComp.getDocument().getLength(); index = getNextMatch(textComp, str, offset, bias); if (index != -1) { textComp.select(index, index + str.length()); return true; } else return false; } }
public void actionPerformed(JTextComponent text) { indentationLogic = ((EditorPane) text).getIndentationLogic(); StyledDocument doc = (StyledDocument) text.getDocument(); Element map = doc.getDefaultRootElement(); Caret c = text.getCaret(); int dot = c.getDot(); int mark = c.getMark(); int line1 = map.getElementIndex(dot); if (dot != mark) { int line2 = map.getElementIndex(mark); int begin = Math.min(line1, line2); int end = Math.max(line1, line2); Element elem; try { for (line1 = begin; line1 < end; line1++) { elem = map.getElement(line1); handleDecreaseIndent(line1, elem, doc); } elem = map.getElement(end); int start = elem.getStartOffset(); if (Math.max(c.getDot(), c.getMark()) != start) { handleDecreaseIndent(end, elem, doc); } } catch (BadLocationException ble) { Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage()); UIManager.getLookAndFeel().provideErrorFeedback(text); } } else { Element elem = map.getElement(line1); try { handleDecreaseIndent(line1, elem, doc); } catch (BadLocationException ble) { Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage()); UIManager.getLookAndFeel().provideErrorFeedback(text); } } }
/** Build the interface. */ private void buildUI() { if (colorEnabled) { JTextPane text = new JTextPane(); this.textComponent = text; } else { JTextArea text = new JTextArea(); this.textComponent = text; text.setLineWrap(true); } textComponent.addMouseListener(this); textComponent.setFont(getMonospaceFont()); textComponent.setEditable(false); DefaultCaret caret = (DefaultCaret) textComponent.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); document = textComponent.getDocument(); document.addDocumentListener(new LimitLinesDocumentListener(numLines, true)); JScrollPane scrollText = new JScrollPane(textComponent); scrollText.setBorder(null); scrollText.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); add(scrollText, BorderLayout.CENTER); }
public void actionPerformed(JTextComponent text) { indentationLogic = ((EditorPane) text).getIndentationLogic(); boolean indentError = false; Document doc = text.getDocument(); Element map = doc.getDefaultRootElement(); String tabWhitespace = PreferencesUser.getInstance().getTabWhitespace(); Caret c = text.getCaret(); int dot = c.getDot(); int mark = c.getMark(); int dotLine = map.getElementIndex(dot); int markLine = map.getElementIndex(mark); if (dotLine != markLine) { int first = Math.min(dotLine, markLine); int last = Math.max(dotLine, markLine); Element elem; int start; try { for (int i = first; i < last; i++) { elem = map.getElement(i); start = elem.getStartOffset(); doc.insertString(start, tabWhitespace, null); } elem = map.getElement(last); start = elem.getStartOffset(); if (Math.max(c.getDot(), c.getMark()) != start) { doc.insertString(start, tabWhitespace, null); } } catch (BadLocationException ble) { Debug.error(me + "Problem while indenting line\n%s", ble.getMessage()); UIManager.getLookAndFeel().provideErrorFeedback(text); } } else { text.replaceSelection(tabWhitespace); } }
@Override public void actionPerformed(ActionEvent e) { JTextComponent textArea = (JTextComponent) e.getSource(); Caret caret = textArea.getCaret(); int dot = caret.getDot(); /* * Move to the beginning/end of selection on a "non-shifted" * left- or right-keypress. We shouldn't have to worry about * navigation filters as, if one is being used, it let us get * to that position before. */ if (!select) { switch (direction) { case SwingConstants.EAST: int mark = caret.getMark(); if (dot != mark) { caret.setDot(Math.max(dot, mark)); return; } break; case SwingConstants.WEST: mark = caret.getMark(); if (dot != mark) { caret.setDot(Math.min(dot, mark)); return; } break; default: } } Position.Bias[] bias = new Position.Bias[1]; Point magicPosition = caret.getMagicCaretPosition(); try { if (magicPosition == null && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) { Rectangle r = textArea.modelToView(dot); magicPosition = new Point(r.x, r.y); } NavigationFilter filter = textArea.getNavigationFilter(); if (filter != null) { dot = filter.getNextVisualPositionFrom( textArea, dot, Position.Bias.Forward, direction, bias); } else { if (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH) { dot = getNSVisualPosition((EditorPane) textArea, dot, direction); } else { dot = textArea .getUI() .getNextVisualPositionFrom( textArea, dot, Position.Bias.Forward, direction, bias); } } if (select) { caret.moveDot(dot); } else { caret.setDot(dot); } if (magicPosition != null && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) { caret.setMagicCaretPosition(magicPosition); } } catch (BadLocationException ble) { Debug.error(me + "Problem while trying to move caret\n%s", ble.getMessage()); } }
public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); // Application.debug(keyCode+""); // boolean shiftDown = e.isShiftDown(); boolean altDown = e.isAltDown(); boolean ctrlDown = Application.isControlDown(e) // Windows ctrl/Mac Meta || e.isControlDown(); // Fudge (Mac ctrl key) int row = table.getSelectedRow(); int column = table.getSelectedColumn(); switch (keyCode) { case KeyEvent.VK_UP: if (Application.isControlDown(e)) { if (model.getValueAt(row, column) != null) { // move to top of current "block" // if shift pressed, select cells too while (row > 0 && model.getValueAt(row - 1, column) != null) row--; table.changeSelection(row, column, false, e.isShiftDown()); } else { // move up to next defined cell while (row > 0 && model.getValueAt(row - 1, column) == null) row--; table.changeSelection(Math.max(0, row - 1), column, false, false); } e.consume(); } // copy description into input bar when a cell is entered // GeoElement geo = (GeoElement) getModel().getValueAt(table.getSelectedRow() - 1, // table.getSelectedColumn()); // if (geo != null) { // AlgebraInput ai = (AlgebraInput)(app.getGuiManager().getAlgebraInput()); // ai.setString(geo); // } break; case KeyEvent.VK_LEFT: if (Application.isControlDown(e)) { if (model.getValueAt(row, column) != null) { // move to left of current "block" // if shift pressed, select cells too while (column > 0 && model.getValueAt(row, column - 1) != null) column--; table.changeSelection(row, column, false, e.isShiftDown()); } else { // move left to next defined cell while (column > 0 && model.getValueAt(row, column - 1) == null) column--; table.changeSelection(row, Math.max(0, column - 1), false, false); } e.consume(); } // // copy description into input bar when a cell is entered // geo = (GeoElement) getModel().getValueAt(table.getSelectedRow(), // table.getSelectedColumn() - 1); // if (geo != null) { // AlgebraInput ai = (AlgebraInput)(app.getGuiManager().getAlgebraInput()); // ai.setString(geo); // } break; case KeyEvent.VK_DOWN: // auto increase spreadsheet size when you go off the bottom if (table.getSelectedRow() + 1 == table.getRowCount() && table.getSelectedRow() < Kernel.MAX_SPREADSHEET_ROWS) { model.setRowCount(table.getRowCount() + 1); // getView().getRowHeader().revalidate(); //G.STURR 2010-1-9 } else if (Application.isControlDown(e)) { if (model.getValueAt(row, column) != null) { // move to bottom of current "block" // if shift pressed, select cells too while (row < table.getRowCount() - 1 && model.getValueAt(row + 1, column) != null) row++; table.changeSelection(row, column, false, e.isShiftDown()); } else { // move down to next selected cell while (row < table.getRowCount() - 1 && model.getValueAt(row + 1, column) == null) row++; table.changeSelection(Math.min(table.getRowCount() - 1, row + 1), column, false, false); } e.consume(); } // // copy description into input bar when a cell is entered // geo = (GeoElement) getModel().getValueAt(table.getSelectedRow()+1, // table.getSelectedColumn()); // if (geo != null) { // AlgebraInput ai = (AlgebraInput)(app.getGuiManager().getAlgebraInput()); // ai.setString(geo); // } break; case KeyEvent.VK_HOME: // if shift pressed, select cells too if (Application.isControlDown(e)) { // move to top left of spreadsheet table.changeSelection(0, 0, false, e.isShiftDown()); } else { // move to left of current row table.changeSelection(row, 0, false, e.isShiftDown()); } e.consume(); break; case KeyEvent.VK_END: // move to bottom right of spreadsheet // if shift pressed, select cells too // find rectangle that will contain all cells for (int c = 0; c < table.getColumnCount(); c++) for (int r = 0; r < table.getRowCount(); r++) if ((r > row || c > column) && model.getValueAt(r, c) != null) { if (r > row) row = r; if (c > column) column = c; } table.changeSelection(row, column, false, e.isShiftDown()); e.consume(); break; case KeyEvent.VK_RIGHT: // auto increase spreadsheet size when you go off the right if (table.getSelectedColumn() + 1 == table.getColumnCount() && table.getSelectedColumn() < Kernel.MAX_SPREADSHEET_COLUMNS) { table.setMyColumnCount(table.getColumnCount() + 1); view.getColumnHeader().revalidate(); // these two lines are a workaround for Java 6 // (Java bug?) table.changeSelection(row, column + 1, false, false); e.consume(); } else if (Application.isControlDown(e)) { if (model.getValueAt(row, column) != null) { // move to bottom of current "block" // if shift pressed, select cells too while (column < table.getColumnCount() - 1 && model.getValueAt(row, column + 1) != null) column++; table.changeSelection(row, column, false, e.isShiftDown()); } else { // move right to next defined cell while (column < table.getColumnCount() - 1 && model.getValueAt(row, column + 1) == null) column++; table.changeSelection( row, Math.min(table.getColumnCount() - 1, column + 1), false, false); } e.consume(); } // // copy description into input bar when a cell is entered // geo = (GeoElement) getModel().getValueAt(table.getSelectedRow(), // table.getSelectedColumn() + 1); // if (geo != null) { // AlgebraInput ai = (AlgebraInput)(app.getGuiManager().getAlgebraInput()); // ai.setString(geo); // } break; case KeyEvent.VK_SHIFT: case KeyEvent.VK_CONTROL: case KeyEvent.VK_ALT: case KeyEvent.VK_META: // MAC_OS Meta e.consume(); // stops editing start break; case KeyEvent.VK_F9: kernel.updateConstruction(); e.consume(); // stops editing start break; case KeyEvent.VK_R: if (Application.isControlDown(e)) { kernel.updateConstruction(); e.consume(); } else letterOrDigitTyped(); break; // needs to be here to stop keypress starting a cell edit after the undo case KeyEvent.VK_Z: // undo if (ctrlDown) { // Application.debug("undo"); app.getGuiManager().undo(); e.consume(); } else letterOrDigitTyped(); break; // needs to be here to stop keypress starting a cell edit after the redo case KeyEvent.VK_Y: // redo if (ctrlDown) { // Application.debug("redo"); app.getGuiManager().redo(); e.consume(); } else letterOrDigitTyped(); break; case KeyEvent.VK_C: case KeyEvent.VK_V: case KeyEvent.VK_X: case KeyEvent.VK_DELETE: case KeyEvent.VK_BACK_SPACE: if (!editor.isEditing()) { if (Character.isLetterOrDigit(e.getKeyChar()) && !editor.isEditing() && !(ctrlDown || e.isAltDown())) { letterOrDigitTyped(); } else if (ctrlDown) { e.consume(); if (keyCode == KeyEvent.VK_C) { table.copy(altDown); } else if (keyCode == KeyEvent.VK_V) { boolean storeUndo = table.paste(); view.getRowHeader().revalidate(); if (storeUndo) app.storeUndoInfo(); } else if (keyCode == KeyEvent.VK_X) { boolean storeUndo = table.cut(); if (storeUndo) app.storeUndoInfo(); } } if (keyCode == KeyEvent.VK_DELETE || keyCode == KeyEvent.VK_BACK_SPACE) { e.consume(); // Application.debug("deleting..."); boolean storeUndo = table.delete(); if (storeUndo) app.storeUndoInfo(); } return; } break; // case KeyEvent.VK_ENTER: case KeyEvent.VK_F2: if (!editor.isEditing()) { table.setAllowEditing(true); table.editCellAt(table.getSelectedRow(), table.getSelectedColumn()); final JTextComponent f = (JTextComponent) table.getEditorComponent(); f.requestFocus(); f.getCaret().setVisible(true); table.setAllowEditing(false); } e.consume(); break; case KeyEvent.VK_ENTER: if (MyCellEditor.tabReturnCol > -1) { table.changeSelection(row, MyCellEditor.tabReturnCol, false, false); MyCellEditor.tabReturnCol = -1; } // fall through case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_PAGE_UP: // stop cell being erased before moving break; // stop TAB erasing cell before moving case KeyEvent.VK_TAB: // disable shift-tab in column A if (table.getSelectedColumn() == 0 && e.isShiftDown()) e.consume(); break; case KeyEvent.VK_A: if (Application.isControlDown(e)) { // select all cells row = 0; column = 0; // find rectangle that will contain all defined cells for (int c = 0; c < table.getColumnCount(); c++) for (int r = 0; r < table.getRowCount(); r++) if ((r > row || c > column) && model.getValueAt(r, c) != null) { if (r > row) row = r; if (c > column) column = c; } table.changeSelection(0, 0, false, false); table.changeSelection(row, column, false, true); e.consume(); } // no break, fall through default: if (!Character.isIdentifierIgnorable(e.getKeyChar()) && !editor.isEditing() && !(ctrlDown || e.isAltDown())) { letterOrDigitTyped(); } else e.consume(); break; } /* if (keyCode >= 37 && keyCode <= 40) { if (editor.isEditing()) return; } for (int i = 0; i < defaultKeyListeners.length; ++ i) { if (e.isConsumed()) break; defaultKeyListeners[i].keyPressed(e); } */ }
@Override public void actionPerformed(ActionEvent e, JTextComponent target) { Caret caret = target.getCaret(); int prev = getPreviousWordPosition(target.getDocument(), caret.getDot()); caret.setDot(prev); }