package eg;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//--Eadgyth--//
import eg.ui.MainWin;
import eg.ui.ViewSettingWin;

/**
 * The view settings in the main window.
 */
public class ViewSetter {

   private final MainWin mw;
   private final ViewSettingWin vsw;
   private final Formatter f;
   private final Prefs prefs = new Prefs();

   private boolean isShowLineNumbers;
   private boolean isShowToolbar;
   private boolean isShowStatusbar;
   private int iconSizeIndex;
   private int lafIndex;

   /**
    * @param mw  the reference to <code>MainWin</code>
    * @param vsw  the reference to <code>ViewSettingWin</code>
    * @param f  the reference to {@link Formatter}
    */
   public ViewSetter(MainWin mw, ViewSettingWin vsw, Formatter f) {
      this.mw = mw;
      this.vsw = vsw;
      this.f = f;
      initSettings();
      vsw.setCancelAct(e -> undoSettings());   
      vsw.setDefaultCloseAction(DefaultClosing);
   }

   /**
    * Applies the selections made in {@link ViewSettingWin} and sets the
    * corresponding properties in <code>Prefs</code>
    */
   public void applySettings() {
      boolean show;
      String state;
      int index;
      show = vsw.isShowLineNumbers();
      if (show != isShowLineNumbers) {
         f.showLineNumbers(show);
         isShowLineNumbers = show;
         state = show ? "show" : "hide";
         prefs.setProperty("LineNumbers", state);
      }    
      show = vsw.isShowToolbar();
      if (show != isShowToolbar) {
         mw.showToolbar(show);
         isShowToolbar = show; 
         state = isShowToolbar ? "show" : "hide";
         prefs.setProperty("Toolbar", state);
      }
      show = vsw.isShowStatusbar();
      if (show != isShowStatusbar) {
         mw.showStatusbar(show);
         isShowStatusbar = show;
         state = isShowStatusbar ? "show" : "hide";
         prefs.setProperty("Statusbar", state);
      }
      index = vsw.iconSizeIndex();
      if (index != iconSizeIndex) {
         iconSizeIndex = index;
         prefs.setProperty("IconSize",
               ViewSettingWin.ICON_SIZES[iconSizeIndex]);
      }
      index = vsw.lafIndex();
      if (index != lafIndex) {
         lafIndex = index;
         prefs.setProperty("LaF", ViewSettingWin.LAF_OPT[lafIndex]);
      }
   }
   
   private void initSettings() {
      isShowLineNumbers = "show".equals(prefs.getProperty("LineNumbers"));
      vsw.setShowLineNumbers(isShowLineNumbers);
      f.showLineNumbers(isShowLineNumbers);
      
      isShowToolbar =  "show".equals(prefs.getProperty("Toolbar"));
      vsw.setShowToolbar(isShowToolbar);
      mw.showToolbar(isShowToolbar);
      
      isShowStatusbar =  "show".equals(prefs.getProperty("Statusbar"));
      vsw.setShowStatusbar(isShowStatusbar);
      mw.showStatusbar(isShowStatusbar);
      
      vsw.setIconSize(prefs.getProperty("IconSize"));
      iconSizeIndex = vsw.iconSizeIndex();
      
      String laf = prefs.getProperty("LaF");
      if (laf.length() > 0) {
         vsw.setLaf(laf);
      }
      else {
         vsw.setLaf(ViewSettingWin.LAF_OPT[1]);
      }      
      lafIndex = vsw.lafIndex();
   }
   
   private void undoSettings() {
      vsw.setShowLineNumbers(isShowLineNumbers);
      vsw.setShowToolbar(isShowToolbar);
      vsw.setShowStatusbar(isShowStatusbar);
      vsw.setIconSize(iconSizeIndex);
      vsw.setLaf(lafIndex);
      vsw.setVisible(false);
   }      
   
   private final WindowAdapter DefaultClosing = new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent we) {
         undoSettings();
      }
   };
}

@@ -468,24 +468,56 @@
 wStatusbar;%0A
+   private int backgroundIndex;%0A
    private i
@@ -530,24 +530,24 @@
 nSizeIndex;%0A
-
    private i
@@ -937,19 +937,16 @@
 ings());
-   
 %0A      v
@@ -1467,20 +1467,16 @@
 %0A      %7D
-    
 %0A      s
@@ -1598,17 +1598,16 @@
  = show;
- 
 %0A       
@@ -1942,32 +1942,247 @@
 state);%0A      %7D%0A
+      index = vsw.backgroundIndex();%0A      if (index != backgroundIndex) %7B%0A         backgroundIndex = index;%0A         prefs.setProperty(%22Background%22,%0A             ViewSettingWin.BKGRD_OPT%5BbackgroundIndex%5D);%0A      %7D%0A
       index = vs
@@ -2539,28 +2539,25 @@
      %7D%0A   %7D%0A
-   
 %0A
+
    private v
@@ -2745,23 +2745,17 @@
 mbers);%0A
-      
 %0A
+
       is
@@ -2888,31 +2888,25 @@
 owToolbar);%0A
-      
 %0A
+
       isShow
@@ -3051,23 +3051,17 @@
 usbar);%0A
-      
 %0A
+
       vs
@@ -3149,23 +3149,17 @@
 ndex();%0A
-      
 %0A
+
       St
@@ -3323,30 +3323,24 @@
 1%5D);%0A      %7D
-      
 %0A      lafIn
@@ -3364,18 +3364,270 @@
 x();
+%0A%0A      String bkgrd = prefs.getProperty(%22Background%22);%0A      if (bkgrd.length() %3E 0) %7B%0A         vsw.setBackground(bkgrd);%0A      %7D%0A      else %7B%0A         vsw.setBackground(ViewSettingWin.BKGRD_OPT%5B0%5D);%0A      %7D%0A      backgroundIndex = vsw.backgroundIndex();
 %0A   %7D%0A
-   
 %0A
+
    p
@@ -3786,24 +3786,66 @@
 Statusbar);%0A
+      vsw.setBackground(backgroundIndex);%0A
       vsw.se
@@ -3935,18 +3935,9 @@
    %7D
-      %0A   
+%0A
 %0A   

make changes because of the setting of the background theme