Eclipse e4 RCP SourceViewer syntax coloring - eclipse

To implement syntax coloring in an eclipse e4 RCP application, I have created a basic plugin project with a Part including a SourceViewer control.
public class SyntaxColoringTest {
/** The SourceViewer control to create the editor. */
public SourceViewer sv = null;
#Inject
public SyntaxColoringTest() {
}
#PostConstruct
public void postConstruct(Composite parent) {
IVerticalRuler verticalRuler = new VerticalRuler(10);
OverviewRuler overviewRuler = new OverviewRuler(null, 20, null);
sv = new SourceViewer(parent, verticalRuler, overviewRuler, true, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
sv.configure(new TestSourceViewerConf());
}
}
Being TestSourceViewerConf as follows:
public class TestSourceViewerConf extends SourceViewerConfiguration {
public ITokenScanner tokenScanner;
public IRule patternRule;
public IRule endOfLineRule;
public TestSourceViewerConf(){
tokenScanner = createTokenScanner();
}
public IPresentationReconciler getPresentationReconciler(ISourceViewer viewer) {
PresentationReconciler reconciler= new PresentationReconciler();
DefaultDamagerRepairer defDamagerRepairer= new DefaultDamagerRepairer(tokenScanner);
reconciler.setDamager(defDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(defDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
return reconciler;
}
private ITokenScanner createTokenScanner() {
RuleBasedScanner scanner= new RuleBasedScanner();
scanner.setRules(createRules());
return scanner;
}
private IRule[] createRules() {
Display display = Display.getCurrent();
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
IToken tokenA= new Token(new TextAttribute(blue));
IToken tokenB= new Token(new TextAttribute(blue));
patternRule= new PatternRule("<", ">", tokenA, '\\', false);
endOfLineRule = new EndOfLineRule("++ ", tokenB);
return new IRule[] {patternRule, endOfLineRule};
}
}
When running the application nothing is colored when typing after "++ " or in between < >
Thanks

This code works for me testing in one of my own e4 editors.
What you haven't shown is any set up of the document for the source viewer. If you don't set a document my test shows the behavior you are seeing. Set the document with:
IDocument doc = new Document(contents);
sv.setDocument(doc);
where contents is the initial contents of the document.

Related

Combo Box is not observed

I have the following Code:
public class GuiView extends Application {
private ObservableList<String> shareNames = FXCollections.observableArrayList();
public void start(Stage stage) {
...
ComboBox<String> comboBox = new ComboBox<String>();
comboBox.getItems().addAll(this.shareNames);
MenuItem open = new MenuItem("Open...");
open.setOnAction( e -> {
// FileChooser code...
if (selctedFile != null) {
this.shareNames.addAll("teststring");
}
});
}
}
When I run through the open dialog successfully the combo box doesn't update and shows the teststring. What is going wrong here?
You are updating shareNames, but that is not the list used by the combo box.
Either replace
comboBox.getItems().addAll(this.shareNames);
with
comboBox.setItems(this.shareNames);
or replace
this.shareNames.addAll("teststring");
with
comboBox.getItems().add("teststring");

Eclipse Plugin - How to open a wizard page in a command handler?

I write a plugin for eclipse. I have a button in the toolbar menu. and I want that on pressing on it - a wizard page dialog will be opened. I wrote already a class which extends wizard and implements IWizardPage, and I wrote also all the 5 relevant pages, I only don't find any way to open this in the command handler.
Here is the pieces of my code:
The command handler:
public class AddProjectHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
return null;
}
}
The wizard page manager:
public class NewProjectWizardManager extends Wizard implements INewWizard {
private NewProjectWizardPage1 _page1;
private NewProjectWizardPage2 _page2;
private NewProjectWizardPage3 _page3;
private NewProjectWizardPage4 _page4;
private NewProjectWizardPage5 _page5;
// constructor
public NewProjectWizardManager() {
super();
setWindowTitle("New Project");
}
#Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
}
#Override
public boolean performCancel() {
return true;
}
#Override
public void addPages() {
super.addPages();
_page1 = new NewProjectWizardPage1();
addPage(_page1);
_page2 = new NewProjectWizardPage2(_page1);
addPage(_page2);
_page3 = new NewProjectWizardPage3(_page1);
addPage(_page3);
_page4 = new NewProjectWizardPage4();
addPage(_page4);
_page5 = new NewProjectWizardPage5(_page1);
addPage(_page5);
}
#Override
public boolean canFinish() {
IWizardContainer container = getContainer();
if (_page5.equals(container.getCurrentPage())) {
return true;
} else {
return false;
}
}
#Override
public IWizardPage getNextPage(IWizardPage page) {
IWizardPage nextPage = super.getNextPage(page);
IWizardContainer container = getContainer();
if (nextPage != null) {
if (_page2.equals(container.getCurrentPage()) && _page2.isCheckFinishChecked())
nextPage = super.getNextPage(super.getNextPage(nextPage));
}
return nextPage;
}
#Override
public boolean performFinish() {
}
}
The plugin.xml pieces:
<command
categoryId="com.commands.category"
description="Add new Project"
id="com.commands.AddProject"
name="Add new Project">
</command>
<handler
class="com.handlers.AddProjectHandler"
commandId="com.commands.AddProject">
</handler>
Do you have any idea?
Use WizardDialog to show a wizard. Something like:
public Object execute(ExecutionEvent event) throws ExecutionException
{
Shell activeShell = HandlerUtil.getActiveShell(event);
IWizard wizard = new NewProjectWizardManager();
WizardDialog dialog = new WizardDialog(activeShell, wizard);
dialog.open();
return null;
}
I found the code below from org.eclipse.jdt.ui.actions.AbstractOpenWizardAction.
Before Eclipse3.4 you can extend this class to create an Action.but action is deprecated now,I wonder if Eclipse.org provide something like AbstractOpenWizardAction to do the same work in command-handler mode. I'v not found it yet.
public void run() {
Shell shell = getShell();
if (!(doCreateProjectFirstOnEmptyWorkspace(shell)))
return;
try {
INewWizard wizard = createWizard();
wizard.init(PlatformUI.getWorkbench(), getSelection());
WizardDialog dialog = new WizardDialog(shell, wizard);
PixelConverter converter = new PixelConverter(JFaceResources.getDialogFont());
dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70),
converter.convertHeightInCharsToPixels(20));
dialog.create();
int res = dialog.open();
if ((res == 0) && (wizard instanceof NewElementWizard)) {
this.fCreatedElement = ((NewElementWizard) wizard).getCreatedElement();
}
notifyResult(res == 0);
} catch (CoreException e) {
String title = NewWizardMessages.AbstractOpenWizardAction_createerror_title;
String message = NewWizardMessages.AbstractOpenWizardAction_createerror_message;
ExceptionHandler.handle(e, shell, title, message);
}
}

