Java Code Examples for javax.swing.JEditorPane#setEditorKit()

The following examples show how to use javax.swing.JEditorPane#setEditorKit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwingForm.java    From teamengine with Apache License 2.0 6 votes vote down vote up
private SwingForm(String name) {
    setTitle(name);
    // setBackground(Color.gray);
    getContentPane().setLayout(new BorderLayout());

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    jedit = new JEditorPane();
    jedit.setEditorKit(new CustomHTMLEditorKit());
    jedit.setEditable(false);
    jedit.addHyperlinkListener(this);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().add(jedit, BorderLayout.CENTER);
    topPanel.add(scrollPane, BorderLayout.CENTER);
}
 
Example 2
Source File: PlainTextEditor.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
public PlainTextEditor (final String text) {
  initComponents();

  final JEditorPane editor = UI_COMPO_FACTORY.makeEditorPane();
  editor.setEditorKit(getEditorKit());
  this.document = Utilities.getDocument(editor);

  setText(text);

  final Preferences docPreferences = CodeStylePreferences.get(this.document).getPreferences();
  this.oldWrapping = Wrapping.findFor(docPreferences.get(SimpleValueNames.TEXT_LINE_WRAP, "none"));
  this.wrapping = oldWrapping;

  this.lastComponent = makeEditorForText(this.document);
  this.lastComponent.setPreferredSize(new Dimension(620, 440));
  this.add(this.lastComponent, BorderLayout.CENTER);

  this.labelWrapMode.setMinimumSize(new Dimension(55, this.labelWrapMode.getMinimumSize().height));

  updateBottomPanel();
}
 
Example 3
Source File: CodeTemplatesTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@RandomlyFails
public void testMemoryRelease() throws Exception { // Issue #147984
    org.netbeans.junit.Log.enableInstances(Logger.getLogger("TIMER"), "CodeTemplateInsertHandler", Level.FINEST);

    JEditorPane pane = new JEditorPane();
    NbEditorKit kit = new NbEditorKit();
    pane.setEditorKit(kit);
    Document doc = pane.getDocument();
    assertTrue(doc instanceof BaseDocument);
    CodeTemplateManager mgr = CodeTemplateManager.get(doc);
    String templateText = "Test with parm ";
    CodeTemplate ct = mgr.createTemporary(templateText + " ${a}");
    ct.insert(pane);
    assertEquals(templateText + " a", doc.getText(0, doc.getLength()));

    // Send Enter to stop editing
    KeyEvent enterKeyEvent = new KeyEvent(pane, KeyEvent.KEY_PRESSED,
            EventQueue.getMostRecentEventTime(),
            0, KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED);

    SwingUtilities.processKeyBindings(enterKeyEvent);
    // CT editing should be finished

    org.netbeans.junit.Log.assertInstances("CodeTemplateInsertHandler instances not GCed");
}
 
Example 4
Source File: HTMLViewer.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new CoBrowser object to be used with the specifid ChatRoom.
 */
