|
17 | 17 | import java.net.URI; |
18 | 18 | import java.net.URISyntaxException; |
19 | 19 | import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Collections; |
21 | 22 | import java.util.HashMap; |
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
22 | 25 |
|
23 | 26 |
|
24 | 27 | /** |
25 | 28 | * Created by nilfoe on 12/03/2018. |
26 | 29 | */ |
27 | 30 | public class PuzzleHelperGUI extends JFrame { |
28 | 31 |
|
| 32 | + public static final String INSTRUCTION_IMWIDTH = "imwidth"; |
| 33 | + public static final String INSTRUCTION_IMHEIGHT = "imheight"; |
| 34 | + public static final String INSTRUCTION_PWIDTH = "pwidth"; |
| 35 | + public static final String INSTRUCTION_PHEIGHT = "pheight"; |
| 36 | + public static final String INSTRUCTION_PDIR = "pdir"; |
| 37 | + public static final String INSTRUCTION_PFLIPROW = "pfliprow"; |
| 38 | + public static final String INSTRUCTION_PFLIPRESULT = "pflipresult"; |
| 39 | + public static final String INSTRUCTION_VERSION = "version"; |
29 | 40 | private FileNameExtensionFilter exportFileFilter, imageFileFilter; |
30 | 41 |
|
31 | 42 | private JButton button1; |
@@ -163,7 +174,12 @@ public void actionPerformed(ActionEvent actionEvent) { |
163 | 174 | importBT.addActionListener(new ActionListener() { |
164 | 175 | @Override |
165 | 176 | public void actionPerformed(ActionEvent actionEvent) { |
166 | | - |
| 177 | + try { |
| 178 | + actionImportFile(); |
| 179 | + } catch (IOException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + JOptionPane.showMessageDialog(PuzzleHelperGUI.this, "Failed to read instruction file:\n" + e.getMessage(), CellomicsPuzzleHelper.NAME, JOptionPane.ERROR_MESSAGE); |
| 182 | + } |
167 | 183 | } |
168 | 184 | }); |
169 | 185 |
|
@@ -209,15 +225,14 @@ private void exportFile() throws IOException { |
209 | 225 | if (!exportFileFilter.accept(f)) return; |
210 | 226 |
|
211 | 227 | HashMap<String, String> map = new HashMap<String, String>(); |
212 | | - map.put("imwidth", String.valueOf(imWidthSP.getValue())); |
213 | | - map.put("imheight", String.valueOf(imHeightSP.getValue())); |
214 | | - map.put("pwidth", String.valueOf(pWidthSP.getValue())); |
215 | | - map.put("pheight", String.valueOf(pHeightSP.getValue())); |
216 | | - map.put("pdir", String.valueOf(directionCB.getSelectedItem())); |
217 | | - map.put("pfliprow", String.valueOf(flipRowCB.isSelected())); |
218 | | - map.put("pflipresult", String.valueOf(flipFinalImageCB.isSelected())); |
219 | | - map.put("version", CellomicsPuzzleHelper.VERSION); |
220 | | - //TODO extract as constant to help instruction import |
| 228 | + map.put(INSTRUCTION_IMWIDTH, String.valueOf(imWidthSP.getValue())); |
| 229 | + map.put(INSTRUCTION_IMHEIGHT, String.valueOf(imHeightSP.getValue())); |
| 230 | + map.put(INSTRUCTION_PWIDTH, String.valueOf(pWidthSP.getValue())); |
| 231 | + map.put(INSTRUCTION_PHEIGHT, String.valueOf(pHeightSP.getValue())); |
| 232 | + map.put(INSTRUCTION_PDIR, String.valueOf(directionCB.getSelectedItem())); |
| 233 | + map.put(INSTRUCTION_PFLIPROW, String.valueOf(flipRowCB.isSelected())); |
| 234 | + map.put(INSTRUCTION_PFLIPRESULT, String.valueOf(flipFinalImageCB.isSelected())); |
| 235 | + map.put(INSTRUCTION_VERSION, CellomicsPuzzleHelper.VERSION); |
221 | 236 |
|
222 | 237 | FileOutputStream fout = new FileOutputStream(f); |
223 | 238 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout)); |
@@ -252,6 +267,69 @@ private File chooseFile() { |
252 | 267 | } else return null; |
253 | 268 | } |
254 | 269 |
|
| 270 | + private void actionImportFile() throws IOException { |
| 271 | + JFileChooser chooser = new JFileChooser(); |
| 272 | + chooser.setDialogTitle("Select instruction file"); |
| 273 | + chooser.setFileFilter(exportFileFilter); |
| 274 | + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
| 275 | + chooser.setApproveButtonText("Import"); |
| 276 | + int i = chooser.showOpenDialog(this); |
| 277 | + |
| 278 | + if (i != JFileChooser.APPROVE_OPTION) return; |
| 279 | + File f = chooser.getSelectedFile(); |
| 280 | + |
| 281 | + ArrayList<String> XMLArgumentList = new ArrayList<>(); |
| 282 | + XMLArgumentList.add(INSTRUCTION_IMWIDTH); |
| 283 | + XMLArgumentList.add(INSTRUCTION_IMHEIGHT); |
| 284 | + XMLArgumentList.add(INSTRUCTION_PWIDTH); |
| 285 | + XMLArgumentList.add(INSTRUCTION_PHEIGHT); |
| 286 | + XMLArgumentList.add(INSTRUCTION_PDIR); |
| 287 | + XMLArgumentList.add(INSTRUCTION_PFLIPROW); |
| 288 | + XMLArgumentList.add(INSTRUCTION_PFLIPRESULT); |
| 289 | + XMLArgumentList.add(INSTRUCTION_VERSION); |
| 290 | + HashMap<String, String> xmlMap = new HashMap<>(); |
| 291 | + |
| 292 | + BufferedReader br = new BufferedReader(new FileReader(f)); |
| 293 | + String line; |
| 294 | + while ((line = br.readLine()) != null) { |
| 295 | + line = line.trim(); |
| 296 | + ArrayList<String> temp = new ArrayList<>(XMLArgumentList); |
| 297 | + for (String s : temp) { |
| 298 | + String regex = "<" + s + ">(\\w+)</" + s + ">"; |
| 299 | + Matcher m = Pattern.compile(regex).matcher(line); |
| 300 | + if (m.find()) { |
| 301 | + System.out.println("Regex done: " + regex + " -> " + m.group(1)); |
| 302 | + xmlMap.put(s, m.group(1)); |
| 303 | + XMLArgumentList.remove(s); |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + if (!xmlMap.containsKey(INSTRUCTION_VERSION)) { |
| 309 | + xmlMap.put(INSTRUCTION_VERSION, String.valueOf(CellomicsPuzzleHelper.VERSION)); |
| 310 | + XMLArgumentList.remove(INSTRUCTION_VERSION); |
| 311 | + JOptionPane.showMessageDialog(this, "Failed to read the Version on this file. This could lead to unexpected behaviour."); |
| 312 | + } |
| 313 | + |
| 314 | + if (!XMLArgumentList.isEmpty()) { |
| 315 | + JOptionPane.showMessageDialog(this, "Not enough instruction lines. This may lead to unexpected behaviour! Missing expected lines:\n" + Arrays.toString(XMLArgumentList.toArray())); |
| 316 | + } |
| 317 | + |
| 318 | + if (!xmlMap.get(INSTRUCTION_VERSION).equals(CellomicsPuzzleHelper.VERSION)) { |
| 319 | + JOptionPane.showMessageDialog(this, "The instruction file was created from a different version. This could lead to unexpected behaviour."); |
| 320 | + } |
| 321 | + |
| 322 | + imWidthSP.setValue(Integer.valueOf(xmlMap.get(INSTRUCTION_IMWIDTH))); |
| 323 | + imHeightSP.setValue(Integer.valueOf(xmlMap.get(INSTRUCTION_IMHEIGHT))); |
| 324 | + pWidthSP.setValue(Integer.valueOf(xmlMap.get(INSTRUCTION_PWIDTH))); |
| 325 | + pHeightSP.setValue(Integer.valueOf(xmlMap.get(INSTRUCTION_PHEIGHT))); |
| 326 | + flipFinalImageCB.setSelected(Boolean.valueOf(xmlMap.get(INSTRUCTION_PFLIPRESULT))); |
| 327 | + flipRowCB.setSelected(Boolean.valueOf(xmlMap.get(INSTRUCTION_PFLIPROW))); |
| 328 | + directionCB.setSelectedItem(PuzzleDirection.valueOf(xmlMap.get(INSTRUCTION_PDIR))); |
| 329 | + |
| 330 | + update(); |
| 331 | + } |
| 332 | + |
255 | 333 | private void autoReadImageSize() { |
256 | 334 | File selectFile; |
257 | 335 |
|
|
0 commit comments