GWT-Editors and sub-editors

I'm trying to run an example of Editors with sub-editors.
When flushing the parent the value of child editor is null.
The classes are Person and Address.
The main editor is:
// editor fields
public TextField firstname;
public TextField lastname;
public NumberField<Integer> id;
public AddressEditor address = new AddressEditor();
public PersonEditor(Person p){
asWidget();
}
#Override
public Widget asWidget() {
setWidth(400);
setBodyStyle("padding: 5px;");
setHeaderVisible(false);
VerticalLayoutContainer c = new VerticalLayoutContainer();
id = new NumberField<Integer>(new IntegerPropertyEditor());
// id.setName("id");
id.setFormat(NumberFormat.getFormat("0.00"));
id.setAllowNegative(false);
c.add(new FieldLabel(id, "id"), new VerticalLayoutData(1, -1));
firstname = new TextField();
// firstname.setName("firstname");
c.add(new FieldLabel(firstname, "firstname"), new VerticalLayoutData(1, -1));
lastname = new TextField();
lastname.setName("lastname");
c.add(new FieldLabel(lastname, "lastname"), new VerticalLayoutData(1, -1));
c.add(address);
add(c);
return this;
The sub-editor:
public class AddressEditor extends Composite implements Editor<Address> {
private AddressProperties props = GWT.create(AddressProperties.class);
private ListStore<Address> store = new ListStore<Address>(props.key());
ComboBox<Address> address;
public AddressEditor() {
for(int i = 0; i < 5; i ++)
store.add(new Address("city" + i));
address = new ComboBox<Address>(store, props.nameLabel());
address.setAllowBlank(false);
address.setForceSelection(true);
address.setTriggerAction(TriggerAction.ALL);
initWidget(address);
}
And this is where the Driver is created:
private HorizontalPanel hp;
private Person googleContact;
PersonDriver driver = GWT.create(PersonDriver.class);
public void onModuleLoad() {
hp = new HorizontalPanel();
hp.setSpacing(10);
googleContact = new Person();
PersonEditor pe = new PersonEditor(googleContact);
driver.initialize(pe);
driver.edit(googleContact);
TextButton save = new TextButton("Save");
save.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
googleContact = driver.flush();
System.out.println(googleContact.getFirstname() + ", " + googleContact.getAddress().getCity());
if (driver.hasErrors()) {
new MessageBox("Please correct the errors before saving.").show();
return;
}
}
});
The value of googleContact.getFirstname() is filled but googleContact.getAddress() is always null.
What I'm missing?
The AddressEditor needs to map to the Address model - presently, it doesn't seem to, unless Address only has one getter/setter, called getAddress() and setAddress(Address), which wouldn't really make a lot of sense.
If you want just a ComboBox<Address> (which implements Editor<Address> already), consider putting that combo in the PersonEditor directly. Otherwise, you'll need to add #Path("") to the AddressEditor.address field, to indicate that it should be directly editing the value itself, and not a sub property (i.e. person.getAddress().getAddress()).
Another way to build an address editor would be to list each of the properties of the Address type in the AddressEditor. This is what the driver is expecting by default, so it is confused when it sees a field called 'address'.
Two quick thoughts on the code itself: there is no need to pass a person into the PersonEditor - thats the job of the driver itself. Second, your editor fields do not need to be public, they just can't be private.