public HTMLViewer() {
    final JPanel mainPanel = new JPanel();
    browser = new JEditorPane();
    browser.setEditorKit(new HTMLEditorKit());

    setLayout(new GridBagLayout());

    this.add(mainPanel, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
}
 
Example 5
Source File: ResultPanelOutput.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of ResultPanelOutput
 */
ResultPanelOutput(ResultDisplayHandler displayHandler) {
    super();
    if (LOG) {
        System.out.println("ResultPanelOutput.<init>");
    }
    
    textPane = new JEditorPane();
    textPane.setFont(new Font("monospaced", Font.PLAIN, getFont().getSize()));
    textPane.setEditorKit(new OutputEditorKit());
    textPane.setEditable(false);
    textPane.getCaret().setVisible(true);
    textPane.getCaret().setBlinkRate(0);
    textPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    setViewportView(textPane);

    /*
     * On GTK L&F, background of the text pane is gray, even though it is
     * white on a JTextArea. The following is a hack to fix it:
     */
    Color background = UIManager.getColor("TextPane.background");   //NOI18N
    if (background != null) {
        textPane.setBackground(background);
    }

    doc = textPane.getDocument();

    AccessibleContext ac = textPane.getAccessibleContext();
    ac.setAccessibleName(NbBundle.getMessage(getClass(),
                                            "ACSN_OutputTextPane"));//NOI18N
    ac.setAccessibleDescription(NbBundle.getMessage(getClass(),
                                            "ACSD_OutputTextPane"));//NOI18N
    
    this.displayHandler = displayHandler;
}
 
Example 6
Source File: JEditorPaneBrowser.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public JEditorPaneBrowser() {
	setLayout(new BorderLayout());
	browse = new JEditorPane() ;
	browse.setContentType("text/html");
	HTMLEditorKit kit = new HTMLEditorKit();
	browse.setEditorKit(kit);
	browse.setEditable(false);
	add(browse,BorderLayout.CENTER);
	client = URLTools.newClient();
	
}
 
Example 7
Source File: FoldView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public JComponent getToolTip(double x, double y, Shape allocation) {
    Container container = getContainer();
    if (container instanceof JEditorPane) {
        JEditorPane editorPane = (JEditorPane) getContainer();
        JEditorPane tooltipPane = new JEditorPane();
        EditorKit kit = editorPane.getEditorKit();
        Document doc = getDocument();
        if (kit != null && doc != null) {
            Element lineRootElement = doc.getDefaultRootElement();
            tooltipPane.putClientProperty(FoldViewFactory.DISPLAY_ALL_FOLDS_EXPANDED_PROPERTY, true);
            try {
                // Start-offset of the fold => line start => position
                int lineIndex = lineRootElement.getElementIndex(fold.getStartOffset());
                Position pos = doc.createPosition(
                        lineRootElement.getElement(lineIndex).getStartOffset());
                // DocumentView.START_POSITION_PROPERTY
                tooltipPane.putClientProperty("document-view-start-position", pos); // NOI18N
                // End-offset of the fold => line end => position
                lineIndex = lineRootElement.getElementIndex(fold.getEndOffset());
                pos = doc.createPosition(lineRootElement.getElement(lineIndex).getEndOffset());
                // DocumentView.END_POSITION_PROPERTY
                tooltipPane.putClientProperty("document-view-end-position", pos); // NOI18N
                tooltipPane.putClientProperty("document-view-accurate-span", true); // NOI18N
                // Set the same kit and document
                tooltipPane.setEditorKit(kit);
                tooltipPane.setDocument(doc);
                tooltipPane.setEditable(false);
                return new FoldToolTip(editorPane, tooltipPane, getBorderColor());
            } catch (BadLocationException e) {
                // => return null
            }
        }
    }
    return null;
}
 
Example 8
Source File: NbEditorToolBarTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the action context for the context-aware toolbar actions
 * contains the editor pane if there is no Lookup.Provider ancestor and no DataObject
 * corresponding to the current document.
 */
public void testActionContextNullWhenNoDataObject() {
    JPanel parent = new JPanel();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(new NbEditorKit());
    parent.add(editor);

    Lookup actionContext = NbEditorToolBar.createActionContext(editor);
    // changed when fixing #127757
    //assertNull(actionContext);
    assertNotNull(actionContext);
    Collection<?> all = actionContext.lookupAll(Object.class);
    assertEquals("Expecting singleton Lookup", 1, all.size());
    assertSame("Expecting the editor pane", editor, all.iterator().next());
}
 
Example 9
Source File: ModelItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void reformatAndUseRightEditor(JEditorPane pane, String data, String contentType) {
    if (contentType == null) {
        contentType = "text/plain"; // NOI18N
    } else {
        contentType = contentType.trim();
    }
    if ("application/javascript".equals(contentType)) {
        // check whether this JSONP response, that is a JS method call returning JSON:
        String json = getJSONPResponse(data);
        if (json != null) {
            data = json;
            contentType = "application/json";
        }
    }
    if ("application/json".equals(contentType) || "text/x-json".equals(contentType)) {
        data = reformatJSON(data);
        contentType = "text/x-json";
    }
    if ("application/xml".equals(contentType)) {
        contentType = "text/xml";
    }
    EditorKit editorKit;
    try {
        editorKit = CloneableEditorSupport.getEditorKit(contentType);
    } catch (IllegalArgumentException iaex) {
        contentType = "text/plain"; // NOI18N
        editorKit = CloneableEditorSupport.getEditorKit(contentType);
    }
    pane.setEditorKit(editorKit);
    pane.setText(data);
}
 
Example 10
Source File: HelpButton.java    From thunderstorm with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a BrowserPane object using a custom classloader that prefers
 * classes in our jar
 */
public static JEditorPane createEditorUsingOurClassLoader() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    ClassLoader our = JarFirstClassLoader.getInstance();
    Class c = Class.forName("org.fit.cssbox.swingbox.BrowserPane", true, our);
    JEditorPane editor = (JEditorPane) c.newInstance();

    Class c2 = Class.forName("org.fit.cssbox.swingbox.SwingBoxEditorKit", true, our);
    editor.setEditorKit((EditorKit) c2.newInstance());
    return editor;
}
 
