示例#1
0
 @Override
 public void setText(String text) {
   super.setText(text);
   if (myEditorPane != null) {
     myEditorPane.setText(getText());
     updateStyle(myEditorPane);
     checkMultiline();
   }
 }
示例#2
0
  public JBLabel setCopyable(boolean copyable) {
    if (copyable ^ myEditorPane != null) {
      if (myEditorPane == null) {
        final JLabel ellipsisLabel = new JBLabel("...");
        myIconLabel = new JLabel(getIcon());
        myEditorPane =
            new JEditorPane() {
              @Override
              public void paint(Graphics g) {
                Dimension size = getSize();
                boolean paintEllipsis = getPreferredSize().width > size.width && !myMultiline;

                if (!paintEllipsis) {
                  super.paint(g);
                } else {
                  Dimension ellipsisSize = ellipsisLabel.getPreferredSize();
                  int endOffset = size.width - ellipsisSize.width;
                  try {
                    // do not paint half of the letter
                    endOffset = modelToView(viewToModel(new Point(endOffset, 0)) - 1).x;
                  } catch (BadLocationException ignore) {
                  }
                  Shape oldClip = g.getClip();
                  g.clipRect(0, 0, endOffset, size.height);

                  super.paint(g);
                  g.setClip(oldClip);

                  g.translate(endOffset, 0);
                  ellipsisLabel.setSize(ellipsisSize);
                  ellipsisLabel.paint(g);
                  g.translate(-endOffset, 0);
                }
              }
            };
        myEditorPane.addFocusListener(
            new FocusAdapter() {
              @Override
              public void focusLost(FocusEvent e) {
                if (myEditorPane == null) return;
                int caretPosition = myEditorPane.getCaretPosition();
                myEditorPane.setSelectionStart(caretPosition);
                myEditorPane.setSelectionEnd(caretPosition);
              }
            });
        myEditorPane.setContentType("text/html");
        myEditorPane.setEditable(false);
        myEditorPane.setBackground(UIUtil.TRANSPARENT_COLOR);
        myEditorPane.setOpaque(false);
        myEditorPane.setBorder(null);
        myEditorPane.setText(getText());
        checkMultiline();
        myEditorPane.setCaretPosition(0);
        UIUtil.putClientProperty(
            myEditorPane, UIUtil.NOT_IN_HIERARCHY_COMPONENTS, Collections.singleton(ellipsisLabel));
        updateStyle(myEditorPane);
        setLayout(new BorderLayout(getIconTextGap(), 0));
        add(myIconLabel, BorderLayout.WEST);
        add(myEditorPane, BorderLayout.CENTER);
      } else {
        removeAll();
        myEditorPane = null;
        myIconLabel = null;
      }
    }
    return this;
  }