|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2024 Luís Vázquez Lema |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +package jxmlDixitAll; |
| 28 | + |
| 29 | +import java.io.*; |
| 30 | +import javax.swing.*; |
| 31 | +import java.awt.*; |
| 32 | +import java.util.*; |
| 33 | +import javax.xml.parsers.*; |
| 34 | +import javax.xml.transform.*; |
| 35 | +import javax.xml.transform.dom.*; |
| 36 | +import javax.xml.transform.stream.*; |
| 37 | +import org.w3c.dom.*; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + * @author Luís Vázquez Lema |
| 42 | + */ |
| 43 | + |
| 44 | +public class jxmlDixitAll extends JFrame { |
| 45 | + private HashMap<String, String> datos; |
| 46 | + private JTextField claveEntry, consultarEntry; |
| 47 | + private JTextArea valorEntry, salida; |
| 48 | + private JList<String> lista; |
| 49 | + private DefaultListModel<String> listModel; |
| 50 | + |
| 51 | + public jxmlDixitAll() { |
| 52 | + super("jxmlDixitAll"); |
| 53 | + verificarCarpetaDatos(); |
| 54 | + cargarDatos(); |
| 55 | + |
| 56 | + setLayout(new FlowLayout()); |
| 57 | + |
| 58 | + add(new JLabel("Término:")); |
| 59 | + claveEntry = new JTextField(20); |
| 60 | + add(claveEntry); |
| 61 | + |
| 62 | + add(new JLabel("Definición:")); |
| 63 | + valorEntry = new JTextArea(10, 50); |
| 64 | + valorEntry.setWrapStyleWord(true); |
| 65 | + valorEntry.setLineWrap(true); |
| 66 | + JScrollPane valorScroll = new JScrollPane(valorEntry); |
| 67 | + add(valorScroll); |
| 68 | + |
| 69 | + JButton guardarButton = new JButton("Guardar"); |
| 70 | + guardarButton.addActionListener(e -> guardar()); |
| 71 | + add(guardarButton); |
| 72 | + |
| 73 | + JButton modificarButton = new JButton("Modificar"); |
| 74 | + modificarButton.addActionListener(e -> modificar()); |
| 75 | + add(modificarButton); |
| 76 | + |
| 77 | + JButton eliminarButton = new JButton("Eliminar"); |
| 78 | + eliminarButton.addActionListener(e -> eliminar()); |
| 79 | + add(eliminarButton); |
| 80 | + |
| 81 | + add(new JLabel("Término a consultar:")); |
| 82 | + consultarEntry = new JTextField(20); |
| 83 | + add(consultarEntry); |
| 84 | + |
| 85 | + JButton consultarButton = new JButton("Consultar"); |
| 86 | + consultarButton.addActionListener(e -> consultar()); |
| 87 | + add(consultarButton); |
| 88 | + |
| 89 | + add(new JLabel("Definición:")); |
| 90 | + salida = new JTextArea(10, 50); |
| 91 | + salida.setEditable(false); |
| 92 | + salida.setWrapStyleWord(true); |
| 93 | + salida.setLineWrap(true); |
| 94 | + JScrollPane salidaScroll = new JScrollPane(salida); |
| 95 | + add(salidaScroll); |
| 96 | + |
| 97 | + add(new JLabel("Términos almacenados:")); |
| 98 | + listModel = new DefaultListModel<>(); |
| 99 | + lista = new JList<>(listModel); |
| 100 | + JScrollPane listaScroll = new JScrollPane(lista); |
| 101 | + listaScroll.setPreferredSize(new Dimension(500, 180)); |
| 102 | + add(listaScroll); |
| 103 | + |
| 104 | + actualizarLista(); |
| 105 | + setSize(600, 800); |
| 106 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 107 | + setVisible(true); |
| 108 | + } |
| 109 | + |
| 110 | + private void verificarCarpetaDatos() { |
| 111 | + File dir = new File("src/jxmlDixitAll/datos"); |
| 112 | + if (!dir.exists()) { |
| 113 | + dir.mkdirs(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void cargarDatos() { |
| 118 | + File xmlFile = new File("src/jxmlDixitAll/datos/datos.xml"); |
| 119 | + if (!xmlFile.exists()) { |
| 120 | + datos = new HashMap<>(); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + try { |
| 125 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 126 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 127 | + Document doc = dBuilder.parse(xmlFile); |
| 128 | + doc.getDocumentElement().normalize(); |
| 129 | + |
| 130 | + datos = new HashMap<>(); |
| 131 | + NodeList nList = doc.getElementsByTagName("Término"); |
| 132 | + |
| 133 | + for (int i = 0; i < nList.getLength(); i++) { |
| 134 | + Node nNode = nList.item(i); |
| 135 | + if (nNode.getNodeType() == Node.ELEMENT_NODE) { |
| 136 | + Element eElement = (Element) nNode; |
| 137 | + String clave = eElement.getElementsByTagName("Clave").item(0).getTextContent(); |
| 138 | + String valor = eElement.getElementsByTagName("Valor").item(0).getTextContent(); |
| 139 | + datos.put(clave, valor); |
| 140 | + } |
| 141 | + } |
| 142 | + } catch (Exception e) { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private void guardar() { |
| 148 | + String clave = claveEntry.getText(); |
| 149 | + String valor = valorEntry.getText(); |
| 150 | + datos.put(clave, valor); |
| 151 | + guardarDatos(); |
| 152 | + actualizarLista(); |
| 153 | + } |
| 154 | + |
| 155 | + private void modificar() { |
| 156 | + String clave = claveEntry.getText(); |
| 157 | + String valor = valorEntry.getText(); |
| 158 | + if (datos.containsKey(clave)) { |
| 159 | + datos.put(clave, valor); |
| 160 | + guardarDatos(); |
| 161 | + actualizarLista(); |
| 162 | + } else { |
| 163 | + salida.setText("El término no existe"); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void eliminar() { |
| 168 | + String clave = claveEntry.getText(); |
| 169 | + if (datos.containsKey(clave)) { |
| 170 | + datos.remove(clave); |
| 171 | + guardarDatos(); |
| 172 | + actualizarLista(); |
| 173 | + } else { |
| 174 | + salida.setText("El término no existe"); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private void consultar() { |
| 179 | + String clave = consultarEntry.getText(); |
| 180 | + if (datos.containsKey(clave)) { |
| 181 | + salida.setText(datos.get(clave)); |
| 182 | + } else { |
| 183 | + salida.setText("El término no existe"); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private void guardarDatos() { |
| 188 | + try { |
| 189 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 190 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 191 | + Document doc = dBuilder.newDocument(); |
| 192 | + |
| 193 | + Element rootElement = doc.createElement("Lista"); |
| 194 | + doc.appendChild(rootElement); |
| 195 | + |
| 196 | + for (Map.Entry<String, String> entry : datos.entrySet()) { |
| 197 | + Element termino = doc.createElement("Término"); |
| 198 | + rootElement.appendChild(termino); |
| 199 | + |
| 200 | + Element clave = doc.createElement("Clave"); |
| 201 | + clave.appendChild(doc.createTextNode(entry.getKey())); |
| 202 | + termino.appendChild(clave); |
| 203 | + |
| 204 | + Element valor = doc.createElement("Valor"); |
| 205 | + valor.appendChild(doc.createTextNode(entry.getValue())); |
| 206 | + termino.appendChild(valor); |
| 207 | + } |
| 208 | + |
| 209 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 210 | + Transformer transformer = transformerFactory.newTransformer(); |
| 211 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 212 | + DOMSource source = new DOMSource(doc); |
| 213 | + StreamResult result = new StreamResult(new File("src/jxmlDixitAll/datos/datos.xml")); |
| 214 | + |
| 215 | + transformer.transform(source, result); |
| 216 | + } catch (Exception e) { |
| 217 | + e.printStackTrace(); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private void actualizarLista() { |
| 222 | + listModel.clear(); |
| 223 | + datos.keySet().stream().sorted().forEach(listModel::addElement); |
| 224 | + } |
| 225 | + |
| 226 | + public static void main(String[] args) { |
| 227 | + new jxmlDixitAll(); |
| 228 | + } |
| 229 | +} |
0 commit comments