jEdit Community - Resources for users of the jEdit Text Editor
Writing text in two buffers
Submitted by DirkK on Sunday, 28 March, 2010 - 13:31
Hi to all,

I'd like to write text in two new buffers, so I wrote this short beanshell script:

buffer1 = jEdit.newFile(view);
editPane = view.goToBuffer(buffer1);
textArea = editPane.getTextArea();

textArea.setSelectedText("Buffer 1\n");

buffer1.save(view, "C:/Work/test1", true);

buffer2 = jEdit.newFile(view);
editPane = view.goToBuffer(buffer2);
textArea = editPane.getTextArea();

textArea.setSelectedText("Buffer 2\n");

buffer2.save(view, "C:/Work/test2", true);

as soon as the script tries to execute the textArea.setSelectedText, I get an exception "Text component read only". Has anyone an idea why I get this exception?
Moving the buffer1.save downward to the buffer2.save the script works fine.

The next challenge: if I try to write in the buffers alternate (first in buffer1, next buffer2 and then in buffer1 again) all the text is written in just one buffer. how do I select where my text is written?

thanks in advance for your responses, Dirk
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Workaround
by Robert Schwenn on Sun, 28/03/2010 - 18:39
Hi,
1. I don't know why the exception is thrown. Maybe it's a threading issue.

2. As long as You have no split view, there's only ONE editpane and also only ONE textArea. Because You inserts text into this textArea, it is written into the current visible buffer. After You have created the second buffer, this is the current one until You change this (I guess You didn't).

Here is a sample for work around both problems by only dealing with buffers. The downside is that You have to deal with absolute caret positions...

buffer1 = jEdit.openFile(view, "test1");
buffer2 = jEdit.openFile(view, "test2");

buffer1.insert(0, "Buffer 1a\n");
buffer2.insert(0, "Buffer 2a\n");

buffer1.insert(buffer1.getLength(), "Buffer 1b\n");
buffer2.insert(buffer2.getLength(), "Buffer 2b\n");

buffer1.save(view, "G:/Tools/jEdit_43/macros/_test/test1", true);
buffer2.save(view, "G:/Tools/jEdit_43/macros/_test/test2", true);

Robert
 
Hi Robert, thanks for your
by DirkK on Mon, 29/03/2010 - 20:05
Hi Robert,

thanks for your suggestion.

I tried it by changing my source code and, because of that, it didn't work: I used newfile instead of openfile an second I forgot the second parameter. The use of newfile results in an other problem, but if you forget the second paramater the behavior of the macro is sligtly different: first you'll get an error "Each buffer can only execute one Input/output operation at a time. please wait until the current operation finishes (or abort it in the I/O monitor) before starting another one" and next the all the text is in just one buffer (again).
Putting the second parameter in newfile, the text is in the buffers it should be in, but.... after the saves, there is just one file left in the buffertabs and as soon as I switch the buffers I get the following exception (thrown by the recentbufferswitcher I think)

java.lang.ArrayIndexOutOfBoundsException: 7
at recentbuffer.BufferAccessMonitor.getBufferList(BufferAccessMonitor.java:123)
at recentbuffer.RecentBuffer.createLayout(RecentBuffer.java:54)
at recentbuffer.RecentBuffer.(RecentBuffer.java:43)


if I use openFile, as you suggested, it all works fine, I only have to understand why I have to use openFile instead of newFile if I want to create a new file.
thanks to you, and sorry for the bunch of text I wrote.
regards, Dirk
 
When You have trouble with I/
by Robert Schwenn on Tue, 30/03/2010 - 18:38
When You have trouble with I/O You can try to insert this line:

VFSManager.waitForRequests();

Other things I don't know..

Robert
 
No trouble anymore
by DirkK on Wed, 31/03/2010 - 17:50
As long as I do it the way you discribed in your first reply, everything works fine.

thx, Dirk
User login
Browse archives
« April 2024  
MoTuWeThFrSaSu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82347
Context Free Art (*.cfdg)   0.31   46054
JBuilder scheme   .001   18494
BBEdit scheme   1.0   18115
ColdFusion scheme   1.0   18023
R Edit Mode - extensive version   0.1   17472
Advanced HTML edit mode   1.0   16205
Matlab Edit Mode   1.0   16067
jEdit XP icons   1.0   15229
XP icons for jEdit   1.1   14292