Java Code Examples for javax.swing.text.JTextComponent#requestFocusInWindow()

The following examples show how to use javax.swing.text.JTextComponent#requestFocusInWindow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionMappings.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void replacePattern(String pattern, JTextComponent area, String replace, boolean select) {
    String props = area.getText();
    Matcher match = Pattern.compile(pattern, Pattern.DOTALL).matcher(props);
    if (match.matches()) {
        int begin = props.indexOf(TestChecker.PROP_SKIP_TEST);
        props = props.replace(TestChecker.PROP_SKIP_TEST + match.group(1), replace); //NOI18N
        area.setText(props);
        if (select) {
            area.setSelectionStart(begin);
            area.setSelectionEnd(begin + replace.length());
            area.requestFocusInWindow();
        }
    } else {
        String sep = "\n";//NOI18N
        if (props.endsWith("\n") || props.trim().length() == 0) {//NOI18N
            sep = "";//NOI18N
        }
        props = props + sep + replace; //NOI18N
        area.setText(props);
        if (select) {
            area.setSelectionStart(props.length() - replace.length());
            area.setSelectionEnd(props.length());
            area.requestFocusInWindow();
        }
    }

}
 
Example 2
Source File: DocumentEditor.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt) {
  if(searchDialog == null) {
    Window parent = SwingUtilities.getWindowAncestor(DocumentEditor.this);
    searchDialog =
        (parent instanceof Dialog)
            ? new SearchDialog((Dialog)parent)
            : new SearchDialog((Frame)parent);
    searchDialog.pack();
    searchDialog.setLocationRelativeTo(DocumentEditor.this);
    searchDialog.setResizable(true);
    MainFrame.getGuiRoots().add(searchDialog);
  }
  JTextComponent textPane = getTextComponent();
  // if the user never gives the focus to the textPane then
  // there will never be any selection in it so we force it
  textPane.requestFocusInWindow();
  // put the selection of the document into the search text field
  if(textPane.getSelectedText() != null) {
    searchDialog.patternTextField.setText(textPane.getSelectedText());
  }
  if(searchDialog.isVisible()) {
    searchDialog.toFront();
  } else {
    searchDialog.setVisible(true);
  }
  searchDialog.patternTextField.selectAll();
  searchDialog.patternTextField.requestFocusInWindow();
}
 
Example 3
Source File: SwingUtils.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(@Nonnull final ActionEvent e) {
  final JTextComponent component = getFocusedComponent();
  if (component != null) {
    component.selectAll();
    component.requestFocusInWindow();
  }
}
 
Example 4
Source File: SeaGlassTextFieldUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * DOCUMENT ME!
 */
private void doPopup() {
    if (findPopup != null) {
        JTextComponent c = getComponent();

        findPopup.pack();
        // The "-1" just snugs us up a bit under the text field.
        findPopup.show(c, 0, c.getHeight() - 1);

        // Set focus back to the text field.
        // TODO Fix caret positioning, selection, etc.
        c.requestFocusInWindow();
    }
}
 
Example 5
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void show(Component c, int x, int y) {
  System.out.println(c.getClass().getName() + ": " + c.getName());
  if (c instanceof JTextComponent) {
    JTextComponent tc = (JTextComponent) c;
    tc.requestFocusInWindow();
    boolean hasSelectedText = Objects.nonNull(tc.getSelectedText());
    if (tc instanceof JTextField && !tc.isFocusOwner() && !hasSelectedText) {
      tc.selectAll();
      hasSelectedText = true;
    }
    cutAction.setEnabled(hasSelectedText);
    copyAction.setEnabled(hasSelectedText);
    super.show(c, x, y);
  }
}
 
Example 6
Source File: mxCellEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void startEditing(Object cell, EventObject evt) {
  if (editingCell != null) {
    stopEditing(true);
  }

  mxCellState state = graphComponent.getGraph().getView().getState(cell);

  if (state != null) {
    editingCell = cell;
    trigger = evt;

    double scale = Math.max(minimumEditorScale, graphComponent.getGraph().getView().getScale());
    scrollPane.setBounds(getEditorBounds(state, scale));
    scrollPane.setVisible(true);

    String value = getInitialValue(state, evt);
    JTextComponent currentEditor = null;

    // Configures the style of the in-place editor
    textArea.setFont(mxUtils.getFont(state.getStyle(), scale));
    Color fontColor = mxUtils.getColor(state.getStyle(), mxConstants.STYLE_FONTCOLOR, Color.black);
    textArea.setForeground(fontColor);
    textArea.setText(value);

    scrollPane.setViewportView(textArea);
    currentEditor = textArea;

    graphComponent.getGraphControl().add(scrollPane, 0);

    if (isHideLabel(state)) {
      graphComponent.redraw(state);
    }

    currentEditor.revalidate();
    currentEditor.requestFocusInWindow();
    currentEditor.selectAll();

    configureActionMaps();
  }
}
 
