[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jedit-devel
Subject:    Re: [ jEdit-devel ] how to move the insertion point to the start of a file?
From:       Alan Ezust <alan.ezust () gmail ! com>
Date:       2019-12-08 22:26:30
Message-ID: CALy5K9q+EWd+9iny98hZOKTbHE8na+0vbQz00LsWW0fZkr7+Ow () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I think the buffer.insertFile() can sometimes take longer and the function
returns immediately, so if you try to move to a position before the thing
is loaded, that would give you the effect of things not working.
I am not sure of the "best practice" for this, but I look at how other
people did it and that usually gives me a way to make things do what I want.

(MarkerSetsPlugin.java:430)
if (buffer.isLoaded())
{
moveCaret.run();
}
else
{
buffer.addBufferListener(new BufferAdapter() {
@Override
public void bufferLoaded(JEditBuffer buffer) {
SwingUtilities.invokeLater(moveCaret);
}
});
}
}

So you create a Runnable that runs "later" and you use
SwingUtilities.invokeLater() to run it, and only after the Buffer is loaded.
This is an EBMessage.




On Sun, Dec 8, 2019 at 9:30 AM Eric Le Lay <dev@elelay.fr> wrote:

> Hi Conal,
>
> the Templates plugin should do what you want. Strangely it doesn't work
> for me now (NullPointerException), but it should. It has the #caret
> directive to specify where the cursor should go after insertion.
> See the XML plugin for how to install your templates.
>
> By the way, your code works fine for me when run as a beanshell macro
> (attached).
>
> Cheers,
>
> Le Wed, 4 Dec 2019 23:01:21 +1000,
> Conal Tuohy <conal.tuohy@gmail.com> a écrit :
>
> > I have written a plugin for TEI XML encoding, which (among other
> > things) can create a new TEI XML file based on a template. I create a
> > new Buffer and insert the contents of the template file into it. This
> > leaves the cursor at the end of the file. I'd like to move the cursor
> > back up to the top (or alternatively I'd like to insert the text into
> > the buffer and leave the cursor at the start).
> >
> > Can anyone suggest what I'm doing wrong in the example? The bottom two
> > lines are my unsuccessful attempts to jump to the start of the buffer.
> >
> > BTW the static methods "debug" and "error" of the TEI class are just
> > utility methods which write to the activity log.
> >
> > Thanks!
> >
> > Conal
> >
> >
> > TEI.debug("Creating from template file", "'" + templateFileName +
> > "'");
> > > TEI.debug("View", view);
> > > Buffer buffer = jEdit.newFile(view.getEditPane(),
> > > System.getProperty("user.home"));
> > > view.showBuffer(buffer);
> > > buffer.setMode("xml");
> > > boolean inserted = buffer.insertFile(view, templateFileName);
> > > if (!inserted) {
> > >      TEI.error("Failed to insert template file", templateFileName);
> > > }
> > > // neither of these lines seems to do anything
> > > view.getEditPane().getTextArea().goToBufferStart(false);
> > > view.getEditPane().getTextArea().setCaretPosition(0);
> >
> >
>
> --
> -----------------------------------------------
> jEdit Developers' List
> jEdit-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jedit-devel
>

