diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dd29b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ \ No newline at end of file diff --git a/bin/gui/CustomIcon.class b/bin/gui/CustomIcon.class deleted file mode 100644 index acb0a5d..0000000 Binary files a/bin/gui/CustomIcon.class and /dev/null differ diff --git a/bin/gui/DrawboxEditor.class b/bin/gui/DrawboxEditor.class deleted file mode 100644 index 633801c..0000000 Binary files a/bin/gui/DrawboxEditor.class and /dev/null differ diff --git a/bin/gui/Editable.class b/bin/gui/Editable.class deleted file mode 100644 index 8a062bf..0000000 Binary files a/bin/gui/Editable.class and /dev/null differ diff --git a/bin/gui/HitboxCircleEditor.class b/bin/gui/HitboxCircleEditor.class deleted file mode 100644 index ea0cbeb..0000000 Binary files a/bin/gui/HitboxCircleEditor.class and /dev/null differ diff --git a/bin/gui/HitboxPoligonEditor.class b/bin/gui/HitboxPoligonEditor.class deleted file mode 100644 index 4596ec9..0000000 Binary files a/bin/gui/HitboxPoligonEditor.class and /dev/null differ diff --git a/bin/gui/HitboxRectangleEditor.class b/bin/gui/HitboxRectangleEditor.class deleted file mode 100644 index 0d36f0a..0000000 Binary files a/bin/gui/HitboxRectangleEditor.class and /dev/null differ diff --git a/bin/gui/ListGUI$ListEntityRenderer.class b/bin/gui/ListGUI$ListEntityRenderer.class deleted file mode 100644 index 668643a..0000000 Binary files a/bin/gui/ListGUI$ListEntityRenderer.class and /dev/null differ diff --git a/bin/gui/ListGUI.class b/bin/gui/ListGUI.class deleted file mode 100644 index 9cf779a..0000000 Binary files a/bin/gui/ListGUI.class and /dev/null differ diff --git a/bin/gui/MainGUI.class b/bin/gui/MainGUI.class deleted file mode 100644 index 2f3596c..0000000 Binary files a/bin/gui/MainGUI.class and /dev/null differ diff --git a/bin/gui/PropertyFrameAddElement.class b/bin/gui/PropertyFrameAddElement.class deleted file mode 100644 index b690d0d..0000000 Binary files a/bin/gui/PropertyFrameAddElement.class and /dev/null differ diff --git a/bin/launch/Launcher.class b/bin/launch/Launcher.class deleted file mode 100644 index 212fc9c..0000000 Binary files a/bin/launch/Launcher.class and /dev/null differ diff --git a/bin/listeners/AddListElementEntityListener.class b/bin/listeners/AddListElementEntityListener.class deleted file mode 100644 index 244ea73..0000000 Binary files a/bin/listeners/AddListElementEntityListener.class and /dev/null differ diff --git a/bin/listeners/CopyImageToFile.class b/bin/listeners/CopyImageToFile.class deleted file mode 100644 index c81e148..0000000 Binary files a/bin/listeners/CopyImageToFile.class and /dev/null differ diff --git a/bin/listeners/CreateFrameAddElementListener.class b/bin/listeners/CreateFrameAddElementListener.class deleted file mode 100644 index c79016e..0000000 Binary files a/bin/listeners/CreateFrameAddElementListener.class and /dev/null differ diff --git a/bin/listeners/OpenXMLFileButtonListener.class b/bin/listeners/OpenXMLFileButtonListener.class deleted file mode 100644 index 6dacc1a..0000000 Binary files a/bin/listeners/OpenXMLFileButtonListener.class and /dev/null differ diff --git a/bin/listeners/RemoveListElementEntityListener.class b/bin/listeners/RemoveListElementEntityListener.class deleted file mode 100644 index 93b2bcf..0000000 Binary files a/bin/listeners/RemoveListElementEntityListener.class and /dev/null differ diff --git a/bin/model/Drawbox.class b/bin/model/Drawbox.class deleted file mode 100644 index cae7a81..0000000 Binary files a/bin/model/Drawbox.class and /dev/null differ diff --git a/bin/model/Entity.class b/bin/model/Entity.class deleted file mode 100644 index c4a0f23..0000000 Binary files a/bin/model/Entity.class and /dev/null differ diff --git a/bin/model/Formatter.class b/bin/model/Formatter.class deleted file mode 100644 index f5b57d8..0000000 Binary files a/bin/model/Formatter.class and /dev/null differ diff --git a/bin/model/FormatterCircle.class b/bin/model/FormatterCircle.class deleted file mode 100644 index fc01d5e..0000000 Binary files a/bin/model/FormatterCircle.class and /dev/null differ diff --git a/bin/model/FormatterRectangle.class b/bin/model/FormatterRectangle.class deleted file mode 100644 index fe598b0..0000000 Binary files a/bin/model/FormatterRectangle.class and /dev/null differ diff --git a/bin/model/Hitbox.class b/bin/model/Hitbox.class deleted file mode 100644 index ed8283f..0000000 Binary files a/bin/model/Hitbox.class and /dev/null differ diff --git a/bin/model/Point.class b/bin/model/Point.class deleted file mode 100644 index 6df5717..0000000 Binary files a/bin/model/Point.class and /dev/null differ diff --git a/bin/repository/Project.class b/bin/repository/Project.class deleted file mode 100644 index 2109a97..0000000 Binary files a/bin/repository/Project.class and /dev/null differ diff --git a/src/exception/DuplicateEntryException.java b/src/exception/DuplicateEntryException.java new file mode 100644 index 0000000..e3925ae --- /dev/null +++ b/src/exception/DuplicateEntryException.java @@ -0,0 +1,13 @@ +package exception; + +public class DuplicateEntryException extends EntityListIntegrityException { + + public DuplicateEntryException() { + super(); + } + + public DuplicateEntryException(String message) { + super(message); + } + +} diff --git a/src/exception/EntityListIntegrityException.java b/src/exception/EntityListIntegrityException.java new file mode 100644 index 0000000..08fe501 --- /dev/null +++ b/src/exception/EntityListIntegrityException.java @@ -0,0 +1,12 @@ +package exception; + +public class EntityListIntegrityException extends Exception { + + public EntityListIntegrityException() { + super(); + } + + public EntityListIntegrityException(String message) { + super(message); + } +} diff --git a/src/gui/ListGUI.java b/src/gui/ListGUI.java index 0ae529c..c703bb5 100644 --- a/src/gui/ListGUI.java +++ b/src/gui/ListGUI.java @@ -29,6 +29,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; +import exception.DuplicateEntryException; import listeners.CreateFrameAddElementListener; import listeners.RemoveListElementEntityListener; import model.Drawbox; @@ -90,7 +91,7 @@ public class ListGUI extends JPanel { return (String) list.getSelectedValue(); } - public void addListElement(String name,String solid) { + public void addListElement(String name,String solid) throws DuplicateEntryException { // плейсхолдеры для новых хитбоксов и дроубоксов, иначе всё валится с NPE List hitboxPoints = new LinkedList(); List drawboxPoints = new LinkedList(); @@ -101,7 +102,7 @@ public class ListGUI extends JPanel { Entity e = new Entity(name, hitbox, drawbox); e.setType(solid); logger.finer("Entity \""+name+"\" was created."); - Project.getInstance().addEntity(e); + Project.getInstance().addEntity(e); updateList(); } diff --git a/src/listeners/AddListElementEntityListener.java b/src/listeners/AddListElementEntityListener.java index a37ca4c..d4634fa 100644 --- a/src/listeners/AddListElementEntityListener.java +++ b/src/listeners/AddListElementEntityListener.java @@ -10,6 +10,7 @@ import javax.swing.JTextField; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; +import exception.DuplicateEntryException; import gui.ListGUI; import gui.PropertyFrameAddElement; @@ -24,7 +25,11 @@ public class AddListElementEntityListener implements ActionListener @Override public void actionPerformed(ActionEvent e) { - listGUI.addListElement(propertyFrame.getInputName(),propertyFrame.getInputType()); - propertyFrame.dispose(); + try { + listGUI.addListElement(propertyFrame.getInputName(),propertyFrame.getInputType()); + propertyFrame.dispose(); + } catch (DuplicateEntryException e1) { + JOptionPane.showMessageDialog(propertyFrame, e1.getMessage(), "Cannot create an entity.", JOptionPane.ERROR_MESSAGE); + } } } diff --git a/src/repository/Project.java b/src/repository/Project.java index ec120d9..a025478 100644 --- a/src/repository/Project.java +++ b/src/repository/Project.java @@ -14,6 +14,7 @@ import javax.imageio.ImageIO; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; @@ -27,6 +28,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import exception.DuplicateEntryException; import model.Entity; @@ -198,29 +200,49 @@ public class Project implements Iterable { } return null; } - /* - * Добавляется обьект как в список всех сущностей, так и в xml-метаданные дерева(element objecttype) - * Это лишь добавление в метаданные, а не парсинг в файл! не путать! - * Для парсинга будет сделана отдельная функция! + /** + * Adds a new Entity to in-editor list of all entities and it's in-memory XML-representiation.
+ * Does not write anything to actual XML-file, use {@link #writeXML()} for it + * @throws DuplicateEntryException - thrown if an entity with such a name already exists + * @params e - entity to add */ - public void addEntity(Entity e) { - listEntity.add(e); - Element objecttype = document.createElement("objecttype"); - objecttype.setAttribute("name", e.getName()); - objecttype.setAttribute("color", "000000");//color of entity, needed by Tiled editor - document.getElementsByTagName("objecttypes").item(0).appendChild(objecttype); - /* - * TODO: set "class" property too (code below) - Element property = document.createElement("property"); - objecttype.appendChild(property); - document.appendChild(objecttype); - property.setAttribute("name", "class"); - property.setAttribute("type", "string"); - property.setAttribute("default", e.getType()); + public void addEntity(Entity e) throws DuplicateEntryException { - * creating hitbox or drowbox attributes in advance is insufficient - * they will be added while saving - */ + if(getEntityByName(e.getName()) != null) + throw new DuplicateEntryException("The entity with the name '" + e.getName() + "' already exists!"); + + listEntity.add(e); + + Element objecttypeElement = document.createElement("objecttype"); + document.getElementsByTagName("objecttypes")// get document root element named "objecttypes" + .item(0) + .appendChild(objecttypeElement); // add new "objecttype" element as a child + + objecttypeElement.setAttribute("name", e.getName()); + objecttypeElement.setAttribute("color", "000000");//color of entity, needed by Tiled editor + + // format: + Element classProperty = document.createElement("property"); + classProperty.setAttribute("name", "class"); + classProperty.setAttribute("type", "string"); + classProperty.setAttribute("default", e.getType()); + objecttypeElement.appendChild(classProperty); + + // format: + Element drawboxProperty = document.createElement("property"); + drawboxProperty.setAttribute("name", "drawbox"); + drawboxProperty.setAttribute("type", "string"); + drawboxProperty.setAttribute("default", ""); // empty, because on creation there is no drawbox yet + objecttypeElement.appendChild(drawboxProperty); + + // format: + Element hitboxProperty = document.createElement("property"); + hitboxProperty.setAttribute("name", "hitbox"); + hitboxProperty.setAttribute("type", "string"); + hitboxProperty.setAttribute("default", ""); // empty, no hitbox yet + objecttypeElement.appendChild(hitboxProperty); + + //printXMlToConsole(); //DEBUG! } /* @@ -243,7 +265,7 @@ public class Project implements Iterable { /** * Возвращает объект сущности с заданным именем, или вбрасывает исключение, если такой сущности не существует. * */ - public Entity getEntityByName(String name) throws Exception { + public Entity getEntityByName(String name) { //debug print //System.out.println("----- session started ------"); for(Entity e: listEntity) { @@ -252,45 +274,18 @@ public class Project implements Iterable { return e; } - // можно было бы и просто null возвращать, но обращение за несуществующей сущностью само по себе нехороший прецедент - // так что исключение призвано обратить внимание пользователя, если такое действительно случится - throw new Exception("No entity with such name!"); + return null; } - public void save() { - /* - * thanks to addEntity() and removeEntity() our Document and listEntity - * are interchangeable - they content exactly the same set of objects. - * So we'll iterate over Document instead listEntity. It's more convient - * in this case. - * */ - NodeList objecttypes = document.getElementsByTagName("objecttype"); - for(int i = 0; i < objecttypes.getLength(); i++) { - Node objecttype = objecttypes.item(i); - NodeList propertiesList = objecttype.getChildNodes(); - for(int j = 0; j < objecttypes.getLength(); j++) { - Element property = (Element) propertiesList.item(j); - NamedNodeMap attributes = property.getAttributes(); - String name = attributes.getNamedItem("name").getNodeValue(); - Entity entity = null; - try { - entity = getEntityByName(name); - - } catch (Exception e) { - //if there is no entity with such name we'll skip further parsing - System.err.println("Entity "+name+" is not found"); - } - } - } - - // parsing complete, writing to file now - writeXML(); - } private void writeXML() { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new FileOutputStream(path)); transformer.transform(source, result); @@ -300,11 +295,26 @@ public class Project implements Iterable { } public void PrintEntitys() { - // TODO Auto-generated method stub for(Entity ent:listEntity) { ent.PrintEntity(); } - + } + + + public void printXMlToConsole() { + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformerFactory.setAttribute("indent-number", 4); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(System.out); + transformer.transform(source, result); + } catch (TransformerException e) { + System.err.println("Cannot print XML to console. "+e); + } } @Override