Search in sources :

Example 41 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project freeplane by freeplane.

the class SplitNode method splitNode.

private String[] splitNode(final String text) {
    if (text.startsWith("<html>")) {
        String[] parts = null;
        final HTMLEditorKit kit = new HTMLEditorKit();
        final HTMLDocument doc = new HTMLDocument();
        final StringReader buf = new StringReader(text);
        try {
            kit.read(buf, doc, 0);
            final Element parent = getParentElement(doc);
            if (parent == null) {
                return null;
            }
            final int elementCount = parent.getElementCount();
            int notEmptyElementCount = 0;
            parts = new String[elementCount];
            int start = parent.getStartOffset();
            for (int i = 0; i < elementCount; i++) {
                final Element current = parent.getElement(i);
                if (current.isLeaf() && !current.getName().equals(HTML.Tag.BR.toString()) && i < elementCount - 1)
                    continue;
                final int end = current.getEndOffset();
                final String newFragment = getFragment(doc, start, end);
                if (newFragment != null) {
                    parts[i] = newFragment;
                    notEmptyElementCount++;
                }
                start = end;
            }
            if (notEmptyElementCount <= 1) {
                return null;
            }
        } catch (final IOException e) {
            LogUtils.severe(e);
        } catch (final BadLocationException e) {
            LogUtils.severe(e);
        }
        return parts;
    }
    return text.split("\n");
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) StringReader(java.io.StringReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 42 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project freeplane by freeplane.

the class EditNodeTextField method show.

/* (non-Javadoc)
	 * @see org.freeplane.view.swing.map.INodeTextField#show()
	 */
@SuppressWarnings("serial")
@Override
public void show(final RootPaneContainer frame) {
    final ModeController modeController = Controller.getCurrentModeController();
    final IMapViewManager viewController = modeController.getController().getMapViewManager();
    final MTextController textController = (MTextController) TextController.getController(modeController);
    nodeView = (NodeView) SwingUtilities.getAncestorOfClass(NodeView.class, parent);
    font = parent.getFont();
    zoom = viewController.getZoom();
    if (zoom != 1F) {
        final float fontSize = (int) (Math.rint(font.getSize() * zoom));
        font = font.deriveFont(fontSize);
    }
    final HTMLEditorKit kit = new ScaledEditorKit() {

        @Override
        public void write(Writer out, Document doc, int pos, int len) throws IOException, BadLocationException {
            if (doc instanceof HTMLDocument) {
                HTMLWriter w = new SHTMLWriter(out, (HTMLDocument) doc, pos, len);
                w.write();
            } else {
                super.write(out, doc, pos, len);
            }
        }
    };
    textfield.setEditorKit(kit);
    final InputMap inputMap = textfield.getInputMap();
    final ActionMap actionMap = textfield.getActionMap();
    actionMap.put(DefaultEditorKit.pasteAction, pasteAction);
    inputMap.put((KeyStroke) boldAction.getValue(Action.ACCELERATOR_KEY), "boldAction");
    actionMap.put("boldAction", boldAction);
    inputMap.put((KeyStroke) italicAction.getValue(Action.ACCELERATOR_KEY), "italicAction");
    actionMap.put("italicAction", italicAction);
    inputMap.put((KeyStroke) underlineAction.getValue(Action.ACCELERATOR_KEY), "underlineAction");
    actionMap.put("underlineAction", underlineAction);
    inputMap.put((KeyStroke) redAction.getValue(Action.ACCELERATOR_KEY), "redAction");
    actionMap.put("redAction", redAction);
    inputMap.put((KeyStroke) greenAction.getValue(Action.ACCELERATOR_KEY), "greenAction");
    actionMap.put("greenAction", greenAction);
    inputMap.put((KeyStroke) blueAction.getValue(Action.ACCELERATOR_KEY), "blueAction");
    actionMap.put("blueAction", blueAction);
    inputMap.put((KeyStroke) blackAction.getValue(Action.ACCELERATOR_KEY), "blackAction");
    actionMap.put("blackAction", blackAction);
    inputMap.put((KeyStroke) defaultColorAction.getValue(Action.ACCELERATOR_KEY), "defaultColorAction");
    actionMap.put("defaultColorAction", defaultColorAction);
    inputMap.put((KeyStroke) removeFormattingAction.getValue(Action.ACCELERATOR_KEY), "removeFormattingAction");
    actionMap.put("removeFormattingAction", removeFormattingAction);
    final Color nodeTextColor = parent.getForeground();
    textfield.setCaretColor(nodeTextColor);
    final StringBuilder ruleBuilder = new StringBuilder(100);
    ruleBuilder.append("body {");
    final int labelHorizontalAlignment = parent.getHorizontalAlignment();
    ruleBuilder.append(new CssRuleBuilder().withCSSFont(font, UITools.FONT_SCALE_FACTOR).withColor(nodeTextColor).withBackground(getBackground()).withAlignment(labelHorizontalAlignment));
    ruleBuilder.append("}\n");
    final HTMLDocument document = (HTMLDocument) textfield.getDocument();
    final StyleSheet styleSheet = document.getStyleSheet();
    styleSheet.addRule(ruleBuilder.toString());
    textfield.setText(text);
    final MapView mapView = nodeView.getMap();
    if (!mapView.isValid())
        mapView.validate();
    final NodeStyleController nsc = NodeStyleController.getController(modeController);
    maxWidth = Math.max(mapView.getZoomed(nsc.getMaxWidth(node).toBaseUnitsRounded()), parent.getWidth());
    final Icon icon = parent.getIcon();
    if (icon != null) {
        maxWidth -= mapView.getZoomed(icon.getIconWidth());
        maxWidth -= mapView.getZoomed(parent.getIconTextGap());
    }
    Insets parentInsets = parent.getZoomedInsets();
    maxWidth -= parentInsets.left + parentInsets.right;
    extraWidth = ResourceController.getResourceController().getIntProperty("editor_extra_width", 80);
    extraWidth = mapView.getZoomed(extraWidth);
    final TextFieldListener textFieldListener = new TextFieldListener();
    this.textFieldListener = textFieldListener;
    textfield.addFocusListener(textFieldListener);
    textfield.addKeyListener(textFieldListener);
    textfield.addMouseListener(textFieldListener);
    mapViewChangeListener = new MapViewChangeListener();
    Controller.getCurrentController().getMapViewManager().addMapViewChangeListener(mapViewChangeListener);
    SpellCheckerController.getController().enableAutoSpell(textfield, true);
    mapView.scrollNodeToVisible(nodeView);
    assert (parent.isValid());
    final int nodeWidth = parent.getWidth();
    final int textFieldBorderWidth = 2;
    textfield.setBorder(new MatteBorder(textFieldBorderWidth, textFieldBorderWidth, textFieldBorderWidth, textFieldBorderWidth, nodeView.getSelectedColor()));
    final Dimension textFieldMinimumSize = textfield.getPreferredSize();
    textFieldMinimumSize.width += 1;
    if (textFieldMinimumSize.width < extraWidth)
        textFieldMinimumSize.width = extraWidth;
    if (textFieldMinimumSize.width < 10)
        textFieldMinimumSize.width = 10;
    if (textFieldMinimumSize.width > maxWidth) {
        textFieldMinimumSize.width = maxWidth;
        setLineWrap();
        textFieldMinimumSize.height = textfield.getPreferredSize().height;
    }
    final ZoomableLabelUI parentUI = (ZoomableLabelUI) parent.getUI();
    final LayoutData layoutData = parentUI.getLayoutData(parent);
    Rectangle iconR = layoutData.iconR;
    final Rectangle textR = layoutData.textR;
    int textFieldX = parentInsets.left - textFieldBorderWidth + (iconR.width > 0 ? textR.x - iconR.x : 0);
    final EventBuffer eventQueue = MTextController.getController().getEventQueue();
    KeyEvent firstEvent = eventQueue.getFirstEvent();
    Point mouseEventPoint = null;
    if (firstEvent == null) {
        MouseEvent currentEvent = eventQueue.getMouseEvent();
        if (currentEvent != null) {
            MouseEvent mouseEvent = (MouseEvent) currentEvent;
            if (mouseEvent.getComponent().equals(parent)) {
                mouseEventPoint = mouseEvent.getPoint();
                mouseEventPoint.x -= textR.x;
                mouseEventPoint.y -= textR.y;
            }
        }
    }
    textFieldMinimumSize.width = Math.max(textFieldMinimumSize.width, nodeWidth - textFieldX - (parentInsets.right - textFieldBorderWidth));
    textFieldMinimumSize.height = Math.max(textFieldMinimumSize.height, textR.height);
    textfield.setSize(textFieldMinimumSize.width, textFieldMinimumSize.height);
    final int textY = Math.max(textR.y - (textFieldMinimumSize.height - textR.height) / 2, 0);
    final Dimension newParentSize = new Dimension(textFieldX + textFieldMinimumSize.width + parentInsets.right, 2 * textY + textFieldMinimumSize.height);
    horizontalSpace = newParentSize.width - textFieldMinimumSize.width;
    verticalSpace = 2 * textY;
    final int widthAddedToParent = newParentSize.width - parent.getWidth();
    final Point location = new Point(textR.x - textFieldBorderWidth, textY);
    final int widthAddedToTextField = textFieldMinimumSize.width - (textR.width + 2 * textFieldBorderWidth);
    if (widthAddedToTextField > 0) {
        switch(labelHorizontalAlignment) {
            case SwingConstants.CENTER:
                location.x -= (widthAddedToTextField - widthAddedToParent) / 2;
                if (mouseEventPoint != null)
                    mouseEventPoint.x += widthAddedToTextField / 2;
                break;
            case SwingConstants.RIGHT:
                location.x -= widthAddedToTextField - widthAddedToParent;
                if (mouseEventPoint != null)
                    mouseEventPoint.x += widthAddedToTextField;
                break;
        }
    }
    keepNodePosition();
    parent.setPreferredSize(newParentSize);
    parent.setText("");
    parent.setHorizontalAlignment(JLabel.LEFT);
    if (!layoutMapOnTextChange) {
        mapView.doLayout();
        UITools.convertPointToAncestor(parent, location, mapView);
    }
    textfield.setBounds(location.x, location.y, textFieldMinimumSize.width, textFieldMinimumSize.height);
    if (layoutMapOnTextChange)
        parent.add(textfield, 0);
    else
        mapView.add(textfield, 0);
    redispatchKeyEvents(textfield, firstEvent);
    if (firstEvent == null) {
        int pos = document.getLength();
        if (mouseEventPoint != null)
            pos = textfield.viewToModel(mouseEventPoint);
        textfield.setCaretPosition(pos);
    }
    document.addDocumentListener(documentListener);
    if (textController.isMinimized(node)) {
        layout();
    }
    textfield.repaint();
    textfield.requestFocusInWindow();
}
Also used : Insets(java.awt.Insets) HTMLDocument(javax.swing.text.html.HTMLDocument) Rectangle(java.awt.Rectangle) EventBuffer(org.freeplane.features.text.mindmapmode.EventBuffer) ModeController(org.freeplane.features.mode.ModeController) ScaledEditorKit(org.freeplane.core.ui.components.html.ScaledEditorKit) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) KeyEvent(java.awt.event.KeyEvent) SHTMLWriter(com.lightdev.app.shtm.SHTMLWriter) MatteBorder(javax.swing.border.MatteBorder) ZoomableLabelUI(org.freeplane.view.swing.map.ZoomableLabelUI) MapView(org.freeplane.view.swing.map.MapView) HTMLWriter(javax.swing.text.html.HTMLWriter) SHTMLWriter(com.lightdev.app.shtm.SHTMLWriter) MouseEvent(java.awt.event.MouseEvent) CssRuleBuilder(org.freeplane.core.ui.components.html.CssRuleBuilder) ActionMap(javax.swing.ActionMap) Color(java.awt.Color) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Dimension(java.awt.Dimension) Point(java.awt.Point) Point(java.awt.Point) IMapViewManager(org.freeplane.features.ui.IMapViewManager) StyleSheet(javax.swing.text.html.StyleSheet) IMapViewChangeListener(org.freeplane.features.ui.IMapViewChangeListener) LayoutData(org.freeplane.view.swing.map.ZoomableLabelUI.LayoutData) NodeStyleController(org.freeplane.features.nodestyle.NodeStyleController) InputMap(javax.swing.InputMap) MTextController(org.freeplane.features.text.mindmapmode.MTextController) Icon(javax.swing.Icon) HTMLWriter(javax.swing.text.html.HTMLWriter) Writer(java.io.Writer) SHTMLWriter(com.lightdev.app.shtm.SHTMLWriter)

