Java Code Examples for javax.swing.text.AbstractDocument#insertString()
The following examples show how to use
javax.swing.text.AbstractDocument#insertString() .
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: SimpleFoldManagerTest.java From netbeans with Apache License 2.0 | 5 votes |
/** * Test the creation of several folds. */ public void test() throws Exception { FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new SimpleFoldManagerFactory()); AbstractDocument doc = env.getDocument(); doc.insertString(0, "1234567890", null); FoldHierarchy hierarchy = env.getHierarchy(); doc.readLock(); try { hierarchy.lock(); try { Fold rootFold = hierarchy.getRootFold(); int foldCount = rootFold.getFoldCount(); int expectedFoldCount = 1; assertTrue("Incorrect fold count " + foldCount, // NOI18N (foldCount == expectedFoldCount) ); Fold fold = rootFold.getFold(0); FoldType foldType = fold.getType(); int foldStartOffset = fold.getStartOffset(); int foldEndOffset = fold.getEndOffset(); assertTrue("Incorrect fold type " + foldType, // NOI18N (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N (foldStartOffset == FOLD_START_OFFSET_1)); assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N (foldEndOffset == FOLD_END_OFFSET_1)); // Check fold size assertSize("Size of the fold " , Collections.singleton(fold), // NOI18N MAX_FOLD_MEMORY_SIZE, new FoldMemoryFilter(fold)); } finally { hierarchy.unlock(); } } finally { doc.readUnlock(); } }
Example 2
Source File: SimpleFoldManagerTest.java From netbeans with Apache License 2.0 | 5 votes |
/** * A provider is registered for the hierarchy. * Active == true should be reported to FoldHierarchyMonitor. */ public void testFoldHierararchyMonitorWithProvider() throws Exception { MockMimeLookup.setInstances(MimePath.parse(""), new FHM()); FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new SimpleFoldManagerFactory()); AbstractDocument doc = env.getDocument(); doc.insertString(0, "1234567890", null); env.getHierarchy(); assertTrue("Folds not attached", attached); assertTrue("Hierarchy must be active", active); }
Example 3
Source File: FoldOperationTest.java From netbeans with Apache License 2.0 | 4 votes |
/** * Checks that no changes (update with the same infos) will not fire events, remove or add any folds. * * @throws Exception */ public void testUpdateNoChanges() throws Exception { final TestFoldManager[] mgr = new TestFoldManager[1]; FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new FoldManagerFactory() { @Override public FoldManager createFoldManager() { return mgr[0] = new TestFoldManager(); } }); AbstractDocument doc = env.getDocument(); doc.insertString(0, "12345678901234567890", null); FoldHierarchy hierarchy = env.getHierarchy(); Collection<FoldInfo> infos = new ArrayList<FoldInfo>(10); infos.add(FoldInfo.range(0, 1, FoldType.MEMBER)); infos.add(FoldInfo.range(9, 10, FoldType.MEMBER)); infos.add(FoldInfo.range(14, 16, FoldType.MEMBER)); // not sorted, check :) infos.add(FoldInfo.range(3, 12, FoldType.MEMBER)); infos.add(FoldInfo.range(5, 6, FoldType.MEMBER)); doc.readLock(); try { hierarchy.lock(); TestFoldManager m = mgr[0]; try { Collection<Fold> remove = new ArrayList<Fold>(); Collection<FoldInfo> create = new ArrayList<FoldInfo>(); final boolean[] changed = new boolean[1]; hierarchy.addFoldHierarchyListener(new FoldHierarchyListener() { @Override public void foldHierarchyChanged(FoldHierarchyEvent evt) { changed[0] = true; } }); Map<FoldInfo, Fold> mapping = m.operation.update(infos, remove, create); assertEquals(m.initFolds.size(), mapping.size()); assertTrue(m.initFolds.containsAll(mapping.values())); assertFalse(changed[0]); } finally { hierarchy.unlock(); } } finally { doc.readUnlock(); } }
Example 4
Source File: FoldOperationTest.java From netbeans with Apache License 2.0 | 4 votes |
/** * Checks that folds are created beween two folds, encapsulating existing folds. * * @throws Exception */ public void testUpdateCreateFold() throws Exception { final TestFoldManager[] mgr = new TestFoldManager[1]; FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new FoldManagerFactory() { @Override public FoldManager createFoldManager() { return mgr[0] = new TestFoldManager(); } }); AbstractDocument doc = env.getDocument(); doc.insertString(0, "12345678901234567890", null); FoldHierarchy hierarchy = env.getHierarchy(); List<FoldInfo> infos = createDefaultInfos(); // add a new fold between #1 and #2 infos.add(FoldInfo.range(2, 3, FoldType.MEMBER)); // add a new fold at the end: infos.add(FoldInfo.range(19,20, FoldType.MEMBER)); // add a fold, which encapsulates #2 - #5 infos.add(FoldInfo.range(3, 18, FoldType.MEMBER)); // add a fold, which encapsulates ##5 infos.add(FoldInfo.range(13, 16, FoldType.MEMBER)); doc.readLock(); try { hierarchy.lock(); TestFoldManager m = mgr[0]; try { Collection<Fold> remove = new ArrayList<Fold>(); Collection<FoldInfo> create = new ArrayList<FoldInfo>(); final boolean[] changed = new boolean[1]; hierarchy.addFoldHierarchyListener(new FoldHierarchyListener() { @Override public void foldHierarchyChanged(FoldHierarchyEvent evt) { changed[0] = true; } }); Map<FoldInfo, Fold> mapping = m.operation.update(infos, remove, create); // 3 folds added, no deleted: assertEquals(4, create.size()); assertEquals(0, remove.size()); } finally { hierarchy.unlock(); } } finally { doc.readUnlock(); } }
Example 5
Source File: NestedFoldManagerTest.java From netbeans with Apache License 2.0 | 4 votes |
/** * Test the creation of several folds. */ public void test(boolean outerFirst) throws BadLocationException { FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new NestedFoldManagerFactory(outerFirst)); AbstractDocument doc = env.getDocument(); doc.insertString(0, "1234567890", null); FoldHierarchy hierarchy = env.getHierarchy(); doc.readLock(); try { hierarchy.lock(); try { Fold rootFold = hierarchy.getRootFold(); int foldCount = rootFold.getFoldCount(); int expectedFoldCount = 1; assertTrue("Incorrect fold count " + foldCount, // NOI18N (foldCount == expectedFoldCount) ); Fold fold = rootFold.getFold(0); FoldType foldType = fold.getType(); int foldStartOffset = fold.getStartOffset(); int foldEndOffset = fold.getEndOffset(); assertTrue("Incorrect fold type " + foldType, // NOI18N (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N (foldStartOffset == FOLD_START_OFFSET_OUTER)); assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N (foldEndOffset == FOLD_END_OFFSET_OUTER)); // Test inner fold Fold outerFold = fold; foldCount = outerFold.getFoldCount(); expectedFoldCount = 1; assertTrue("Incorrect fold count " + foldCount, // NOI18N (foldCount == expectedFoldCount) ); fold = outerFold.getFold(0); assertTrue("Folds must differ", (fold != outerFold)); // NOI18N foldType = fold.getType(); foldStartOffset = fold.getStartOffset(); foldEndOffset = fold.getEndOffset(); assertTrue("Incorrect fold type " + foldType, // NOI18N (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); assertTrue("Incorrect fold start offset " + foldStartOffset, // NOI18N (foldStartOffset == FOLD_START_OFFSET_INNER)); assertTrue("Incorrect fold end offset " + foldEndOffset, // NOI18N (foldEndOffset == FOLD_END_OFFSET_INNER)); } finally { hierarchy.unlock(); } } finally { doc.readUnlock(); } }
Example 6
Source File: MainPanel.java From java-swing-tips with MIT License | 4 votes |
private MainPanel() { super(new GridLayout(2, 1)); String str = "red green blue 111111111111111111111111111111111"; JTextPane textPane = new JTextPane() { @Override public void scrollRectToVisible(Rectangle rect) { rect.grow(getInsets().right, 0); super.scrollRectToVisible(rect); } }; // @see https://ateraimemo.com/Swing/NoWrapTextPane.html textPane.setEditorKit(new NoWrapEditorKit()); AbstractDocument doc = new SimpleSyntaxDocument(); textPane.setDocument(doc); try { doc.insertString(0, str, null); } catch (BadLocationException ex) { // should never happen RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested()); wrap.initCause(ex); throw wrap; } String key = "Do-Nothing"; InputMap im = textPane.getInputMap(JComponent.WHEN_FOCUSED); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), key); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), key); textPane.getActionMap().put(key, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // Do nothing } }); // @see https://ateraimemo.com/Swing/FocusTraversalKeys.html int ftk = KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS; Set<AWTKeyStroke> forwardKeys = new HashSet<>(textPane.getFocusTraversalKeys(ftk)); forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK)); textPane.setFocusTraversalKeys(ftk, forwardKeys); // // @see https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/ // textPane.addCaretListener(new VisibleCaretListener()); JScrollPane scrollPane = new JScrollPane(textPane) { @Override public void updateUI() { super.updateUI(); setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); setBorder(BorderFactory.createLineBorder(Color.GRAY)); setViewportBorder(BorderFactory.createEmptyBorder()); } @Override public Dimension getMinimumSize() { return super.getPreferredSize(); } }; add(makeTitledPanel("JTextField", new JTextField(str))); add(makeTitledPanel("JTextPane+StyledDocument+JScrollPane", scrollPane)); setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); setPreferredSize(new Dimension(320, 240)); }