Example 11
Source File: mxCellEditor.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
public mxCellEditor(mxGraphComponent graphComponent)
{
	this.graphComponent = graphComponent;

	// Creates the plain text editor
	textArea = new JTextArea();
	textArea.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	textArea.setOpaque(false);

	// Creates the HTML editor
	editorPane = new JEditorPane();
	editorPane.setOpaque(false);
	editorPane.setBackground(new Color(0,0,0,0));
	editorPane.setContentType("text/html");

	// Workaround for inserted linefeeds in HTML markup with
	// lines that are longar than 80 chars
	editorPane.setEditorKit(new NoLinefeedHtmlEditorKit());

	// Creates the scollpane that contains the editor
	// FIXME: Cursor not visible when scrolling
	scrollPane = new JScrollPane();
	scrollPane.setBorder(BorderFactory.createEmptyBorder());
	scrollPane.getViewport().setOpaque(false);
	scrollPane.setVisible(false);
	scrollPane.setOpaque(false);

	// Installs custom actions
	editorPane.getActionMap().put(CANCEL_EDITING, cancelEditingAction);
	textArea.getActionMap().put(CANCEL_EDITING, cancelEditingAction);
	editorPane.getActionMap().put(SUBMIT_TEXT, textSubmitAction);
	textArea.getActionMap().put(SUBMIT_TEXT, textSubmitAction);

	// Remembers the action map key for the enter keystroke
	editorEnterActionMapKey = editorPane.getInputMap().get(enterKeystroke);
	textEnterActionMapKey = editorPane.getInputMap().get(enterKeystroke);
}
 
Example 12
Source File: JavaViewHierarchyRandomTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRandomModsPlainText() throws Exception {
        loggingOn();
        RandomTestContainer container = createContainer();
        final JEditorPane pane = container.getInstance(JEditorPane.class);
        final Document doc = pane.getDocument();
        doc.putProperty("mimeType", "text/plain");
        ViewHierarchyRandomTesting.initRandomText(container);
        ViewHierarchyRandomTesting.addRound(container).setOpCount(OP_COUNT);
        ViewHierarchyRandomTesting.testFixedScenarios(container);
        container.run(1271950385168L); // Failed at op=750
//        container.run(1270806278503L);
//        container.run(1270806786819L);
//        container.run(1270806387223L);
//        container.run(1271372510390L);

//        RandomTestContainer.Context context = container.context();
//        DocumentTesting.undo(context, 2);
//        DocumentTesting.redo(context, 2);
        // Simulate tooltip pane
        final JEditorPane[] toolTipPaneRef = new JEditorPane[1];
        final BadLocationException[] excRef = new BadLocationException[1];
        final JFrame[] toolTipFrameRef = new JFrame[1];
        Runnable tooltipRun = new Runnable() {
            @Override
            public void run() {
                JEditorPane toolTipPane = new JEditorPane();
                toolTipPaneRef[0] = toolTipPane;
                toolTipPane.setEditorKit(pane.getEditorKit());
                try {
                    toolTipPane.putClientProperty("document-view-start-position", doc.createPosition(4));
                    toolTipPane.putClientProperty("document-view-end-position", doc.createPosition(20));
                } catch (BadLocationException ex) {
                    excRef[0] = ex;
                }
                toolTipPane.setDocument(doc);
                JFrame toolTipFrame = new JFrame("ToolTip Frame");
                toolTipFrameRef[0] = toolTipFrame;
                toolTipFrame.getContentPane().add(new JScrollPane(toolTipPane));
                toolTipFrame.setSize(100, 100);
                toolTipFrame.setVisible(true);
            }
        };
        SwingUtilities.invokeAndWait(tooltipRun);
        if (excRef[0] != null) {
            throw new IllegalStateException(excRef[0]);
        }

        container.run(0L); // Test random ops
        // Exclude caret row highlighting
        excludeHighlights(pane);
        container.run(0L); // Re-run test

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (toolTipFrameRef[0] != null) {
                    toolTipFrameRef[0].setVisible(false);
                }
            }
        });
    }
 