How to implement something like a wizard screen?

I want to place a "Next" button which , when clicked , will display another group of components ; and I want also to place a "Previous" button which , when clicked , then display the previous group of components. How to achieve that ?
I recently implemented forms for data entry. Typically i have a wizard class that holds all the forms in the wizard, so i can easily navigate back and forth between them. And when i call a new form, i pass along the object of the wizard.
Below is my wizard, with implementation omitted.
public final class ReportWizard {
public static ReportWizard instance = null;
Form parent = null;
Form titleForm = null;
Form budgetForm = null;
Form iconForm = null;
final Report reports[] = new Report[20];
public ReportWizard(Form parent) {
this.parent = parent;
this.instance = this;
}
void getTitle() {
AddReportForm reportForm = new AddReportForm(parent, this);
reportForm.showReportForm();
titleForm = reportForm;
ImageListPicker getIcon = new ImageListPicker(titleForm, reports, this);
iconForm = getIcon.imageListForm;
}
void getIcon() {
iconForm.show();
}
public void cancelWizard() {
titleForm = null;
iconForm = null;
budgetForm = null;
instance = null;
parent.show();
parent = null;
System.gc();
}
}

setSelectionProvider over two different controls not working

I am Trying to create Eclipse Plugin which has a composite with two TreeViewer side by side. On click of each TreeViewer content Eclipse property view should give appropriate information. Now I wanted to set Selection provider for both of this treeviewer hence I used
setSelectionProvider(treeViewer1)
setSelectionProvider(treeviewer2)
But only the second added treeviewer get set since the first one is overwritten. I am intiating this two treeviewer from class Queue.java. Hence I implemented the interface ISelectionProvider over Queue.java as below:
public void addSelectionChangedListener(ISelectionChangedListener listener)
{
selectionChangedListeners.add(listener);
}
public void
removeSelectionChangedListener(ISelectionChangedListener listener)
{
selectionChangedListeners.remove(listener);
}
private void fireSelectionChanged(final SelectionChangedEvent event)
{
Object[] listeners = selectionChangedListeners.getListeners();
for (int i = 0; i < listeners.length; ++i)
{
final ISelectionChangedListener l =
(ISelectionChangedListener) listeners[i];
Platform.run(new SafeRunnable()
{
public void run()
{
l.selectionChanged(event);
}
#Override
public void handleException(Throwable e)
{
removeSelectionChangedListener(l);
}
});
}
}
public void setSelection(ISelection selection)
{
fireSelectionChanged(new SelectionChangedEvent(this, selection));
}
public ISelection getSelection()
{
ArrayList<Object> list = new ArrayList<Object>();
Object o = getProperties();
if (o instanceof IPropertySource)
list.add(o);
return new StructuredSelection(list);
}
Can anyone help me how to resolve this issue. I will be grateful. thanks in advance. Tor.
Your view would have to write a selection provider wrapper or mediator that would delegate to the viewer that currently had focus. Then your view would set it up something like this:
SelectionProviderWrapper wrapper = new SelectionProviderWrapper();
wrapper.addViewer(treeViewer1);
wrapper.addViewer(treeViewer2);
getSite().setSelectionProvider(wrapper);
I would check out org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator for an example of a selection provider for multiple JFace viewers.