Commit 03d006c8 authored by Benedikt Engeser's avatar Benedikt Engeser

DocListener

parent f012dc71
package de.tum.in.www2.cupplugin.controller;
import java.util.HashMap;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.IDocument;
public class Controller {
// Static reference to the controller
static Controller instance;
// The Job handling changes in a document is triggered after x seconds
static final int SET_CHANGED_AFTER_SECONDS = 3;
// Static reference to the controller instance per document
static HashMap<IDocument, Controller> instances = new HashMap<IDocument, Controller>();
// attributes
// The Job that's executed after a document change is detected (changeJob)
private DocumentDidChangeJob myJob = null;
// the document, the instance is managing
private IDocument myDocument = null;
// if the changeJob takes some time, it's good to know if it has to be
// reexecuted after it finishes
private long changeJobReRunRequested = 0;
// a lock, since some operations might not be threadsafe
private final Object lock = new Object();
/*
* Static singelton getter
*/
static Controller getInstance() {
public static Controller getInstance(IDocument doc) {
Controller instance = instances.get(doc);
if (instance != null) {
return instance;
} else {
instance = new Controller();
return instance;
instances.put(doc, new Controller(doc));
return instances.get(doc);
}
}
public Controller() {
/*
* The Constructor, taking the document it's controlling as a parameter
*/
public Controller(IDocument doc) {
myDocument = doc;
myJob = new DocumentDidChangeJob(doc);
myJob.setSystem(true);
}
/*
* notify method that's to be called, when a change in the document happens
*/
public void notifyChange() {
synchronized (lock) {
if (myJob.getState() == Job.NONE
|| (!myJob.running() && myJob.getState() == Job.SLEEPING)) {
myJob.cancel();
myJob.schedule(SET_CHANGED_AFTER_SECONDS * 1000);
} else {
changeJobReRunRequested = System.currentTimeMillis();
}
}
}
/*
* notify callback, that the changeJob did finish
*/
public void notifyChangeJobFinished() {
synchronized (lock) {
System.out.println("job finished -> " + changeJobReRunRequested); // Log message TODO: DELETE!
if (changeJobReRunRequested > 0) {
changeJobReRunRequested = 0;
long remainderOfDelay = (SET_CHANGED_AFTER_SECONDS * 1000)
- (System.currentTimeMillis() - changeJobReRunRequested);
myJob.schedule((remainderOfDelay >= 0) ? remainderOfDelay : 0);
}
}
}
}
package de.tum.in.www2.cupplugin.editors;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import java_cup.*;
import java_cup.runtime.ComplexSymbolFactory;
public class CupTextEditor extends TextEditor {
public CupTextEditor() {
super();
// ComplexSymbolFactory f = new ComplexSymbolFactory();
// java_cup.Lexer = new java_cup.Lexer(arg0, f);
}
// ComplexSymbolFactory f = new ComplexSymbolFactory();
// java_cup.Lexer = new java_cup.Lexer(arg0, f);
}
}
......@@ -13,6 +13,9 @@ import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
......@@ -30,8 +33,11 @@ import org.eclipse.ui.*;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.ide.IDE;
/**
* An example showing how to create a multi-page editor.
* This example has 3 pages:
......@@ -64,6 +70,7 @@ public class MultiPageEditor extends MultiPageEditorPart implements IResourceCha
editor = new CupTextEditor();
int index = addPage(editor, getEditorInput());
setPageText(index, editor.getTitle());
addHooksToMainTextEditor(editor);
} catch (PartInitException e) {
ErrorDialog.openError(
getSite().getShell(),
......@@ -193,4 +200,23 @@ public class MultiPageEditor extends MultiPageEditorPart implements IResourceCha
}
text.setText(displayText.toString());
}
boolean addHooksToMainTextEditor(ITextEditor editor) {
// Add Document listener to react on changes
IDocumentProvider dp = editor.getDocumentProvider();
if(dp == null){
return false;
}
IDocument doc = dp.getDocument(getEditorInput());
if(doc == null){
return false;
}
doc.addDocumentListener(new CTEDocumentListener());
return true;
}
}
package de.tum.in.www2.cupplugin.model;
public interface ModelObserverInterface {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment