15
15
16
16
import javax .swing .*;
17
17
import javax .swing .event .ChangeListener ;
18
+ import javax .swing .event .DocumentEvent ;
19
+ import javax .swing .event .DocumentListener ;
18
20
import javax .swing .filechooser .FileNameExtensionFilter ;
19
21
import javax .xml .parsers .DocumentBuilder ;
20
22
import javax .xml .parsers .DocumentBuilderFactory ;
@@ -72,7 +74,6 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
72
74
private JButton autoReadImageSizesButton ;
73
75
private JCheckBox flipFinalImageCB ;
74
76
private JButton importBT ;
75
- private JSpinner magnificationSP ;
76
77
private JCheckBox mirrorRowTilingCB ;
77
78
private JCheckBox mirrorColumnTilingCB ;
78
79
private JCheckBox highlightPuzzleCB ;
@@ -81,6 +82,7 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
81
82
private JLabel magnificationLB ;
82
83
private JLabel partitionsWarningLB ;
83
84
private JTextField separatorTF ;
85
+ private JTextField magnificationTF ;
84
86
85
87
private JCheckBoxMenuItem autoUpdateCB ;
86
88
@@ -102,7 +104,6 @@ public void mouseExited(MouseEvent e) {
102
104
}
103
105
};
104
106
105
- magnificationSP .setModel (new SpinnerNumberModel (1.0 , -100.0 , 100.0 , 0.001 ));
106
107
pWidthSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
107
108
pHeightSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
108
109
imWidthSP .setModel (new SpinnerNumberModel (1000 , 1 , Integer .MAX_VALUE , 1 ));
@@ -115,9 +116,23 @@ public void mouseExited(MouseEvent e) {
115
116
imWidthSP .addChangeListener (l );
116
117
imHeightSP .addChangeListener (l );
117
118
partitionsSP .addChangeListener (changeEvent -> update ());
118
- magnificationSP .addChangeListener (changeEvent -> update ());
119
- magnificationSP .addMouseListener (mouseListenerUpdate );
120
119
exportInstructionFileButton .addMouseListener (mouseListenerUpdate );
120
+ magnificationTF .getDocument ().addDocumentListener (new DocumentListener () {
121
+ @ Override
122
+ public void insertUpdate (DocumentEvent documentEvent ) {
123
+ update ();
124
+ }
125
+
126
+ @ Override
127
+ public void removeUpdate (DocumentEvent documentEvent ) {
128
+ update ();
129
+ }
130
+
131
+ @ Override
132
+ public void changedUpdate (DocumentEvent documentEvent ) {
133
+ update ();
134
+ }
135
+ });
121
136
122
137
directionCB .addItem (PuzzleDirection .RIGHT );
123
138
directionCB .addItem (PuzzleDirection .DOWN );
@@ -258,6 +273,13 @@ private void actionExportFile() throws IOException {
258
273
259
274
if (!exportFileFilter .accept (f )) return ;
260
275
276
+ double magnification = 0 ;
277
+ try {
278
+ magnification = getMagnification ();
279
+ }catch (Exception e ){
280
+ JOptionPane .showMessageDialog (this ,"Cannot save the puzzle instructions:\n Invalid magnification!" ,"Fatal Error!" ,JOptionPane .ERROR_MESSAGE );
281
+ }
282
+
261
283
HashMap <String , String > map = new HashMap <String , String >();
262
284
map .put (INSTRUCTION_IMWIDTH , String .valueOf (imWidthSP .getValue ()));
263
285
map .put (INSTRUCTION_IMHEIGHT , String .valueOf (imHeightSP .getValue ()));
@@ -268,7 +290,7 @@ private void actionExportFile() throws IOException {
268
290
map .put (INSTRUCTION_MIRROR_ROW_TILING , String .valueOf (mirrorRowTilingCB .isSelected ()));
269
291
map .put (INSTRUCTION_PFLIPROW , String .valueOf (flipRowCB .isSelected ()));
270
292
map .put (INSTRUCTION_PFLIPRESULT , String .valueOf (flipFinalImageCB .isSelected ()));
271
- map .put (INSTRUCTION_MAGNIFICATION , String .valueOf (getMagnification () ));
293
+ map .put (INSTRUCTION_MAGNIFICATION , String .valueOf (magnification ));
272
294
map .put (INSTRUCTION_SEPARATOR , String .valueOf (separatorTF .getText ()));
273
295
map .put (INSTRUCTION_VERSION , CellomicsPuzzleHelper .VERSION );
274
296
@@ -389,7 +411,7 @@ private void actionImportFile() throws IOException, ParserConfigurationException
389
411
imWidthSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_IMWIDTH ).item (0 ).getTextContent ()));
390
412
imHeightSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_IMHEIGHT ).item (0 ).getTextContent ()));
391
413
pWidthSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_PWIDTH ).item (0 ).getTextContent ()));
392
- magnificationSP . setValue ( Double .valueOf (document .getElementsByTagName (INSTRUCTION_MAGNIFICATION ).item (0 ).getTextContent ()));
414
+ magnificationTF . setText ( String .valueOf (document .getElementsByTagName (INSTRUCTION_MAGNIFICATION ).item (0 ).getTextContent ()));
393
415
pHeightSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_PHEIGHT ).item (0 ).getTextContent ()));
394
416
separatorTF .setText (String .valueOf (document .getElementsByTagName (INSTRUCTION_SEPARATOR ).item (0 ).getTextContent ()));
395
417
flipFinalImageCB .setSelected (Boolean .valueOf (document .getElementsByTagName (INSTRUCTION_PFLIPRESULT ).item (0 ).getTextContent ()));
@@ -399,7 +421,16 @@ private void actionImportFile() throws IOException, ParserConfigurationException
399
421
directionCB .setSelectedItem (PuzzleDirection .getViaString (document .getElementsByTagName (INSTRUCTION_PDIR ).item (0 ).getTextContent ()));
400
422
} catch (Exception e ) {
401
423
e .printStackTrace ();
402
- JOptionPane .showMessageDialog (this , "The file did not contain every smart well parameter required by this version and importing stopped. This may lead to unexpected behaviour." , "Error!" , JOptionPane .ERROR_MESSAGE );
424
+ StringBuilder builder = new StringBuilder ();
425
+ for (StackTraceElement element : e .getStackTrace ()){
426
+ builder .append ("\n " +element );
427
+ }
428
+ String className = e .getClass ().getName ();
429
+
430
+ int answer = JOptionPane .showConfirmDialog (this , "The file did not contain every smart well parameter required by this version and importing stopped. This may lead to unexpected behaviour.\n Exception: " +className +": '" +e .getMessage ()+"'\n Do you want to see a detailed error report?" , "Error!" ,JOptionPane .YES_NO_OPTION , JOptionPane .ERROR_MESSAGE );
431
+ if (answer ==JOptionPane .YES_OPTION ){
432
+ JOptionPane .showMessageDialog (this ,e .getMessage ()+"\n " +builder .toString (),className ,JOptionPane .ERROR_MESSAGE );
433
+ }
403
434
return ;
404
435
}
405
436
@@ -514,8 +545,14 @@ private void update() {
514
545
previewPL .update (w , h , mirrorRowTiling , mirrorColumnTiling , highlightPuzzle , direction );
515
546
imagecountLB .setText ("Image count per well: " + w * h + " [" + w + "x" + h + "]" );
516
547
517
- double magnification = getMagnification ();
518
- magnificationLB .setText ("Scaling factor: 1 px represents " +magnification +" \u00B5 m" );
548
+ String magnificationText = "INVALID NUMBER!" ;
549
+ try {
550
+ double magnification = getMagnification ();
551
+ magnificationText ="1 px represents " +magnification +" \u00B5 m" ;
552
+ }catch (Exception e ){
553
+ e .printStackTrace ();
554
+ }
555
+ magnificationLB .setText ("Scaling factor: " +magnificationText );
519
556
520
557
updateWellPartitionTabs ();
521
558
partitionsWarningLB .setVisible (!hasWellPartitionsWithAtLeastOneControl ());
@@ -559,26 +596,15 @@ public boolean hasWellPartitionsWithAtLeastOneControl(){
559
596
return true ;
560
597
}
561
598
562
- public double getMagnification (){
599
+ public double getMagnification () throws NumberFormatException {
563
600
DecimalFormat format = (DecimalFormat ) DecimalFormat .getInstance ();
564
601
DecimalFormatSymbols symbols = format .getDecimalFormatSymbols ();
565
602
char sep =symbols .getDecimalSeparator ();
566
603
DecimalFormat df = new DecimalFormat ("#.###" );
567
604
568
- double magnification ;
569
- // So, there is a bug within Java to round the double spinner value to the nearest integer using "getValue()"
570
- // That's why we gotta read the spinner text. Great.
571
- JSpinner .NumberEditor numEditor = (JSpinner .NumberEditor ) magnificationSP .getEditor ();
572
- JFormattedTextField textField = numEditor .getTextField ();
573
- JFormattedTextField .AbstractFormatter formatter = textField .getFormatter ();
574
- String text = textField .getText ();
575
- if (sep ==',' ){
576
- // Turns out we are on a German Locale. That means we gotta read the text field directly.
577
- text = text .replace ("," ,"." );
578
- magnification = Double .parseDouble (text );
579
- }else {
580
- magnification =(double ) magnificationSP .getValue ();
581
- }
605
+ String inputMag = magnificationTF .getText ();
606
+ inputMag =inputMag .replace ("," ,"." ).trim ().toLowerCase ();
607
+ double magnification = Double .parseDouble (inputMag );
582
608
583
609
String formattedMagnification = df .format (magnification );
584
610
formattedMagnification =formattedMagnification .replace ("," ,"." );
0 commit comments