Skip to content

Commit

Permalink
Fix for bug in #585 and even less code duplication
Browse files Browse the repository at this point in the history
 - fixed the Papyrus visualization failure when there is already a "gen" folder present
    - the problem was solved by refreshing the folder after cleanup
 - reduced cleanup code duplication in `TxtUMLVisualizeWizard` and it's children (#582)
  • Loading branch information
szokolai-mate committed Apr 19, 2018
1 parent 10a14cd commit b2476fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package hu.elte.txtuml.export.javascript.wizardz;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import hu.elte.txtuml.api.layout.ClassDiagram;
import hu.elte.txtuml.api.layout.CompositeDiagram;
import hu.elte.txtuml.api.layout.StateMachineDiagram;
import hu.elte.txtuml.export.javascript.Exporter;
import hu.elte.txtuml.layout.export.TxtUMLLayoutDescriptor;
import hu.elte.txtuml.utils.Pair;
import hu.elte.txtuml.utils.eclipse.preferences.PreferencesManager;
import hu.elte.txtuml.utils.eclipse.wizards.UML2VisualizeWizard;
import hu.elte.txtuml.utils.eclipse.wizards.VisualizeTxtUMLPage;

Expand Down Expand Up @@ -59,49 +49,14 @@ protected void exportDiagram(TxtUMLLayoutDescriptor layoutDescriptor, IProgressM
@Override
protected void cleanBeforeVisualization(Set<Pair<String, String>> layouts) throws CoreException, IOException {
for (Pair<String, String> model : layouts) {
String txtUMLModelName = model.getFirst();
String txtUMLProjectName = model.getSecond();

String generatedFolderName = PreferencesManager
.getString(PreferencesManager.TXTUML_VISUALIZE_DESTINATION_FOLDER);

String projectAbsLocation = ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProjectName).getLocation()
final String txtUMLProjectName = model.getSecond();
final String projectAbsLocation = ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProjectName).getLocation()
.toFile().getAbsolutePath();

Path notationFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".di");
Path mappingFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".mapping");
Path diFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".notation");
Path umlFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".uml");
Path profileFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".profile.uml");

Path modelFolderPath = Paths.get(projectAbsLocation, generatedFolderName, "js", txtUMLModelName);

Path htmlFilePath = Paths.get(modelFolderPath.toString(), "visualize.html");
URI htmlFileURI = URI.createFileURI(htmlFilePath.toString());

IFile htmlFile = ResourcesPlugin.getWorkspace().getRoot()
.getFile(new org.eclipse.core.runtime.Path(CommonPlugin.resolve(htmlFileURI).toFileString()));
Path genFolderPath = Paths.get(projectAbsLocation, getGeneratedFolderName());
deleteFolderRecursively(genFolderPath);

IEditorInput input = new FileEditorInput(htmlFile);

IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor(input);
if (editor != null) {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editor, false);
}

deleteFolderRecursively(modelFolderPath);

profileFilePath.toFile().delete();
mappingFilePath.toFile().delete();
umlFilePath.toFile().delete();
diFilePath.toFile().delete();
notationFilePath.toFile().delete();
}
}

private void deleteFolderRecursively(Path modelFolderPath) throws IOException {
if (modelFolderPath.toFile().exists()) {
Files.walk(modelFolderPath).map(Path::toFile).sorted((o1, o2) -> -o1.compareTo(o2)).forEach(File::delete);
refreshGeneratedFolder(txtUMLProjectName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import hu.elte.txtuml.layout.export.TxtUMLLayoutDescriptor;
import hu.elte.txtuml.utils.Pair;
import hu.elte.txtuml.utils.eclipse.Dialogs;
import hu.elte.txtuml.utils.eclipse.preferences.PreferencesManager;
import hu.elte.txtuml.utils.eclipse.wizards.UML2VisualizeWizard;
import hu.elte.txtuml.utils.eclipse.wizards.VisualizeTxtUMLPage;

Expand Down Expand Up @@ -87,26 +86,25 @@ protected IStatus run(IProgressMonitor innerMonitor) {
@Override
protected void cleanBeforeVisualization(Set<Pair<String, String>> layouts) throws CoreException, IOException {
for (Pair<String, String> model : layouts) {
String txtUMLModelName = model.getFirst();
String txtUMLProjectName = model.getSecond();
final String txtUMLModelName = model.getFirst();
final String txtUMLProjectName = model.getSecond();

String generatedFolderName = PreferencesManager
.getString(PreferencesManager.TXTUML_VISUALIZE_DESTINATION_FOLDER);

String projectAbsLocation = ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProjectName).getLocation()
final String projectAbsLocation = ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProjectName).getLocation()
.toFile().getAbsolutePath();

Path notationFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".di");
Path mappingFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".mapping");
Path diFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".notation");
Path umlFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".uml");
Path profileFilePath = Paths.get(projectAbsLocation, generatedFolderName, txtUMLModelName + ".profile.uml");
Path notationFilePath = Paths.get(projectAbsLocation, getGeneratedFolderName(), txtUMLModelName + ".di");
Path mappingFilePath = Paths.get(projectAbsLocation, getGeneratedFolderName(), txtUMLModelName + ".mapping");
Path diFilePath = Paths.get(projectAbsLocation, getGeneratedFolderName(), txtUMLModelName + ".notation");
Path umlFilePath = Paths.get(projectAbsLocation, getGeneratedFolderName(), txtUMLModelName + ".uml");
Path profileFilePath = Paths.get(projectAbsLocation, getGeneratedFolderName(), txtUMLModelName + ".profile.uml");

