JAL-3210 Barebones gradle/buildship/eclipse. See README
[jalview.git] / src / jalview / appletgui / FontChooser.java
index 727aa88..8b74e32 100644 (file)
@@ -20,7 +20,6 @@
  */
 package jalview.appletgui;
 
-import jalview.api.ViewStyleI;
 import jalview.util.MessageManager;
 
 import java.awt.BorderLayout;
@@ -32,9 +31,9 @@ import java.awt.FlowLayout;
 import java.awt.Font;
 import java.awt.FontMetrics;
 import java.awt.Frame;
+import java.awt.GraphicsEnvironment;
 import java.awt.Label;
 import java.awt.Panel;
-import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
@@ -71,10 +70,20 @@ public class FontChooser extends Panel implements ItemListener
 
   private Font oldFont;
 
+  private Font oldComplementFont;
+
   private int oldCharWidth = 0;
 
+  /*
+   * the state of 'scale protein to cDNA' on opening the dialog
+   */
   private boolean oldScaleProtein = false;
 
+  /*
+   * the state of 'same font for protein and cDNA' on opening the dialog
+   */
+  boolean oldMirrorFont;
+
   private Font lastSelected = null;
 
   private int lastSelStyle = 0;
@@ -116,8 +125,9 @@ public class FontChooser extends Panel implements ItemListener
   {
     this.ap = ap;
     oldFont = ap.av.getFont();
-    oldCharWidth = ap.av.getViewStyle().getCharWidth();
-    oldScaleProtein = ap.av.getViewStyle().isScaleProteinAsCdna();
+    oldCharWidth = ap.av.getCharWidth();
+    oldScaleProtein = ap.av.isScaleProteinAsCdna();
+    oldMirrorFont = ap.av.isProteinFontAsCdna();
 
     try
     {
@@ -134,7 +144,9 @@ public class FontChooser extends Panel implements ItemListener
    */
   void init()
   {
-    String fonts[] = Toolkit.getDefaultToolkit().getFontList();
+    // String fonts[] = Toolkit.getDefaultToolkit().getFontList();
+    String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
+            .getAvailableFontFamilyNames();
     for (int i = 0; i < fonts.length; i++)
     {
       fontName.addItem(fonts[i]);
@@ -184,6 +196,29 @@ public class FontChooser extends Panel implements ItemListener
     {
       scaleAsCdna_actionPerformed();
     }
+    else if (source == fontAsCdna)
+    {
+      mirrorFont_actionPerformed();
+    }
+  }
+
+  /**
+   * Action on checking or unchecking 'use same font across split screen'
+   * option. When checked, the font settings are copied to the other half of the
+   * split screen. When unchecked, the other half is restored to its initial
+   * settings.
+   */
+  protected void mirrorFont_actionPerformed()
+  {
+    boolean selected = fontAsCdna.getState();
+    ap.av.setProteinFontAsCdna(selected);
+    ap.av.getCodingComplement().setProteinFontAsCdna(selected);
+
+    if (!selected)
+    {
+      ap.av.getCodingComplement().setFont(oldComplementFont, true);
+    }
+    changeFont();
   }
 
   /**
@@ -210,21 +245,25 @@ public class FontChooser extends Panel implements ItemListener
     if (ap != null)
     {
       ap.av.setScaleProteinAsCdna(oldScaleProtein);
+      ap.av.setProteinFontAsCdna(oldMirrorFont);
+
       if (ap.av.getCodingComplement() != null)
       {
         ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
-        ap.av.getCodingComplement().setFont(oldFont, true);
-        ap.alignFrame.getSplitFrame().repaint();
+        ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
+        ap.av.getCodingComplement().setFont(oldComplementFont, true);
+        SplitFrame splitFrame = ap.alignFrame.getSplitFrame();
+        splitFrame.adjustLayout();
+        splitFrame.getComplement(ap.alignFrame).alignPanel.fontChanged();
+        splitFrame.repaint();
       }
 
       ap.av.setFont(oldFont, true);
-      ViewStyleI style = ap.av.getViewStyle();
-      if (style.getCharWidth() != oldCharWidth)
+      if (ap.av.getCharWidth() != oldCharWidth)
       {
-        style.setCharWidth(oldCharWidth);
-        ap.av.setViewStyle(style);
+        ap.av.setCharWidth(oldCharWidth);
       }
-      ap.paintAlignment(true);
+      ap.paintAlignment(true, false);
     }
     else if (tp != null)
     {
@@ -253,11 +292,11 @@ public class FontChooser extends Panel implements ItemListener
     }
 
     Font newFont = new Font(fontName.getSelectedItem().toString(),
-            fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
-                    .getSelectedItem().toString()));
+            fontStyle.getSelectedIndex(),
+            Integer.parseInt(fontSize.getSelectedItem().toString()));
     FontMetrics fm = getGraphics().getFontMetrics(newFont);
-    double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
-            .getStringBounds("I", getGraphics()).getWidth();
+    double mw = fm.getStringBounds("M", getGraphics()).getWidth(),
+            iw = fm.getStringBounds("I", getGraphics()).getWidth();
     if (mw < 1 || iw < 1)
     {
       // TODO: JAL-1100
@@ -288,11 +327,15 @@ public class FontChooser extends Panel implements ItemListener
       /*
        * and change font in other half of split frame if any
        */
-      if (inSplitFrame && fontAsCdna.getState())
+      if (inSplitFrame)
       {
-        ap.av.getCodingComplement().setFont(newFont, true);
+        if (fontAsCdna.getState())
+        {
+          ap.av.getCodingComplement().setFont(newFont, true);
+        }
         SplitFrame splitFrame = ap.alignFrame.getSplitFrame();
         splitFrame.adjustLayout();
+        splitFrame.getComplement(ap.alignFrame).alignPanel.fontChanged();
         splitFrame.repaint();
       }
     }
@@ -363,7 +406,8 @@ public class FontChooser extends Panel implements ItemListener
 
     fontAsCdna.setLabel(MessageManager.getString("label.font_as_cdna"));
     fontAsCdna.setFont(VERDANA_11PT);
-    fontAsCdna.setState(true);
+    fontAsCdna.addItemListener(this);
+    fontAsCdna.setState(ap.av.isProteinFontAsCdna());
 
     ok.setFont(VERDANA_11PT);
     ok.setLabel(MessageManager.getString("action.ok"));
@@ -425,6 +469,8 @@ public class FontChooser extends Panel implements ItemListener
     if (ap.alignFrame.getSplitFrame() != null)
     {
       inSplitFrame = true;
+      oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
+              .getFont();
       this.add(scalePanel, BorderLayout.CENTER);
     }
     this.add(okCancelPanel, BorderLayout.SOUTH);
@@ -437,11 +483,9 @@ public class FontChooser extends Panel implements ItemListener
   protected void scaleAsCdna_actionPerformed()
   {
     ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
-    ap.av.getCodingComplement().setScaleProteinAsCdna(
-            scaleAsCdna.getState());
-    ap.alignFrame.getSplitFrame().adjustLayout();
-    ap.paintAlignment(true);
-    ap.alignFrame.getSplitFrame().repaint();
+    ap.av.getCodingComplement()
+            .setScaleProteinAsCdna(scaleAsCdna.getState());
+    changeFont();
   }
 
 }