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

The following examples show how to use javax.swing.text.JTextComponent#getParent() . 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: FlatTextFieldUI.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
static void paintPlaceholder( Graphics g, JTextComponent c, Color placeholderForeground ) {
	// check whether text component is empty
	if( c.getDocument().getLength() > 0 )
		return;

	// check for JComboBox
	Container parent = c.getParent();
	JComponent jc = (parent instanceof JComboBox) ? (JComboBox<?>) parent : c;

	// get placeholder text
	Object placeholder = jc.getClientProperty( FlatClientProperties.PLACEHOLDER_TEXT );
	if( !(placeholder instanceof String) )
		return;

	// compute placeholder location
	Insets insets = c.getInsets();
	FontMetrics fm = c.getFontMetrics( c.getFont() );
	int x = insets.left;
	int y = insets.top + fm.getAscent() + ((c.getHeight() - insets.top - insets.bottom - fm.getHeight()) / 2);

	// paint placeholder
	g.setColor( placeholderForeground );
	FlatUIUtils.drawString( c, g, (String) placeholder, x, y );
}
 
Example 2
Source File: DarculaTextFieldUI.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void paintBackground(Graphics g) {
  JTextComponent component = getComponent();
  if (component != null) {
    Container parent = component.getParent();
    if (parent != null && component.isOpaque()) {
      g.setColor(parent.getBackground());
      g.fillRect(0, 0, component.getWidth(), component.getHeight());
    }

    if (component.getBorder() instanceof DarculaTextBorder && !DarculaUIUtil.isTableCellEditor(component)) {
      paintDarculaBackground(g, component);
    }
    else if (component.isOpaque()) {
      super.paintBackground(g);
    }
  }
}
 
Example 3
Source File: MacIntelliJTextFieldUI.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  final Rectangle r = getDrawingRect();
  if (c.isOpaque() && parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }

  if (isSearchField(c)) {
    paintSearchField(g, c, r);
  }
  else {
    if (c.getBorder() instanceof MacIntelliJTextBorder) {
      g.setColor(c.getBackground());
      g.fillRect(JBUI.scale(3), JBUI.scale(3), c.getWidth() - JBUI.scale(6), c.getHeight() - JBUI.scale(6));
    }
    else {
      super.paintBackground(g);
    }
  }
}
 
Example 4
Source File: HintsUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getUsableWidth(JTextComponent component) {
    Container parent = component.getParent();
    if (parent instanceof JLayeredPane) {
        parent = parent.getParent();
    }
    return (parent instanceof JViewport)
        ? ((JViewport)parent).getExtentSize().width
        : component.getSize().width;
}
 
Example 5
Source File: BraceMatchingSidebarComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public BraceMatchingSidebarComponent(JTextComponent editor) {
    this.editor = editor;
    this.mimeType = DocumentUtilities.getMimeType(editor);
    this.prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
    
    final Lookup.Result r = MimeLookup.getLookup(org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(editor)).lookupResult(
            FontColorSettings.class);
    prefListenerGC = new PrefListener();
    this.colorResult = r;
    r.addLookupListener(WeakListeners.create(LookupListener.class, this , r));
    prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, prefListenerGC, prefs));
    loadPreferences();
    
    editorPane = findEditorPane(editor);
    Component parent = editor.getParent();
    if (parent instanceof JLayeredPane) {
        parent = parent.getParent();
    }
    if (parent instanceof JViewport) {
        this.viewport = (JViewport)parent;
        // see #219015; need to listen on viewport change to show/hide the tooltip
        viewport.addChangeListener(WeakListeners.change(this, viewport));
    }
    TextUI ui = editor.getUI();
    if (ui instanceof BaseTextUI) {
        baseUI = (BaseTextUI)ui;
        MasterMatcher.get(editor).addMatchListener(this);
    } else {
        baseUI = null;
    }
    setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
    updatePreferredSize();
}
 
Example 6
Source File: NbURLMapper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected URL getUrl(JTextComponent comp) {
    FileObject f = null;
    
    if (comp instanceof Lookup.Provider) {
        f = ((Lookup.Provider) comp).getLookup().lookup(FileObject.class);
    }
    
    if (f == null) {
        Container container = comp.getParent();
        while (container != null) {
            if (container instanceof Lookup.Provider) {
                f = ((Lookup.Provider) container).getLookup().lookup(FileObject.class);
                if (f != null) {
                    break;
                }
            }
            
            container = container.getParent();
        }
    }
    
    if (f != null) {
        try {
            return f.getURL();
        } catch (FileStateInvalidException e) {
            LOG.log(Level.WARNING, "Can't get URL for " + f, e); //NOI18N
        }
    }
    
    return null;
}
 
Example 7
Source File: LineNumbersRuler.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the JscrollPane that contains this EditorPane, or null if no
 * JScrollPane is the parent of this editor
 * @param editorPane
 * @return
 */
public JScrollPane getScrollPane(JTextComponent editorPane) {
    Container p = editorPane.getParent();
    while (p != null) {
        if (p instanceof JScrollPane) {
            return (JScrollPane) p;
        }
        p = p.getParent();
    }
    return null;
}
 
Example 8
Source File: QuickFindDialog.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public void showFor(final JTextComponent target) {
    oldCaretPosition = target.getCaretPosition();
    Container view = target.getParent();
    Dimension wd = getPreferredSize();
    //wd.width = target.getVisibleRect().width;
    Point loc = new Point(0, view.getHeight());
    setSize(wd);
    setLocationRelativeTo(view);
    SwingUtilities.convertPointToScreen(loc, view);
    setLocation(loc);
    jTxtFind.setFont(target.getFont());
    jTxtFind.getDocument().addDocumentListener(this);
    WindowAdapter closeListener = new WindowAdapter() {

        @Override
        public void windowDeactivated(WindowEvent e) {
            target.getDocument().removeDocumentListener(QuickFindDialog.this);
            Markers.removeMarkers(target, marker);
            if (escaped) {
                Rectangle aRect;
                try {
                    aRect = target.modelToView(oldCaretPosition);
                    target.setCaretPosition(oldCaretPosition);
                    target.scrollRectToVisible(aRect);
                } catch (BadLocationException ex) {
                }
            }
            dispose();
        }
    };
    addWindowListener(closeListener);
    this.target = new WeakReference<JTextComponent>(target);
    Pattern p = dsd.get().getPattern();
    if (p != null) {
        jTxtFind.setText(p.pattern());
    }
    jChkWrap.setSelected(dsd.get().isWrap());
    setVisible(true);
    jTxtFind.selectAll();
}
 
Example 9
Source File: LineNumbersRuler.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the JscrollPane that contains this EditorPane, or null if no
 * JScrollPane is the parent of this editor
 *
 * @param editorPane
 * @return
 */
public JScrollPane getScrollPane(JTextComponent editorPane) {
    Container p = editorPane.getParent();
    while (p != null) {
        if (p instanceof JScrollPane) {
            return (JScrollPane) p;
        }
        p = p.getParent();
    }
    return null;
}
 
Example 10
Source File: DarculaPasswordFieldUI.java    From Darcula with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  if (parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }
  final Border border = c.getBorder();
  if (border instanceof DarculaTextBorder) {
    g.setColor(c.getBackground());
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Insets i = border.getBorderInsets(c);
    if (c.hasFocus()) {
      final GraphicsConfig config = new GraphicsConfig(g);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

      g.fillRoundRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6, 5, 5);
      config.restore();
    }
    else {
      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
    }
  } else {
    super.paintBackground(g);
  }
}
 
Example 11
Source File: ModernPasswordFieldUI.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  if (parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }
  final Border border = c.getBorder();
  if (border instanceof ModernTextBorder) {
    g.setColor(c.getBackground());
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Insets i = border.getBorderInsets(c);
    if (c.hasFocus()) {
      final GraphicsConfig config = new GraphicsConfig(g);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6);
      config.restore();
    }
    else {
      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
    }
  }
  else {
    super.paintBackground(g);
  }
}
 
Example 12
Source File: DarculaPasswordFieldUI.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  if (parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }
  final Border border = c.getBorder();
  if (border instanceof DarculaTextBorder) {
    g.setColor(c.getBackground());
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Insets i = border.getBorderInsets(c);
    if (c.hasFocus()) {
      final GraphicsConfig config = new GraphicsConfig(g);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

      g.fillRoundRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6, 5, 5);
      config.restore();
    }
    else {
      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
    }
  } else {
    super.paintBackground(g);
  }
}
 
Example 13
Source File: HintsUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
boolean invokeDefaultAction(boolean onlyActive) {
    JTextComponent comp = getComponent();
    if (comp == null) {
        Logger.getLogger(HintsUI.class.getName()).log(Level.WARNING, "HintsUI.invokeDefaultAction called, but comp == null");
        return false;
    }

    Document doc = comp.getDocument();

    cancel.set(false);
    
    if (doc instanceof BaseDocument) {
        try {
            Rectangle carretRectangle = comp.modelToView(comp.getCaretPosition());
            int line = Utilities.getLineOffset((BaseDocument) doc, comp.getCaretPosition());
            FixData fixes;
            String description;

            if (!onlyActive) {
                refresh(doc, comp.getCaretPosition());
                AnnotationHolder holder = getAnnotationHolder(doc);
                Pair<FixData, String> fixData = holder != null ? holder.buildUpFixDataForLine(line) : null;

                if (fixData == null) return false;

                fixes = fixData.first();
                description = fixData.second();
            } else {
                AnnotationDesc activeAnnotation = ((BaseDocument) doc).getAnnotations().getActiveAnnotation(line);
                if (activeAnnotation == null) {
                    return false;
                }
                String type = activeAnnotation.getAnnotationType();
                if (!FixAction.getFixableAnnotationTypes().contains(type) && onlyActive) {
                    return false;
                }
                if (onlyActive) {
                    refresh(doc, comp.getCaretPosition());
                }
                Annotations annotations = ((BaseDocument) doc).getAnnotations();
                AnnotationDesc desc = annotations.getAnnotation(line, type);
                ParseErrorAnnotation annotation = null;
                if (desc != null) {
                    annotations.frontAnnotation(desc);
                    annotation = findAnnotation(doc, desc, line);
                }

                if (annotation == null) {
                    return false;
                }
                
                fixes = annotation.getFixes();
                description = annotation.getDescription();
            }

            Point p = comp.modelToView(Utilities.getRowStartFromLineOffset((BaseDocument) doc, line)).getLocation();
            p.y += carretRectangle.height;
            if(comp.getParent() instanceof JViewport) {
                p.x += ((JViewport)comp.getParent()).getViewPosition().x;
            }
            if(comp.getParent() instanceof JLayeredPane &&
                    comp.getParent().getParent() instanceof JViewport) {
                p.x += ((JViewport)comp.getParent().getParent()).getViewPosition().x;
            }

            showPopup(fixes, description, comp, p);

            return true;
        } catch (BadLocationException ex) {
            ErrorManager.getDefault().notify(ex);
        }
    }

    return false;
}