Example 13
Source File: AnnotationViewDataImplTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testGetMainMarkForBlock() throws /*BadLocation*/Exception {
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    BaseDocument bd = (BaseDocument) editor.getDocument();

    bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
    
    TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2});
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2});
    TestMark mark3 = new TestMark(Status.STATUS_WARNING, null, null, new int[] {2, 4});
    
    AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(7), "test-annotation-1");
    AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-2");
    AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-8");
    AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(9), "test-annotation-8");
    
    bd.getAnnotations().addAnnotation(test1);
    bd.getAnnotations().addAnnotation(test2);
    bd.getAnnotations().addAnnotation(test3);
    bd.getAnnotations().addAnnotation(test4);
    
    List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3});
    List marks2 = Arrays.asList(new Mark[]{mark1, mark3});
    List marks3 = Arrays.asList(new Mark[]{mark2, mark3});
    List marks4 = Arrays.asList(new Mark[]{mark1, mark2});
    List marks5 = Arrays.asList(new Mark[]{mark3});
    
    TestMarkProvider provider = new TestMarkProvider(marks1, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData();
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus());
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    bd.getAnnotations().activateNextAnnotation(8);
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    bd.getAnnotations().activateNextAnnotation(8);
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    assertNull(data.getMainMarkForBlock(9, 9));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus());
    
    provider.setMarks(marks2);
    
    bd.getAnnotations().removeAnnotation(test3);
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));

    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus());
    assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus());
    assertNull(data.getMainMarkForBlock(9, 9));
    assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus());
    
    provider.setMarks(marks3);
    
    assertEquals(mark3, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(2, 4));
    assertEquals(mark3, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks4);
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    assertEquals(mark1, data.getMainMarkForBlock(2, 3));
    assertEquals(mark1, data.getMainMarkForBlock(2, 4));
    assertEquals(mark1, data.getMainMarkForBlock(2, 6));
    assertEquals(null, data.getMainMarkForBlock(3, 6));
    assertEquals(null, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks5);
    
    assertEquals(mark3, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(2, 4));
    assertEquals(mark3, data.getMainMarkForBlock(2, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 6));
    assertEquals(mark3, data.getMainMarkForBlock(3, 3));
    assertEquals(null, data.getMainMarkForBlock(6, 6));
}
 
Example 14
Source File: ArtARDemo.java    From COMP3204 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public JPanel getComponent(int width, int height) throws IOException {
	final JPanel container = new JPanel();
	container.setSize(width, height);
	container.setPreferredSize(container.getSize());

	final OverlayLayout overlay = new OverlayLayout(container);
	container.setLayout(overlay);

	labelField = new JEditorPane();
	labelField.setOpaque(false);
	labelField.setSize(640 - 50, 480 - 50);
	labelField.setPreferredSize(labelField.getSize());
	labelField.setMaximumSize(labelField.getSize());
	labelField.setContentType("text/html");

	// add a HTMLEditorKit to the editor pane
	final HTMLEditorKit kit = new HTMLEditorKit();
	labelField.setEditorKit(kit);

	final StyleSheet styleSheet = kit.getStyleSheet();
	styleSheet.addRule("body {color:#FF00FF; font-family:courier;}");
	styleSheet.addRule("h1 {font-size: 60pt}");
	styleSheet.addRule("h2 {font-size: 50pt }");

	final Document doc = kit.createDefaultDocument();
	labelField.setDocument(doc);

	// final GridBagConstraints gbc = new GridBagConstraints();
	// gbc.gridy = 1;
	// panel.add(labelField, gbc);
	container.add(labelField);
	// labelField.setAlignmentX(0.5f);
	// labelField.setAlignmentY(0.5f);

	final JPanel panel = super.getComponent(width, height);
	container.add(panel);

	vc.getDisplay().addVideoListener(this);

	isRunning = true;
	new Thread(this).start();

	return container;
}
 
Example 15
Source File: CslTestBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected JEditorPane getPane(String text) throws Exception {
    if (!SwingUtilities.isEventDispatchThread()) {
        fail("You must run this test from the event dispatch thread! To do that, add @Override protected boolean runInEQ() { return true } from your testcase!");
    }
    String BEGIN = "$start$"; // NOI18N
    String END = "$end$"; // NOI18N
    int sourceStartPos = text.indexOf(BEGIN);
    int caretPos = -1;
    int sourceEndPos = -1;
    if (sourceStartPos != -1) {
        text = text.substring(0, sourceStartPos) + text.substring(sourceStartPos+BEGIN.length());
        sourceEndPos = text.indexOf(END);
        assertTrue(sourceEndPos != -1);
        text = text.substring(0, sourceEndPos) + text.substring(sourceEndPos+END.length());
    } else {
        caretPos = text.indexOf('^');
        if (caretPos != -1) {
            text = text.substring(0, caretPos) + text.substring(caretPos+1);
        }
    }

    JEditorPane pane = new JEditorPane();
    pane.setContentType(getPreferredMimeType());
    final NbEditorKit kit = ((NbEditorKit)getEditorKit(getPreferredMimeType()));


    Thread preload = new Thread(new Runnable() {

        @Override
        public void run() {
            // Preload actions and other stuff
            if (kit instanceof Callable) {
                try {
                    ((Callable) kit).call();
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            kit.getActions();
        }
    });
    preload.start();
    preload.join();
    pane.setEditorKit(kit);
    pane.setText(text);

    BaseDocument bdoc = (BaseDocument)pane.getDocument();

    bdoc.putProperty(org.netbeans.api.lexer.Language.class, getPreferredLanguage().getLexerLanguage());
    bdoc.putProperty("mimeType", getPreferredMimeType());

    //bdoc.insertString(0, text, null);
    if (sourceStartPos != -1) {
        assertTrue(sourceEndPos != -1);
        pane.setSelectionStart(sourceStartPos);
        pane.setSelectionEnd(sourceEndPos);
    } else if (caretPos != -1) {
        pane.getCaret().setDot(caretPos);
    }
    pane.getCaret().setSelectionVisible(true);

    return pane;
}
 
Example 16
Source File: JavaViewHierarchyRandomTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToolTipView() throws Exception {
        loggingOn();
        RandomTestContainer container = createContainer();
        final JEditorPane pane = container.getInstance(JEditorPane.class);
        final Document doc = pane.getDocument();
        doc.putProperty("mimeType", "text/plain");
        RandomTestContainer.Context context = container.context();
//        ViewHierarchyRandomTesting.disableHighlighting(container);
        DocumentTesting.setSameThreadInvoke(context, true); // Do not post to EDT
        DocumentTesting.insert(context, 0, "abc\ndef\nghi\n");
        final JEditorPane[] toolTipPaneRef = new JEditorPane[1];
        final BadLocationException[] excRef = new BadLocationException[1];
        final JFrame[] toolTipFrameRef = new JFrame[1];
        Runnable tooltipRun = new Runnable() {
            @Override
            public void run() {
                JEditorPane toolTipPane = new JEditorPane();
                toolTipPaneRef[0] = toolTipPane;
                toolTipPane.setEditorKit(pane.getEditorKit());
                try {
                    Position startPos = doc.createPosition(4); // Line begining
                    Position endPos = doc.createPosition(8); // Line boundary too
                    toolTipPane.putClientProperty("document-view-start-position", startPos);
                    toolTipPane.putClientProperty("document-view-end-position", endPos);
                    toolTipPane.setDocument(doc);
                    JFrame toolTipFrame = new JFrame("ToolTip Frame");
                    toolTipFrameRef[0] = toolTipFrame;
                    toolTipFrame.getContentPane().add(new JScrollPane(toolTipPane));
                    toolTipFrame.setSize(100, 100);
                    toolTipFrame.setVisible(true);

                    doc.insertString(4, "o", null);
                    toolTipPane.setFont(new Font("Monospaced", Font.PLAIN, 22)); // Force VH rebuild
                    toolTipPane.modelToView(6);
                    doc.remove(3, 3);
                    doc.insertString(4, "ab", null);
                    
                    assert (endPos.getOffset() == 8);
                    doc.remove(7, 2);
                    toolTipPane.setFont(new Font("Monospaced", Font.PLAIN, 23)); // Force VH rebuild
                    toolTipPane.modelToView(6);

                } catch (BadLocationException ex) {
                    excRef[0] = ex;
                }

            }
        };
        SwingUtilities.invokeAndWait(tooltipRun);
        if (excRef[0] != null) {
            throw new IllegalStateException(excRef[0]);
        }

        DocumentTesting.setSameThreadInvoke(context, false);
        DocumentTesting.undo(context, 2);
        DocumentTesting.undo(context, 1);
        DocumentTesting.undo(context, 1);
        DocumentTesting.redo(context, 4);
        
        // Hide tooltip's frame
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (toolTipFrameRef[0] != null) {
                    toolTipFrameRef[0].setVisible(false);
                }
            }
        });
    }
 
