Search in sources :

Example 6 with AttributeSet

use of javax.swing.text.AttributeSet in project Botnak by Gocnak.

the class ListenerURL method mouseReleased.

@Override
public void mouseReleased(MouseEvent e) {
    JTextPane editor = (JTextPane) e.getSource();
    Point pt = new Point(e.getX(), e.getY());
    int pos = editor.viewToModel(pt);
    if (pos >= 0) {
        Document doc = editor.getDocument();
        if (doc instanceof DefaultStyledDocument) {
            DefaultStyledDocument hdoc = (DefaultStyledDocument) doc;
            Element el = hdoc.getCharacterElement(pos);
            AttributeSet a = el.getAttributes();
            String href = (String) a.getAttribute(HTML.Attribute.HREF);
            if (href != null) {
                Utils.openWebPage(href);
            }
        }
    }
}
Also used : AttributeSet(javax.swing.text.AttributeSet) Element(javax.swing.text.Element) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) Document(javax.swing.text.Document) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument)

Example 7 with AttributeSet

use of javax.swing.text.AttributeSet in project adempiere by adempiere.

the class Worker method run.

/**
	 *  Worker: Read available Online Help Pages
	 */
public void run() {
    if (m_links == null)
        return;
    URL url = null;
    try {
        url = new URL(m_urlString);
    } catch (Exception e) {
        System.err.println("OnlineHelp.Worker.run (url) - " + e);
    }
    if (url == null)
        return;
    //  Read Reference Page
    try {
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
        kit.read(new InputStreamReader(is), doc, 0);
        //  Get The Links to the Help Pages
        HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
        Object target = null;
        Object href = null;
        while (it != null && it.isValid()) {
            AttributeSet as = it.getAttributes();
            //  key keys
            if (target == null || href == null) {
                Enumeration en = as.getAttributeNames();
                while (en.hasMoreElements()) {
                    Object o = en.nextElement();
                    if (target == null && o.toString().equals("target"))
                        //	javax.swing.text.html.HTML$Attribute
                        target = o;
                    else if (href == null && o.toString().equals("href"))
                        href = o;
                }
            }
            if (target != null && "Online".equals(as.getAttribute(target))) {
                //  Format: /help/<AD_Window_ID>/index.html
                String hrefString = (String) as.getAttribute(href);
                if (hrefString != null) {
                    try {
                        //		System.err.println(hrefString);
                        String AD_Window_ID = hrefString.substring(hrefString.indexOf('/', 1), hrefString.lastIndexOf('/'));
                        m_links.put(AD_Window_ID, hrefString);
                    } catch (Exception e) {
                        System.err.println("OnlineHelp.Worker.run (help) - " + e);
                    }
                }
            }
            it.next();
        }
        is.close();
    } catch (ConnectException e) {
    //	System.err.println("OnlineHelp.Worker.run URL=" + url + " - " + e);
    } catch (UnknownHostException uhe) {
    //	System.err.println("OnlineHelp.Worker.run " + uhe);
    } catch (Exception e) {
        System.err.println("OnlineHelp.Worker.run (e) " + e);
    //	e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("OnlineHelp.Worker.run (t) " + t);
    //	t.printStackTrace();
    }
//	System.out.println("OnlineHelp - Links=" + m_links.size());
}
Also used : Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) URL(java.net.URL) UnknownHostException(java.net.UnknownHostException) ConnectException(java.net.ConnectException) URLConnection(java.net.URLConnection) AttributeSet(javax.swing.text.AttributeSet) ConnectException(java.net.ConnectException)

Example 8 with AttributeSet

use of javax.swing.text.AttributeSet in project JMRI by JMRI.

the class AbstractMonPane method initComponents.

