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