From 0c193e97c4cbf94e27ed356c20108fce733f4123 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 23 Apr 2015 09:09:57 +0100 Subject: [PATCH] JAL-1706 fixed to correctly restore the last dialog selection after warning --- src/jalview/gui/FontChooser.java | 57 +++++++++++++++++++++------------ test/jalview/gui/FontChooserTest.java | 5 +-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/jalview/gui/FontChooser.java b/src/jalview/gui/FontChooser.java index 381fbe3..92d2b2b 100755 --- a/src/jalview/gui/FontChooser.java +++ b/src/jalview/gui/FontChooser.java @@ -44,6 +44,9 @@ public class FontChooser extends GFontChooser TreePanel tp; + /* + * The font on opening the dialog (to be restored on Cancel) + */ Font oldFont; boolean oldProteinScale; @@ -52,6 +55,13 @@ public class FontChooser extends GFontChooser JInternalFrame frame; + /* + * The last font settings selected in the dialog + */ + private Font lastSelected = null; + + private boolean lastSelMono = false; + /** * Creates a new FontChooser object. * @@ -212,14 +222,6 @@ public class FontChooser extends GFontChooser } } - private Font lastSelected = null; - - private int lastSelStyle = 0; - - private int lastSelSize = 0; - - private boolean lastSelMono = false; - /** * DOCUMENT ME! */ @@ -229,12 +231,10 @@ public class FontChooser extends GFontChooser { // initialise with original font lastSelected = oldFont; - lastSelSize = oldFont.getSize(); - lastSelStyle = oldFont.getStyle(); FontMetrics fm = getGraphics().getFontMetrics(oldFont); - double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm - .getStringBounds("I", getGraphics()).getWidth(); - lastSelMono = mw == iw; + double mw = fm.getStringBounds("M", getGraphics()).getWidth(); + double iw = fm.getStringBounds("I", getGraphics()).getWidth(); + lastSelMono = (mw == iw); // == on double - flaky? } Font newFont = new Font(fontName.getSelectedItem().toString(), @@ -251,13 +251,26 @@ public class FontChooser extends GFontChooser MessageManager.getString("label.font_doesnt_have_letters_defined"), MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE); /* - * Restore previous values - size first to avoid recursive calls to this - * point! + * Restore the changed value - note this will reinvoke this method via the + * ActionListener, but now validation should pass */ - fontSize.setSelectedItem(lastSelSize); - fontName.setSelectedItem(lastSelected.getName()); - fontStyle.setSelectedIndex(lastSelStyle); - monospaced.setSelected(lastSelMono); + if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing + { + fontSize.setSelectedItem(lastSelected.getSize()); + } + if (!lastSelected.getName().equals( + fontName.getSelectedItem().toString())) + { + fontName.setSelectedItem(lastSelected.getName()); + } + if (lastSelected.getStyle() != fontStyle.getSelectedIndex()) + { + fontStyle.setSelectedIndex(lastSelected.getStyle()); + } + if (lastSelMono != monospaced.isSelected()) + { + monospaced.setSelected(lastSelMono); + } return; } if (tp != null) @@ -271,7 +284,11 @@ public class FontChooser extends GFontChooser } monospaced.setSelected(mw == iw); - // remember last selected + + /* + * Remember latest valid selection, so it can be restored if followed by an + * invalid one + */ lastSelected = newFont; } diff --git a/test/jalview/gui/FontChooserTest.java b/test/jalview/gui/FontChooserTest.java index d724fac..338a48f 100644 --- a/test/jalview/gui/FontChooserTest.java +++ b/test/jalview/gui/FontChooserTest.java @@ -20,10 +20,11 @@ public class FontChooserTest { String[] fonts = java.awt.GraphicsEnvironment .getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); + final Canvas canvas = new Canvas(); for (int pointSize = 1;; pointSize++) { System.out.println(System.lineSeparator() - + "Fonts with insufficient width at " + pointSize + "pt:"); + + "Plain fonts with sub-pixel width at " + pointSize + "pt:"); if (pointSize == 1) { System.out.println("All except:"); @@ -32,7 +33,7 @@ public class FontChooserTest for (String fontname : fonts) { Font newFont = new Font(fontname, Font.PLAIN, pointSize); - FontMetrics fontm = new Canvas().getFontMetrics(newFont); + FontMetrics fontm = canvas.getFontMetrics(newFont); double iw = fontm.getStringBounds("I", null).getWidth(); final boolean tooSmall = iw < 1d; if (tooSmall) -- 1.7.10.2