Example usage for javax.swing.text Document StreamDescriptionProperty

List of usage examples for javax.swing.text Document StreamDescriptionProperty

Introduction

In this page you can find the example usage for javax.swing.text Document StreamDescriptionProperty.

Prototype

String StreamDescriptionProperty

To view the source code for javax.swing.text Document StreamDescriptionProperty.

Click Source Link

Document

The property name for the description of the stream used to initialize the document.

Usage

From source file:de.codesourcery.jasm16.ide.ui.views.CPUView.java

private void internalRefreshDisplay() {
    if (emulator == null) {
        return;//from   w w w  . j a va2  s.  co m
    }
    final IReadOnlyCPU cpu = emulator.getCPU();

    final StringBuilder builder = new StringBuilder();
    final List<ITextRegion> redRegions = new ArrayList<ITextRegion>();

    Throwable lastError = emulator.getLastEmulationError();
    if (lastError != null) {
        final String msg = StringUtils.isBlank(lastError.getMessage()) ? lastError.getClass().getName()
                : lastError.getMessage();
        builder.append("Emulation stopped with an error: " + msg + "\n");
        redRegions.add(new TextRegion(0, builder.length()));
    }

    int itemsInLine = 0;
    for (int i = 0; i < ICPU.COMMON_REGISTER_NAMES.length; i++) {
        final int value = cpu.getRegisterValue(ICPU.COMMON_REGISTERS[i]);
        builder.append(ICPU.COMMON_REGISTER_NAMES[i] + ": " + Misc.toHexString(value) + "    ");

        Address address = Address.wordAddress(value);
        final byte[] data = MemUtils.getBytes(emulator.getMemory(), address, Size.words(4), true);

        builder.append(Misc.toHexDump(address, data, data.length, 4, true, false, true));
        builder.append("\n");
        itemsInLine++;
        if (itemsInLine == 4) {
            itemsInLine = 0;
            builder.append("\n");
        }
    }
    builder.append("\nPC: " + Misc.toHexString(cpu.getPC().getValue()));
    builder.append(" (elapsed cycles: " + cpu.getCurrentCycleCount()).append(")");
    builder.append("\n");

    builder.append("EX: " + Misc.toHexString(cpu.getEX())).append("\n");
    builder.append("IA: " + Misc.toHexString(cpu.getInterruptAddress())).append("\n");

    builder.append("IQ: Interrupt queueing is ");
    if (cpu.isQueueInterrupts()) {
        int start = builder.length();
        builder.append("ON");
        redRegions.add(new TextRegion(start, builder.length() - start));
    } else {
        builder.append("OFF");
    }
    builder.append("\n");
    builder.append("IRQs: " + StringUtils.join(cpu.getInterruptQueue(), ",")).append("\n");
    builder.append("SP: " + Misc.toHexString(cpu.getSP().getValue())).append("\n");

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            final StyledDocument doc = textArea.getStyledDocument();
            doc.putProperty(Document.StreamDescriptionProperty, null);

            textArea.setText(builder.toString());
            for (ITextRegion region : redRegions) {
                doc.setCharacterAttributes(region.getStartingOffset(), region.getLength(), errorStyle, true);
            }
        }
    });
}

From source file:EditorPaneExample10A.java

public URL[] findLinks(Document doc, String protocol) {
    Vector links = new Vector();
    Vector urlNames = new Vector();
    URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty);

    if (doc instanceof HTMLDocument) {
        HTMLDocument.Iterator iterator = ((HTMLDocument) doc).getIterator(HTML.Tag.A);
        for (; iterator.isValid(); iterator.next()) {
            AttributeSet attrs = iterator.getAttributes();
            Object linkAttr = attrs.getAttribute(HTML.Attribute.HREF);
            if (linkAttr instanceof String) {
                try {
                    URL linkURL = new URL(baseURL, (String) linkAttr);
                    if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) {
                        String linkURLName = linkURL.toString();
                        if (urlNames.contains(linkURLName) == false) {
                            urlNames.addElement(linkURLName);
                            links.addElement(linkURL);
                        }/*from  w  w w.j  a  v a  2  s  .c  o  m*/
                    }
                } catch (MalformedURLException e) {
                    // Ignore invalid links
                }
            }
        }
    }

    URL[] urls = new URL[links.size()];
    links.copyInto(urls);
    links.removeAllElements();
    urlNames.removeAllElements();

    return urls;
}

From source file:EditorPaneExample16.java