@Override
public void initComponents() throws Exception {
    pm = jmri.InstanceManager.getNullableDefault(jmri.UserPreferencesManager.class);
    // the following code sets the frame's initial state
    // NOI18N
    clearButton.setText(Bundle.getMessage("ButtonClearScreen"));
    clearButton.setVisible(true);
    // NOI18N
    clearButton.setToolTipText(Bundle.getMessage("TooltipClearMonHistory"));
    // NOI18N
    freezeButton.setText(Bundle.getMessage("ButtonFreezeScreen"));
    freezeButton.setVisible(true);
    // NOI18N
    freezeButton.setToolTipText(Bundle.getMessage("TooltipStopScroll"));
    // NOI18N
    enterButton.setText(Bundle.getMessage("ButtonAddMessage"));
    enterButton.setVisible(true);
    // NOI18N
    enterButton.setToolTipText(Bundle.getMessage("TooltipAddMessage"));
    createDataPanes();
    // NOI18N
    entryField.setToolTipText(Bundle.getMessage("TooltipEntryPane"));
    // cap vertical size to avoid over-growth
    Dimension currentPreferredSize = entryField.getPreferredSize();
    Dimension currentMaximumSize = entryField.getMaximumSize();
    currentMaximumSize.height = currentPreferredSize.height;
    entryField.setMaximumSize(currentMaximumSize);
    //setup filterField
    // NOI18N
    filterField.setToolTipText(Bundle.getMessage("TooltipFilter"));
    filterField.setMaximumSize(currentMaximumSize);
    try {
        //restore prev values
        filterField.setText(pm.getProperty(filterFieldCheck, filterFieldCheck).toString());
    } catch (Exception e1) {
    //leave blank if previous value not retrieved
    }
    //automatically uppercase input in filterField, and only accept spaces and valid hex characters
    ((AbstractDocument) filterField.getDocument()).setDocumentFilter(new DocumentFilter() {

        // typing inserts individual characters
        static final String pattern = "[0-9a-fA-F ]*+";

        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attrs) throws BadLocationException {
            if (text.matches(pattern)) {
                // NOI18N
                fb.insertString(offset, text.toUpperCase(), attrs);
            } else {
                fb.insertString(offset, "", attrs);
            }
        }

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (text.matches(pattern)) {
                // NOI18N
                fb.replace(offset, length, text.toUpperCase(), attrs);
            } else {
                fb.replace(offset, length, "", attrs);
            }
        }
    });
    // NOI18N
    startLogButton.setText(Bundle.getMessage("ButtonStartLogging"));
    startLogButton.setVisible(true);
    // NOI18N
    startLogButton.setToolTipText(Bundle.getMessage("TooltipStartLogging"));
    // NOI18N
    stopLogButton.setText(Bundle.getMessage("ButtonStopLogging"));
    stopLogButton.setVisible(true);
    // NOI18N
    stopLogButton.setToolTipText(Bundle.getMessage("TooltipStopLogging"));
    // NOI18N
    rawCheckBox.setText(Bundle.getMessage("ButtonShowRaw"));
    rawCheckBox.setVisible(true);
    // NOI18N
    rawCheckBox.setToolTipText(Bundle.getMessage("TooltipShowRaw"));
    if (pm != null)
        rawCheckBox.setSelected(pm.getSimplePreferenceState(rawDataCheck));
    // NOI18N
    timeCheckBox.setText(Bundle.getMessage("ButtonShowTimestamps"));
    timeCheckBox.setVisible(true);
    // NOI18N
    timeCheckBox.setToolTipText(Bundle.getMessage("TooltipShowTimestamps"));
    if (pm != null)
        timeCheckBox.setSelected(pm.getSimplePreferenceState(timeStampCheck));
    // NOI18N
    alwaysOnTopCheckBox.setText(Bundle.getMessage("ButtonWindowOnTop"));
    alwaysOnTopCheckBox.setVisible(true);
    // NOI18N
    alwaysOnTopCheckBox.setToolTipText(Bundle.getMessage("TooltipWindowOnTop"));
    if (pm != null)
        alwaysOnTopCheckBox.setSelected(pm.getSimplePreferenceState(alwaysOnTopCheck));
    if (getTopLevelAncestor() != null) {
        ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
    } else {
        // which can be normal, but 
        if (alwaysOnTopCheckBox.isSelected()) {
            // in this case we want to access the enclosing frame to setAlwaysOnTop.  So defer for a bit....
            log.debug("Cannot set Always On Top from preferences due to no Top Level Ancestor");
            timerCount = 0;
            timer = new javax.swing.Timer(20, (java.awt.event.ActionEvent evt) -> {
                if (getTopLevelAncestor() != null && timerCount > 3) {
                    timer.stop();
                    ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
                    log.debug("set Always On Top");
                } else {
                    log.debug("Have to repeat attempt to set Always on Top");
                    timerCount++;
                    if (timerCount > 50) {
                        log.debug("Set Always on Top failed");
                        timer.stop();
                    }
                }
            });
            timer.start();
        }
    }
    // NOI18N
    autoScrollCheckBox.setText(Bundle.getMessage("ButtonAutoScroll"));
    autoScrollCheckBox.setVisible(true);
    // NOI18N
    autoScrollCheckBox.setToolTipText(Bundle.getMessage("TooltipAutoScroll"));
    if (pm != null)
        autoScrollCheckBox.setSelected(!pm.getSimplePreferenceState(autoScrollCheck));
    // NOI18N
    openFileChooserButton.setText(Bundle.getMessage("ButtonChooseLogFile"));
    openFileChooserButton.setVisible(true);
    // NOI18N
    openFileChooserButton.setToolTipText(Bundle.getMessage("TooltipChooseLogFile"));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // add items to GUI
    addDataPanes();
    JPanel paneA = new JPanel();
    paneA.setLayout(new BoxLayout(paneA, BoxLayout.Y_AXIS));
    JPanel pane1 = new JPanel();
    pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS));
    pane1.add(clearButton);
    pane1.add(freezeButton);
    pane1.add(rawCheckBox);
    pane1.add(timeCheckBox);
    pane1.add(alwaysOnTopCheckBox);
    pane1.add(autoScrollCheckBox);
    paneA.add(pane1);
    JPanel pane2 = new JPanel();
    pane2.setLayout(new BoxLayout(pane2, BoxLayout.X_AXIS));
    pane2.add(filterLabel);
    pane2.add(filterField);
    pane2.add(openFileChooserButton);
    pane2.add(startLogButton);
    pane2.add(stopLogButton);
    paneA.add(pane2);
    JPanel pane3 = new JPanel();
    pane3.setLayout(new BoxLayout(pane3, BoxLayout.X_AXIS));
    pane3.add(enterButton);
    pane3.add(entryField);
    paneA.add(pane3);
    add(paneA);
    // connect actions to buttons
    clearButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            clearButtonActionPerformed(e);
        }
    });
    startLogButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            startLogButtonActionPerformed(e);
        }
    });
    stopLogButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            stopLogButtonActionPerformed(e);
        }
    });
    openFileChooserButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            openFileChooserButtonActionPerformed(e);
        }
    });
    enterButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            enterButtonActionPerformed(e);
        }
    });
    alwaysOnTopCheckBox.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if (getTopLevelAncestor() != null) {
                ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
            }
        }
    });
    autoScrollCheckBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            doAutoScroll(monTextPane, autoScrollCheckBox.isSelected());
        }
    });
    // set file chooser to a default
    logFileChooser.setSelectedFile(new File("monitorLog.txt"));
    // connect to data source
    init();
}
Also used : JPanel(javax.swing.JPanel) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) AbstractDocument(javax.swing.text.AbstractDocument) Dimension(java.awt.Dimension) DocumentFilter(javax.swing.text.DocumentFilter) ActionEvent(java.awt.event.ActionEvent) BadLocationException(javax.swing.text.BadLocationException) ActionListener(java.awt.event.ActionListener) AttributeSet(javax.swing.text.AttributeSet) File(java.io.File) BadLocationException(javax.swing.text.BadLocationException)

