Search in sources :

Example 26 with Highlighter

use of javax.swing.text.Highlighter in project bytecode-viewer by Konloch.

the class PluginConsole method highlight.

public void highlight(JTextComponent textComp, String pattern) {
    if (pattern.isEmpty()) {
        textComp.getHighlighter().removeAllHighlights();
        return;
    }
    try {
        Highlighter hilite = textComp.getHighlighter();
        hilite.removeAllHighlights();
        javax.swing.text.Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;
        if (!check.isSelected()) {
            pattern = pattern.toLowerCase();
            text = text.toLowerCase();
        }
        // Search for pattern
        while ((pos = text.indexOf(pattern, pos)) >= 0) {
            // Create highlighter using private painter and apply around
            // pattern
            hilite.addHighlight(pos, pos + pattern.length(), painter);
            pos += pattern.length();
        }
    } catch (Exception e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 27 with Highlighter

use of javax.swing.text.Highlighter in project jtharness by openjdk.

the class ShowFile method showFile.

private synchronized void showFile() {
    if (frame == null) {
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension screenSize = t.getScreenSize();
        int dpi = t.getScreenResolution();
        frame = new JFrame();
        if (title != null) {
            frame.setTitle(title);
        }
        textArea = new JTextArea();
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
        textArea.setLineWrap(false);
        scrollPane = new JScrollPane(textArea);
        frame.setContentPane(scrollPane);
        Color highlightColor = new Color(0xffffb0);
        highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(highlightColor);
        int w = DEFAULT_WIDTH * dpi;
        int h = DEFAULT_HEIGHT * dpi;
        frame.setBounds(screenSize.width - w, 0, w, h);
    }
    try {
        Document d = view.getDocument();
        URL base = ((HTMLDocument) d).getBase();
        URL p = new URL(base, path);
        BufferedReader r = new BufferedReader(new InputStreamReader(p.openStream()));
        StringBuilder sb = new StringBuilder();
        char[] buf = new char[1024];
        int n;
        while ((n = r.read(buf, 0, buf.length)) != -1) {
            sb.append(buf, 0, n);
        }
        r.close();
        textArea.setText(sb.toString());
    } catch (IOException e) {
        textArea.setText(e.toString());
    }
    textArea.setSize(textArea.getPreferredSize());
    frame.validate();
    String text = textArea.getText();
    int startLineIndex = startLine == null ? -1 : text.indexOf(startLine);
    if (startLineIndex == -1) {
        textArea.setCaretPosition(0);
    } else {
        try {
            int start = getStartOfLine(text, startLineIndex);
            int end = getEndOfLine(text, startLineIndex);
            Highlighter h = textArea.getHighlighter();
            h.removeAllHighlights();
            h.addHighlight(start, end + 1, highlightPainter);
            textArea.setCaretPosition(start);
            Rectangle startRect = textArea.modelToView(start);
            JViewport vp = scrollPane.getViewport();
            int yOffset = 50;
            if (vp != null && startRect != null) {
                vp.setViewPosition(new Point(startRect.x, Math.max(0, startRect.y - yOffset)));
            }
        } catch (BadLocationException e) {
            textArea.setCaretPosition(0);
        }
    }
    frame.setVisible(true);
}
Also used : JScrollPane(javax.swing.JScrollPane) JTextArea(javax.swing.JTextArea) InputStreamReader(java.io.InputStreamReader) HTMLDocument(javax.swing.text.html.HTMLDocument) JViewport(javax.swing.JViewport) Color(java.awt.Color) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) IOException(java.io.IOException) Point(java.awt.Point) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) Point(java.awt.Point) Font(java.awt.Font) URL(java.net.URL) JFrame(javax.swing.JFrame) BufferedReader(java.io.BufferedReader) Toolkit(java.awt.Toolkit) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 28 with Highlighter

use of javax.swing.text.Highlighter in project EditCalculateAndChart by mathhobbit.

