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

The following examples show how to use javax.swing.JEditorPane#putClientProperty() . 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: HighlightingManagerTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testExcludeTwoLayers() {
    OffsetsBag bag = new OffsetsBag(new PlainDocument());
    
    MemoryMimeDataProvider.reset(null);
    MemoryMimeDataProvider.addInstances(
        "text/plain", new SingletonLayerFactory("layer", ZOrder.DEFAULT_RACK, true, bag));

    JEditorPane pane = new JEditorPane();
    String [] removed = new String[] {"^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.TextSelectionHighlighting$"};
    pane.putClientProperty("HighlightsLayerExcludes", removed);
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());
    
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    HighlightsContainer hc = hm.getHighlights(HighlightsLayerFilter.IDENTITY);

    assertNotNull("Can't get fixed HighlightsContainer", hc);
    assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext());
}
 
Example 2
Source File: NbFormServices.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void setupCodeEditorPane(JEditorPane editor, FileObject srcFile, int ccPosition) {
    DataObject dob = null;
    try {
        dob = DataObject.find(srcFile);
    } catch (DataObjectNotFoundException dnfex) {
        FormUtils.LOGGER.log(Level.INFO, dnfex.getMessage(), dnfex);
    }
    if (!(dob instanceof FormDataObject)) {
        FormUtils.LOGGER.log(Level.INFO, "Unable to find FormDataObject for {0}", srcFile); // NOI18N
        return;
    }
    FormDataObject formDob = (FormDataObject)dob;
    Document document = formDob.getFormEditorSupport().getDocument();
    DialogBinding.bindComponentToDocument(document, ccPosition, 0, editor);

    // do not highlight current row
    editor.putClientProperty(
        "HighlightsLayerExcludes", //NOI18N
        "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" //NOI18N
    );

    FormUtils.setupTextUndoRedo(editor);
}
 
Example 3
Source File: AnnotationDrawer.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new drawer for the specified model and decorator.
 *
 * @param model
 *            the model containing all relevant drawing data
 * @param rendererModel
 *            the process renderer model
 */
public AnnotationDrawer(final AnnotationsModel model, final ProcessRendererModel rendererModel) {
	this.model = model;
	this.rendererModel = rendererModel;

	this.displayCache = new HashMap<>();
	this.cachedID = new HashMap<>();

	pane = new JEditorPane("text/html", "");
	pane.setBorder(null);
	pane.setOpaque(false);
	pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
}
 
Example 4
Source File: JavaHelp.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void adjustFontSize(JEditorPane contentViewer) {
    if(contentViewer != null) {
        contentViewer.putClientProperty(
                JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
        contentViewer.putClientProperty(
                JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    }
}
 
Example 5
Source File: MasterMatcherTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAreas() throws Exception {
    MockServices.setServices(MockMimeLookup.class);
    MockMimeLookup.setInstances(MimePath.EMPTY, new TestMatcher());
    
    AttributeSet EAS = SimpleAttributeSet.EMPTY;
    JEditorPane c = new JEditorPane();
    Document d = c.getDocument();
    OffsetsBag bag = new OffsetsBag(d);
    d.insertString(0, "text text { text } text", null);

    c.putClientProperty(MasterMatcher.PROP_MAX_BACKWARD_LOOKAHEAD, 256);
    c.putClientProperty(MasterMatcher.PROP_MAX_FORWARD_LOOKAHEAD, 256);
    
    TestMatcher.origin = new int [] { 2, 3 };
    TestMatcher.matches = new int [] { 10, 11 };
    
    MasterMatcher.get(c).highlight(d, 7, bag, EAS, EAS, EAS, EAS);
    TestMatcher.waitUntilCreated(1000);
    {
    TestMatcher tm = TestMatcher.lastMatcher;
    assertNotNull("No matcher created", tm);
    
    HighlightsSequence hs = bag.getHighlights(0, Integer.MAX_VALUE);
    assertTrue("Wrong number of highlighted areas", hs.moveNext());
    assertEquals("Wrong origin startOfset", 2, hs.getStartOffset());
    assertEquals("Wrong origin endOfset", 3, hs.getEndOffset());
    
    assertTrue("Wrong number of highlighted areas", hs.moveNext());
    assertEquals("Wrong match startOfset", 10, hs.getStartOffset());
    assertEquals("Wrong match endOfset", 11, hs.getEndOffset());
    }        
}
 
Example 6
Source File: HighlightingManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSimpleLayer() {
    OffsetsBag bag = new OffsetsBag(new PlainDocument());
    
    MemoryMimeDataProvider.reset(null);
    MemoryMimeDataProvider.addInstances(
        "text/plain", new SingletonLayerFactory("layer", ZOrder.DEFAULT_RACK, true, bag));

    JEditorPane pane = new JEditorPane();
    pane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$");
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());
    
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    HighlightsContainer hc = hm.getHighlights(HighlightsLayerFilter.IDENTITY);
    assertNotNull("Can't get fixed HighlightsContainer", hc);
    assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext());
    
    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute("attrib-A", "value");
    
    bag.addHighlight(10, 20, attributes);
    
    HighlightsSequence highlights = hc.getHighlights(0, Integer.MAX_VALUE);
    assertTrue("Highlight has not been added", moveNextSkipNullAttrs(highlights));
    assertEquals("Wrong start offset", 10, highlights.getStartOffset());
    assertEquals("Wrong end offset", 20, highlights.getEndOffset());
    assertEquals("Can't find attribute", "value", highlights.getAttributes().getAttribute("attrib-A"));
}
 
