List of usage examples for javax.swing.text JTextComponent getCaretPosition
@Transient public int getCaretPosition()
From source file:org.executequery.gui.text.TextUtilities.java
public static void deleteWord(JTextComponent textComponent) { char space = ' '; String _space = " "; int caretIndex = textComponent.getCaretPosition(); String text = textComponent.getText(); StringBuilder sb = new StringBuilder(text); int startOfWordIndex = -1; int endOfWordIndex = sb.indexOf(_space, caretIndex); char[] textChars = text.toCharArray(); for (int i = 0; i < textChars.length; i++) { if (i >= caretIndex) { break; }//w w w .j a v a2 s. co m else { if (textChars[i] == space) startOfWordIndex = i; } } if (endOfWordIndex == -1) return; else if (startOfWordIndex == 0 && endOfWordIndex == 0) return; else if (startOfWordIndex == endOfWordIndex) return; sb.delete(startOfWordIndex + 1, endOfWordIndex); textComponent.setText(sb.toString()); textComponent.setCaretPosition(startOfWordIndex + 1); }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertFromFile(JTextComponent textComponent) { StringBuffer buf = null;/*w w w . j a va 2 s .c o m*/ String text = null; FileChooserDialog fileChooser = new FileChooserDialog(); fileChooser.setDialogTitle("Insert from file"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); int result = fileChooser.showDialog(GUIUtilities.getInFocusDialogOrWindow(), "Insert"); if (result == JFileChooser.CANCEL_OPTION) return; File file = fileChooser.getSelectedFile(); try { FileInputStream input = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); buf = new StringBuffer(10000); char newLine = '\n'; while ((text = reader.readLine()) != null) buf.append(text).append(newLine); reader.close(); reader = null; input.close(); input = null; int index = textComponent.getCaretPosition(); StringBuffer sb = new StringBuffer(textComponent.getText()); sb.insert(index, buf.toString()); textComponent.setText(sb.toString()); textComponent.setCaretPosition(index + buf.length()); } catch (OutOfMemoryError e) { buf = null; text = null; System.gc(); GUIUtilities.displayErrorMessage("Out of Memory.\nThe file is " + "too large to\nopen for viewing."); } catch (IOException e) { e.printStackTrace(); StringBuffer sb = new StringBuffer(); sb.append("An error occurred opening the selected file.").append("\n\nThe system returned:\n") .append(e.getMessage()); GUIUtilities.displayExceptionErrorDialog(sb.toString(), e); } }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertLineAfter(JTextComponent textComponent) { String newLine = "\n"; int caretIndex = textComponent.getCaretPosition(); StringBuilder sb = new StringBuilder(textComponent.getText()); int endOfLineIndex = sb.indexOf(newLine, caretIndex); int length = sb.length(); if (caretIndex == length || endOfLineIndex == length) sb.append(newLine);/*w w w . jav a 2 s .c o m*/ else sb.insert(endOfLineIndex == -1 ? 0 : endOfLineIndex, newLine); textComponent.setText(sb.toString()); textComponent.setCaretPosition(endOfLineIndex == -1 ? length : endOfLineIndex + 1); sb = null; }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertLineBefore(JTextComponent textComponent) { int caretIndex = textComponent.getCaretPosition(); int insertIndex = -1; char newLine = '\n'; String text = textComponent.getText(); char[] textChars = text.toCharArray(); for (int i = 0; i < textChars.length; i++) { if (i > caretIndex) { break; }//from w w w . j av a2s.co m else { if (textChars[i] == newLine) insertIndex = i; } } StringBuilder sb = new StringBuilder(text); sb.insert(insertIndex == -1 ? 0 : insertIndex, newLine); textComponent.setText(sb.toString()); textComponent.setCaretPosition(insertIndex + 1); }
From source file:org.fife.ui.rtextarea.RTATextTransferHandler.java
/** * This method causes a transfer to a component from a clipboard or a * DND drop operation. The Transferable represents the data to be * imported into the component. /*from w ww .ja va2s .c o m*/ * * @param comp The component to receive the transfer. This * argument is provided to enable sharing of TransferHandlers by * multiple components. * @param t The data to import * @return <code>true</code> iff the data was inserted into the component. */ @Override public boolean importData(JComponent comp, Transferable t) { JTextComponent c = (JTextComponent) comp; withinSameComponent = c == exportComp; // if we are importing to the same component that we exported from // then don't actually do anything if the drop location is inside // the drag location and set shouldRemove to false so that exportDone // knows not to remove any data if (withinSameComponent && c.getCaretPosition() >= p0 && c.getCaretPosition() <= p1) { shouldRemove = false; return true; } boolean imported = false; DataFlavor importFlavor = getImportFlavor(t.getTransferDataFlavors(), c); if (importFlavor != null) { try { InputContext ic = c.getInputContext(); if (ic != null) ic.endComposition(); Reader r = importFlavor.getReaderForText(t); handleReaderImport(r, c); imported = true; } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } catch (BadLocationException ble) { ble.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } return imported; }
From source file:org.parosproxy.paros.view.FindDialog.java
private void find() { JTextComponent txtComp = lastInvoker; if (txtComp == null) { JFrame parent = (JFrame) (this.getParent()); Component c = parent.getMostRecentFocusOwner(); if (c instanceof JTextComponent) { txtComp = (JTextComponent) c; }// ww w . ja v a 2 s . co m } // ZAP: Check if a JTextComponent was really found. if (txtComp == null) { return; } try { String findText = txtFind.getText().toLowerCase(); String txt = txtComp.getText().toLowerCase(); int startPos = txt.indexOf(findText, txtComp.getCaretPosition()); // Enable Wrap Search if (startPos <= 0) { txtComp.setCaretPosition(0); startPos = txt.indexOf(findText, txtComp.getCaretPosition()); } int length = findText.length(); if (startPos > -1) { txtComp.select(startPos, startPos + length); txtComp.requestFocusInWindow(); txtFind.requestFocusInWindow(); } else { Toolkit.getDefaultToolkit().beep(); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }
From source file:ro.nextreports.designer.querybuilder.SQLViewPanel.java
private void initUI() { sqlEditor = new Editor() { public void afterCaretMove() { removeHighlightErrorLine();/* w w w .j a va 2s . c o m*/ } }; this.queryArea = sqlEditor.getEditorPanel().getEditorPane(); queryArea.setText(DEFAULT_QUERY); errorPainter = new javax.swing.text.Highlighter.HighlightPainter() { @Override public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) { try { Rectangle r = c.modelToView(c.getCaretPosition()); g.setColor(Color.RED.brighter().brighter()); g.fillRect(0, r.y, c.getWidth(), r.height); } catch (BadLocationException e) { // ignore } } }; ActionMap actionMap = sqlEditor.getEditorPanel().getEditorPane().getActionMap(); // create the toolbar JToolBar toolBar = new JToolBar(); toolBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); // hide buttons borders toolBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH); toolBar.setBorderPainted(false); // add cut action Action cutAction = actionMap.get(BaseEditorKit.cutAction); cutAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("cut")); cutAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.cut")); toolBar.add(cutAction); // add copy action Action copyAction = actionMap.get(BaseEditorKit.copyAction); copyAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("copy")); copyAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.copy")); toolBar.add(copyAction); // add paste action Action pasteAction = actionMap.get(BaseEditorKit.pasteAction); pasteAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("paste")); pasteAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqlviewpanel.paste")); toolBar.add(pasteAction); // add separator SwingUtil.addCustomSeparator(toolBar); // add undo action Action undoAction = actionMap.get(BaseEditorKit.undoAction); undoAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("undo")); undoAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("undo")); toolBar.add(undoAction); // add redo action Action redoAction = actionMap.get(BaseEditorKit.redoAction); redoAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("redo")); redoAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("redo")); toolBar.add(redoAction); // add separator SwingUtil.addCustomSeparator(toolBar); // add find action Action findReplaceAction = actionMap.get(BaseEditorKit.findReplaceAction); findReplaceAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("find")); findReplaceAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("sqleditor.findReplaceActionName")); toolBar.add(findReplaceAction); // add separator SwingUtil.addCustomSeparator(toolBar); // add run action runAction = new SQLRunAction(); runAction.putValue(Action.SMALL_ICON, ImageUtil.getImageIcon("run")); runAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(ShortcutsUtil.getShortcut("query.run.accelerator", "control 4"))); runAction.putValue(Action.SHORT_DESCRIPTION, I18NSupport.getString("run.query") + " (" + ShortcutsUtil.getShortcut("query.run.accelerator.display", "Ctrl 4") + ")"); // runAction is globally registered in QueryBuilderPanel ! toolBar.add(runAction); // ro.nextreports.designer.util.SwingUtil.registerButtonsForFocus(buttonsPanel); // create the table resultTable = new JXTable(); resultTable.setDefaultRenderer(Integer.class, new ToStringRenderer()); // to remove thousand separators resultTable.setDefaultRenderer(Long.class, new ToStringRenderer()); resultTable.setDefaultRenderer(Date.class, new DateRenderer()); resultTable.setDefaultRenderer(Double.class, new DoubleRenderer()); resultTable.addMouseListener(new CopyTableMouseAdapter(resultTable)); TableUtil.setRowHeader(resultTable); resultTable.setColumnControlVisible(true); // resultTable.getTableHeader().setReorderingAllowed(false); resultTable.setHorizontalScrollEnabled(true); // highlight table Highlighter alternateHighlighter = HighlighterFactory.createAlternateStriping(Color.WHITE, ColorUtil.PANEL_BACKROUND_COLOR); Highlighter nullHighlighter = new TextHighlighter(ResultSetTableModel.NULL_VALUE, Color.YELLOW.brighter()); Highlighter blobHighlighter = new TextHighlighter(ResultSetTableModel.BLOB_VALUE, Color.GRAY.brighter()); Highlighter clobHighlighter = new TextHighlighter(ResultSetTableModel.CLOB_VALUE, Color.GRAY.brighter()); resultTable.setHighlighters(alternateHighlighter, nullHighlighter, blobHighlighter, clobHighlighter); resultTable.setBackground(ColorUtil.PANEL_BACKROUND_COLOR); resultTable.setGridColor(Color.LIGHT_GRAY); resultTable.setRolloverEnabled(true); resultTable.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, Color.RED)); JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); split.setResizeWeight(0.66); split.setOneTouchExpandable(true); JPanel topPanel = new JPanel(); topPanel.setLayout(new GridBagLayout()); topPanel.add(toolBar, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); topPanel.add(sqlEditor, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new GridBagLayout()); JScrollPane scrPanel = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); statusPanel = new SQLStatusPanel(); bottomPanel.add(scrPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); bottomPanel.add(statusPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); split.setTopComponent(topPanel); split.setBottomComponent(bottomPanel); split.setDividerLocation(400); setLayout(new BorderLayout()); this.add(split, BorderLayout.CENTER); }
From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxUtil.java
/** * Return the line of text at the document's current position. * //w w w . j a va 2s . c o m * @param target * @return */ public static String getLine(JTextComponent target) { PlainDocument document = (PlainDocument) target.getDocument(); return getLineAt(document, target.getCaretPosition()); }