public URL[] findLinks(Document doc, String protocol) {
    Vector links = new Vector();
    Vector urlNames = new Vector();
    URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty);

    if (doc instanceof HTMLDocument) {
        Element elem = doc.getDefaultRootElement();
        ElementIterator iterator = new ElementIterator(elem);

        while ((elem = iterator.next()) != null) {
            AttributeSet attrs = elem.getAttributes();
            Object link = attrs.getAttribute(HTML.Tag.A);
            if (link instanceof AttributeSet) {
                Object linkAttr = ((AttributeSet) link).getAttribute(HTML.Attribute.HREF);
                if (linkAttr instanceof String) {
                    try {
                        URL linkURL = new URL(baseURL, (String) linkAttr);
                        if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) {
                            String linkURLName = linkURL.toString();
                            if (urlNames.contains(linkURLName) == false) {
                                urlNames.addElement(linkURLName);
                                links.addElement(linkURL);
                            }/*from  w ww . j  av a 2s. c om*/
                        }
                    } catch (MalformedURLException e) {
                        // Ignore invalid links
                    }
                }
            }
        }
    }

    URL[] urls = new URL[links.size()];
    links.copyInto(urls);
    links.removeAllElements();
    urlNames.removeAllElements();

    return urls;
}

From source file:EditorPaneExample16.java

public HTMLDocument loadDocument(HTMLDocument doc, URL url, String charSet) throws IOException {
    doc.putProperty(Document.StreamDescriptionProperty, url);

    /*//from w w  w.  j  av  a  2s  . c o  m
     * This loop allows the document read to be retried if the character
     * encoding changes during processing.
     */
    InputStream in = null;
    boolean ignoreCharSet = false;

    for (;;) {
        try {
            // Remove any document content
            doc.remove(0, doc.getLength());

            URLConnection urlc = url.openConnection();
            in = urlc.getInputStream();
            Reader reader = (charSet == null) ? new InputStreamReader(in) : new InputStreamReader(in, charSet);

            HTMLEditorKit.Parser parser = getParser();
            HTMLEditorKit.ParserCallback htmlReader = getParserCallback(doc);
            parser.parse(reader, htmlReader, ignoreCharSet);
            htmlReader.flush();

            // All done
            break;
        } catch (BadLocationException ex) {
            // Should not happen - throw an IOException
            throw new IOException(ex.getMessage());
        } catch (ChangedCharSetException e) {
            // The character set has changed - restart
            charSet = getNewCharSet(e);

            // Prevent recursion by suppressing further exceptions
            ignoreCharSet = true;

            // Close original input stream
            in.close();

            // Continue the loop to read with the correct encoding
        }
    }

    return doc;
}

From source file:de.codesourcery.jasm16.utils.ASTInspector.java

private void openFile(final File file) throws IOException {

    FileInputStream in = new FileInputStream(file);
    final String source;
    try {/*from w  ww . java 2 s . co  m*/
        source = Misc.readSource(in);
    } finally {
        in.close();
    }

    disableDocumentListener();
    final Document doc = editorPane.getDocument();
    doc.putProperty(Document.StreamDescriptionProperty, null);

    editorPane.setText(source);

    final IResource resource = new AbstractResource(ResourceType.UNKNOWN) {

        @Override
        public String readText(ITextRegion range) throws IOException {
            return range.apply(getSourceFromEditor());
        }

        private String getSourceFromEditor() throws IOException {
            try {
                return editorPane.getDocument().getText(0, editorPane.getDocument().getLength());
            } catch (BadLocationException e) {
                throw new IOException("Internal error", e);
            }
        }

        @Override
        public long getAvailableBytes() throws IOException {
            return editorPane.getDocument().getLength();
        }

        @Override
        public OutputStream createOutputStream(boolean append) throws IOException {
            throw new UnsupportedOperationException("Not implemented");
        }

        @Override
        public InputStream createInputStream() throws IOException {
            return new ByteArrayInputStream(getSourceFromEditor().getBytes());
        }

        @Override
        public String getIdentifier() {
            return file.getAbsolutePath();
        }
    };

    this.file = file;
    this.currentUnit = CompilationUnit.createInstance(file.getAbsolutePath(), resource);

    enableDocumentListener();

    frame.setTitle(Compiler.VERSION + "    /    " + file.getName());
    compile();
}

From source file:de.codesourcery.jasm16.ide.ui.views.SourceCodeView.java