the class Calc_Action method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    String mkey, mvalue;
    // TEdit.saveOld();
    // TEdit.updateTextArea("","Untitled");
    // ActionMap m = TEdit.getActionMap();
    // m.get(DefaultEditorKit.cutAction).actionPerformed(e);
    JTextArea area = TEdit.getTextArea();
    // System.out.println(area.getSelectedText());
    Highlighter hilite = area.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    // String input;
    String insertIt;
    String txt = area.getText();
    int Shift = 0;
    if (hilites != null)
        if (hilites.length == 0 && TEdit.getSwingPool().containsKey("area.hilites")) {
            hilites = (Highlighter.Highlight[]) TEdit.getSwingPool().get("area.hilites");
            TEdit.getSwingPool().remove("area.hilites");
        }
    if (hilites != null)
        for (Highlighter.Highlight hilite1 : hilites) {
            Shift = Shift + hilite1.getStartOffset();
            String exeString = "";
            for (String input : (txt.substring(hilite1.getStartOffset(), hilite1.getEndOffset())).split("\\r?\\n")) try {
                if (input.replace(" ", "").contentEquals("")) {
                    Shift = Shift + input.length() + 1;
                    continue;
                }
                if (input.endsWith("\\")) {
                    exeString = exeString + input.substring(0, input.length() - 1);
                    Shift = Shift + input.length() + 1;
                    continue;
                }
                exeString = exeString + input;
                // if (input.trim().startsWith("?")) {
                if (exeString.trim().startsWith("?")) {
                    String name = exeString.trim().substring(1);
                    if (name.indexOf('(') != -1)
                        name = name.substring(0, name.indexOf('('));
                    try {
                        CalcFunction fn = (CalcFunction) Class.forName("org.ioblako.math.calculator.CalC" + name).getDeclaredConstructor().newInstance();
                        insertIt = System.lineSeparator() + fn.getHelp() + System.lineSeparator();
                        Shift = Shift + input.length();
                        area.insert(insertIt, Shift);
                        Shift = Shift + insertIt.length() + 1;
                    // System.out.println(fn.getHelp());
                    // TEdit.showDialog(fn.getHelp());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException exee) {
                        try {
                            if (name.contentEquals("plot"))
                                name = "grPlt";
                            FramePlt frPlt = (FramePlt) Class.forName("org.ioblako.edit." + name).getDeclaredConstructor().newInstance();
                            Shift = Shift + input.length();
                            insertIt = System.lineSeparator() + frPlt.getHelp() + System.lineSeparator();
                            area.insert(insertIt, Shift);
                            Shift = Shift + insertIt.length() + 1;
                        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException exee1) {
                            TEdit.showDialog("There is no help for \"" + exeString.substring(1) + "\"");
                        }
                    }
                    exeString = "";
                    // return;
                    continue;
                }
                // if(input.contains("<-") && !input.endsWith("<-")){
                if (exeString.contains("<-") && !exeString.endsWith("<-")) {
                    Shift = Shift + input.length() + 1;
                    mkey = exeString.trim().substring(0, exeString.trim().indexOf("<-")).trim();
                    mvalue = exeString.substring(exeString.indexOf("<-") + 2).trim();
                    try {
                        if (mvalue.length() > 5) {
                            switch(mvalue.trim().toLowerCase().substring(0, 5)) {
                                case "eval(":
                                    TEdit.put(mkey, jc.eval(jc.getInside(substitute(mvalue.substring(5)), '(', ')')));
                                    jc.Report = "";
                                    break;
                                case "file(":
                                    File fl = new File(jc.getInside(substitute(mvalue.trim().substring(5)), '(', ')'));
                                    if (!fl.exists() || fl.isDirectory()) {
                                        JFileChooser dialog = TEdit.getFileChooser();
                                        if (fl.isDirectory())
                                            dialog.setCurrentDirectory(fl);
                                        if (dialog.showSaveDialog((Component) TEdit) == JFileChooser.APPROVE_OPTION) {
                                            fl = new File(dialog.getSelectedFile().getAbsolutePath());
                                            TEdit.put(mkey, returnFile(fl));
                                            exeString = "";
                                            // return;
                                            continue;
                                        }
                                        exeString = "";
                                        // return;
                                        continue;
                                    } else {
                                        TEdit.put(mkey, returnFile(fl));
                                        exeString = "";
                                        // return;
                                        continue;
                                    }
                                default:
                                    TEdit.put(mkey, mvalue);
                                    exeString = "";
                                    break;
                            }
                        } else
                            TEdit.put(mkey, mvalue);
                    } catch (Exception evalEx) {
                        TEdit.showDialog(evalEx.getMessage());
                    }
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.endsWith("->")) {
                    mkey = exeString.trim().substring(0, exeString.trim().indexOf("->"));
                    if (TEdit.containsKey(mkey)) {
                        Shift = Shift + input.length();
                        insertIt = System.lineSeparator() + mkey + " = " + TEdit.get(mkey) + System.lineSeparator();
                        area.insert(insertIt, Shift);
                        // Shift=Shift+insertIt.length()+1;
                        Shift = Shift + insertIt.length();
                    } else {
                        TEdit.showDialog("Memory does not contain \"" + mkey + "\"");
                    }
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.contains("->")) {
                    mkey = exeString.trim().substring(0, exeString.trim().indexOf("->"));
                    mvalue = exeString.substring(exeString.indexOf("->") + 2).trim();
                    Shift = Shift + input.length() + 1;
                    if (TEdit.containsKey(mkey) && mvalue.trim().length() > 5 && mvalue.trim().toLowerCase().substring(0, 5).contentEquals("file(")) {
                        File fl = new File(jc.getInside(substitute(mvalue.trim().substring(5)), '(', ')'));
                        if (fl.isDirectory()) {
                            JFileChooser dialog = TEdit.getFileChooser();
                            dialog.setCurrentDirectory(fl);
                            if (dialog.showSaveDialog((Component) TEdit) == JFileChooser.APPROVE_OPTION) {
                                fl = new File(dialog.getSelectedFile().getAbsolutePath());
                                writeFile(TEdit.get(mkey), fl);
                                exeString = "";
                                // return;
                                continue;
                            }
                            exeString = "";
                            // return;
                            continue;
                        } else {
                            if (fl.getParentFile() == null || !fl.getParentFile().isDirectory()) {
                                JFileChooser dialog = TEdit.getFileChooser();
                                if (dialog.showSaveDialog((Component) TEdit) == JFileChooser.APPROVE_OPTION) {
                                    fl = new File(dialog.getSelectedFile().getAbsolutePath());
                                    writeFile(TEdit.get(mkey), fl);
                                    exeString = "";
                                    // return;
                                    continue;
                                }
                            } else
                                writeFile(TEdit.get(mkey), fl);
                            exeString = "";
                            // return;
                            continue;
                        }
                    }
                    if (TEdit.containsKey(mkey)) {
                        TEdit.put(mvalue, TEdit.get(mkey));
                    } else {
                        TEdit.showDialog("Memory does not contain \"" + mkey + "\"");
                    }
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.trim().contentEquals("clear")) {
                    Shift = Shift + input.length() + 1;
                    TEdit.clear();
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.trim().startsWith("clear") && exeString.length() > 5) {
                    Shift = Shift + input.length() + 1;
                    mkey = exeString.trim().substring(5).trim();
                    if (TEdit.containsKey(mkey)) {
                        TEdit.remove(mkey);
                    } else {
                        TEdit.showDialog("Memory does not contain \"" + mkey + "\"");
                    }
                    exeString = "";
                    // return;
                    continue;
                }
                int input_length = input.length();
                exeString = substitute(exeString);
                // input.startsWith("plrPlt")){
                if (exeString.trim().startsWith("plot")) {
                    Shift = Shift + input_length + 1;
                    // String inputV=input.trim();
                    FramePlt frm = (FramePlt) Class.forName("org.ioblako.edit.grPlt").getDeclaredConstructor().newInstance();
                    frm.setInput("grPlt" + exeString.trim().substring(4));
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    // MathContext MathC= jc.MC;
                    javax.swing.SwingUtilities.invokeLater(() -> {
                        try {
                            frm.createAndShow();
                        } catch (Exception e1) {
                            TEdit.showDialog(e1.getMessage());
                        // System.out.println(e1.getMessage());
                        }
                    });
                    // jc.setMathContext(MathC);
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.indexOf('(') != -1 && exeString.substring(0, exeString.indexOf('(')).contains("Plt")) {
                    Shift = Shift + input_length + 1;
                    String inputV = exeString.trim();
                    FramePlt frm = (FramePlt) Class.forName("org.ioblako.edit." + inputV.substring(0, inputV.indexOf('('))).getDeclaredConstructor().newInstance();
                    frm.setInput(inputV);
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    // MathContext MathC= jc.MC;
                    javax.swing.SwingUtilities.invokeLater(() -> {
                        try {
                            frm.createAndShow();
                        } catch (Exception e1) {
                            TEdit.showDialog(e1.getMessage());
                        // System.out.println(e1.getMessage());
                        }
                    });
                    // jc.setMathContext(MathC);
                    exeString = "";
                    // return;
                    continue;
                }
                if (exeString.trim().toLowerCase().startsWith("file(")) {
                    File fl = new File(jc.getInside(exeString.trim().substring(5), '(', ')'));
                    if (!fl.exists() || fl.isDirectory()) {
                        JFileChooser dialog = TEdit.getFileChooser();
                        if (dialog.showOpenDialog((Component) TEdit) == JFileChooser.APPROVE_OPTION) {
                            fl = new File(dialog.getSelectedFile().getAbsolutePath());
                            Shift = Shift + input_length;
                            insertIt = System.lineSeparator() + returnFile(fl) + System.lineSeparator();
                            area.insert(insertIt, Shift);
                            Shift = Shift + insertIt.length() + 1;
                            exeString = "";
                            // return;
                            continue;
                        }
                    } else {
                        Shift = Shift + input_length;
                        insertIt = System.lineSeparator() + returnFile(fl) + System.lineSeparator();
                        area.insert(insertIt, Shift);
                        Shift = Shift + insertIt.length() + 1;
                        exeString = "";
                        // return;
                        continue;
                    }
                }
                if (exeString.indexOf('=') != -1 && !exeString.contains("dffEq") && !exeString.contains("Rec") && !exeString.contains("Seq") && !exeString.contains("Int") && !exeString.contains("xySeq") && !exeString.contains("plot") && !exeString.contains("Plt")) {
                    String prev = null;
                    insertIt = "";
                    for (String current : exeString.split("=")) {
                        if (prev != null) {
                            current = jc.eval(current.trim());
                            if (current.contentEquals(prev)) {
                                insertIt = System.lineSeparator() + prev + " = " + current;
                            } else {
                                insertIt = System.lineSeparator() + prev + " ?= " + current;
                            }
                            prev = current;
                        } else
                            prev = jc.eval(current.trim());
                    }
                    if (jc.Report.contentEquals("")) {
                        Shift = Shift + input_length;
                        insertIt = insertIt + System.lineSeparator();
                        area.insert(insertIt, Shift);
                        Shift = Shift + insertIt.length() + 1;
                    } else {
                        Shift = Shift + input_length;
                        insertIt = insertIt + System.lineSeparator() + jc.Report + System.lineSeparator();
                        area.insert(insertIt, Shift);
                        Shift = Shift + insertIt.length() + 1;
                    }
                    jc.Report = "";
                    exeString = "";
                    // return;
                    continue;
                }
                /*  if(input.trim().startsWith("xySeq")){
                               insertIt = jc.hInside("xySeq(", input,'(',')')[1];
                               insertIt=(new CalCxySeq()).eval(insertIt);
                           }
                           else
                     */
                insertIt = jc.eval(substitute(exeString));
                if (jc.Report.contentEquals("")) {
                    insertIt = "=" + insertIt;
                    Shift = Shift + input_length;
                    area.insert(insertIt, Shift);
                    Shift = Shift + insertIt.length() + 1;
                } else {
                    Shift = Shift + input_length;
                    insertIt = "=" + insertIt + System.lineSeparator() + jc.Report + System.lineSeparator();
                    area.insert(insertIt, Shift);
                    Shift = Shift + insertIt.length() + 1;
                }
                jc.Report = "";
            // hilite.removeHighlight(hilites[i]);
            } catch (Exception Ex) {
                TEdit.showDialog(Ex.toString());
                exeString = "";
                return;
            }
        }
    // TEdit.setEnabled("Delete",false);
    TEdit.setEnabled("Calc", false);
    TEdit.setEnabled("Save", true);
    TEdit.setEnabled("SaveAs", true);