Example 7
Source File: mxCellEditor.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
public void startEditing(Object cell, EventObject evt)
{
	if (editingCell != null)
	{
		stopEditing(true);
	}

	mxCellState state = graphComponent.getGraph().getView().getState(cell);

	if (state != null)
	{
		editingCell = cell;
		trigger = evt;

		double scale = Math.max(minimumEditorScale, graphComponent
				.getGraph().getView().getScale());
		scrollPane.setBounds(getEditorBounds(state, scale));
		scrollPane.setVisible(true);

		String value = getInitialValue(state, evt);
		JTextComponent currentEditor = null;

		// Configures the style of the in-place editor
		if (graphComponent.getGraph().isHtmlLabel(cell))
		{
			if (isExtractHtmlBody())
			{
				value = mxUtils.getBodyMarkup(value,
						isReplaceHtmlLinefeeds());
			}

			editorPane.setDocument(mxUtils.createHtmlDocumentObject(
					state.getStyle(), scale));
			editorPane.setText(value);

			// Workaround for wordwrapping in editor pane
			// FIXME: Cursor not visible at end of line
			JPanel wrapper = new JPanel(new BorderLayout());
			wrapper.setOpaque(false);
			wrapper.add(editorPane, BorderLayout.CENTER);
			scrollPane.setViewportView(wrapper);

			currentEditor = editorPane;
		}
		else
		{
			textArea.setFont(mxUtils.getFont(state.getStyle(), scale));
			Color fontColor = mxUtils.getColor(state.getStyle(),
					mxConstants.STYLE_FONTCOLOR, Color.black);
			textArea.setForeground(fontColor);
			textArea.setText(value);

			scrollPane.setViewportView(textArea);
			currentEditor = textArea;
		}

		graphComponent.getGraphControl().add(scrollPane, 0);

		if (isHideLabel(state))
		{
			graphComponent.redraw(state);
		}

		currentEditor.revalidate();
		currentEditor.requestFocusInWindow();
		currentEditor.selectAll();

		configureActionMaps();
	}
}
 
Example 8
Source File: NavigationHistoryBackAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static void show(NavigationHistory.Waypoint wpt) {
    final int offset = wpt.getOffset();
    if (offset < 0) {
        return;
    }
    
    Lookup lookup = findLookupFor(wpt);
    if (lookup != null) {
        final EditorCookie editorCookie = lookup.lookup(EditorCookie.class);
        final LineCookie lineCookie = lookup.lookup(LineCookie.class);
        Document doc = null;

        if (editorCookie != null && lineCookie != null) {
            try {
                doc = editorCookie.openDocument();
            } catch (IOException ioe) {
                LOG.log(Level.WARNING, "Can't open document", ioe); //NOI18N
            }
        }

        if (doc instanceof BaseDocument) {
            final BaseDocument baseDoc = (BaseDocument) doc;
            final Line[] line = new Line[1];
            final int column[] = new int[1];
            baseDoc.render(new Runnable() {
                @Override
                public void run() {
                    Element lineRoot = baseDoc.getParagraphElement(0).getParentElement();
                    int lineIndex = lineRoot.getElementIndex(offset);

                    if (lineIndex != -1) {
                        Element lineElement = lineRoot.getElement(lineIndex);
                        column[0] = offset - lineElement.getStartOffset();
                        line[0] = lineCookie.getLineSet().getCurrent(lineIndex);
                    }
                }
            });
            // Line.show() must NOT be called under doc.writeLock().
            // By possible thread's waiting in CloneableEditor.getEditorPane()
            // an asynchronous editor pane opening would be blocked
            // by the write-lock.
            // In case the current unlocked Line.show() solution would be found
            // unsatisfactory then issue #232175 should be reopened.
            if (line[0] != null) {
                line[0].show(ShowOpenType.REUSE, ShowVisibilityType.FOCUS, column[0]);
                return;
            }
        }
    }
    
    // lookup didn't work try simple navigation in the text component
    JTextComponent component = wpt.getComponent();
    if (component != null && component.getCaret() != null) {
        component.setCaretPosition(offset);
        component.requestFocusInWindow();
    }
}