Example 17
Source File: AnnotationViewDataImplTest.java    From netbeans with Apache License 2.0 3 votes vote down vote up
public void testComputeTotalStatus() throws Exception {
    JFrame f = new JFrame();
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    AnnotationView aView = new AnnotationView(editor);
    AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData();
    
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
    f.getContentPane().add(aView, BorderLayout.EAST);
    
    f.setSize(500, 500);
    
    f.setVisible(true);

    BaseDocument bd = (BaseDocument) editor.getDocument();
    
    bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
    
    Position start = bd.createPosition(Utilities.getRowStartFromLineOffset(bd, 2));
    
    AnnotationDesc a1 = new AnnotationTestUtilities.TestAnnotationDesc1(bd, start);
    AnnotationDesc a2 = new AnnotationTestUtilities.TestAnnotationDesc2(bd, start);
    
    bd.getAnnotations().addAnnotation(a1);
    bd.getAnnotations().addAnnotation(a2);
    
    assertEquals(Status.STATUS_ERROR, data.computeTotalStatus());
    
    bd.getAnnotations().activateNextAnnotation(2);
    
    assertEquals(Status.STATUS_ERROR, data.computeTotalStatus());
    
    f.setVisible(false);
}
 
Example 18
Source File: AnnotationViewTest.java    From netbeans with Apache License 2.0 2 votes vote down vote up
private static void performTest(final Action action) throws Exception {
    JFrame f = new JFrame();
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {6, 6});
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {6, 6});
    
    List marks = Arrays.asList(new Mark[]{mark1, mark2});
    
    TestMarkProvider provider = new TestMarkProvider(Collections.EMPTY_LIST, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
    f.getContentPane().add(aView, BorderLayout.EAST);
    
    f.setSize(500, 500);
    
    f.setVisible(true);

    String[] contents = getContents();
    
    for (int index = 0; index < contents.length; index++) {
        BaseDocument bd = (BaseDocument) editor.getDocument();
        
        bd.insertString(0, contents[index], null);
        
        provider.setMarks(marks);
        
        action.test(aView, bd);
        
        provider.setMarks(Collections.EMPTY_LIST);
        
        bd.remove(0, bd.getLength());
    }
    
    f.setVisible(false);
}
 
Example 19
Source File: AnnotationViewDataImplTest.java    From netbeans with Apache License 2.0 2 votes vote down vote up
public void testMarkUpdates() {
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2});
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2});
    TestMark mark3 = new TestMark(Status.STATUS_OK, null, null, new int[] {4, 6});
    
    List<Mark> marks = Arrays.asList(new Mark[]{mark1, mark2});
    List<Mark> marksOnlyFirst = Arrays.asList(new Mark[]{mark1});
    List<Mark> marksOnlySecond = Arrays.asList(new Mark[]{mark2});
    List<Mark> marksFirstAndThird = Arrays.asList(new Mark[]{mark1, mark3});
    
    TestMarkProvider provider = new TestMarkProvider(marks, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData();
    
    Collection<Mark> mergedMarks;
    SortedMap<Integer, List<Mark>> map;
    
    mergedMarks = data.getMergedMarks();
    
    assertEquals(new LinkedHashSet<Mark>(marks), mergedMarks);
    
    map = data.getMarkMap();
    
    assertEquals(1, map.size());
    assertEquals(marks, map.get(map.firstKey()));
    
    provider.setMarks(marksOnlyFirst);
    
    mergedMarks = data.getMergedMarks();
    
    assertEquals(new LinkedHashSet<Mark>(marksOnlyFirst), mergedMarks);
    
    map = data.getMarkMap();
    
    assertEquals(1, map.size());
    assertEquals(marksOnlyFirst, map.get(map.firstKey()));
    
    provider.setMarks(marksFirstAndThird);
    
    mergedMarks = data.getMergedMarks();
    
    assertEquals(new LinkedHashSet<Mark>(marksFirstAndThird), mergedMarks);
    
    map = data.getMarkMap();
    
    assertEquals(4, map.size());
    assertEquals(new HashSet(Arrays.asList(new Integer[] {new Integer(2), new Integer(4), new Integer(5), new Integer(6)})), map.keySet());
    assertEquals(Arrays.asList(new Mark[] {mark1}), map.get(new Integer(2)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(4)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(5)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(6)));
    
    provider.setMarks(Collections.EMPTY_LIST);
    
    mergedMarks = data.getMergedMarks();
    
    assertEquals(Collections.emptySet(), mergedMarks);
    
    map = data.getMarkMap();
    
    assertEquals(0, map.size());
    
    provider.setMarks(marksFirstAndThird);
    
    mergedMarks = data.getMergedMarks();
    
    assertEquals(new LinkedHashSet<Mark>(marksFirstAndThird), mergedMarks);
    
    map = data.getMarkMap();
    
    assertEquals(4, map.size());
    assertEquals(new HashSet(Arrays.asList(new Integer[] {new Integer(2), new Integer(4), new Integer(5), new Integer(6)})), map.keySet());
    assertEquals(Arrays.asList(new Mark[] {mark1}), map.get(new Integer(2)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(4)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(5)));
    assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer(6)));
}
 
