Bladeren bron

Some warnings for load and pasting, also new Optionpane for choosing own extension for save file

Teh-Hai Julian Zheng 8 jaren geleden
bovenliggende
commit
663b54631c

+ 0 - 15
src/exceptions/CorruptSaveFileException.java

@@ -1,15 +0,0 @@
-package exceptions;
-
-public class CorruptSaveFileException extends Exception{
-
-	private static final long serialVersionUID = 1L;
-	
-	public CorruptSaveFileException() {
-		super();
-	}
-	
-	public CorruptSaveFileException(String s) {
-		super(s);
-	}
-	
-}

+ 12 - 15
src/ui/controller/ClipboardController.java

@@ -136,7 +136,8 @@ public class ClipboardController {
 	 * 
 	 * @param upperNode
 	 */
-	public void paste(CpsUpperNode upperNode, Point p) {
+	public void paste(CpsUpperNode upperNode, Point p)
+			throws UnsupportedFlavorException, IOException, JsonParseException {
 
 		if (p == null)
 			return;
@@ -145,17 +146,13 @@ public class ClipboardController {
 		Transferable content = clipboard.getContents(null);
 
 		if (content != null && content.isDataFlavorSupported(DataFlavor.stringFlavor)
-				&& !content.isDataFlavorSupported(DataFlavor.allHtmlFlavor))
-			try {
-				String str = (String) content.getTransferData(DataFlavor.stringFlavor);
-
-				if (parser.parse(str).isJsonObject())
-					json = (JsonObject) parser.parse(str);
-			} catch (UnsupportedFlavorException | IOException | JsonParseException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
-		else
+				&& !content.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
+
+			String str = (String) content.getTransferData(DataFlavor.stringFlavor);
+
+			if (parser.parse(str).isJsonObject())
+				json = (JsonObject) parser.parse(str);
+		} else
 			return;
 
 		List<String> keys = load.getKeys(json);
@@ -165,7 +162,7 @@ public class ClipboardController {
 		HashMap<Integer, AbstractCpsObject> objDispatch = new HashMap<>();
 		HashMap<Integer, HolonElement> eleDispatch = new HashMap<>();
 		model.getSelectedCpsObjects().clear();
-		
+
 		objIDMap = new HashMap<>();
 		eleIDMap = new HashMap<>();
 		sav = json.get("SAV").getAsString();
@@ -174,7 +171,7 @@ public class ClipboardController {
 		point = new Point(old.x - p.x, old.y - p.y);
 
 		forwardObjects(keys, json, objDispatch, eleDispatch, upperNode);
-		//for selecting Cps
+		// for selecting Cps
 		getObjectsInDepth();
 		forwardEdges(edges, json, objDispatch, upperNode);
 
@@ -248,7 +245,7 @@ public class ClipboardController {
 				cvsC.addObject(temp);
 			else
 				uppC.addObjectInUpperNode(temp, upperNode);
-			//mark the Pasted Objects
+			// mark the Pasted Objects
 			model.getSelectedCpsObjects().add(temp);
 		} else {
 			// else look up the table and put it into the right Uppernode

+ 6 - 2
src/ui/controller/Control.java

@@ -2,12 +2,16 @@ package ui.controller;
 
 import java.awt.Color;
 import java.awt.Point;
+import java.awt.datatransfer.UnsupportedFlavorException;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.zip.ZipException;
 
 import org.apache.commons.compress.archivers.ArchiveException;
 
+import com.google.gson.JsonParseException;
+
 import classes.AbstractCpsObject;
 import classes.Category;
 import classes.CpsEdge;
@@ -478,7 +482,7 @@ public class Control {
 	 *             exception
 	 * @throws ArchiveException 
 	 */
-	public void loadFile(String path) throws IOException, ArchiveException {
+	public void loadFile(String path) throws IOException, ArchiveException, ZipException {
 		loadController.readSave(path);
 	}
 
@@ -843,7 +847,7 @@ public class Control {
 		clipboardController.copy(upperNode);
 	}
 	
-	public void paste(CpsUpperNode upperNode, Point point) {
+	public void paste(CpsUpperNode upperNode, Point point) throws JsonParseException, UnsupportedFlavorException, IOException {
 		clipboardController.paste(upperNode, point);
 	}
 	

+ 2 - 1
src/ui/controller/LoadController.java

@@ -13,6 +13,7 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
+import java.util.zip.ZipException;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveException;
@@ -102,7 +103,7 @@ public class LoadController {
 	 * @throws IOException
 	 *             exception
 	 */
-	public void readSave(String path) throws IOException, ArchiveException {
+	public void readSave(String path) throws IOException, ArchiveException, ZipException {
 
 		if (path.contains("json")) {
 			readJson(path);

+ 45 - 22
src/ui/view/GUI.java

@@ -5,9 +5,13 @@ import java.awt.Color;
 import java.awt.Component;
 import java.awt.Cursor;
 import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.HeadlessException;
 import java.awt.Image;
+import java.awt.Label;
 import java.awt.Point;
 import java.awt.Toolkit;
+import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
@@ -22,6 +26,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.zip.ZipException;
 
 import javax.swing.AbstractAction;
 import javax.swing.ActionMap;
@@ -65,6 +70,8 @@ import javax.swing.tree.TreeCellRenderer;
 
 import org.apache.commons.compress.archivers.ArchiveException;
 
+import com.google.gson.JsonParseException;
+
 import classes.AbstractCpsObject;
 import classes.Category;
 import classes.CpsUpperNode;
@@ -122,7 +129,7 @@ public class GUI<E> implements CategoryListener {
 	private JMenuItem editItem = new JMenuItem("Edit Object");
 	private String catOfObjToBeEdited;
 	private final JScrollPane statTab = new JScrollPane();
-	
+
 	private FlexiblePane flexPane;
 	private StatPanel2 statSplitPane;
 	private JScrollPane statScrollPane;
@@ -283,7 +290,7 @@ public class GUI<E> implements CategoryListener {
 		statScrollPane = new JScrollPane(statSplitPane);
 		this.canvas = new MyCanvas(model, control);
 		this.holonCanvas = new HolonCanvas(model, control);
-		//this.unitGraph = new UnitGraph(model, control);
+		// this.unitGraph = new UnitGraph(model, control);
 		control.initListener(this);
 		controller.setCanvas(canvas);
 		model.setConsole(console);
@@ -519,15 +526,22 @@ public class GUI<E> implements CategoryListener {
 
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				if (((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
-						.getComponent(0) instanceof UpperNodeCanvas)
-					controller.paste(
-							((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
-									.getComponent(0)).upperNode,
-							((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
-									.getComponent(0)).getMousePosition());
-				else
-					controller.paste(null, canvas.getMousePosition());
+				try {
+					if (((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+							.getComponent(0) instanceof UpperNodeCanvas)
+						
+						controller.paste(
+								((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+										.getComponent(0)).upperNode,
+								((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+										.getComponent(0)).getMousePosition());
+					else
+						controller.paste(null, canvas.getMousePosition());
+				} catch (HeadlessException | JsonParseException | UnsupportedFlavorException | IOException e1) {
+					// TODO Auto-generated catch block
+					JLabel message = new JLabel("The Clipboard information can't be pasted into Application.");
+					JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
+				}
 				canvas.repaint();
 			}
 		});
@@ -1498,10 +1512,8 @@ public class GUI<E> implements CategoryListener {
 			private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
 				JFileChooser fileChooser = new JFileChooser();
 				JFrame test = new JFrame();
-				FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(.holon)", "holon");
-				FileNameExtensionFilter ownFilter = new FileNameExtensionFilter("Own Extension(.ext)", " ");
+				FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
 				fileChooser.addChoosableFileFilter(holonFilter);
-				fileChooser.addChoosableFileFilter(ownFilter);
 				fileChooser.setFileFilter(holonFilter);
 
 				if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
@@ -1526,11 +1538,10 @@ public class GUI<E> implements CategoryListener {
 						}
 						unitGraph.empty();
 						tree.repaint();
-					} catch (IOException e) {
+					} catch (IOException | ArchiveException e) {
 						// TODO Auto-generated catch block
-						e.printStackTrace();
-					} catch (ArchiveException e) {
-						e.printStackTrace();
+						JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
+						JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
 					}
 				}
 			}
@@ -1547,18 +1558,30 @@ public class GUI<E> implements CategoryListener {
 			private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
 				JFileChooser fileChooser = new JFileChooser();
 				JFrame test = new JFrame();
-				FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(.holon)", "holon");
-				FileNameExtensionFilter ownFilter = new FileNameExtensionFilter("Own Extension(.ext)", " ");
+				FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
 				fileChooser.addChoosableFileFilter(holonFilter);
-				fileChooser.addChoosableFileFilter(ownFilter);
 				fileChooser.setFileFilter(holonFilter);
 
 				if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
 					String file = fileChooser.getSelectedFile().getPath();
 					if (fileChooser.getFileFilter().equals(holonFilter)) {
-						if (!file.endsWith(".holon"))
+						if (!file.contains("."))
 							file += ".holon";
+						if (!file.endsWith(".holon")) {
+							String suffix = file.substring(file.lastIndexOf("."), file.length());
+							String[] options = new String[] { "keep .holon", "use " + suffix };
+
+							JLabel message = new JLabel(
+									"Are you sure to use the extension \"" + suffix + "\" instead of \".holon\"?");
+
+							int response = JOptionPane.showOptionDialog(null, message, "", JOptionPane.DEFAULT_OPTION,
+									JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
+
+							if (response == 0)
+								file = file.replace(suffix, ".holon");
+						}
 					}
+
 					try {
 						controller.saveFile(new File(file).getAbsolutePath());
 					} catch (IOException | ArchiveException e) {

+ 12 - 1
src/ui/view/MyCanvas.java

@@ -7,6 +7,7 @@ import java.awt.Graphics2D;
 import java.awt.Image;
 import java.awt.Point;
 import java.awt.RenderingHints;
+import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
@@ -20,12 +21,16 @@ import java.util.Timer;
 import java.util.TimerTask;
 
 import javax.swing.ImageIcon;
+import javax.swing.JLabel;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
 
+import com.google.gson.JsonParseException;
+
 import classes.CpsEdge;
 import classes.CpsNode;
 import classes.CpsUpperNode;
@@ -373,7 +378,13 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		itemPaste.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				controller.paste(null, mousePosition);
+				try {
+					controller.paste(null, mousePosition);
+				} catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
+					// TODO Auto-generated catch block
+					JLabel message = new JLabel("The Clipboard information can't be pastet into Application.");
+					JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
+				}
 				repaint();
 			}
 		});

+ 16 - 6
src/ui/view/UpperNodeCanvas.java

@@ -7,6 +7,7 @@ import java.awt.Graphics2D;
 import java.awt.Image;
 import java.awt.Point;
 import java.awt.RenderingHints;
+import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
@@ -22,10 +23,14 @@ import java.util.TimerTask;
 import javax.swing.ImageIcon;
 import javax.swing.JLabel;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
+
+import com.google.gson.JsonParseException;
+
 import classes.CpsEdge;
 import classes.CpsNode;
 import classes.CpsUpperNode;
@@ -113,8 +118,6 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 	private Position unPos;
 	private ArrayList<Position> savePos;
 
-	
-
 	/**
 	 * Constructor.
 	 * 
@@ -389,7 +392,13 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		itemPaste.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				controller.paste(upperNode, mousePosition);
+				try {
+					controller.paste(upperNode, mousePosition);
+				} catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
+					// TODO Auto-generated catch block
+					JLabel message = new JLabel("The Clipboard information can't be pastet into Application.");
+					JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
+				}
 				repaint();
 			}
 		});
@@ -484,7 +493,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 					if (con.getState()) {
 						g2.setColor(Color.GREEN);
 						if (con.getCapacity() != -1) {
-							g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3)+1), 4)));
+							g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
 						}
 					} else {
 						g2.setColor(Color.RED);
@@ -529,7 +538,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 			g2.setColor(Color.BLUE);
 			for (CpsEdge con : upperNode.getNodeEdges()) {
 				if (con.getFlow() <= con.getCapacity()) {
-					g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 3)+1, 4)));
+					g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 3) + 1, 4)));
 				} else {
 					g2.setStroke(new BasicStroke(2));
 				}
@@ -570,7 +579,8 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		} else if (edgeHighlight != null) {
 			g2.setColor(Color.BLUE);
 			if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
-				g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3)+1, 4)));
+				g2.setStroke(
+						new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1, 4)));
 			} else {
 				g2.setStroke(new BasicStroke(2));
 			}