Example 43 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project cytoscape-impl by cytoscape.

the class JResultsPane method clear.

/**
 * Recreate new Document, set it to the current JResultsPane and reset the rootElement
 * to be used while inserting HTML Element
 */
public void clear() {
    currentDocument = (HTMLDocument) new HTMLEditorKit().createDefaultDocument();
    setStyledDocument(currentDocument);
    rootElement = currentDocument.getDefaultRootElement();
}
Also used : HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Example 44 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method installWidgetsFromHTML.

/**
 * @param event The {@link DragEvent} containing the dragged data.
 * @param selection_tracker Used to get the grid steps from its model to be
 *            used in offsetting multiple widgets.
 * @param widgets The container of the created widgets.
 */
private static void installWidgetsFromHTML(final DragEvent event, final SelectedWidgetUITracker selection_tracker, final List<Widget> widgets) {
    final Dragboard db = event.getDragboard();
    String html = db.getHtml();
    HTMLEditorKit htmlParser = new HTMLEditorKit();
    Document document = htmlParser.createDefaultDocument();
    try {
        htmlParser.read(new ByteArrayInputStream(html.getBytes()), document, 0);
        installWidgetsFromString(event, document.getText(0, document.getLength()), selection_tracker, widgets);
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Invalid HTML string", ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Document(javax.swing.text.Document) Dragboard(javafx.scene.input.Dragboard)

Example 45 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project open-ecard by ecsec.

the class LogSettingsGroup method createSupportPanel.

private JComponent createSupportPanel() throws IOException {
    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);
    editorPane.setEditorKit(kit);
    editorPane.setDocument(doc);
    // editorPane.setMaximumSize(new Dimension(520, 400));
    editorPane.setPreferredSize(new Dimension(520, 250));
    URL url = I18n.getTranslation("richclient").translationForFile("debug", "html");
    editorPane.setPage(url);
    editorPane.addHyperlinkListener(new HyperlinkListener() {

        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            openUrl(e);
        }
    });
    return editorPane;
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) HyperlinkListener(javax.swing.event.HyperlinkListener) HTMLDocument(javax.swing.text.html.HTMLDocument) JEditorPane(javax.swing.JEditorPane) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Dimension(java.awt.Dimension) URL(java.net.URL)

Aggregations

HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)47 HTMLDocument (javax.swing.text.html.HTMLDocument)18 JEditorPane (javax.swing.JEditorPane)10 StyleSheet (javax.swing.text.html.StyleSheet)9 IOException (java.io.IOException)8 JScrollPane (javax.swing.JScrollPane)8 Dimension (java.awt.Dimension)7 URL (java.net.URL)7 Document (javax.swing.text.Document)6 MouseEvent (java.awt.event.MouseEvent)5 StringReader (java.io.StringReader)5 HyperlinkEvent (javax.swing.event.HyperlinkEvent)5 ActionListener (java.awt.event.ActionListener)4 HyperlinkListener (javax.swing.event.HyperlinkListener)4 BorderLayout (java.awt.BorderLayout)3 ActionEvent (java.awt.event.ActionEvent)3 InputStream (java.io.InputStream)3 BadLocationException (javax.swing.text.BadLocationException)3 MouseAdapter (java.awt.event.MouseAdapter)2 InputStreamReader (java.io.InputStreamReader)2