package de.uni.freiburg.iig.telematik.wolfgang.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import de.invation.code.toval.properties.PropertyException;
import de.uni.freiburg.iig.telematik.sepia.graphic.AbstractGraphicalPN;
import de.uni.freiburg.iig.telematik.sepia.graphic.GraphicalCPN;
import de.uni.freiburg.iig.telematik.sepia.graphic.GraphicalPTNet;
import de.uni.freiburg.iig.telematik.sepia.parser.pnml.PNMLParser;
import de.uni.freiburg.iig.telematik.wolfgang.editor.AbstractWolfgang;
import de.uni.freiburg.iig.telematik.wolfgang.editor.WolfgangCPN;
import de.uni.freiburg.iig.telematik.wolfgang.editor.WolfgangPT;
import de.uni.freiburg.iig.telematik.wolfgang.editor.properties.WolfgangProperties;
import de.uni.freiburg.iig.telematik.wolfgang.icons.IconFactory;

public class LoadAction extends AbstractWolfgangAction {

	private static final long serialVersionUID = 7716993627349722001L;

	protected boolean success = false;
	protected String errorMessage = null;

	@SuppressWarnings("rawtypes")
	public LoadAction(AbstractWolfgang wolfgang) throws PropertyException, IOException {
		super(wolfgang, "Net PT Net", IconFactory.getIcon("save"));
	}

	@Override
	protected void doFancyStuff(ActionEvent e) throws Exception {
		if (wolfgang.getEditorComponent() == null)
			return;
		success = true;
		JFileChooser fc;

		fc = new JFileChooser(System.getProperty("user.home"));

		fc.addChoosableFileFilter(new FileFilter() {
			public String getDescription() {
				return "PNML Documents (*.pnml)";
			}

			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				} else {
					return f.getName().toLowerCase().endsWith(".pnml");
				}
			}
		});
		fc.setDialogTitle("Load PNML");
		int returnVal = fc.showDialog(wolfgang.getEditorComponent().getGraphComponent(), "load PNML");

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String filename = fc.getSelectedFile().getAbsolutePath();
			if (!filename.toLowerCase().endsWith(".pnml"))
				filename += ".pnml";
			@SuppressWarnings("rawtypes")
			AbstractGraphicalPN net = new PNMLParser().parse(filename, WolfgangProperties.getInstance().getRequestNetType(), WolfgangProperties.getInstance().getPNValidation());
			switch(net.getPetriNet().getNetType()){
			case CPN:
				wolfgang = new WolfgangCPN((GraphicalCPN) net);
				break;
			case PTNet:
				wolfgang = new WolfgangPT((GraphicalPTNet) net);
				break;
			default: 
				throw new Exception("Incompatible net type: " + net.getPetriNet().getNetType());
			}
			wolfgang.setUpGUI();
		}

	}
}

@@ -106,16 +106,54 @@
 o.File;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -172,16 +172,16 @@
 eption;%0A
-
 %0Aimport 
@@ -1276,14 +1276,12 @@
 g, %22
-Net PT
+Open
  Net
@@ -1543,17 +1543,68 @@
 ome%22));%0A
+%09%09fc.removeChoosableFileFilter(fc.getFileFilter());
 %0A
-
 %09%09fc.add
@@ -2136,16 +2136,16 @@
 ();%0A
+%0A
 %09%09%09if (
-!
 file
@@ -2185,67 +2185,12 @@
 l%22))
+ %7B
 %0A%09
-%09%09%09filename += %22.pnml%22;%0A%09%09%09@SuppressWarnings(%22rawtypes%22)%0A
 %09%09%09A
@@ -2361,14 +2361,16 @@
 %0A%09%09%09
+%09
 switch
+ 
 (net
@@ -2397,21 +2397,23 @@
 tType())
+ 
 %7B%0A%09%09%09
+%09
 case CPN
@@ -2414,16 +2414,17 @@
 se CPN:%0A
+%09
 %09%09%09%09wolf
@@ -2471,23 +2471,25 @@
 t);%0A%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09case 
@@ -2495,24 +2495,25 @@
  PTNet:%0A%09%09%09%09
+%09
 wolfgang = n
@@ -2553,23 +2553,25 @@
 t);%0A%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09defau
@@ -2577,14 +2577,14 @@
 ult:
- 
 %0A%09%09%09%09
+%09
 thro
@@ -2663,18 +2663,20 @@
 ());%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09wolfg
@@ -2691,16 +2691,82 @@
 pGUI();%0A
+%09%09%09%7D else%0A%09%09%09%09throw new Exception(%22File is not in .pnml format%22);%0A
 %09%09%7D%0A%0A%09%7D%0A

Fix F08