Search in sources :

Example 1 with AbstractDocument

use of javax.swing.text.AbstractDocument in project jdk8u_jdk by JetBrains.

the class bug7165725 method execute.

public void execute(final String urlStr, final GoldenElement goldenElement) throws Exception {
    System.out.println();
    System.out.println("***** TEST: " + urlStr + " *****");
    System.out.println();
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            try {
                editorPane = new JEditorPane();
                editorPane.setEditorKit(new HTMLEditorKit() {

                    public Document createDefaultDocument() {
                        AbstractDocument doc = (AbstractDocument) super.createDefaultDocument();
                        doc.setAsynchronousLoadPriority(-1);
                        return doc;
                    }
                });
                editorPane.setPage(new URL(urlStr));
            } catch (IOException ex) {
                throw new RuntimeException("Test failed", ex);
            }
            editorPane.setEditable(false);
            JScrollPane scroller = new JScrollPane();
            JViewport vp = scroller.getViewport();
            vp.add(editorPane);
            add(scroller, BorderLayout.CENTER);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 400);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    });
    ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
            doc.dump(System.out);
            goldenElement.checkStructureEquivalence((AbstractElement) doc.getDefaultRootElement());
            dispose();
        }
    });
    System.out.println();
    System.out.println("*********************************");
    System.out.println();
}
Also used : AbstractDocument(javax.swing.text.AbstractDocument) AbstractElement(javax.swing.text.AbstractDocument.AbstractElement) HTMLDocument(javax.swing.text.html.HTMLDocument) SunToolkit(sun.awt.SunToolkit) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) URL(java.net.URL)

Example 2 with AbstractDocument

use of javax.swing.text.AbstractDocument in project ACS by ACS-Community.

the class FilterIntegerPanel method buildIntegerTextField.

/**
	 * All the integer text fields have the same properties:
	 * this method avoid code repetition.
	 *   
	 * @param initialValue The initial value in the text field
	 * @return The JTextField
	 * 
	 */
private JTextField buildIntegerTextField(int initialValue) {
    JTextField ret = new JTextField("" + initialValue);
    ret.setColumns(10);
    AbstractDocument document = (AbstractDocument) ret.getDocument();
    document.setDocumentFilter(new FormattedIntDocFilter(9));
    return ret;
}
Also used : AbstractDocument(javax.swing.text.AbstractDocument) JTextField(javax.swing.JTextField)

Example 3 with AbstractDocument

use of javax.swing.text.AbstractDocument 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 4 with AbstractDocument

use of javax.swing.text.AbstractDocument in project jdk8u_jdk by JetBrains.

the class TextComponentPrintable method releaseReadLock.

/**
     * Tries to release document's readlock
     *
     * Note: Not to be called on the EDT.
     */
private void releaseReadLock() {
    assert !SwingUtilities.isEventDispatchThread();
    Document document = textComponentToPrint.getDocument();
    if (document instanceof AbstractDocument) {
        try {
            ((AbstractDocument) document).readUnlock();
            needReadLock = true;
        } catch (Error ignore) {
        // readUnlock() might throw StateInvariantError
        }
    }
}
Also used : AbstractDocument(javax.swing.text.AbstractDocument) AbstractDocument(javax.swing.text.AbstractDocument) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document)

Example 5 with AbstractDocument

use of javax.swing.text.AbstractDocument in project jdk8u_jdk by JetBrains.

the class TextComponentPrintable method acquireReadLock.

/**
     * Tries to acquire document's readLock if it was released
     * in releaseReadLock() method.
     *
     * Note: Not to be called on the EDT.
     */
private void acquireReadLock() {
    assert !SwingUtilities.isEventDispatchThread();
    if (needReadLock) {
        try {
            /*
                 * wait until all the EDT events are processed
                 * some of the document changes are asynchronous
                 * we need to make sure we get the lock after those changes
                 */
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                }
            });
        } catch (InterruptedException ignore) {
        } catch (java.lang.reflect.InvocationTargetException ignore) {
        }
        Document document = textComponentToPrint.getDocument();
        ((AbstractDocument) document).readLock();
        needReadLock = false;
    }
}
Also used : AbstractDocument(javax.swing.text.AbstractDocument) AbstractDocument(javax.swing.text.AbstractDocument) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document)

Aggregations

AbstractDocument (javax.swing.text.AbstractDocument)6 HTMLDocument (javax.swing.text.html.HTMLDocument)3 JPanel (javax.swing.JPanel)2 Document (javax.swing.text.Document)2 Dimension (java.awt.Dimension)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 BoxLayout (javax.swing.BoxLayout)1 JComboBox (javax.swing.JComboBox)1 JLabel (javax.swing.JLabel)1 JTextField (javax.swing.JTextField)1 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)1 AttributeSet (javax.swing.text.AttributeSet)1 BadLocationException (javax.swing.text.BadLocationException)1