Skip to content

Commit 7d0daa3

Browse files
author
NilsFo
committed
Implemented xml import feature
1 parent 30779be commit 7d0daa3

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

CellomicsPuzzleHelper.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
</library>
1919
</orderEntry>
2020
<orderEntry type="library" name="mij" level="application" />
21+
<orderEntry type="library" name="xstream-1.4.10" level="project" />
2122
</component>
2223
</module>

lib/xstream-1.4.10.jar

576 KB
Binary file not shown.

src/de/rub/bph/CellomicsPuzzleHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class CellomicsPuzzleHelper {
1111

1212
public static final String NAME = "ThermoFischer SCIENTIFIC ArrayScan Cellomics Image Puzzle-Helper";
13-
public static final String VERSION = "0.8.1b";
13+
public static final String VERSION = "1.0";
1414
public static final String AUTHOR = "Nils Förster";
1515

1616
public static PuzzleHelperGUI helperGUI;

src/de/rub/bph/data/PuzzleDirection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ public enum PuzzleDirection {
77

88
LEFT, RIGHT, UP, DOWN;
99

10+
public static PuzzleDirection getViaString(String name) {
11+
for (PuzzleDirection direction : values()) {
12+
if (direction.toString().equals(name)) return direction;
13+
}
14+
return null;
15+
}
16+
1017
@Override
1118
public String toString() {
1219
switch (this) {

src/de/rub/bph/ui/PuzzleHelperGUI.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
209209
</constraints>
210210
<properties>
211-
<enabled value="false"/>
211+
<enabled value="true"/>
212212
<text value="Import instructions"/>
213213
</properties>
214214
</component>

src/de/rub/bph/ui/PuzzleHelperGUI.java

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@
1717
import java.net.URI;
1818
import java.net.URISyntaxException;
1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.HashMap;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
2225

2326

2427
/**
2528
* Created by nilfoe on 12/03/2018.
2629
*/
2730
public class PuzzleHelperGUI extends JFrame {
2831

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";
2940
private FileNameExtensionFilter exportFileFilter, imageFileFilter;
3041

3142
private JButton button1;
@@ -163,7 +174,12 @@ public void actionPerformed(ActionEvent actionEvent) {
163174
importBT.addActionListener(new ActionListener() {
164175
@Override
165176
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+
}
167183
}
168184
});
169185

@@ -209,15 +225,14 @@ private void exportFile() throws IOException {
209225
if (!exportFileFilter.accept(f)) return;
210226

211227
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);
221236

222237
FileOutputStream fout = new FileOutputStream(f);
223238
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
@@ -252,6 +267,69 @@ private File chooseFile() {
252267
} else return null;
253268
}
254269

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+
255333
private void autoReadImageSize() {
256334
File selectFile;
257335

0 commit comments

Comments
 (0)