1515
1616import javax .swing .*;
1717import javax .swing .event .ChangeListener ;
18+ import javax .swing .event .DocumentEvent ;
19+ import javax .swing .event .DocumentListener ;
1820import javax .swing .filechooser .FileNameExtensionFilter ;
1921import javax .xml .parsers .DocumentBuilder ;
2022import javax .xml .parsers .DocumentBuilderFactory ;
@@ -72,7 +74,6 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
7274 private JButton autoReadImageSizesButton ;
7375 private JCheckBox flipFinalImageCB ;
7476 private JButton importBT ;
75- private JSpinner magnificationSP ;
7677 private JCheckBox mirrorRowTilingCB ;
7778 private JCheckBox mirrorColumnTilingCB ;
7879 private JCheckBox highlightPuzzleCB ;
@@ -81,6 +82,7 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
8182 private JLabel magnificationLB ;
8283 private JLabel partitionsWarningLB ;
8384 private JTextField separatorTF ;
85+ private JTextField magnificationTF ;
8486
8587 private JCheckBoxMenuItem autoUpdateCB ;
8688
@@ -102,7 +104,6 @@ public void mouseExited(MouseEvent e) {
102104 }
103105 };
104106
105- magnificationSP .setModel (new SpinnerNumberModel (1.0 , -100.0 , 100.0 , 0.001 ));
106107 pWidthSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
107108 pHeightSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
108109 imWidthSP .setModel (new SpinnerNumberModel (1000 , 1 , Integer .MAX_VALUE , 1 ));
@@ -115,9 +116,23 @@ public void mouseExited(MouseEvent e) {
115116 imWidthSP .addChangeListener (l );
116117 imHeightSP .addChangeListener (l );
117118 partitionsSP .addChangeListener (changeEvent -> update ());
118- magnificationSP .addChangeListener (changeEvent -> update ());
119- magnificationSP .addMouseListener (mouseListenerUpdate );
120119 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+ });
121136
122137 directionCB .addItem (PuzzleDirection .RIGHT );
123138 directionCB .addItem (PuzzleDirection .DOWN );
@@ -258,6 +273,13 @@ private void actionExportFile() throws IOException {
258273
259274 if (!exportFileFilter .accept (f )) return ;
260275
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+
261283 HashMap <String , String > map = new HashMap <String , String >();
262284 map .put (INSTRUCTION_IMWIDTH , String .valueOf (imWidthSP .getValue ()));
263285 map .put (INSTRUCTION_IMHEIGHT , String .valueOf (imHeightSP .getValue ()));
@@ -268,7 +290,7 @@ private void actionExportFile() throws IOException {
268290 map .put (INSTRUCTION_MIRROR_ROW_TILING , String .valueOf (mirrorRowTilingCB .isSelected ()));
269291 map .put (INSTRUCTION_PFLIPROW , String .valueOf (flipRowCB .isSelected ()));
270292 map .put (INSTRUCTION_PFLIPRESULT , String .valueOf (flipFinalImageCB .isSelected ()));
271- map .put (INSTRUCTION_MAGNIFICATION , String .valueOf (getMagnification () ));
293+ map .put (INSTRUCTION_MAGNIFICATION , String .valueOf (magnification ));
272294 map .put (INSTRUCTION_SEPARATOR , String .valueOf (separatorTF .getText ()));
273295 map .put (INSTRUCTION_VERSION , CellomicsPuzzleHelper .VERSION );
274296
@@ -389,7 +411,7 @@ private void actionImportFile() throws IOException, ParserConfigurationException
389411 imWidthSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_IMWIDTH ).item (0 ).getTextContent ()));
390412 imHeightSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_IMHEIGHT ).item (0 ).getTextContent ()));
391413 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 ()));
393415 pHeightSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_PHEIGHT ).item (0 ).getTextContent ()));
394416 separatorTF .setText (String .valueOf (document .getElementsByTagName (INSTRUCTION_SEPARATOR ).item (0 ).getTextContent ()));
395417 flipFinalImageCB .setSelected (Boolean .valueOf (document .getElementsByTagName (INSTRUCTION_PFLIPRESULT ).item (0 ).getTextContent ()));
@@ -399,7 +421,16 @@ private void actionImportFile() throws IOException, ParserConfigurationException
399421 directionCB .setSelectedItem (PuzzleDirection .getViaString (document .getElementsByTagName (INSTRUCTION_PDIR ).item (0 ).getTextContent ()));
400422 } catch (Exception e ) {
401423 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+ }
403434 return ;
404435 }
405436
@@ -514,8 +545,14 @@ private void update() {
514545 previewPL .update (w , h , mirrorRowTiling , mirrorColumnTiling , highlightPuzzle , direction );
515546 imagecountLB .setText ("Image count per well: " + w * h + " [" + w + "x" + h + "]" );
516547
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 );
519556
520557 updateWellPartitionTabs ();
521558 partitionsWarningLB .setVisible (!hasWellPartitionsWithAtLeastOneControl ());
@@ -559,26 +596,15 @@ public boolean hasWellPartitionsWithAtLeastOneControl(){
559596 return true ;
560597 }
561598
562- public double getMagnification (){
599+ public double getMagnification () throws NumberFormatException {
563600 DecimalFormat format = (DecimalFormat ) DecimalFormat .getInstance ();
564601 DecimalFormatSymbols symbols = format .getDecimalFormatSymbols ();
565602 char sep =symbols .getDecimalSeparator ();
566603 DecimalFormat df = new DecimalFormat ("#.###" );
567604
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 );
582608
583609 String formattedMagnification = df .format (magnification );
584610 formattedMagnification =formattedMagnification .replace ("," ,"." );
0 commit comments