package de.uni.freiburg.iig.telematik.swat.logs;

import de.invation.code.toval.file.FileUtils;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import de.invation.code.toval.properties.PropertyException;
import de.invation.code.toval.validate.ParameterException;
import de.uni.freiburg.iig.telematik.swat.icons.IconFactory;
import de.uni.freiburg.iig.telematik.swat.workbench.Workbench;
import de.uni.freiburg.iig.telematik.swat.workbench.action.SciffAnalyzeAction;
import de.uni.freiburg.iig.telematik.wolfgang.editor.component.ViewComponent;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * presents view on mxml files. extends {@link JEditorPane}, implements
 * {@link ViewComponent}
 **/
public class LogFileViewer extends JScrollPane implements ViewComponent {
	private static final long serialVersionUID = 7051631037013916120L;
	private JComponent properties = null;
	private JButton analyzeButton = null;

	private LogModel model;

	public LogFileViewer(LogModel model) throws Exception {
		setModel(model);
	}
	
	public LogModel getModel() {
		return model;
	}

	public void setModel(LogModel model) {
		this.model = model;
	}

	@Override
	public JComponent getMainComponent() {
		try {
			JPanel pane = new JPanel();
			pane.setLayout(new GridLayout());
			JEditorPane editor = new JEditorPane(model.getFileReference().toURI().toURL());
			editor.setEditable(false);
			getViewport().add(editor);
		} catch (MalformedURLException e) {
			Workbench.errorMessage("Could not generate Log viewer, URL malformed", e, true);
		} catch (IOException e) {
			Workbench.errorMessage("Could not generate Log viewer, I/O Error", e, true);
		}
		return this;
	}

	public JButton getSciffButton() {
		if (analyzeButton == null) {
			analyzeButton = new JButton("Analyze with SCIFF");
			ImageIcon icon;
			try {
				icon = IconFactory.getIcon("search");
				analyzeButton.setIcon(icon);
			} catch (ParameterException e) {
			} catch (PropertyException e) {
			} catch (IOException e) {
			}

			analyzeButton.addActionListener(new SciffAnalyzeAction(model.getFileReference()));
		}
		return analyzeButton;
	}

	@Override
	public JComponent getPropertiesView() {
		if (properties == null) {
                    try {
                        createPropertiesView();
                    } catch (IOException ex) {
                        Workbench.errorMessage("Could not get properties", ex, true);
                    }
		}
		return properties;
	}

	public String getName() {
		//Wegen Nimbus LookaAndFeel
		if (model == null)
			return "null";
		return model.getName();
	}

	public String toString() {
		return model.getName();
	}

	public File getFile() {
		return model.getFileReference();
	}

	private void createPropertiesView() throws IOException {
		properties = new JPanel();
		properties.setLayout(new FlowLayout());
		properties.add(new JLabel("Textual file"));
		properties.add(new JLabel("Size: " + model.getFileReference().length() / 1024 + "kB"));
		properties.add(new JLabel("Lines: "+ FileUtils.getLineCount(model.getFileReference().getAbsolutePath(), Charset.defaultCharset().toString())));
		properties.add(getSciffButton());
		properties.validate();
		properties.repaint();
	}

	public int hashCode() {
		return model.hashCode();
	}

}

@@ -88,16 +88,44 @@
 leUtils;
+%0A%0Aimport java.awt.Component;
 %0Aimport 
@@ -1579,16 +1579,18 @@
 %09%09try %7B%0A
+//
 %09%09%09JPane
@@ -1612,16 +1612,18 @@
 anel();%0A
+//
 %09%09%09pane.
@@ -1658,117 +1658,8 @@
 %0A%09%09%09
-JEditorPane editor = new JEditorPane(model.getFileReference().toURI().toURL());%0A%09%09%09editor.setEditable(false);
 %0A%09%09%09
@@ -1676,22 +1676,32 @@
 t().add(
-e
+getE
 ditor
+Field()
 );%0A%09%09%7D c
@@ -1925,24 +1925,24 @@
  true);%0A%09%09%7D%0A
-
 %09%09return thi
@@ -1942,24 +1942,465 @@
 urn this;%0A%09%7D
+%0A%09%0A%0A%09private Component getEditorField() throws MalformedURLException, IOException %7B%0A%09%09if(model.getFileReference().length()%3E2097152l)%7B%0A%09%09%09//do not show editor%0A%09%09%09JLabel label=new JLabel(%22file too big to dislpay - analysis is possible but might run into performance problems%22);%0A%09%09%09return label;%0A%09%09%7D else %7B%0A%09%09%09JEditorPane editor = new JEditorPane(model.getFileReference().toURI().toURL());%0A%09%09%09editor.setEditable(false);%0A%09%09%09return editor;%0A%09%09%7D%0A%09%7D
 %0A%0A%09public JB

Support for big log-files