Example 20
Source File: AnnotationViewDataImplTest.java    From netbeans with Apache License 2.0 2 votes vote down vote up
public void testMarkPriorities() throws Exception {
    JEditorPane editor = new JEditorPane();
    
    editor.setEditorKit(BaseKit.getKit(PlainKit.class));
    
    BaseDocument bd = (BaseDocument) editor.getDocument();

    bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null);
    
    //test marks:
    TestMark mark1 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 99);
    TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 10);
    TestMark mark3 = new TestMark(Status.STATUS_OK, null, null, new int[] {3, 4}, 5);
    
    TestMark mark4 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 1000);
    TestMark mark5 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 100);
    TestMark mark6 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {3, 4}, 50);
    
    List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3});
    List marks2 = Arrays.asList(new Mark[]{mark2, mark1, mark3});
    List marks3 = Arrays.asList(new Mark[]{mark1, mark2, mark3, mark4, mark5, mark6});
    
    TestMarkProvider provider = new TestMarkProvider(marks1, UpToDateStatus.UP_TO_DATE_OK);
    TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault();
    
    creator.setProvider(provider);
    
    AnnotationView aView = new AnnotationView(editor);
    AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData();
    
    assertEquals(mark2, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(3, 4));
    
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks2);
    
    assertEquals(mark2, data.getMainMarkForBlock(2, 2));
    assertEquals(mark3, data.getMainMarkForBlock(2, 3));
    assertEquals(mark3, data.getMainMarkForBlock(3, 4));
    
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(marks3);
    
    assertEquals(mark5, data.getMainMarkForBlock(2, 2));
    assertEquals(mark6, data.getMainMarkForBlock(2, 3));
    assertEquals(mark6, data.getMainMarkForBlock(3, 4));
    
    assertEquals(null, data.getMainMarkForBlock(6, 6));
    
    provider.setMarks(Collections.EMPTY_LIST);
    
    //test annotations:
    AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-1");
    AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-2");
    AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-3");
    AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-4");
    
    bd.getAnnotations().addAnnotation(test1);
    bd.getAnnotations().addAnnotation(test2);
    
    assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
    
    bd.getAnnotations().activateNextAnnotation(2);
    
    assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
    
    bd.getAnnotations().activateNextAnnotation(2);
    
    bd.getAnnotations().addAnnotation(test3);
    bd.getAnnotations().addAnnotation(test4);
    
    assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
    
    bd.getAnnotations().activateNextAnnotation(2);
    
    assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
    
    bd.getAnnotations().activateNextAnnotation(2);
    
    assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
    
    //test interaction between annotations and marks:
    List marks4 = Arrays.asList(new Mark[]{mark1});
    
    provider.setMarks(marks4);
    
    bd.getAnnotations().removeAnnotation(test2);
    bd.getAnnotations().removeAnnotation(test3);
    bd.getAnnotations().removeAnnotation(test4);
    
    assertEquals(mark1, data.getMainMarkForBlock(2, 2));
    
    bd.getAnnotations().addAnnotation(test2);
    
    assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc());
}