public Browser(String currentHref) { super(); this.currentHref = currentHref; try { if (Bither.getMainFrame() != null) { Bither.getMainFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); } addHyperlinkListener(new ActivatedHyperlinkListener(Bither.getMainFrame(), this)); loadingMessage = LocaliserUtils.getString("browser.loadingMessage"); setEditable(false); setBackground(Themes.currentTheme.detailPanelBackground()); String fontName = null; if (fontName == null || "".equals(fontName)) { fontName = ColorAndFontConstants.BITHER_DEFAULT_FONT_NAME; } // Add in san-serif as a fallback. fontName = fontName + ", san-serif"; int fontSize = ColorAndFontConstants.BITHER_DEFAULT_FONT_SIZE; boolean isItalic = false; boolean isBold = false; Font adjustedFont = FontSizer.INSTANCE.getAdjustedDefaultFont(); if (adjustedFont != null) { setFont(adjustedFont); fontSize = adjustedFont.getSize(); isItalic = adjustedFont.isItalic(); isBold = adjustedFont.isBold(); } String fontCSS = "font-size:" + fontSize + "pt; font-family:" + fontName + ";"; if (isItalic) { fontCSS = fontCSS + "font-style:italic;"; } else { fontCSS = fontCSS + "font-style:normal;"; } if (isBold) { fontCSS = fontCSS + "font-weight:bold;"; } else { fontCSS = fontCSS + "font-weight:normal;"; } HTMLEditorKit kit = new HTMLEditorKit(); setEditorKit(kit); javax.swing.text.html.StyleSheet styleSheet = kit.getStyleSheet(); styleSheet.addRule("body {" + fontCSS + "}"); Document doc = kit.createDefaultDocument(); setDocument(doc); log.debug("Trying to load '" + currentHref + "'..."); } catch (Exception ex) { showUnableToLoadMessage(ex.getClass().getCanonicalName() + " " + ex.getMessage()); } }
/** Configures the stylesheet used to render HTML in this view. */ public void setStyleSheet(String stylesheet) { StyleSheet ss = new StyleSheet(); try { // parse the stylesheet definition ss.loadRules(new StringReader(stylesheet), null); setStyleSheet(ss); } catch (Throwable t) { log.warning("Failed to parse stylesheet.", "sheet", stylesheet, t); } }
/** @see javax.swing.JPanel#updateUI() */ @Override public void updateUI() { super.updateUI(); if (feedItemDetail != null) { Font font = UIManager.getFont("Label.font"); HTMLEditorKit kit = (HTMLEditorKit) feedItemDetail.getEditorKit(); StyleSheet sheet = kit.getStyleSheet(); sheet.addRule( "body {font-family: " + font.getFamily() + "; font-size: " + font.getSize() + "pt;}"); } }
public Details(EventBus bus, Explorer controller) { super(); this.controller = controller; setEditable(false); setContentType(HTML_MIME_TYPE); setText(INITIAL_CONTENT_HTML); HTMLEditorKit kit = (HTMLEditorKit) getEditorKitForContentType(HTML_MIME_TYPE); StyleSheet css = kit.getStyleSheet(); for (String rule : CSS_RULES) { css.addRule(rule); } bus.register(this); }
public TableFeedBody() { this.setOrientation(JSplitPane.VERTICAL_SPLIT); this.setOneTouchExpandable(false); this.setContinuousLayout(true); // the top component (the list) takes the extra space this.setResizeWeight(1); JScrollPane scroll = new JScrollPane(); feedTable = new JTable(); feedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); feedTable.setRowSelectionAllowed(true); feedTable.changeSelection(0, 0, false, false); feedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); feedTable.addMouseListener(this); feedTable.setRowHeight(25); feedTable.addKeyListener(this); scroll.setViewportView(feedTable); this.setTopComponent(scroll); feedItemDetail = new JEditorPane(); feedItemDetail.setContentType("text/html"); feedItemDetail.addHyperlinkListener(this); feedItemDetail.setBackground(Color.WHITE); feedItemDetail.setEditable(false); HTMLEditorKit kit = (HTMLEditorKit) feedItemDetail.getEditorKit(); StyleSheet sheet = kit.getStyleSheet(); Font font = UIManager.getFont("Label.font"); sheet.addRule( "body {font-family: " + font.getFamily() + "; font-size: " + font.getSize() + "pt;}"); JScrollPane detailScroll = new JScrollPane(); Dimension minDetailSize = detailScroll.getMinimumSize(); minDetailSize.height = 100; detailScroll.setMinimumSize(minDetailSize); Dimension prefDetailSize = detailScroll.getPreferredSize(); prefDetailSize.height = 200; detailScroll.setPreferredSize(prefDetailSize); detailScroll.setViewportView(feedItemDetail); this.setBottomComponent(detailScroll); this.setDividerSize(2); this.setDividerLocation(-1); }
private void updateStyle(@NotNull JEditorPane pane) { EditorKit kit = pane.getEditorKit(); if (kit instanceof HTMLEditorKit) { StyleSheet css = ((HTMLEditorKit) kit).getStyleSheet(); css.addRule( "body, p {" + "color:#" + ColorUtil.toHex(getForeground()) + ";" + "font-family:" + getFont().getFamily() + ";" + "font-size:" + getFont().getSize() + "pt;" + "white-space:nowrap;}"); } }
private MainPanel() { super(new GridLayout(3, 1)); add(makePanel("Default", HREF)); // [Customize detault html link color in java swing - Stack Overflow] // http://stackoverflow.com/questions/26749495/customize-detault-html-link-color-in-java-swing HTMLEditorKit kit = new HTMLEditorKit(); StyleSheet styleSheet = kit.getStyleSheet(); styleSheet.addRule("a{color:#FF0000;}"); add(makePanel("styleSheet.addRule(\"a{color:#FF0000;}\")", HREF)); add( makePanel( "<a style='color:#00FF00'...", "<html><a style='color:#00FF00' href='" + MYSITE + "'>" + MYSITE + "</a>")); setPreferredSize(new Dimension(320, 240)); }
/** Update any cached values that come from attributes. */ protected void setPropertiesFromAttributes() { StyleSheet sheet = getStyleSheet(); this.attr = sheet.getViewAttributes(this); // Gutters borderSize = (short) getIntAttr(HTML.Attribute.BORDER, isLink() ? DEFAULT_BORDER : 0); leftInset = rightInset = (short) (getIntAttr(HTML.Attribute.HSPACE, 0) + borderSize); topInset = bottomInset = (short) (getIntAttr(HTML.Attribute.VSPACE, 0) + borderSize); borderColor = ((StyledDocument) getDocument()).getForeground(getAttributes()); AttributeSet attr = getElement().getAttributes(); // Alignment. // PENDING: This needs to be changed to support the CSS versions // when conversion from ALIGN to VERTICAL_ALIGN is complete. Object alignment = attr.getAttribute(HTML.Attribute.ALIGN); vAlign = 1.0f; if (alignment != null) { alignment = alignment.toString(); if ("top".equals(alignment)) { vAlign = 0f; } else if ("middle".equals(alignment)) { vAlign = .5f; } } AttributeSet anchorAttr = (AttributeSet) attr.getAttribute(HTML.Tag.A); if (anchorAttr != null && anchorAttr.isDefined(HTML.Attribute.HREF)) { synchronized (this) { state |= LINK_FLAG; } } else { synchronized (this) { state = (state | LINK_FLAG) ^ LINK_FLAG; } } }
void loadHTMLContent(String shortUrl) { try { StyleSheet style = htmlKit.getStyleSheet(); BufferedReader r = new BufferedReader( new InputStreamReader( peer.getFileInputStream("assets/ExhibitContents/exhibits.css"))); style.loadRules(r, null); } catch (Exception e) { // TODO do the try block differently if css has been modified. } InputStream r = peer.getFileInputStream("assets/" + shortUrl); // TODO do differently if modified StringBuffer sb = new StringBuffer(128); try { byte[] buffer = new byte[128]; for (int result = r.read(buffer); result != -1; result = r.read(buffer)) { sb.append(new String(buffer, 0, result)); } } catch (IOException e) { } htmlContentViewer.setText(sb.toString()); }
/** * Builds the style sheet used in the internal help browser * * @return the style sheet */ protected StyleSheet buildStyleSheet() { StyleSheet ss = new StyleSheet(); BufferedReader reader = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("/data/help-browser.css"))); StringBuffer css = new StringBuffer(); try { String line = null; while ((line = reader.readLine()) != null) { css.append(line); css.append("\n"); } } catch (Exception e) { Main.error( tr("Failed to read CSS file ''help-browser.css''. Exception is: {0}", e.toString())); e.printStackTrace(); return ss; } finally { Utils.close(reader); } ss.addRule(css.toString()); return ss; }
public Commentary(VisPanel panel, JScrollPane sp) { super(); this.panel = panel; setContentType("text/html; charset=iso-8859-2"); setEditable(false); final Font font = UIManager.getFont("Label.font"); final StyleSheet css = ((HTMLDocument) getDocument()).getStyleSheet(); css.addRule( "body { font-family: " + font.getFamily() + "; " + "font-size: 12pt; margin: 10pt; margin-top: 0pt; " + "}"); css.addRule(".step { margin-bottom: 5pt; }"); css.addRule("ol { padding-left: 14px; margin: 0px; }"); css.addRule("a { color: black; text-decoration:none; }"); css.addRule("p.note { font-style: italic; margin: 0pt; margin-bottom: 5pt; }"); this.sp = sp; Languages.addListener(this); addHyperlinkListener(this); setText("<html><body></body></html>"); }
/** * Apply a new font for the label. * * @param font new font to use. */ public void setFont(Font font) { getLabel().setFont(font); // Set font so that getFont can be used direclty // Set the properties to the text (only for text label not for HTML or MATHML) if ((labelText != null) && !(labelText.startsWith(DOLLAR) && labelText.endsWith(DOLLAR)) && !(labelText.startsWith("<") && labelText.endsWith(">"))) { // Now set the stylesheet because of text/html contents StyleSheet styleSheet = ((HTMLDocument) getLabel().getDocument()).getStyleSheet(); styleSheet.addRule("body {font-family:" + font.getName() + ";}"); styleSheet.addRule("body {font-size:" + font.getSize() + "pt;}"); if (font.isBold()) { styleSheet.addRule("body {font-weight:bold;}"); } else { styleSheet.addRule("body {font-weight:normal;}"); } if (font.isItalic()) { styleSheet.addRule("body {font-style:italic;}"); } else { styleSheet.addRule("body {font-style:normal;}"); } } }
public void createPartControl(Composite parent) { listener = new WorkbenchWindowListener( new WorkbenchPageListener( new PartListener(new DropTemplateWorkflowModelEditorAction()))); listener.register(); FillLayout parentLayout = new FillLayout(); parent.setLayout(parentLayout); SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL); Tree tree = new Tree(sashForm, SWT.BORDER); tree.setLinesVisible(false); tree.setLayoutData(FormBuilder.createDefaultMultiLineWidgetGridData()); Composite composite = FormBuilder.createComposite(sashForm, 1); Composite swingComposite = new Composite(composite, SWT.EMBEDDED); swingComposite.setLayoutData(FormBuilder.createDefaultMultiLineWidgetGridData()); java.awt.Frame locationFrame = SWT_AWT.new_Frame(swingComposite); textPane = new JTextPane(); JScrollPane scrollPane = new JScrollPane(textPane); locationFrame.add(scrollPane); textPane.setContentType("text/html"); // $NON-NLS-1$ StyleSheet css = ((HTMLEditorKit) textPane.getEditorKit()).getStyleSheet(); URL url = this.getClass().getResource("/html/carnot.css"); // $NON-NLS-1$ css.importStyleSheet(url); textPane.setEditable(false); sashForm.setWeights(new int[] {1, 2}); final TreeViewer viewer = new TreeViewer(tree); viewer.setContentProvider(new PatternsContentProvider()); viewer.setLabelProvider(new PatternsLabelProvider()); viewer.addSelectionChangedListener( new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { TreeSelection selection = (TreeSelection) event.getSelection(); Object object = selection.getFirstElement(); if (object instanceof ITemplate) { ITemplate template = (ITemplate) object; textPane.setEditorKit(new ExtendedHTMLEditorKit(template)); textPane.setText(template.getDescription()); } else { if (object instanceof ITemplateFactory) { ITemplateFactory templateFactory = (ITemplateFactory) object; textPane.setText(templateFactory.getDescription()); } } } }); viewer.addDragSupport( DND.DROP_COPY, new Transfer[] {LocalSelectionTransfer.getTransfer()}, new DragSourceAdapter() { public void dragStart(DragSourceEvent event) { ISelection selection = viewer.getSelection(); LocalSelectionTransfer.getTransfer().setSelection(selection); LocalSelectionTransfer.getTransfer().setSelectionSetTime(event.time & 0xFFFFFFFFL); event.doit = DropTemplateWorkflowModelEditorAction.isValidDndSelection(selection); } public void dragSetData(DragSourceEvent event) { event.data = LocalSelectionTransfer.getTransfer().getSelection(); } public void dragFinished(DragSourceEvent event) { LocalSelectionTransfer.getTransfer().setSelection(null); LocalSelectionTransfer.getTransfer().setSelectionSetTime(0); } }); // Add actions to the local tool bar IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager(); Action refreshAction = new Action(Diagram_Messages.LB_VersionRepository_Refresh) { public void run() { viewer.setInput( "org.eclipse.stardust.modeling.templates.templateProvider"); //$NON-NLS-1$ } }; tbm.add(refreshAction); refreshAction.run(); }
/** * This method initializes jEditorPane * * @return javax.swing.JEditorPane */ protected JEditorPane getResultPane() { if (resultPane == null) { HTMLEditorKit kit = new HTMLEditorKit(); resultPane = new JEditorPane(); resultPane.setEditable(false); resultPane.setContentType("text/html"); resultPane.addHyperlinkListener(new RecognitionHyperlinkListener()); resultPane.setCaretPosition(0); resultPane.setEditorKit(kit); StyleSheet styleSheet = kit.getStyleSheet(); styleSheet.addRule("table{border-width: 1px;}"); styleSheet.addRule("table{border-style: solid;}"); styleSheet.addRule("table{border-color: gray;}"); styleSheet.addRule("table{border-spacing: 0x;}"); styleSheet.addRule("table{width: 100%;}"); styleSheet.addRule("th {border-width: 1px;}"); styleSheet.addRule("th {border-style: solid;}"); styleSheet.addRule("th {border-color: gray;}"); styleSheet.addRule("th {padding: 5px;}"); styleSheet.addRule("td {border-width: 1px;}"); styleSheet.addRule("td.selected {background-color: gray;color: black;}"); styleSheet.addRule("td {border-style: solid;}"); styleSheet.addRule("td {color: gray;}"); styleSheet.addRule("td {padding: 5px;}"); styleSheet.addRule("div {width: 100%;}"); styleSheet.addRule("div {position: absolute;}"); styleSheet.addRule("div {text-align: center;}"); styleSheet.addRule("div {padding: 5px;}"); } return resultPane; }
/* * (non-Javadoc) * * @see org.quiltplayer.view.components.View#getUI() */ @Override public JComponent getUI() { panel = new JPanel(new MigLayout("ins 1cm 3cm 0 3cm, center, w 80%!")); panel.setOpaque(true); JEditorPane htmlPane = null; htmlPane = new JEditorPane("text/html", content) { /* * (non-Javadoc) * * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) */ @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); super.paintComponent(g); } }; htmlPane.setOpaque(false); htmlPane.setDragEnabled(false); htmlPane.setEditable(true); htmlPane.setFocusable(false); htmlPane.setFont(FontFactory.getFont(13)); htmlPane.setForeground(Color.white); htmlPane.setBounds(10, 10, panel.getWidth() - 150, panel.getHeight()); final Font font = FontFactory.getFont(13); String pRule = "p { font-family: " + font.getName() + "; " + "font-size: " + font.getSize() + "pt; color: #EEEEEE; }"; String spanRule = "span { font-family: " + font.getName() + "; " + "font-size: " + font.getSize() + "pt; color: #FFFFFF; }"; String linkRule = "a { font-family: " + font.getName() + "; " + "font-size: " + font.getSize() + "pt;" + "color: #AAAAAA;}"; String linkRule2 = "a:hoover { text-decoration: none; font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize() + "pt;" + "color: #FFFFFF;}"; ((HTMLDocument) htmlPane.getDocument()).getStyleSheet().addRule(linkRule); ((HTMLDocument) htmlPane.getDocument()).getStyleSheet().addRule(pRule); ((HTMLDocument) htmlPane.getDocument()).getStyleSheet().addRule(spanRule); ((HTMLDocument) htmlPane.getDocument()).getStyleSheet().addRule(linkRule2); StyleSheet styles = ((HTMLDocument) htmlPane.getDocument()).getStyleSheet(); Enumeration<?> rules = styles.getStyleNames(); while (rules.hasMoreElements()) { String name = (String) rules.nextElement(); Style rule = styles.getStyle(name); System.out.println(rule.toString()); } htmlPane.setCaretPosition(0); panel.add(htmlPane, "w 100%, h 100%"); final QScrollPane pane = new QScrollPane(panel, ScrollDirection.VERTICAL); return new JXLayer<JScrollPane>(pane, new JScrollPaneLayerUI()); }
/** * Creates a new view that represents an IMG element. * * @param elem the element to create a view for */ public MyImageView(Element elem) { super(elem); initialize(elem); StyleSheet sheet = getStyleSheet(); attr = sheet.getViewAttributes(this); }
HtmlPanel(final Map<String, Action> actions, HtmlPage page) throws IOException { super(new BorderLayout()); this.actions = actions; textPane = new JTextPane(); find = new JTextField(); matches = new JLabel(); this.page = null; final Dimension d = matches.getPreferredSize(); matches.setPreferredSize(new Dimension(100, d.height)); textPane.setEditable(false); textPane.setFocusable(false); textPane.addHyperlinkListener( new HyperlinkListener() { public final void hyperlinkUpdate(HyperlinkEvent ev) { if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { final JEditorPane pane = (JEditorPane) ev.getSource(); if (ev instanceof HTMLFrameHyperlinkEvent) { final HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) ev; final HTMLDocument doc = (HTMLDocument) pane.getDocument(); doc.processHTMLFrameHyperlinkEvent(evt); } else if (Desktop.isDesktopSupported()) try { Desktop.getDesktop().browse(ev.getURL().toURI()); } catch (final Exception e) { e.printStackTrace(); } } } }); final HTMLEditorKit kit = new HTMLEditorKit() { private static final long serialVersionUID = 1L; @Override public final Document createDefaultDocument() { final HTMLDocument doc = (HTMLDocument) super.createDefaultDocument(); // Load synchronously. doc.setAsynchronousLoadPriority(-1); return doc; } }; final StyleSheet styleSheet = kit.getStyleSheet(); final InputStream is = getClass().getResourceAsStream("/doogal.css"); try { styleSheet.loadRules(newBufferedReader(is), null); } finally { is.close(); } textPane.setEditorKit(kit); final Document doc = kit.createDefaultDocument(); textPane.setDocument(doc); textPane.addMouseListener( new MouseAdapter() { private final void showPopup(MouseEvent e) { if (e.isPopupTrigger()) { final JPopupMenu menu = newPopupMenu(TableType.DOCUMENT, actions); if (null != menu) menu.show(e.getComponent(), e.getX(), e.getY()); } } @Override public final void mousePressed(MouseEvent e) { showPopup(e); } @Override public final void mouseReleased(MouseEvent e) { showPopup(e); } }); find.setColumns(16); find.setFont(new Font("Dialog", Font.PLAIN, TINY_FONT)); find.setMargin(new Insets(2, 2, 2, 2)); find.addActionListener( new ActionListener() { public final void actionPerformed(ActionEvent ev) { find(find.getText()); } }); final JLabel label = new JLabel("Quick Find: "); label.setLabelFor(find); final JButton clear = new JButton("Clear"); clear.setMargin(new Insets(1, 5, 0, 5)); clear.setFont(new Font("Dialog", Font.PLAIN, TINY_FONT)); clear.addActionListener( new ActionListener() { public final void actionPerformed(ActionEvent e) { find.setText(""); find(""); } }); final JPanel findPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); findPanel.add(label); findPanel.add(find); findPanel.add(clear); findPanel.add(matches); scrollPane = new JScrollPane( textPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setFocusable(false); final JScrollBar scrollBar = scrollPane.getVerticalScrollBar(); scrollBar.setBlockIncrement(scrollBar.getBlockIncrement() * 20); scrollBar.setUnitIncrement(scrollBar.getUnitIncrement() * 20); add(scrollPane, BorderLayout.CENTER); add(findPanel, BorderLayout.SOUTH); setPage(page); }
private static void adjustFontSize(StyleSheet styleSheet) { int size = (int) UIUtil.getFontSize(UIUtil.FontSize.MINI); styleSheet.addRule(TIP_HTML_TEXT_TAGS + " {font-size: " + size + "px;}"); }