protected final void openResource(final IAssemblyProject project, final IResource sourceFile, int caretPosition,
        boolean compileSource) throws IOException {
    if (project == null) {
        throw new IllegalArgumentException("project must not be NULL");
    }// ww  w.j a va 2 s  .  c om
    if (sourceFile == null) {
        throw new IllegalArgumentException("sourceFile must not be NULL");
    }

    // read source first so we don't discard internal state
    // and end up with an IOException later on...
    final String source = Misc.readSource(sourceFile);
    this.initialHashCode = Misc.calcHash(source);

    this.project = project;
    if (sourceFile instanceof InMemorySourceResource) {
        this.sourceInMemory = (InMemorySourceResource) sourceFile;
        this.persistentResource = sourceInMemory.getPersistentResource();
    } else {
        this.sourceInMemory = new InMemorySourceResource(sourceFile, editorPane) {

            @Override
            public String toString() {
                return "SourceCodeView[ " + persistentResource + " ]";
            }
        };
        this.persistentResource = sourceFile;
    }
    clearHighlight();

    try {
        disableDocumentListener();
        try {
            final Document doc = editorPane.getDocument();
            doc.putProperty(Document.StreamDescriptionProperty, null);

            disableNavigationHistoryUpdates();
            System.out.println("Text length: " + (source == null ? 0 : source.length()));

            try {
                editorPane.setText(source);
            } finally {
                enableNavigationHistoryUpdates();
            }

            try {
                editorPane.setCaretPosition(caretPosition);
            } catch (IllegalArgumentException e) {
                LOG.error("openResource(): Invalid caret position " + caretPosition + " in resource "
                        + sourceFile);
            }

            if (panel != null) {
                ICompilationUnit existing = null;
                if (!compileSource) {
                    existing = project.getProjectBuilder().getCompilationUnit(sourceFile);
                }
                validateSourceCode(existing);
            }
        } finally {
            enableDocumentListener();
        }
    } finally {
        enableNavigationHistoryUpdates();
    }

    editorPane.requestFocus();
    updateTitle();
}

From source file:org.debux.webmotion.netbeans.Utils.java

public static FileObject getFO(Document doc) {
    Object sdp = doc.getProperty(Document.StreamDescriptionProperty);
    if (sdp instanceof FileObject) {
        return (FileObject) sdp;
    }/*from w w w.  j  a v a  2s. c o  m*/
    if (sdp instanceof DataObject) {
        DataObject dobj = (DataObject) sdp;
        return dobj.getPrimaryFile();
    }
    return null;
}

From source file:org.fit.cssbox.swingbox.SwingBoxEditorKit.java

private void readImpl(InputStream in, SwingBoxDocument doc, int pos) throws IOException, BadLocationException {

    if (component == null)
        throw new IllegalStateException(
                "Component is null, editor kit is probably deinstalled from a JEditorPane.");
    if (pos > doc.getLength() || pos < 0) {
        BadLocationException e = new BadLocationException("Invalid location", pos);
        readError(null, e);//from   ww w .j  av a  2s.  c om
        throw e;
    }

    ContentReader rdr = new ContentReader();
    URL url = (URL) doc.getProperty(Document.StreamDescriptionProperty);
    CSSBoxAnalyzer analyzer = getCSSBoxAnalyzer();

    Container parent = component.getParent();
    Dimension dim;
    if (parent != null && parent instanceof JViewport) {
        dim = ((JViewport) parent).getExtentSize();
    } else {
        dim = component.getBounds().getSize();
    }

    if (dim.width <= 10) {
        // component might not be initialized, use screen size :)
        Dimension tmp = Toolkit.getDefaultToolkit().getScreenSize();
        dim.setSize(tmp.width / 2.5, tmp.height / 2.5);
    }

    // long time = System.currentTimeMillis();

    List<ElementSpec> elements;
    try {
        String ctype = null;
        Object ct = doc.getProperty("Content-Type");
        if (ct != null) {
            if (ct instanceof List)
                ctype = (String) ((List<?>) ct).get(0);
            else
                ctype = ct.toString();
        }

        DocumentSource docSource = new StreamDocumentSource(in, url, ctype);
        elements = rdr.read(docSource, analyzer, dim);
        String title = analyzer.getDocumentTitle();
        if (title == null)
            title = "No title";
        doc.putProperty(Document.TitleProperty, title);
    } catch (IOException e) {
        readError(url, e);
        throw e;
    }

    // System.out.println(System.currentTimeMillis() - time + " ms");

    ElementSpec elementsArray[] = elements.toArray(new ElementSpec[0]);
    doc.create(elementsArray);
    // component.revalidate();
    // component.repaint();

    // System.out.println(System.currentTimeMillis() - time + " ms");

    // Dictionary<Object, Object> dic = doc.getDocumentProperties();
    // Enumeration<Object> en = dic.keys();
    // while( en.hasMoreElements()) {
    // Object k = en.nextElement();
    // System.out.println(k + "  " + dic.get(k));
    // }

    readFinish(url);

}