public static JEditorPane initPane( @NonNls Html html, final HintHint hintHint, @Nullable final JLayeredPane layeredPane) { final Ref<Dimension> prefSize = new Ref<Dimension>(null); String htmlBody = UIUtil.getHtmlBody(html); @NonNls String text = "<html><head>" + UIUtil.getCssFontDeclaration( hintHint.getTextFont(), hintHint.getTextForeground(), hintHint.getLinkForeground(), hintHint.getUlImg()) + "</head><body>" + htmlBody + "</body></html>"; final boolean[] prefSizeWasComputed = {false}; final JEditorPane pane = new JEditorPane() { @Override public Dimension getPreferredSize() { if (!prefSizeWasComputed[0] && hintHint.isAwtTooltip()) { JLayeredPane lp = layeredPane; if (lp == null) { JRootPane rootPane = UIUtil.getRootPane(this); if (rootPane != null) { lp = rootPane.getLayeredPane(); } } Dimension size; if (lp != null) { size = lp.getSize(); prefSizeWasComputed[0] = true; } else { size = ScreenUtil.getScreenRectangle(0, 0).getSize(); } int fitWidth = (int) (size.width * 0.8); Dimension prefSizeOriginal = super.getPreferredSize(); if (prefSizeOriginal.width > fitWidth) { setSize(new Dimension(fitWidth, Integer.MAX_VALUE)); Dimension fixedWidthSize = super.getPreferredSize(); Dimension minSize = super.getMinimumSize(); prefSize.set( new Dimension( fitWidth > minSize.width ? fitWidth : minSize.width, fixedWidthSize.height)); } else { prefSize.set(new Dimension(prefSizeOriginal)); } } Dimension s = prefSize.get() != null ? new Dimension(prefSize.get()) : super.getPreferredSize(); Border b = getBorder(); if (b != null) { Insets insets = b.getBorderInsets(this); if (insets != null) { s.width += insets.left + insets.right; s.height += insets.top + insets.bottom; } } return s; } }; final HTMLEditorKit.HTMLFactory factory = new HTMLEditorKit.HTMLFactory() { @Override public View create(Element elem) { AttributeSet attrs = elem.getAttributes(); Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute); Object o = elementName != null ? null : attrs.getAttribute(StyleConstants.NameAttribute); if (o instanceof HTML.Tag) { HTML.Tag kind = (HTML.Tag) o; if (kind == HTML.Tag.HR) { return new CustomHrView(elem, hintHint.getTextForeground()); } } return super.create(elem); } }; HTMLEditorKit kit = new HTMLEditorKit() { @Override public ViewFactory getViewFactory() { return factory; } }; pane.setEditorKit(kit); pane.setText(text); pane.setCaretPosition(0); pane.setEditable(false); if (hintHint.isOwnBorderAllowed()) { setBorder(pane); setColors(pane); } else { pane.setBorder(null); } if (!hintHint.isAwtTooltip()) { prefSizeWasComputed[0] = true; } final boolean opaque = hintHint.isOpaqueAllowed(); pane.setOpaque(opaque); if (UIUtil.isUnderNimbusLookAndFeel() && !opaque) { pane.setBackground(UIUtil.TRANSPARENT_COLOR); } else { pane.setBackground(hintHint.getTextBackground()); } return pane; }
/** * Constructs a X509 certificate panel. * * @param certificates <tt>X509Certificate</tt> objects */ public X509CertificatePanel(Certificate[] certificates) { setLayout(new BorderLayout(5, 5)); // Certificate chain list TransparentPanel topPanel = new TransparentPanel(new BorderLayout()); topPanel.add( new JLabel( "<html><body><b>" + R.getI18NString("service.gui.CERT_INFO_CHAIN") + "</b></body></html>"), BorderLayout.NORTH); DefaultMutableTreeNode top = new DefaultMutableTreeNode(); DefaultMutableTreeNode previous = top; for (int i = certificates.length - 1; i >= 0; i--) { Certificate cert = certificates[i]; DefaultMutableTreeNode next = new DefaultMutableTreeNode(cert); previous.add(next); previous = next; } JTree tree = new JTree(top); tree.setBorder(new BevelBorder(BevelBorder.LOWERED)); tree.setRootVisible(false); tree.setExpandsSelectedPaths(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setCellRenderer( new DefaultTreeCellRenderer() { @Override public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { JLabel component = (JLabel) super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus); if (value instanceof DefaultMutableTreeNode) { Object o = ((DefaultMutableTreeNode) value).getUserObject(); if (o instanceof X509Certificate) { component.setText(getSimplifiedName((X509Certificate) o)); } else { // We don't know how to represent this certificate type, // let's use the first 20 characters String text = o.toString(); if (text.length() > 20) { text = text.substring(0, 20); } component.setText(text); } } return component; } }); tree.getSelectionModel() .addTreeSelectionListener( new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { valueChangedPerformed(e); } }); tree.setSelectionPath( new TreePath((((DefaultTreeModel) tree.getModel()).getPathToRoot(previous)))); topPanel.add(tree, BorderLayout.CENTER); add(topPanel, BorderLayout.NORTH); // Certificate details pane Caret caret = infoTextPane.getCaret(); if (caret instanceof DefaultCaret) { ((DefaultCaret) caret).setUpdatePolicy(DefaultCaret.NEVER_UPDATE); } /* * Make JEditorPane respect our default font because we will be using it * to just display text. */ infoTextPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); infoTextPane.setOpaque(false); infoTextPane.setEditable(false); infoTextPane.setContentType("text/html"); infoTextPane.setText(toString(certificates[0])); final JScrollPane certScroll = new JScrollPane(infoTextPane); certScroll.setPreferredSize(new Dimension(300, 500)); add(certScroll, BorderLayout.CENTER); }