[Attachment #5 (text/html)]

<div dir="ltr"><div>I think the buffer.insertFile() can sometimes take longer and the \
function returns immediately, so if you try to move to a position before the thing is \
loaded, that would give you the effect of things not working.</div><div>I am not sure \
of the &quot;best practice&quot; for this, but I look at how other people did it and \
that usually gives me a way to make things do what I \
want.</div><div><br></div><div>(MarkerSetsPlugin.java:430)<br></div><div>		if \
(buffer.isLoaded())<br>		{<br>			moveCaret.run();<br>		}<br>		else<br>		{<br>			buffer.addBufferListener(new \
BufferAdapter() {<br>				@Override<br>				public void bufferLoaded(JEditBuffer \
buffer) {<br>					SwingUtilities.invokeLater(moveCaret);<br>				}<br>			});<br>		}<br>	}</div><div><br></div><div>So \
you create a Runnable that runs &quot;later&quot; and you use \
SwingUtilities.invokeLater() to run it, and only after the Buffer is \
loaded.</div><div>This is an \
EBMessage.</div><div><br></div><div><br></div><div><br></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Dec 8, 2019 at 9:30 AM \
Eric Le Lay &lt;<a href="mailto:dev@elelay.fr">dev@elelay.fr</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Conal,<br> <br>
the Templates plugin should do what you want. Strangely it doesn&#39;t work<br>
for me now (NullPointerException), but it should. It has the #caret<br>
directive to specify where the cursor should go after insertion.<br>
See the XML plugin for how to install your templates.<br>
<br>
By the way, your code works fine for me when run as a beanshell macro<br>
(attached).<br>
<br>
Cheers,<br>
<br>
Le Wed, 4 Dec 2019 23:01:21 +1000,<br>
Conal Tuohy &lt;<a href="mailto:conal.tuohy@gmail.com" \
target="_blank">conal.tuohy@gmail.com</a>&gt; a écrit :<br> <br>
&gt; I have written a plugin for TEI XML encoding, which (among other<br>
&gt; things) can create a new TEI XML file based on a template. I create a<br>
&gt; new Buffer and insert the contents of the template file into it. This<br>
&gt; leaves the cursor at the end of the file. I&#39;d like to move the cursor<br>
&gt; back up to the top (or alternatively I&#39;d like to insert the text into<br>
&gt; the buffer and leave the cursor at the start).<br>
&gt; <br>
&gt; Can anyone suggest what I&#39;m doing wrong in the example? The bottom two<br>
&gt; lines are my unsuccessful attempts to jump to the start of the buffer.<br>
&gt; <br>
&gt; BTW the static methods &quot;debug&quot; and &quot;error&quot; of the TEI class \
are just<br> &gt; utility methods which write to the activity log.<br>
&gt; <br>
&gt; Thanks!<br>
&gt; <br>
&gt; Conal<br>
&gt; <br>
&gt; <br>
&gt; TEI.debug(&quot;Creating from template file&quot;, &quot;&#39;&quot; + \
templateFileName +<br> &gt; &quot;&#39;&quot;);<br>
&gt; &gt; TEI.debug(&quot;View&quot;, view);<br>
&gt; &gt; Buffer buffer = jEdit.newFile(view.getEditPane(),<br>
&gt; &gt; System.getProperty(&quot;user.home&quot;));<br>
&gt; &gt; view.showBuffer(buffer);<br>
&gt; &gt; buffer.setMode(&quot;xml&quot;);<br>
&gt; &gt; boolean inserted = buffer.insertFile(view, templateFileName);<br>
&gt; &gt; if (!inserted) {<br>
&gt; &gt;         TEI.error(&quot;Failed to insert template file&quot;, \
templateFileName);<br> &gt; &gt; }<br>
&gt; &gt; // neither of these lines seems to do anything<br>
&gt; &gt; view.getEditPane().getTextArea().goToBufferStart(false);<br>
&gt; &gt; view.getEditPane().getTextArea().setCaretPosition(0);   <br>
&gt; <br>
&gt; <br>
<br>
-- <br>
-----------------------------------------------<br>
jEdit Developers&#39; List<br>
<a href="mailto:jEdit-devel@lists.sourceforge.net" \
target="_blank">jEdit-devel@lists.sourceforge.net</a><br> <a \
href="https://lists.sourceforge.net/lists/listinfo/jedit-devel" rel="noreferrer" \
target="_blank">https://lists.sourceforge.net/lists/listinfo/jedit-devel</a><br> \
</blockquote></div>





-- 
-----------------------------------------------
jEdit Developers' List
jEdit-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jedit-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic