making the document read only in CompilationUnitEditor ?? [message #215768] |
Tue, 27 September 2005 20:53  |
Eclipse User |
|
|
|
Originally posted by: sbajrach.ics.uci.edu
something like this has been in discussion twice in this newsgroup but
still I see no solutions yet. Here are the details...
#problem: Using the Java Editor (CompilationUnitEditor), how to make
certain parts inside the editor read only ?
#issues:
i am still figuring out what is the right way to do this. First approach
that works is defining a collection of Positions for the document
(source) being edited, track the cursor within these positions and once
the cursor is inside the position, disable editing in the editor (in
StyledText from the CompilationUnitEditor)
the problem with the above is it cannot bypass the Key bindings for
quick edit like Ctrl+E for deleting the line (and when the selected text
is not the whole line but only some text in a line), for that i'll have
to track whether the positions to be protected are in the line that'll
be removed. This seems bit inelegant. And, is there a way to iterate all
possible edits that can be made in the document excluding those that
are made by directly manipulating the text?
a quick search in eclipse's dev newsgroup points to two things
- *make the File readonly*: I tried this (calling setReadOnly(true) for
the File) but, this again has the problem of locking up the document is
a sense that it cannot be made writable again w/o closing and reopening
the file
- *make the Document readonly*: i still have not found the way to do
this. here, I am refereing to the 'Document' instance returned by the
DocumentProvider of the editor. I actually do not see the the Document
class and its parent/childs in the tree have any operations that support
making the document read only. So I am still wandering what does it mean
to say make the Document read-only (read this as an option in the
newsgroup and lost the link). i can think of listening to the
IDocumentListener so that I can get inside the pre change method and
cancel the change to be performed, something like -
documentAboutToBeChanged(DocumentEvent event)
{..I want to cancel this change request here..}
but this doesnot seem to be possible.
I am quite lost now.
i would appreciate any pointer to the right direction regarding this.
- sushil
|
|
|
Re: making the document read only in CompilationUnitEditor ?? [message #215965 is a reply to message #215768] |
Fri, 30 September 2005 03:52  |
Eclipse User |
|
|
|
Sushil,
In short: There is no framework support for read-only regions inside an
otherwise editable document. See my comments below.
Sushil K Bajracharya wrote:
> i am still figuring out what is the right way to do this. First approach
> that works is defining a collection of Positions for the document
> (source) being edited, track the cursor within these positions and once
> the cursor is inside the position, disable editing in the editor (in
> StyledText from the CompilationUnitEditor)
>
> the problem with the above is it cannot bypass the Key bindings for
> quick edit like Ctrl+E for deleting the line (and when the selected text
> is not the whole line but only some text in a line), for that i'll have
> to track whether the positions to be protected are in the line that'll
> be removed. This seems bit inelegant. And, is there a way to iterate all
> possible edits that can be made in the document excluding those that
> are made by directly manipulating the text?
Modifying actions always check for whether the editor input is
modifiable. See TextEditorAction#canModifyEditor for the logic.
> - *make the File readonly*:
Probably not the right thing to do: the file is editable, ie. not
write-protected by the file system, as certain parts are editable. This
property has more to do with whether a file may need saving etc.
AbstractTextEditor.isEditorInputModifiable will answer whether the
editor's input can be modified (independently of whether the underlying
file is read-only). However, this predicate only talks about the entire
input document. There is no notion of editable regions vs. read-only ones.
> - *make the Document readonly*: i still have not found the way to do
> this.
There is no support for this in the framework.
Your best bet is probably to track the edit position and based on it not
only disable editing on the StyledText widget but also modify the
isEditorInputModifiable property of the editor (I assume you have a
subclass of CompilationUnitEditor).
Shortcomings of this approach: user actions that are not local to the
edit location (e.g. Organize Imports, generate constructor using fields,
many source actions, formatting, Java model operations, refactorings,
quick fixes etc...) may modify the document in a different location than
where the caret is. And there is really no good solution to this: how
would you locally rename a variable, if one occurrence is read-only, but
not the other?
Hope this explains some of your questions.
-tom
|
|
|
Powered by
FUDForum. Page generated in 0.72029 seconds