Example 7
Source File: GithubPanel.java    From MakeLobbiesGreatAgain with MIT License 5 votes vote down vote up
/**
 * Creates the new Panel and parses the supplied HTML.  <br>
 * <b> Supported Github Markdown: </b><i> Lists (unordered), Links, Images, Bold ('**' and '__'), Strikethrough, & Italics.  </i>
 *
 * @param currentVersion The version of the Jar currently running.
 */
public GithubPanel(double currentVersion) {
	this.version = currentVersion;

	setTitle("MLGA Update");
	try {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		parseReleases();
	} catch (Exception e1) {
		e1.printStackTrace();
	}
	if (updates <= 0) {
		return;
	}
	ed = new JEditorPane("text/html", html);
	ed.setEditable(false);
	ed.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
	ed.setFont(new Font("Helvetica", 0, 12));

	ed.addHyperlinkListener(he -> {
		// Listen to link clicks and open them in the browser.
		if (he.getEventType() == EventType.ACTIVATED && Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().browse(he.getURL().toURI());
				System.exit(0);
			} catch (IOException | URISyntaxException e) {
				e.printStackTrace();
			}
		}
	});
	final JScrollPane scrollPane = new JScrollPane(ed);
	scrollPane.setPreferredSize(new Dimension(1100, 300));
	add(scrollPane);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	pack();
	setLocationRelativeTo(null);
}
 
Example 8
Source File: HighlightingManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@RandomlyFails
public void testSimple() {
    JEditorPane pane = new JEditorPane();
    pane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$");
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    assertNotNull("Can't get instance of HighlightingManager", hm);
    HighlightsContainer hc = hm.getHighlights(HighlightsLayerFilter.IDENTITY);
    assertNotNull("Can't get fixed HighlightsContainer", hc);
    assertEquals(0, pane.getDocument().getLength());
    assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext());
}
 
Example 9
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEmptyCustomBounds() throws Exception {
    loggingOn();    
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    pane.modelToView(0);
    doc.insertString(0, "hello\nworld\ngood\nmorning", null);
    Position startPos = doc.createPosition(3);
    pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, startPos);
    pane.modelToView(0); // Force rebuild of VH

    Position endPos = doc.createPosition(2);
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
    pane.modelToView(0); // Force rebuild of VH
}
 
Example 10
Source File: ViewUpdatesTesting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static HighlightsContainer getSingleHighlightingLayerImpl(
        final JEditorPane pane, HighlightsContainer container, boolean offsets)
{
    String propName = "test-single-highlighting-container";
    HighlightsContainer tshc = (HighlightsContainer) pane.getClientProperty(propName);
    if (tshc == null) {
        final Document doc = pane.getDocument();
        final HighlightsContainer bag = (container != null)
                ? container
                : (offsets ? new OffsetsBag(doc) : new PositionsBag(doc));
        doc.render(new Runnable() {
            @Override
            public void run() {
                DocumentView docView = DocumentView.get(pane);
                if (docView != null) {
                    setTestValues(NO_OP_TEST_VALUE);
                }
                // Following will trigger rebuild_all in view hierarchy => skip this rebuild
                HighlightingManager.getInstance(pane).testSetSingleContainer(bag);
                if (docView != null) {
                    docView.op.viewsRebuildOrMarkInvalid(); // Rebiuld all views
                }
            }
        });
        tshc = bag;
        pane.putClientProperty(propName, tshc);
    }
    return tshc;
}
 
Example 11
Source File: ViewHierarchyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testCustomBounds() throws Exception {
    loggingOn();    
    JEditorPane pane = ViewUpdatesTesting.createPane();
    Document doc = pane.getDocument();
    doc.insertString(0, "hello\nworld\ngood\nmorning", null);
    Position startPos = doc.createPosition(3);
    pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
    pane.modelToView(0); // Force rebuild of VH
    doc.insertString(startPos.getOffset(), "a", null);
    doc.insertString(startPos.getOffset() - 1, "a", null);
    Element line0 = doc.getDefaultRootElement().getElement(0);
    Position endPos = doc.createPosition(line0.getEndOffset() + 3); // Middle of line 1
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
    pane.modelToView(0); // Force rebuild of VH

    TestHighlightsViewFactory testFactory = ViewUpdatesTesting.getTestFactory(pane);
    List<TestHighlight> hlts = ViewUpdatesTesting.getHighlightsCopy(testFactory);
    int hlStartOffset = startPos.getOffset() + 1;
    int hlEndOffset = endPos.getOffset() - 1; 
    TestHighlight hi = TestHighlight.create(doc, hlStartOffset, hlEndOffset, colorAttrs[0]);
    hlts.add(hi);
    testFactory.setHighlights(hlts);
    testFactory.fireChange(hlStartOffset, hlEndOffset);
    pane.modelToView(0); // Force rebuild of VH
    
    doc.insertString(doc.getLength(), "test\ntest2", null);
    Position endPos2 = doc.createPosition(doc.getLength() - 3);
    pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos2);
    pane.modelToView(0); // Force rebuild of VH
    doc.remove(endPos2.getOffset() - 2, 3);
    pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, null);
    pane.modelToView(0); // Force rebuild of VH
}
 
Example 12
Source File: ToolTipSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private JEditorPane createHtmlTextToolTip() {
    class HtmlTextToolTip extends JEditorPane {
        public @Override void setSize(int width, int height) {
            Dimension prefSize = getPreferredSize();
            if (width >= prefSize.width) {
                width = prefSize.width;
            } else { // smaller available width
                super.setSize(width, 10000); // the height is unimportant
                prefSize = getPreferredSize(); // re-read new pref width
            }
            if (height >= prefSize.height) { // enough height
                height = prefSize.height;
            }
            super.setSize(width, height);
        }
        @Override
        public void setKeymap(Keymap map) {
            //#181722: keymaps are shared among components with the same UI
            //a default action will be set to the Keymap of this component below,
            //so it is necessary to use a Keymap that is not shared with other components
            super.setKeymap(addKeymap(null, map));
        }
    }

    JEditorPane tt = new HtmlTextToolTip();
    /* See NETBEANS-403. It still appears possible to use Escape to close the popup when the
    focus is in the editor. */
    tt.putClientProperty(SUPPRESS_POPUP_KEYBOARD_FORWARDING_CLIENT_PROPERTY_KEY, true);

    // setup tooltip keybindings
    filterBindings(tt.getActionMap());
    tt.getActionMap().put(HIDE_ACTION.getValue(Action.NAME), HIDE_ACTION);
    tt.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), HIDE_ACTION.getValue(Action.NAME));
    tt.getKeymap().setDefaultAction(NO_ACTION);

    Font font = UIManager.getFont(UI_PREFIX + ".font"); // NOI18N
    Color backColor = UIManager.getColor(UI_PREFIX + ".background"); // NOI18N
    Color foreColor = UIManager.getColor(UI_PREFIX + ".foreground"); // NOI18N

    if (font != null) {
        tt.setFont(font);
    }
    if (foreColor != null) {
        tt.setForeground(foreColor);
    }
    if (backColor != null) {
        tt.setBackground(backColor);
    }

    tt.setOpaque(true);
    tt.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createLineBorder(tt.getForeground()),
        BorderFactory.createEmptyBorder(0, 3, 0, 3)
    ));
    tt.setContentType("text/html"); //NOI18N

    return tt;
}
 
Example 13
Source File: HighlightingManagerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testMultipleLayers() {
    OffsetsBag bagA = new OffsetsBag(new PlainDocument());
    OffsetsBag bagB = new OffsetsBag(new PlainDocument());
    OffsetsBag bagC = new OffsetsBag(new PlainDocument());
    OffsetsBag bagD = new OffsetsBag(new PlainDocument());

    MemoryMimeDataProvider.reset(null);
    MemoryMimeDataProvider.addInstances("text/plain",
        new SingletonLayerFactory("layerB", ZOrder.DEFAULT_RACK.forPosition(2), false, bagB),
        new SingletonLayerFactory("layerD", ZOrder.DEFAULT_RACK.forPosition(6), true, bagD),
        new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA),
        new SingletonLayerFactory("layerC", ZOrder.DEFAULT_RACK.forPosition(4), true, bagC)
    );
    
    JEditorPane pane = new JEditorPane();
    pane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$");
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());
    
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    HighlightsContainer variableHC = hm.getHighlights(VARIABLE_SIZE_LAYERS);
    assertNotNull("Can't get variable HighlightsContainer", variableHC);
    assertFalse("There should be no variable highlights", variableHC.getHighlights(0, Integer.MAX_VALUE).moveNext());

    HighlightsContainer fixedHC = hm.getHighlights(FIXED_SIZE_LAYERS);
    assertNotNull("Can't get fixed HighlightsContainer", fixedHC);
    assertFalse("There should be no fixed highlights", fixedHC.getHighlights(0, Integer.MAX_VALUE).moveNext());

    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    SimpleAttributeSet attribsC = new SimpleAttributeSet();
    SimpleAttributeSet attribsD = new SimpleAttributeSet();
    attribsA.addAttribute("set-A", "value");
    attribsA.addAttribute("commonAttribute", "set-A-value");
    attribsB.addAttribute("set-B", "value");
    attribsB.addAttribute("commonAttribute", "set-B-value");
    attribsC.addAttribute("set-C", "value");
    attribsC.addAttribute("commonAttribute", "set-C-value");
    attribsD.addAttribute("set-D", "value");
    attribsD.addAttribute("commonAttribute", "set-D-value");

    bagA.addHighlight(10, 20, attribsA);
    bagB.addHighlight(15, 25, attribsB);
    bagC.addHighlight(10, 20, attribsC);
    bagD.addHighlight(15, 25, attribsD);


    // Check fixed-size leyers sequence - should be C, D
    HighlightsSequence fixed = fixedHC.getHighlights(0, Integer.MAX_VALUE);
    // Check 1. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(fixed));
    assertEquals("Wrong start offset", 10, fixed.getStartOffset());
    assertEquals("Wrong end offset", 15, fixed.getEndOffset());
    assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-D");
    assertEquals("Wrong commonAttribute value", "set-C-value", fixed.getAttributes().getAttribute("commonAttribute"));
    // Check 2. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(fixed));
    assertEquals("Wrong start offset", 15, fixed.getStartOffset());
    assertEquals("Wrong end offset", 20, fixed.getEndOffset());
    assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-C", "set-D", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B");
    assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute"));
    // Check 3. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(fixed));
    assertEquals("Wrong start offset", 20, fixed.getStartOffset());
    assertEquals("Wrong end offset", 25, fixed.getEndOffset());
    assertAttribContains("Can't find attribute", fixed.getAttributes(), "set-D", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", fixed.getAttributes(), "set-A", "set-B", "set-C");
    assertEquals("Wrong commonAttribute value", "set-D-value", fixed.getAttributes().getAttribute("commonAttribute"));

    
    // Check variable-size leyers sequence - should be A, B
    HighlightsSequence variable = variableHC.getHighlights(0, Integer.MAX_VALUE);
    // Check 1. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(variable));
    assertEquals("Wrong start offset", 10, variable.getStartOffset());
    assertEquals("Wrong end offset", 15, variable.getEndOffset());
    assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D", "set-B");
    assertEquals("Wrong commonAttribute value", "set-A-value", variable.getAttributes().getAttribute("commonAttribute"));
    // Check 2. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(variable));
    assertEquals("Wrong start offset", 15, variable.getStartOffset());
    assertEquals("Wrong end offset", 20, variable.getEndOffset());
    assertAttribContains("Can't find attribute", variable.getAttributes(), "set-A", "set-B", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D");
    assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute"));
    // Check 3. highlight
    assertTrue("Wrong number of highlights", moveNextSkipNullAttrs(variable));
    assertEquals("Wrong start offset", 20, variable.getStartOffset());
    assertEquals("Wrong end offset", 25, variable.getEndOffset());
    assertAttribContains("Can't find attribute", variable.getAttributes(), "set-B", "commonAttribute");
    assertAttribNotContains("The attribute should not be there", variable.getAttributes(), "set-C", "set-D", "set-A");
    assertEquals("Wrong commonAttribute value", "set-B-value", variable.getAttributes().getAttribute("commonAttribute"));

    bagA.clear();
    bagB.clear();
    bagC.clear();
    bagD.clear();
}
 
Example 14
Source File: CheckForUpdateAction.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
private JComponent createPanel(String html) {
	System.setProperty("awt.useSystemAAFontSettings", "on");
	final JEditorPane editorPane = new JEditorPane();    	
	HTMLEditorKit kit = new HTMLEditorKit();
	editorPane.setEditorKit(kit);
    editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editorPane.setFont(new Font("Arial", Font.PLAIN, 12));
    editorPane.setPreferredSize(new Dimension(350, 120));
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    editorPane.setBackground(new Color(234, 241, 248));        
   
    Document doc = kit.createDefaultDocument();
    editorPane.setDocument(doc);
    editorPane.setText(html);

    // Add Hyperlink listener to process hyperlinks
    editorPane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(final HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                        editorPane.setToolTipText(e.getURL().toExternalForm());
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {                            
                        SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());
                        editorPane.setToolTipText(null);
                    }
                });
            } else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {       
            	    FileUtil.openUrl(e.getURL().toString(), AboutAction.class);                       
            }
        }
    });        
    editorPane.addMouseListener(mouseListener);
    JScrollPane sp = new JScrollPane(editorPane);       
    return sp;
}
 
Example 15
Source File: ViewUpdatesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRemoveAtBeginning() throws Exception {
        loggingOn();
        ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE);
        JEditorPane pane = ViewUpdatesTesting.createPane();
        Document doc = pane.getDocument();
        final DocumentView docView = DocumentView.get(pane);
        final PositionsBag highlights = ViewUpdatesTesting.getSingleHighlightingLayer(pane);
        int offset;
        String text;
        int cRegionStartOffset;
        int cRegionEndOffset;
        
        offset = 0;
        text = " \t\tabcdef\ta\nebxsu";
        ViewUpdatesTesting.setTestValues(
/*rebuildCause*/        ViewBuilder.RebuildCause.MOD_UPDATE,
/*createLocalViews*/    true,
/*startCreationOffset*/ 0, // Insert at end of existing local view => rebuild prev one
/*matchOffset*/         offset + text.length(),
/*endCreationOffset*/   offset + text.length() + 1, // includes '\n'
/*bmReuseOffset*/       0,
/*bmReusePView*/        docView.getParagraphView(0),
/*bmReuseLocalIndex*/   0,
/*amReuseOffset*/       offset + text.length(),
/*amReusePIndex*/       0,
/*amReusePView*/        docView.getParagraphView(0),
/*amReuseLocalIndex*/   0 // Could reuse NewlineView
        );
        doc.insertString(offset, text, null);
        ViewUpdatesTesting.checkViews(docView, 0, -1,
            1, HI_VIEW,
            2, TAB_VIEW,
            6, HI_VIEW,
            1, TAB_VIEW,
            1, HI_VIEW,
            1, NL_VIEW /* e:12 */,
            5, HI_VIEW,
            1, NL_VIEW /* e:18 */
        );
        // Force line wrap
        // SimpleValueNames.TEXT_LINE_WRAP
        pane.putClientProperty("text-line-wrap", "words"); // Force line wrap type
        ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE); // Will rebuild children
        docView.ensureLayoutValidForInitedChildren(); // acquires lock

        offset = 0;
        int removeLength = 1;
        cRegionStartOffset = 0;
        cRegionEndOffset = removeLength + 1; // 2
        docView.op.viewUpdates.extendCharRebuildRegion(OffsetRegion.create(doc, cRegionStartOffset, cRegionEndOffset));
        ViewUpdatesTesting.setTestValues(
/*rebuildCause*/        ViewBuilder.RebuildCause.MOD_UPDATE,
/*createLocalViews*/    true,
/*startCreationOffset*/ offset,
/*matchOffset*/         offset + 2, // First HI_VIEW#1 removed and one char from second TAB_VIEW#2 removed by cRegion
/*endCreationOffset*/   docView.getParagraphView(0).getEndOffset() - removeLength,
/*bmReuseOffset*/       0,
/*bmReusePView*/        docView.getParagraphView(0),
/*bmReuseLocalIndex*/   0,
/*amReuseOffset*/       offset,
/*amReusePIndex*/       0,
/*amReusePView*/        docView.getParagraphView(0),
/*amReuseLocalIndex*/   1 // Could reuse NewlineView
        );
        doc.remove(offset, removeLength);
        ViewUpdatesTesting.checkViews(docView, 0, -1,
            2, TAB_VIEW,
            6, HI_VIEW,
            1, TAB_VIEW,
            1, HI_VIEW,
            1, NL_VIEW /* e:11 */,
            5, HI_VIEW,
            1, NL_VIEW /* e:17 */
        );


        ViewUpdatesTesting.setTestValues();
    }
 
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: HighlightingManagerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testChangesInLayerFireEvents() {
    OffsetsBag bagA = new OffsetsBag(new PlainDocument());
    OffsetsBag bagB = new OffsetsBag(new PlainDocument());
    OffsetsBag bagC = new OffsetsBag(new PlainDocument());
    OffsetsBag bagD = new OffsetsBag(new PlainDocument());

    MemoryMimeDataProvider.reset(null);
    MemoryMimeDataProvider.addInstances("text/plain",
        new SingletonLayerFactory("layerB", ZOrder.DEFAULT_RACK.forPosition(2), false, bagB),
        new SingletonLayerFactory("layerD", ZOrder.DEFAULT_RACK.forPosition(6), true, bagD),
        new SingletonLayerFactory("layerA", ZOrder.DEFAULT_RACK, true, bagA),
        new SingletonLayerFactory("layerC", ZOrder.DEFAULT_RACK.forPosition(4), true, bagC)
    );

    JEditorPane pane = new JEditorPane();
    pane.putClientProperty("HighlightsLayerExcludes", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$");
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());
    
    HighlightingManager hm = HighlightingManager.getInstance(pane);
    
    // Test the variable-size layers - A,B
    Listener variableL = new Listener();
    HighlightsContainer variableHC = hm.getHighlights(VARIABLE_SIZE_LAYERS);
    assertNotNull("Can't get variable HighlightsContainer", variableHC);
    assertFalse("There should be no variable highlights", variableHC.getHighlights(0, Integer.MAX_VALUE).moveNext());

    variableHC.addHighlightsChangeListener(variableL);
    bagA.addHighlight(10, 20, SimpleAttributeSet.EMPTY);
    assertEquals("Wrong number of events", 1, variableL.eventsCnt);
    assertEquals("Wrong change start offset", 10, variableL.lastStartOffset);
    assertEquals("Wrong change end offset", 20, variableL.lastEndOffset);

    variableL.reset();
    bagB.addHighlight(5, 15, SimpleAttributeSet.EMPTY);
    assertEquals("Wrong number of events", 1, variableL.eventsCnt);
    assertEquals("Wrong change start offset", 5, variableL.lastStartOffset);
    assertEquals("Wrong change end offset", 15, variableL.lastEndOffset);

    // Test the fixed-size layers
    Listener fixedL = new Listener();
    HighlightsContainer fixedHC = hm.getHighlights(FIXED_SIZE_LAYERS);
    assertNotNull("Can't get fixed HighlightsContainer", fixedHC);
    assertFalse("There should be no fixed highlights", fixedHC.getHighlights(0, Integer.MAX_VALUE).moveNext());
    
    fixedHC.addHighlightsChangeListener(fixedL);
    bagC.addHighlight(20, 50, SimpleAttributeSet.EMPTY);
    assertEquals("Wrong number of events", 1, fixedL.eventsCnt);
    assertEquals("Wrong change start offset", 20, fixedL.lastStartOffset);
    assertEquals("Wrong change end offset", 50, fixedL.lastEndOffset);

    fixedL.reset();
    bagD.addHighlight(0, 30, SimpleAttributeSet.EMPTY);
    assertEquals("Wrong number of events", 1, fixedL.eventsCnt);
    assertEquals("Wrong change start offset", 0, fixedL.lastStartOffset);
    assertEquals("Wrong change end offset", 30, fixedL.lastEndOffset);
}
 
Example 18
Source File: FmtOptions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, provider.getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
Example 19
Source File: FmtOptions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
Example 20
Source File: JavaViewHierarchyRandomTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void excludeHighlights(JEditorPane pane) {
    // Exclude certain highlights to test intra-line view rebuilds
    pane.putClientProperty(PROP_HL_EXCLUDES, ".*CaretRowHighlighting$");
}