Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Making part of DSL read-only
Making part of DSL read-only [message #994354] Wed, 26 December 2012 23:30 Go to next message
Ashwani Kr Sharma is currently offline Ashwani Kr Sharma
Messages: 118
Registered: July 2009
Location: Bangalore, India
Senior Member

Hi,

Is it possible to make few lines of my DSL read-only ?

Regards,
Ashwani Kr Sharma
Re: Making part of DSL read-only [message #999613 is a reply to message #994354] Sat, 12 January 2013 12:08 Go to previous messageGo to next message
Ashwani Kr Sharma is currently offline Ashwani Kr Sharma
Messages: 118
Registered: July 2009
Location: Bangalore, India
Senior Member

Hi,

I am kinna stuck with this issue. Can you please give some hints.

Regards,
Ashwani Kr Sharma
Re: Making part of DSL read-only [message #999630 is a reply to message #994354] Sat, 12 January 2013 12:54 Go to previous messageGo to next message
Alex G is currently offline Alex G
Messages: 96
Registered: January 2012
Member
Hi!

I don't know exactly, but it sounds like an Eclipse editor issue. Maybe the API of XTextSourceViewer, e.g. org.eclipse.jface.text.TextViewer could help you. There is a method setEditable.

You could also read a little about partitions:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Feditors_documents.htm&cp=2_0_13_2

Would be interesting to know, if you can come up with a fancy solution Smile.

Best regards,
Alex.
Re: Making part of DSL read-only [message #1000481 is a reply to message #999613] Mon, 14 January 2013 16:38 Go to previous message
Hallvard Traetteberg is currently offline Hallvard Traetteberg
Messages: 594
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

I recently needed something like this and instead of preventing editing
a certain region I ended up with code that prevented moving the text
cursor into a read-only region. I don't think this is a fool-proof
design, but it may still be useful. The code is included below.

Hallvard

package org.ptolemy.xtext.ui.custom;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CaretEvent;
import org.eclipse.swt.custom.CaretListener;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.xtext.ui.editor.XtextSourceViewer;

public class SelectionLimitingXtextSourceViewer extends XtextSourceViewer
implements CaretListener, LineBackgroundListener {

private int minimumOffset = 0, maximumOffset = 0;

public SelectionLimitingXtextSourceViewer(Composite parent,
IVerticalRuler ruler, IOverviewRuler overviewRuler,
boolean showsAnnotationOverview, int styles) {
super(parent, ruler, overviewRuler, showsAnnotationOverview, styles);
getTextWidget().addCaretListener(this);
getTextWidget().addLineBackgroundListener(this);
}

@Override

public void lineGetBackground(LineBackgroundEvent event) {
int offset = event.lineOffset;
if (! isValidOffset(offset)) {
event.lineBackground =
getControl().getDisplay().getSystemColor(SWT.COLOR_GRAY);
}
}

private boolean movingCaret = false;

@Override
public void caretMoved(CaretEvent event) {
// System.out.println("Caret: " + event.caretOffset);
if (movingCaret) {
return;
}
int offset = getValidOffset(event.caretOffset);
if (offset != event.caretOffset) {
System.out.println("Caret: " + event.caretOffset + " != " + offset);
movingCaret = true;
getTextWidget().setCaretOffset(offset);
movingCaret = false;
}
}

public void setReadOnlyRegion(int minimumSelectionOffset, int
maximumSelectionOffset) {
this.minimumOffset = minimumSelectionOffset;
this.maximumOffset = maximumSelectionOffset;
}

@Override
protected void validateSelectionRange(int[] selectionRange) {
// System.out.println("Selection: " + selectionRange[0] + ", " +
selectionRange[1]);
int start = selectionRange[0], end = start + selectionRange[1];
int validStart = getValidOffset(start), validEnd = getValidOffset(end);
selectionRange[0] = validStart;
selectionRange[1] = validEnd - validStart;
// System.out.println("Selection = " + selectionRange[0] + ", " +
selectionRange[1]);
super.validateSelectionRange(selectionRange);
}

protected boolean isValidOffset(int offset) {
return offset == getValidOffset(offset);
}

protected int getValidOffset(int offset) {
IRegion visibleRegion = getVisibleRegion();
int minOffset = minimumOffset;
int maxOffset = maximumOffset;
if (maxOffset <= 0) {
maxOffset += visibleRegion.getLength();
}
int validOffset = getValidOffset(offset, minOffset, maxOffset);
return validOffset;
}

protected int getValidOffset(int offset, int minOffset, int maxOffset) {
// System.out.println("Validating: " + minOffset + " < " + offset + " <
" + maxOffset);
if (offset < minOffset) {
return minOffset;
} else if (offset > maxOffset) {
return maxOffset;
}
return offset;
}

public static class Factory implements XtextSourceViewer.Factory {
@Override
public XtextSourceViewer createSourceViewer(Composite parent,
IVerticalRuler ruler, IOverviewRuler overviewRuler,
boolean showsAnnotationOverview, int styles) {
return new SelectionLimitingXtextSourceViewer(parent, ruler,
overviewRuler, showsAnnotationOverview, styles);
}
}
}


On 12.01.13 09.08, Ashwani Kr Sharma wrote:
> Hi,
>
> I am kinna stuck with this issue. Can you please give some hints.
>
> Regards,
> Ashwani Kr Sharma
Previous Topic:Changing Xtext terminal symbols without changing the underlying model
Next Topic:XText TextViewer instead of Editor
Goto Forum:
  


Current Time: Tue Sep 02 13:00:43 EDT 2014

Powered by FUDForum. Page generated in 0.09631 seconds