| 1 | package de.uka.ipd.sdq.reliability.solver.visualisation; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.FileNotFoundException; |
| 5 | import java.io.FileReader; |
| 6 | import java.io.IOException; |
| 7 | |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.swt.SWT; |
| 10 | import org.eclipse.swt.browser.Browser; |
| 11 | import org.eclipse.swt.widgets.Composite; |
| 12 | import org.eclipse.ui.IEditorInput; |
| 13 | import org.eclipse.ui.IEditorSite; |
| 14 | import org.eclipse.ui.PartInitException; |
| 15 | import org.eclipse.ui.part.EditorPart; |
| 16 | import org.eclipse.ui.part.FileEditorInput; |
| 17 | |
| 18 | public class MarkovResultEditor extends EditorPart { |
| 19 | |
| 20 | @Override |
| 21 | public void createPartControl(Composite parent) { |
| 22 | IEditorInput input = getEditorInput(); |
| 23 | Browser browser = new Browser(parent, SWT.BORDER); |
| 24 | browser.setJavascriptEnabled(true); |
| 25 | if (input != null) { |
| 26 | if (input instanceof MarkovResultEditorInput) { |
| 27 | // display HTML code in browser |
| 28 | browser |
| 29 | .setText(((MarkovResultEditorInput) input) |
| 30 | .getHtmlCode()); |
| 31 | } else { |
| 32 | // see if we can extract HTML code from the given input |
| 33 | String fileContent = getFileContent(input); |
| 34 | if (fileContent.trim().startsWith("<html>")) { |
| 35 | // assume we have an HTML file and try to display it |
| 36 | browser.setText(fileContent); |
| 37 | } else { |
| 38 | browser |
| 39 | .setText("<html><head><title>Markov Results</title></head>" |
| 40 | + "<body><font color=\"red\">Error:" |
| 41 | + " The given editor input could not be handled.</font>" |
| 42 | + "</body></html>"); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void doSave(IProgressMonitor monitor) { |
| 50 | // TODO Auto-generated method stub |
| 51 | |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void doSaveAs() { |
| 56 | // TODO Auto-generated method stub |
| 57 | |
| 58 | } |
| 59 | |
| 60 | private String getFileContent(IEditorInput input) { |
| 61 | StringBuilder fileContent = null; |
| 62 | if (input instanceof FileEditorInput) { |
| 63 | FileEditorInput fileEditorInput = (FileEditorInput) input; |
| 64 | try { |
| 65 | FileReader fileReader = new FileReader(fileEditorInput |
| 66 | .getPath().toOSString()); |
| 67 | BufferedReader in = new BufferedReader(fileReader); |
| 68 | String line = null; |
| 69 | fileContent = new StringBuilder(); |
| 70 | String newLine = System.getProperty("line.separator"); |
| 71 | try { |
| 72 | while ((line = in.readLine()) != null) { |
| 73 | fileContent.append(line); |
| 74 | fileContent.append(newLine); |
| 75 | } |
| 76 | } catch (IOException e) { |
| 77 | e.printStackTrace(); |
| 78 | } |
| 79 | } catch (FileNotFoundException e) { |
| 80 | e.printStackTrace(); |
| 81 | } |
| 82 | } |
| 83 | return fileContent.toString(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void init(IEditorSite site, IEditorInput input) |
| 88 | throws PartInitException { |
| 89 | setSite(site); |
| 90 | setInput(input); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean isDirty() { |
| 95 | // TODO Auto-generated method stub |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean isSaveAsAllowed() { |
| 101 | // TODO Auto-generated method stub |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setFocus() { |
| 107 | // TODO Auto-generated method stub |
| 108 | |
| 109 | } |
| 110 | |
| 111 | } |