// displayResult("Action for first button/menu item", e);
}
Also used : JTextArea(javax.swing.JTextArea) JFileChooser(javax.swing.JFileChooser) Component(java.awt.Component) File(java.io.File) Highlighter(javax.swing.text.Highlighter)

Example 29 with Highlighter

use of javax.swing.text.Highlighter in project EditCalculateAndChart by mathhobbit.

the class Delete_Action method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    JTextArea area = TEdit.getTextArea();
    Highlighter hilite = area.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    int Shift = 0;
    if (hilites != null) {
        if (hilites.length == 0 && TEdit.getSwingPool().containsKey("area.hilites")) {
            hilites = (Highlighter.Highlight[]) TEdit.getSwingPool().get("area.hilites");
            TEdit.getSwingPool().remove("area.hilites");
        }
        for (Highlighter.Highlight hilite1 : hilites) {
            area.replaceRange("", hilite1.getStartOffset() - Shift, hilite1.getEndOffset() - Shift);
            Shift = Shift - (hilite1.getEndOffset() - hilite1.getStartOffset());
        }
        if (hilites.length > 0) {
            area.setCaretPosition(hilites[0].getStartOffset());
            area.getCaret().setVisible(true);
        }
        TEdit.setEnabled("Calc", false);
        TEdit.setEnabled("Delete", false);
        TEdit.setEnabled("Save", true);
        TEdit.setEnabled("SaveAs", true);
    }
}
Also used : JTextArea(javax.swing.JTextArea) Highlighter(javax.swing.text.Highlighter)

Example 30 with Highlighter

use of javax.swing.text.Highlighter in project EditCalculateAndChart by mathhobbit.

the class TextAreaKeyListener method keyReleased.

@Override
public void keyReleased(KeyEvent e) {
    JTextArea area = TEdit.getTextArea();
    Highlighter hilite = area.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    if (hilites != null && hilites.length > 0) {
        TEdit.setEnabled("Delete", true);
        TEdit.setEnabled("Calc", true);
    } else {
        TEdit.setEnabled("Delete", false);
        TEdit.setEnabled("Calc", false);
    }
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Also used : JTextArea(javax.swing.JTextArea) Highlighter(javax.swing.text.Highlighter)

Aggregations

Highlighter (javax.swing.text.Highlighter)68 BadLocationException (javax.swing.text.BadLocationException)40 DefaultHighlighter (javax.swing.text.DefaultHighlighter)37 Matcher (java.util.regex.Matcher)12 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)10 JTextArea (javax.swing.JTextArea)9 Document (javax.swing.text.Document)9 Point (java.awt.Point)6 File (java.io.File)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 DefaultHighlightPainter (javax.swing.text.DefaultHighlighter.DefaultHighlightPainter)5 Editor (omega.ui.component.Editor)5 StringTokenizer (java.util.StringTokenizer)4 JTextComponent (javax.swing.text.JTextComponent)4 Highlight (omega.instant.support.Highlight)4 IOException (java.io.IOException)3 ImageIcon (javax.swing.ImageIcon)3 AbstractErrorHighlighter (omega.instant.support.AbstractErrorHighlighter)3 JavaSyntaxParserGutterIconInfo (omega.instant.support.java.parser.JavaSyntaxParserGutterIconInfo)3 SquiggleUnderlineHighlightPainter (org.fife.ui.rsyntaxtextarea.SquiggleUnderlineHighlightPainter)3