profileFilePath.toFile().delete();
mappingFilePath.toFile().delete();
umlFilePath.toFile().delete();
diFilePath.toFile().delete();
notationFilePath.toFile().delete();

refreshGeneratedFolder(txtUMLProjectName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
Expand Down Expand Up @@ -173,4 +177,10 @@ protected void checkNoLayoutDescriptionsSelected() throws InterruptedException {
}
}

protected static void deleteFolderRecursively(Path modelFolderPath) throws IOException {
if (modelFolderPath.toFile().exists()) {
Files.walk(modelFolderPath).map(Path::toFile).sorted((o1, o2) -> -o1.compareTo(o2)).forEach(File::delete);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.operation.IRunnableWithProgress;
Expand All @@ -23,6 +25,9 @@
import hu.elte.txtuml.utils.eclipse.Dialogs;
import hu.elte.txtuml.utils.eclipse.preferences.PreferencesManager;

/**
* Abstract class for {@link TxtUMLVisualizeWizard} which first exports UML2 then uses that for the visualization.
*/
public abstract class UML2VisualizeWizard extends TxtUMLVisualizeWizard {

public UML2VisualizeWizard() {
Expand Down Expand Up @@ -54,7 +59,7 @@ public void run(IProgressMonitor monitor) throws InterruptedException {
try {
TxtUMLToUML2.exportModel(txtUMLProjectName, txtUMLModelName,
txtUMLProjectName + "/" + generatedFolderName, ExportMode.ErrorHandlingNoActions,
"gen");
generatedFolderName);
monitor.worked(10);
} catch (Exception e) {
Dialogs.errorMsgb("txtUML export Error", "Error occured during the UML2 exportation.", e);
Expand Down Expand Up @@ -94,16 +99,33 @@ public void run(IProgressMonitor monitor) throws InterruptedException {
Logger.sys.error(e.getMessage());
return false;
}
try {
refreshGeneratedFolder(txtUMLProjectName);
} catch (CoreException e) {
Logger.sys.error(e.getMessage());
Dialogs.errorMsgb("txtUML visualization Error",
"Error occured during refreshing generated folder.", e);
}
}
return true;
}


//TODO: doc
/**
* Export a single model's diagrams
* @param layoutDescriptor the model's layout descriptor
* @param monitor for filling the progress bar
* @throws Exception
*/
protected abstract void exportDiagram(TxtUMLLayoutDescriptor layoutDescriptor, IProgressMonitor monitor) throws Exception;

//TODO: doc
protected TxtUMLLayoutDescriptor makeLayoutDescriptor(Map.Entry<Pair<String, String>, List<IType>> layout) throws InterruptedException{
/**
* Returns the layout of a single model
* @param layout the model to export
* @return the layout description
* @throws Exception
*/
protected TxtUMLLayoutDescriptor makeLayoutDescriptor(Map.Entry<Pair<String, String>, List<IType>> layout) throws Exception{
TxtUMLLayoutDescriptor res = null;
final String txtUMLModelName = layout.getKey().getFirst();
final String txtUMLProjectName = layout.getKey().getSecond();
Expand All @@ -118,8 +140,7 @@ protected TxtUMLLayoutDescriptor makeLayoutDescriptor(Map.Entry<Pair<String, Str
try {
res = exporter.exportTxtUMLLayout();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}

List<String> warnings = new LinkedList<String>();
Expand Down Expand Up @@ -151,4 +172,8 @@ protected static String getGeneratedFolderName(){
return PreferencesManager.getString(PreferencesManager.TXTUML_VISUALIZE_DESTINATION_FOLDER);
}

}
protected static void refreshGeneratedFolder(String txtUMLProjectName) throws CoreException{
ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProjectName).getFolder(getGeneratedFolderName()).refreshLocal(IResource.DEPTH_INFINITE, null);
}

}

0 comments on commit b2476fe

Please sign in to comment.