Example 9 with AttributeSet

use of javax.swing.text.AttributeSet in project binnavi by google.

the class BaseTypeTableCellRenderer method convertDocumentToFormattedCharacterBuffer.

public static FormattedCharacterBuffer convertDocumentToFormattedCharacterBuffer(final StyledDocument document, Font font, int desiredWidth) {
    // The following code calculates the number of rows and the number of columns required for the
    // FormattedCharacterBuffer.
    int width = desiredWidth, height = 0;
    String text = "";
    try {
        text = document.getText(0, document.getLength());
    } catch (BadLocationException e) {
    // Cannot happen.
    }
    String[] chunks = text.split("\n");
    height = chunks.length;
    for (String line : chunks) {
        // The +1 is necessary because we wish to store the newline characters in the
        // FormattedCharacterBuffer.
        width = Math.max(width, line.length() + 1);
    }
    // Height & width is calculated, now create the buffer.
    FormattedCharacterBuffer result = new FormattedCharacterBuffer(height, width);
    int lineindex = 0, columnindex = 0;
    for (int index = 0; index < document.getLength(); ++index) {
        if (text.charAt(index) != '\n') {
            AttributeSet attributes = document.getCharacterElement(index).getAttributes();
            Color foreground = document.getForeground(attributes);
            Color background = document.getBackground(attributes);
            columnindex++;
            result.setAt(lineindex, columnindex, text.charAt(index), font, foreground, background);
        } else {
            columnindex = 0;
            lineindex++;
        }
    }
    return result;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) Color(java.awt.Color) BadLocationException(javax.swing.text.BadLocationException) FormattedCharacterBuffer(com.google.security.zynamics.zylib.gui.CodeDisplay.FormattedCharacterBuffer)

Example 10 with AttributeSet

use of javax.swing.text.AttributeSet in project binnavi by google.

the class SyntaxDocument method highlightLinesAfter.

/*
   * Highlight lines to start or end delimiter
   */
private void highlightLinesAfter(final String content, final int line) {
    final int offset = rootElement.getElement(line).getEndOffset();
    // Start/End delimiter not found, nothing to do
    int startDelimiter = indexOf(content, getStartDelimiter(), offset);
    int endDelimiter = indexOf(content, getEndDelimiter(), offset);
    if (startDelimiter < 0) {
        startDelimiter = content.length();
    }
    if (endDelimiter < 0) {
        endDelimiter = content.length();
    }
    final int delimiter = Math.min(startDelimiter, endDelimiter);
    if (delimiter < offset) {
        return;
    }
    // Start/End delimiter found, reapply highlighting
    final int endLine = rootElement.getElementIndex(delimiter);
    for (int i = line + 1; i < endLine; i++) {
        final Element branch = rootElement.getElement(i);
        final Element leaf = doc.getCharacterElement(branch.getStartOffset());
        final AttributeSet as = leaf.getAttributes();
        if (as.isEqual(comment)) {
            applyHighlighting(content, i);
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AttributeSet(javax.swing.text.AttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet) Element(javax.swing.text.Element)

Aggregations

AttributeSet (javax.swing.text.AttributeSet)19 Element (javax.swing.text.Element)6 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)6 HTMLDocument (javax.swing.text.html.HTMLDocument)5 BadLocationException (javax.swing.text.BadLocationException)4 Point (java.awt.Point)3 Color (java.awt.Color)2 Font (java.awt.Font)2 FontMetrics (java.awt.FontMetrics)2 Rectangle (java.awt.Rectangle)2 URL (java.net.URL)2 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)2 Document (javax.swing.text.Document)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 FormattedCharacterBuffer (com.google.security.zynamics.zylib.gui.CodeDisplay.FormattedCharacterBuffer)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 HyperlinkAdapter (com.intellij.ui.HyperlinkAdapter)1 Dimension (java.awt.Dimension)1