diff --git a/src/main/java/edu/rpi/legup/app/GameBoardFacade.java b/src/main/java/edu/rpi/legup/app/GameBoardFacade.java index 67a9a8fa3..1fd9a58a7 100644 --- a/src/main/java/edu/rpi/legup/app/GameBoardFacade.java +++ b/src/main/java/edu/rpi/legup/app/GameBoardFacade.java @@ -89,13 +89,14 @@ public void initializeUI() { public void setPuzzle(Puzzle puzzle) { this.puzzle = puzzle; this.puzzleSolver.setPuzzleView(puzzle); + this.puzzleEditor.setPuzzleView(puzzle); this.history.clear(); } public void setPuzzleEditor(Puzzle puzzle) { this.puzzle = puzzle; this.puzzleEditor.setPuzzleView(puzzle); -// this.history.clear(); + //this.history.clear(); } public void setConfig(Config config) { @@ -184,6 +185,62 @@ public void loadPuzzle(String fileName) throws InvalidFileFormatException { } } + public void loadPuzzleEditor(String fileName) throws InvalidFileFormatException { + try { + loadPuzzleEditor(new FileInputStream(fileName)); + curFileName = fileName; + setWindowTitle(puzzle.getName(), fileName); + } catch (IOException e) { + LOGGER.error("Invalid file " + fileName, e); + throw new InvalidFileFormatException("Could not find file"); + } + } + + public void loadPuzzleEditor(InputStream inputStream) throws InvalidFileFormatException { + Document document; + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + document = builder.parse(inputStream); + } catch (IOException | SAXException | ParserConfigurationException e) { + LOGGER.error("Invalid file", e); + throw new InvalidFileFormatException("Could not find file"); + } + + Element rootNode = document.getDocumentElement(); + if (rootNode.getTagName().equals("Legup")) { + try { + Node node = rootNode.getElementsByTagName("puzzle").item(0); + String qualifiedClassName = config.getPuzzleClassForName(node.getAttributes().getNamedItem("name").getNodeValue()); + if (qualifiedClassName == null) { + throw new InvalidFileFormatException("Puzzle creation error: cannot find puzzle with that name"); + } + LOGGER.debug("Loading " + qualifiedClassName); + + Class c = Class.forName(qualifiedClassName); + Constructor cons = c.getConstructor(); + Puzzle puzzle = (Puzzle) cons.newInstance(); + + PuzzleImporter importer = puzzle.getImporter(); + if (importer == null) { + LOGGER.error("Puzzle importer is null"); + throw new InvalidFileFormatException("Puzzle importer null"); + } + importer.initializePuzzle(node); + puzzle.initializeView(); + puzzle.getBoardView().onTreeElementChanged(puzzle.getTree().getRootNode()); + setPuzzleEditor(puzzle); + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | + IllegalAccessException | InstantiationException e) { + LOGGER.error(e); + throw new InvalidFileFormatException("Puzzle creation error"); + } + } else { + LOGGER.error("Invalid file"); + throw new InvalidFileFormatException("Invalid file: must be a Legup file"); + } + } + /** * Loads a puzzle file from the input stream * diff --git a/src/main/java/edu/rpi/legup/controller/ElementController.java b/src/main/java/edu/rpi/legup/controller/ElementController.java index 80f80db3e..5c47cc46c 100644 --- a/src/main/java/edu/rpi/legup/controller/ElementController.java +++ b/src/main/java/edu/rpi/legup/controller/ElementController.java @@ -72,6 +72,9 @@ public void mouseReleased(MouseEvent e) { TreePanel treePanel = GameBoardFacade.getInstance().getLegupUI().getTreePanel(); TreeView treeView = treePanel.getTreeView(); BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } Board board = boardView.getBoard(); ElementView elementView = boardView.getElement(e.getPoint()); TreeViewSelection selection = treeView.getSelection(); @@ -112,6 +115,12 @@ public void mouseEntered(MouseEvent e) { TreeElement treeElement = boardView.getTreeElement(); DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView(); BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } + if (dynamicView == null) { + dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView(); + } Board board = boardView.getBoard(); ElementView elementView = boardView.getElement(e.getPoint()); ElementSelection selection = boardView.getSelection(); @@ -146,6 +155,12 @@ public void mouseExited(MouseEvent e) { boardView.setFocusable(false); DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView(); BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } + if (dynamicView == null) { + dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView(); + } ElementView element = boardView.getElement(e.getPoint()); if (element != null) { boardView.getSelection().clearHover(); @@ -172,9 +187,15 @@ public void mouseDragged(MouseEvent e) { @Override public void mouseMoved(MouseEvent e) { BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } Board board = boardView.getBoard(); TreeElement treeElement = boardView.getTreeElement(); DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView(); + if (dynamicView == null) { + dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView(); + } ElementView elementView = boardView.getElement(e.getPoint()); ElementSelection selection = boardView.getSelection(); String error = null; @@ -211,6 +232,9 @@ public void changeCell(MouseEvent e, PuzzleElement data) { @Override public void actionPerformed(ActionEvent e) { BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } ElementView selectedElement = boardView.getSelection().getFirstSelection(); PuzzleElement puzzleElement = selectedElement.getPuzzleElement(); @@ -271,6 +295,9 @@ public void keyPressed(KeyEvent e) { public void keyReleased(KeyEvent e) { Puzzle puzzle = GameBoardFacade.getInstance().getPuzzleModule(); BoardView boardView = getInstance().getLegupUI().getBoardView(); + if (boardView == null) { + boardView = getInstance().getLegupUI().getEditorBoardView(); + } Board board = boardView.getBoard(); if (board instanceof CaseBoard) { CaseBoard caseBoard = (CaseBoard) board; diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java index 4b496a251..1609fbf99 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java @@ -4,6 +4,6 @@ public class BlackTile extends PlaceableElement { public BlackTile() { - super("NURI-PLAC-0001", "Black Tile", "The black tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + super("NURI-PLAC-0001", "Black Tile", "The black tile", "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java index abd6ff90e..287d4df82 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java @@ -5,7 +5,7 @@ public class NumberTile extends NonPlaceableElement { private int object_num; public NumberTile( int num ) { - super("NURI-UNPL-0001", "Number Tile", "A numbered tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + super("NURI-UNPL-0001", "Number Tile", "A numbered tile", "edu/rpi/legup/images/nurikabe/tiles/NumberTile.png"); object_num = num; } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java index aeb840263..259e17e74 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java @@ -4,6 +4,6 @@ public class UnknownTile extends NonPlaceableElement { public UnknownTile() { - super("NURI-UNPL-0002", "Unknown Tile", "A blank tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + super("NURI-UNPL-0002", "Unknown Tile", "A blank tile", "edu/rpi/legup/images/nurikabe/tiles/UnknownTile.png"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java index 116575bc2..434e0663e 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java @@ -4,6 +4,6 @@ public class WhiteTile extends PlaceableElement { public WhiteTile() { - super("NURI-PLAC-0002", "White Tile", "The white tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + super("NURI-PLAC-0002", "White Tile", "The white tile", "edu/rpi/legup/images/nurikabe/tiles/WhiteTile.png"); } } diff --git a/src/main/java/edu/rpi/legup/ui/LegupUI.java b/src/main/java/edu/rpi/legup/ui/LegupUI.java index 8bca7f663..dfcb33e22 100644 --- a/src/main/java/edu/rpi/legup/ui/LegupUI.java +++ b/src/main/java/edu/rpi/legup/ui/LegupUI.java @@ -177,10 +177,16 @@ public void windowDeactivated(WindowEvent e) { public BoardView getBoardView() { return getProofEditor().getBoardView(); } + public BoardView getEditorBoardView() { + return getPuzzleEditor().getBoardView(); + } public DynamicView getDynamicBoardView() { return getProofEditor().getDynamicBoardView(); } + public DynamicView getEditorDynamicBoardView() { + return getPuzzleEditor().getDynamicBoardView(); + } public TreePanel getTreePanel() { return getProofEditor().getTreePanel(); diff --git a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java index a3e034447..7febf1dae 100644 --- a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java +++ b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java @@ -88,6 +88,7 @@ public void setMenuBar() { // FILE menus[0] = new JMenu("File"); + // file>new JMenuItem newPuzzle = new JMenuItem("New"); newPuzzle.addActionListener((ActionEvent) -> promptPuzzle()); @@ -205,7 +206,9 @@ public void promptPuzzle() { return; } } - + if (fileDialog == null) { + fileDialog = new FileDialog(this.frame); + } fileDialog.setMode(FileDialog.LOAD); fileDialog.setTitle("Select Puzzle"); fileDialog.setVisible(true); @@ -218,7 +221,7 @@ public void promptPuzzle() { if (puzzleFile != null && puzzleFile.exists()) { try { - GameBoardFacade.getInstance().loadPuzzle(fileName); + GameBoardFacade.getInstance().loadPuzzleEditor(fileName); String puzzleName = GameBoardFacade.getInstance().getPuzzleModule().getName(); frame.setTitle(puzzleName + " - " + puzzleFile.getName()); } catch (InvalidFileFormatException e) { @@ -251,6 +254,10 @@ public void onClearHistory() { } + public BoardView getBoardView() { + return boardView; + } + public JButton[] getToolBarButtons() { return toolBarButtons; } @@ -274,12 +281,15 @@ public void setPuzzleView(Puzzle puzzle) { dynamicBoardView.setBorder(titleBoard); puzzle.addBoardListener(puzzle.getBoardView()); - - elementFrame.getNonPlaceableElementPanel().setElements(puzzle.getNonPlaceableElements()); - elementFrame.getPlaceableElementPanel().setElements(puzzle.getPlaceableElements()); + System.out.println("Setting elements"); + elementFrame.setElements(puzzle); toolBarButtons[ToolbarName.CHECK.ordinal()].setEnabled(true); // toolBarButtons[ToolbarName.SAVE.ordinal()].setEnabled(true); } + public DynamicView getDynamicBoardView() { + return dynamicBoardView; + } + } diff --git a/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementPanel.java b/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementPanel.java index 4c2bebd54..4dc822341 100644 --- a/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementPanel.java +++ b/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementPanel.java @@ -27,11 +27,12 @@ public void setElements(List elements) { clearButtons(); elementButtons = new ElementButton[elements.size()]; - + System.out.println("adding " + elements.size() + " elements to panel"); for (int i = 0; i < elements.size(); i++) { Element element = elements.get(i); elementButtons[i] = new ElementButton(element); elementFrame.getButtonGroup().add(elementButtons[i]); + System.out.printf("added button: %d, element %s\n", i, element.getElementName()); elementButtons[i].setToolTipText(element.getElementName() + ": " + element.getDescription()); elementButtons[i].addActionListener(elementFrame.getController());