Search in sources :

Example 1 with AttributeSet

use of javax.swing.text.AttributeSet in project pcgen by PCGen.

the class NotesView method updateButtons.

//Methods for dealing with button appearance
/**
	 *  Updates Editing buttons based on the location of the cursor
	 *
	 *@param  textPane  text pane to update buttons base on
	 *@param  pos       current text position
	 */
private void updateButtons(JTextPane textPane, int pos) {
    StyledDocument doc = textPane.getStyledDocument();
    AttributeSet set = doc.getCharacterElement(pos - 1).getAttributes();
    AttributeSet set1 = doc.getCharacterElement(pos).getAttributes();
    if (StyleConstants.isBold(set) && StyleConstants.isBold(set1)) {
        highlightButton(boldButton);
    } else {
        stdButton(boldButton);
    }
    if (StyleConstants.isItalic(set) && StyleConstants.isItalic(set1)) {
        highlightButton(italicButton);
    } else {
        stdButton(italicButton);
    }
    if (StyleConstants.isUnderline(set) && StyleConstants.isUnderline(set1)) {
        highlightButton(underlineButton);
    } else {
        stdButton(underlineButton);
    }
    int align = StyleConstants.getAlignment(set);
    stdButton(leftJustifyButton);
    stdButton(rightJustifyButton);
    stdButton(centerJustifyButton);
    if (align == StyleConstants.ALIGN_LEFT) {
        highlightButton(leftJustifyButton);
    } else if (align == StyleConstants.ALIGN_RIGHT) {
        highlightButton(rightJustifyButton);
    } else if (align == StyleConstants.ALIGN_CENTER) {
        highlightButton(centerJustifyButton);
    }
    int fontSize = StyleConstants.getFontSize(set);
    for (int i = 0; i < sizeCB.getItemCount(); i++) {
        String value = (String) sizeCB.getItemAt(i);
        if (value.equals(Integer.toString(fontSize))) {
            sizeCB.setSelectedItem(value);
            break;
        }
    }
}
Also used : AttributeSet(javax.swing.text.AttributeSet) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point)

Example 2 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 3 with AttributeSet

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

the class OutputPanel method createSearchPanel.

private Component createSearchPanel() {
    StyleContext styleContent = StyleContext.getDefaultStyleContext();
    AttributeSet highlightStyle = gradleOutputTextPane.getDefaultStyle().copyAttributes();
    highlightStyle = styleContent.addAttribute(highlightStyle, StyleConstants.Foreground, Color.white);
    highlightStyle = styleContent.addAttribute(highlightStyle, StyleConstants.Background, Color.orange);
    highlightStyle = styleContent.addAttribute(highlightStyle, StyleConstants.Underline, true);
    AttributeSet emphasizedHighlightStyle = highlightStyle.copyAttributes();
    emphasizedHighlightStyle = styleContent.addAttribute(emphasizedHighlightStyle, StyleConstants.Foreground, Color.black);
    emphasizedHighlightStyle = styleContent.addAttribute(emphasizedHighlightStyle, StyleConstants.Background, Color.yellow);
    searchPanel = new SearchPanel(new OutputPanelSearchInteraction(gradleOutputTextPane.getTextComponent(), gradleOutputTextPane.getDefaultStyle(), highlightStyle, emphasizedHighlightStyle));
    searchPanel.hide();
    return searchPanel.getComponent();
}
Also used : AttributeSet(javax.swing.text.AttributeSet) SearchPanel(org.gradle.gradleplugin.userinterface.swing.common.SearchPanel) StyleContext(javax.swing.text.StyleContext)

Example 4 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 5 with AttributeSet

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

the class AbstractNumberHandler method init.

private void init() {
    format = null;
    if (getFormat() != null && getFormat().length() > 0) {
        format = new DecimalFormat(getFormat());
    }
    Number d = null;
    try {
        d = (Number) getNumberValue();
    } catch (final Exception e) {
        e.printStackTrace();
        d = Double.valueOf(0.0);
    }
    // Figure out how to handle the format for this particular value
    if (format == null) {
        double dx = d.doubleValue();
        if (dx > 1000000.0 || dx < 0.001)
            format = new DecimalFormat("0.#####E0");
        else
            format = new DecimalFormat();
    }
    format.setGroupingUsed(false);
    // Set Gui
    DocumentFilter filter = (new DocumentFilter() {

        @Override
        public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
            Document doc = fb.getDocument();
            StringBuilder sb = new StringBuilder();
            sb.append(doc.getText(0, doc.getLength()));
            sb.delete(offset, offset + length);
            if (!isUpdating) {
                if (!setValueFromText(sb.toString())) {
                    return;
                }
            }
            super.remove(fb, offset, length);
        }

        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            Document doc = fb.getDocument();
            StringBuilder sb = new StringBuilder();
            sb.append(doc.getText(0, doc.getLength()));
            sb.insert(offset, string);
            if (!isUpdating) {
                if (!setValueFromText(sb.toString())) {
                    return;
                }
            }
            super.insertString(fb, offset, string, attr);
        }

        @Override
        public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
            Document doc = fb.getDocument();
            StringBuilder sb = new StringBuilder();
            sb.append(doc.getText(0, doc.getLength()));
            sb.replace(i, i + i1, string);
            if (!isUpdating) {
                if (!setValueFromText(sb.toString())) {
                    return;
                }
            }
            super.replace(fb, i, i1, string, as);
        }
    });
    textField = new JTextField(format.format(d));
    textField.setPreferredSize(new Dimension(GUIDefaults.TEXT_BOX_WIDTH, textField.getPreferredSize().height));
    textField.setHorizontalAlignment(JTextField.RIGHT);
    textField.addFocusListener(this);
    ((AbstractDocument) textField.getDocument()).setDocumentFilter(filter);
    final JLabel label = new JLabel(getDescription());
    updateFieldPanel(panel, label, textField, horizontal);
    setTooltip(getTooltip(), label, textField);
}
Also used : DecimalFormat(java.text.DecimalFormat) JLabel(javax.swing.JLabel) DocumentFilter(javax.swing.text.DocumentFilter) Dimension(java.awt.Dimension) AbstractDocument(javax.swing.text.AbstractDocument) Document(javax.swing.text.Document) JTextField(javax.swing.JTextField) BadLocationException(javax.swing.text.BadLocationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractDocument(javax.swing.text.AbstractDocument) AttributeSet(javax.swing.text.AttributeSet) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

AttributeSet (javax.swing.text.AttributeSet)44 Element (javax.swing.text.Element)15 MutableAttributeSet (javax.swing.text.MutableAttributeSet)10 BadLocationException (javax.swing.text.BadLocationException)9 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)9 HTMLDocument (javax.swing.text.html.HTMLDocument)7 AbstractDocument (javax.swing.text.AbstractDocument)6 Document (javax.swing.text.Document)5 DocumentFilter (javax.swing.text.DocumentFilter)5 Font (java.awt.Font)3 Point (java.awt.Point)3 URL (java.net.URL)3 HTML (javax.swing.text.html.HTML)3 java.awt (java.awt)2 Color (java.awt.Color)2 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 Rectangle (java.awt.Rectangle)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2