package tool.editors.constanteditor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;
import tool.ToolEditorActivator;
import tool.editors.ConstantPositionUpdater;
import tool.editors.ToolEditor;
public class ConstantEditor extends MultiPageEditorPart implements IResourceChangeListener {
public static final Pattern planConstanPattern = Pattern.compile("constant\\s+(\\S+)\\s+=\\s+(.+?);");
public static final Pattern classConstanPattern = Pattern.compile("has\\s+(\\w+)\\s+constant\\s+(\\S+)\\s+=\\s+(.+?);");
private String name = "";
private String value = "";
private boolean publicc = true;
private Position namePosition;
private Position valuePosition;
private Position visibilityPosition;
public static final String ID = "tool.editors.ConstantEditor"; //$NON-NLS-1$
private IFile modelFile;
private ToolEditor editor;
private ConstantForm form;
private IDocument document;
/**
* @wbp.parser.constructor
*/
public ConstantEditor() {
super();
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
}
/**
* Creates the pages of the multi-page editor.
*/
protected void createPages() {
// createFormPage(); TODO fix the syncronisation between text and form
createToolEditorPage();
}
/**
* create a ToolEditor page
*/
public void createToolEditorPage(){
try {
editor = new ToolEditor();
int index = addPage(editor, getEditorInput());
setPageText(index, "Source");
document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
try {
parse();
} catch (BadLocationException e) {
ToolEditorActivator.showError("Error parsing constant " + modelFile.getName(), e);
}
} catch (PartInitException e) {
ErrorDialog.openError(
getSite().getShell(),
"Error creating nested Tool editor",
null,
e.getStatus());
}
}
/**
* Create contents of the view part.
* @param parent
*/
public void createFormPage() {
form = new ConstantForm();
int index;
try {
index = addPage(form, getEditorInput());
setPageText(index, "Constant");
} catch (PartInitException e) {
ErrorDialog.openError(
getSite().getShell(),
"Error creating nested Constant editor",
e.getMessage(),
e.getStatus());
}
}
public void dispose() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
this.modelFile = null;
this.document = null;
super.dispose();
}
public void gotoMarker(IMarker marker) {
setActivePage(1);
IDE.gotoMarker(getEditor(1), marker);
}
@Override
public void doSave(IProgressMonitor monitor) {
// check to see of the Constant name has changed and
// prompt to change the file name
// try {
// TODO Form page stuff
// String resourceName = modelFile.getName();
// String constantName = document.get(namePosition.offset, namePosition.length);
// resourceName = resourceName.substring(0, resourceName.lastIndexOf('.'));
// if (constantName != null &&
// !resourceName.equals(constantName)){
// MessageDialog dialog = new MessageDialog(getSite().getShell(),
// "Rename Constant", getTitleImage(),
// "You have changed the name of the constant, do you want to rename the constant file?",
// MessageDialog.QUESTION,
// new String[] {"Yes", "No"},
// 0);
// if (dialog.open() == 0){
// this.modelFile.move(new Path(constantName + ".CON"), true, monitor);
// }
// }
editor.doSave(monitor);
// } catch (CoreException e) {
// ToolEditorActivator.showError("Error renaming Constant resource", e);
// } catch (BadLocationException e) {
// ToolEditorActivator.showError("Error renaming Constant resource", e);
// }
}
@Override
public void doSaveAs() {
}
@Override
public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException{
super.init(site, editorInput);
setPartName(editorInput.getName());
IFileEditorInput ifei = (IFileEditorInput)editorInput;
modelFile = ifei.getFile();
}
/**
* Closes all project files on project close.
*/
public void resourceChanged(final IResourceChangeEvent event){
if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
Display.getDefault().asyncExec(new Runnable(){
public void run(){
IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
for (int i = 0; i<pages.length; i++){
if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
pages[i].closeEditor(editorPart,true);
}
}
}
});
}
}
/**
* Calculates the contents of page 2 when the it is activated.
*/
// protected void pageChange(int newPageIndex) {
// super.pageChange(newPageIndex);
// if (newPageIndex == 0) {
//
// this.form.load(document, visibilityPosition, namePosition, valuePosition);
// }
// }
private void parse() throws BadLocationException{
String source;
source = document.get();
document.addPositionCategory("value");
document.addPositionCategory("name");
document.addPositionCategory("visability");
Matcher matcher = classConstanPattern.matcher(source);
if (matcher.find()){
this.publicc = (matcher.group(1).startsWith("public"));
int start = matcher.start(1);
int length = matcher.end(1)-matcher.start(1);
visibilityPosition = new Position(start, length);
document.addPosition(visibilityPosition);
this.name = matcher.group(2);
start = matcher.start(2);
length = matcher.end(2)-matcher.start(2);
namePosition = new Position(start, length);
document.addPosition(namePosition);
this.value = matcher.group(3);
start = matcher.start(3);
length = matcher.end(3)-matcher.start(3);
valuePosition = new Position(start, length);
document.addPosition(valuePosition);
}
matcher = planConstanPattern.matcher(source);
if (matcher.find()){
this.name = matcher.group(1);
int start = matcher.start(1);
int length = matcher.end(1)-matcher.start(1);
namePosition = new Position(start, length);
document.addPosition(namePosition);
this.value = matcher.group(2);
start = matcher.start(2);
length = matcher.end(2)-matcher.start(2);
valuePosition = new Position(start, length);
document.addPosition(valuePosition);
}
document.addPositionUpdater(new ConstantPositionUpdater("value", valuePosition));
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public Image getTitleImage() {
return ToolEditorActivator.getImage("icons/private_constant.gif");
}
@Override
public String getTitleToolTip() {
return "Edit a Constant";
}
}