Skip to content
Snippets Groups Projects

Resolve "No clean-up on close"

Merged Tobias Ask requested to merge 16-no-clean-up-on-close into master
3 files
+ 169
132
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -19,6 +19,7 @@ import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.SWT;
@@ -54,18 +55,166 @@ import no.tobask.sb4e.handlers.SceneBuilderOperation;
public class FXMLEditor extends EditorPart {
private EditorController editorController;
private FXCanvas canvas;
private URL fxmlUrl;
private ICompilationUnit controllerClass;
private boolean dirty = false;
private EditorController editorController;
private UndoActionHandler undoActionHandler;
private RedoActionHandler redoActionHandler;
private JavaProjectGlossary glossary;
private IUndoContext undoContext;
private IOperationHistory operationHistory;
private ICompilationUnit controllerClass;
private IAction copyHandler;
private IAction cutHandler;
private IAction pasteHandler;
private IAction deleteHandler;
private UndoActionHandler undoActionHandler;
private RedoActionHandler redoActionHandler;
private ChangeListener<Number> editorSelectionListener = (oV, oldNum, newNum) -> {
FXOMObject fxomRoot = editorController.getFxomDocument().getFxomRoot();
if (fxomRoot == null) {
return;
}
String controllerName = fxomRoot.getFxController();
if (controllerName == null) {
return;
}
if (controllerClass == null || !JavaModelUtils.getQualifiedName(controllerClass).equals(controllerName)) {
controllerClass = JavaModelUtils.getClass(fxmlUrl, controllerName);
if (controllerClass == null) {
return;
}
}
IEditorPart editor = getEditorEditingController();
if (editor != null) {
try {
FXOMObject selectedObject = editorController.getSelection().getHitItem();
if (selectedObject != null) {
String fxId = selectedObject.getFxId();
if (fxId != null) {
int[] location = getLocation(fxId, controllerClass);
if (location != null) {
IFile file = ((FileEditorInput) editor.getEditorInput())
.getFile();
IDE.gotoMarker(editor, createMarker(file, location));
}
}
}
} catch (CoreException e) {
e.printStackTrace();
}
}
};
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
setSite(site);
setInputWithNotify(input);
if (input instanceof FileEditorInput) {
FileEditorInput fxmlFile = (FileEditorInput) input;
setPartName(fxmlFile.getName());
try {
fxmlUrl = fxmlFile.getURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
operationHistory = OperationHistoryFactory.getOperationHistory();
undoContext = new ObjectUndoContext(this);
undoActionHandler = new UndoActionHandler(site, undoContext);
redoActionHandler = new RedoActionHandler(site, undoContext);
}
@Override
public void createPartControl(Composite parent) {
// IMPORTANT: instantiate the canvas before the controllers so that the javafx
// toolkit is initialized
canvas = new FXCanvas(parent, SWT.None);
editorController = new EditorController();
EditorWindowController editorWindowController = new EditorWindowController(editorController);
// make sure the other controllers have their references set before setting the input file
// for the editor controller so they can react to the update
try {
editorController.setFxmlTextAndLocation(FXOMDocument
.readContentFromURL(fxmlUrl), fxmlUrl);
editorController.setLibrary(new CustomClassLoaderLibrary(new EclipseProjectsClassLoader()));
FXOMObject root = editorController.getFxomDocument().getFxomRoot();
String controllerName = null;
if (root != null) {
controllerName = root.getFxController();
}
glossary = new JavaProjectGlossary(controllerName, fxmlUrl,
editorWindowController.infoPanelController);
editorController.setGlossary(glossary);
copyHandler = new SceneBuilderControlActionHandler(editorController, ControlAction.COPY);
cutHandler = new SceneBuilderEditActionHandler(editorController, EditAction.CUT);
pasteHandler = new SceneBuilderEditActionHandler(editorController, EditAction.PASTE);
deleteHandler = new SceneBuilderEditActionHandler(editorController, EditAction.DELETE);
setupUndoRedo();
editorController.startFileWatching();
editorController.getSelection().revisionProperty().addListener(editorSelectionListener);
canvas.setScene(editorWindowController.getScene());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean isDirty() {
return dirty;
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void doSave(IProgressMonitor monitor) {
IFile file = ((FileEditorInput) getEditorInput()).getFile();
byte[] fxmlBytes = null;
try {
fxmlBytes = editorController.getFxmlText().getBytes("UTF-8");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
try {
file.setContents(new ByteArrayInputStream(fxmlBytes), IResource.FORCE, monitor);
} catch (CoreException e1) {
e1.printStackTrace();
}
dirty = false;
firePropertyChange(PROP_DIRTY);
}
@Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
super.dispose();
if (canvas != null) {
canvas.dispose();
}
if (glossary != null) {
JavaCore.removeElementChangedListener(glossary);
}
}
public IAction getUndoActionHandler() {
return undoActionHandler;
@@ -94,38 +243,7 @@ public class FXMLEditor extends EditorPart {
public EditorController getEditorController() {
return editorController;
}
@Override
public boolean isDirty() {
return dirty;
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
setSite(site);
setInputWithNotify(input);
if (input instanceof FileEditorInput) {
FileEditorInput fxmlFile = (FileEditorInput) input;
setPartName(fxmlFile.getName());
try {
fxmlUrl = fxmlFile.getURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
operationHistory = OperationHistoryFactory.getOperationHistory();
undoContext = new ObjectUndoContext(this);
undoActionHandler = new UndoActionHandler(site, undoContext);
redoActionHandler = new RedoActionHandler(site, undoContext);
}
private void setupUndoRedo() {
JobManager jobManager = editorController.getJobManager();
jobManager.revisionProperty().addListener((observable, oldValue, newValue) -> {
@@ -141,73 +259,7 @@ public class FXMLEditor extends EditorPart {
}
});
}
@Override
public void createPartControl(Composite parent) {
// IMPORTANT: instantiate the canvas before the controllers so that the javafx
// toolkit is initialized
FXCanvas canvas = new FXCanvas(parent, SWT.None);
editorController = new EditorController();
editorController.setLibrary(new CustomClassLoaderLibrary(new EclipseProjectsClassLoader()));
setupUndoRedo();
EditorWindowController editorWindowController = new EditorWindowController(editorController);
// make sure the other controllers have their references set before setting the input file
// for the editor controller so they can react to the update
try {
editorController.setFxmlTextAndLocation(FXOMDocument
.readContentFromURL(fxmlUrl), fxmlUrl);
} catch (IOException e) {
e.printStackTrace();
}
editorController.startFileWatching();
String controllerName = editorController.getFxomDocument().getFxomRoot().getFxController();
editorController.setGlossary(new JavaProjectGlossary(controllerName, fxmlUrl,
editorWindowController.infoPanelController));
canvas.setScene(editorWindowController.getScene());
editorController.getSelection().revisionProperty().addListener(editorSelectionListener);
copyHandler = new SceneBuilderControlActionHandler(editorController, ControlAction.COPY);
cutHandler = new SceneBuilderEditActionHandler(editorController, EditAction.CUT);
pasteHandler = new SceneBuilderEditActionHandler(editorController, EditAction.PASTE);
deleteHandler = new SceneBuilderEditActionHandler(editorController, EditAction.DELETE);
}
private ChangeListener<Number> editorSelectionListener = (oV, oldNum, newNum) -> {
FXOMObject fxomRoot = editorController.getFxomDocument().getFxomRoot();
if (fxomRoot == null) {
return;
}
String controllerName = fxomRoot.getFxController();
if (controllerName == null) {
return;
}
if (controllerClass == null || !JavaModelUtils.getQualifiedName(controllerClass).equals(controllerName)) {
controllerClass = JavaModelUtils.getClass(fxmlUrl, controllerName);
if (controllerClass == null) {
return;
}
}
IEditorPart editor = getEditorEditingController();
if (editor != null) {
try {
FXOMObject selectedObject = editorController.getSelection().getHitItem();
if (selectedObject != null) {
String fxId = selectedObject.getFxId();
if (fxId != null) {
int[] location = getLocation(fxId, controllerClass);
if (location != null) {
IFile file = ((FileEditorInput) editor.getEditorInput())
.getFile();
IDE.gotoMarker(editor, createMarker(file, location));
}
}
}
} catch (CoreException e) {
e.printStackTrace();
}
}
};
private IMarker createMarker(IFile file, int[] location) throws CoreException {
IMarker marker = file.createMarker(IMarker.TEXT);
marker.setAttribute(IMarker.CHAR_START, location[0]);
@@ -251,34 +303,4 @@ public class FXMLEditor extends EditorPart {
return topOperation == null || topOperation.getJob() != job;
}
@Override
public void doSave(IProgressMonitor monitor) {
IFile file = ((FileEditorInput) getEditorInput()).getFile();
byte[] fxmlBytes = null;
try {
fxmlBytes = editorController.getFxmlText().getBytes("UTF-8");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
try {
file.setContents(new ByteArrayInputStream(fxmlBytes), IResource.FORCE, monitor);
} catch (CoreException e1) {
e1.printStackTrace();
}
dirty = false;
firePropertyChange(PROP_DIRTY);
}
@Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
Loading