2020import javax .xml .parsers .DocumentBuilderFactory ;
2121import javax .xml .parsers .ParserConfigurationException ;
2222import java .awt .*;
23- import java .awt .event .ActionListener ;
2423import java .awt .event .MouseAdapter ;
2524import java .awt .event .MouseEvent ;
2625import java .io .*;
2726import java .net .URI ;
2827import java .net .URISyntaxException ;
2928import java .text .DecimalFormat ;
29+ import java .text .DecimalFormatSymbols ;
3030import java .util .ArrayList ;
31- import java .util .Arrays ;
3231import java .util .Collections ;
3332import java .util .HashMap ;
34- import java .util .regex .Matcher ;
35- import java .util .regex .Pattern ;
3633
3734
3835/**
@@ -44,6 +41,7 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
4441
4542 public static final String INSTRUCTION_IMHEIGHT = "imheight" ;
4643 public static final String INSTRUCTION_IMWIDTH = "imwidth" ;
44+ public static final String INSTRUCTION_SEPARATOR = "separatorChar" ;
4745 public static final String INSTRUCTION_MAGNIFICATION = "immagni" ;
4846 public static final String INSTRUCTION_MIRROR_COLUMN_TILING = "pmirrorcolumntiling" ;
4947 public static final String INSTRUCTION_MIRROR_ROW_TILING = "pmirrorrowtiling" ;
@@ -82,14 +80,29 @@ public class PuzzleHelperGUI extends JFrame implements WellPartitionPanel.WellPa
8280 private JSpinner partitionsSP ;
8381 private JLabel magnificationLB ;
8482 private JLabel partitionsWarningLB ;
83+ private JTextField separatorTF ;
8584
8685 private JCheckBoxMenuItem autoUpdateCB ;
8786
8887 public PuzzleHelperGUI () {
8988 exportFileFilter = new FileNameExtensionFilter ("XML" , "xml" );
9089 imageFileFilter = new FileNameExtensionFilter ("Image files" , "tiff" , "tif" , "png" , "bmp" );
9190
92- magnificationSP .setModel (new SpinnerNumberModel (1 , Integer .MIN_VALUE , Integer .MAX_VALUE , 0.001 ));
91+ MouseAdapter mouseListenerUpdate = new MouseAdapter () {
92+ @ Override
93+ public void mouseEntered (MouseEvent e ) {
94+ super .mouseEntered (e );
95+ requestUpdate ();
96+ }
97+
98+ @ Override
99+ public void mouseExited (MouseEvent e ) {
100+ super .mouseExited (e );
101+ requestUpdate ();
102+ }
103+ };
104+
105+ magnificationSP .setModel (new SpinnerNumberModel (1.0 , -100.0 , 100.0 , 0.001 ));
93106 pWidthSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
94107 pHeightSP .setModel (new SpinnerNumberModel (10 , 1 , Integer .MAX_VALUE , 1 ));
95108 imWidthSP .setModel (new SpinnerNumberModel (1000 , 1 , Integer .MAX_VALUE , 1 ));
@@ -103,6 +116,8 @@ public PuzzleHelperGUI() {
103116 imHeightSP .addChangeListener (l );
104117 partitionsSP .addChangeListener (changeEvent -> update ());
105118 magnificationSP .addChangeListener (changeEvent -> update ());
119+ magnificationSP .addMouseListener (mouseListenerUpdate );
120+ exportInstructionFileButton .addMouseListener (mouseListenerUpdate );
106121
107122 directionCB .addItem (PuzzleDirection .RIGHT );
108123 directionCB .addItem (PuzzleDirection .DOWN );
@@ -254,6 +269,7 @@ private void actionExportFile() throws IOException {
254269 map .put (INSTRUCTION_PFLIPROW , String .valueOf (flipRowCB .isSelected ()));
255270 map .put (INSTRUCTION_PFLIPRESULT , String .valueOf (flipFinalImageCB .isSelected ()));
256271 map .put (INSTRUCTION_MAGNIFICATION , String .valueOf (getMagnification ()));
272+ map .put (INSTRUCTION_SEPARATOR , String .valueOf (separatorTF .getText ()));
257273 map .put (INSTRUCTION_VERSION , CellomicsPuzzleHelper .VERSION );
258274
259275 FileOutputStream fout = new FileOutputStream (f );
@@ -375,6 +391,7 @@ private void actionImportFile() throws IOException, ParserConfigurationException
375391 pWidthSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_PWIDTH ).item (0 ).getTextContent ()));
376392 magnificationSP .setValue (Double .valueOf (document .getElementsByTagName (INSTRUCTION_MAGNIFICATION ).item (0 ).getTextContent ()));
377393 pHeightSP .setValue (Integer .valueOf (document .getElementsByTagName (INSTRUCTION_PHEIGHT ).item (0 ).getTextContent ()));
394+ separatorTF .setText (String .valueOf (document .getElementsByTagName (INSTRUCTION_SEPARATOR ).item (0 ).getTextContent ()));
378395 flipFinalImageCB .setSelected (Boolean .valueOf (document .getElementsByTagName (INSTRUCTION_PFLIPRESULT ).item (0 ).getTextContent ()));
379396 flipRowCB .setSelected (Boolean .valueOf (document .getElementsByTagName (INSTRUCTION_PFLIPROW ).item (0 ).getTextContent ()));
380397 mirrorColumnTilingCB .setSelected (Boolean .valueOf (document .getElementsByTagName (INSTRUCTION_MIRROR_ROW_TILING ).item (0 ).getTextContent ()));
@@ -477,7 +494,12 @@ private void autoReadImageSize() {
477494 }
478495
479496 private void requestUpdate () {
480- if (autoUpdateCB .isSelected ()) update ();
497+ try {
498+ if (autoUpdateCB .isSelected ()) update ();
499+ }catch (Exception e ){
500+ e .printStackTrace ();
501+ JOptionPane .showConfirmDialog (this ,"Error while trying to update the UI:\n " +e .getMessage (),"Error: " +e .getClass ().getName (),JOptionPane .ERROR_MESSAGE );
502+ }
481503 }
482504
483505 private void update () {
@@ -538,9 +560,28 @@ public boolean hasWellPartitionsWithAtLeastOneControl(){
538560 }
539561
540562 public double getMagnification (){
563+ DecimalFormat format = (DecimalFormat ) DecimalFormat .getInstance ();
564+ DecimalFormatSymbols symbols = format .getDecimalFormatSymbols ();
565+ char sep =symbols .getDecimalSeparator ();
541566 DecimalFormat df = new DecimalFormat ("#.###" );
542- double magnification = (double ) magnificationSP .getValue ();
567+
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+ }
582+
543583 String formattedMagnification = df .format (magnification );
584+ formattedMagnification =formattedMagnification .replace ("," ,"." );
544585 return Double .parseDouble (formattedMagnification );
545586 }
546587
0 commit comments