/*
 * Copyright (c) 2014-2018 Paul Scherrer Institute. All rights reserved.
 */

import ch.psi.pshell.bs.ProviderConfig;
import ch.psi.pshell.ui.Console;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.Config;
import ch.psi.utils.State;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;

/**
 *
 */
public class Tomaz extends Panel {

    /*
    
  public JComponent makeUI() {
    SpinnerNumberModel m = new SpinnerNumberModel(10, 0, 1000, 1);
    JSpinner spinner = new JSpinner(m) {
      @Override public void setLayout(LayoutManager mgr) {
        super.setLayout(new SpinnerLayout());
      }
    };
    JPanel p = new JPanel(new BorderLayout(5,5));
    p.add(new JSpinner(m), BorderLayout.NORTH);
    p.add(spinner, BorderLayout.SOUTH);
    p.setBorder(BorderFactory.createEmptyBorder(16,16,16,16));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new SpinnerLayoutTest().makeUI());
    f.setSize(320, 160);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
class SpinnerLayout extends BorderLayout {
  @Override public void addLayoutComponent(Component comp, Object constraints) {
    if("Editor".equals(constraints)) {
      constraints = "Center";
    } else if("Next".equals(constraints)) {
      constraints = "North";
    } else if("Previous".equals(constraints)) {
      constraints = "South";
    }
    super.addLayoutComponent(comp, constraints);
  }
}    */
    
    
 private static class HorizontalSpinnerLayoutManager implements LayoutManager
    {
        final Component bup;
        final Component bdown;
        final Component txt;
        int buttom_width;
        int buttom_height;
        HorizontalSpinnerLayoutManager(JSpinner sp)
        {
            Component[] components = sp.getComponents();
            bup = components[0];
            bdown = components[1];
            txt = components[2];
            //((javax.swing.JButton)bup).getBorder()
            buttom_width=bdown.getPreferredSize().width-3;
       }

        public void addLayoutComponent(String name, Component comp) {
        }

        public void removeLayoutComponent(Component comp) {
        }

        public Dimension preferredLayoutSize(Container parent) {
            Insets in = parent.getInsets();
            //return parent.getPreferredSize();
            return new Dimension(2*buttom_width + Math.max(txt.getPreferredSize().width,12) , //Compressing a bit+ not considering insets on width
                    txt.getPreferredSize().height  + in.top + in.bottom);
            //Insets insets = parent.getInsets();
            //insets
        }

        public Dimension minimumLayoutSize(Container parent) {
            Insets in = parent.getInsets();
            return new Dimension(2*buttom_width + txt.getMinimumSize().width ,
                    bdown.getMinimumSize().height + in.top + in.bottom);
        }

        public void layoutContainer(Container parent) {
            Insets in = parent.getInsets();
            int top=1;
            int bottom=parent.getHeight()-1;
            int left=1;
            int right=parent.getWidth()-1;

            int width=right-left;
            int height=bottom-top;

         //   if ( isStyleXP())
         //   {
         //      bdown.setBounds(left, top+1, buttom_width, height-1);
         //       bup.setBounds(right-buttom_width, top, buttom_width, height-1);
         //   }
       //     else
            {
                bdown.setBounds(left, top, buttom_width, height);
                bup.setBounds(right-buttom_width, top, buttom_width, height);
            }
            txt.setBounds( left+buttom_width, top, width- (2*buttom_width),height);
        }
    }

    public static boolean setHorizontalSpinner(javax.swing.JSpinner sp)
    {
         if (sp.getLayout() instanceof HorizontalSpinnerLayoutManager)
             return true;
         if (sp.getComponents().length!=3)
             return false;
         sp.setLayout(new HorizontalSpinnerLayoutManager(sp));

         //final Component bup = components[0];
         //final Component bdown = components[1];
         //final Component txt = components[2];

         //sp.setComponentZOrder(bdown,0);
         //sp.setComponentZOrder(txt,1);
         //sp.setComponentZOrder(bup,2);
         //javax.swing.BoxLayout layout =  new javax.swing.BoxLayout(sp, javax.swing.BoxLayout.LINE_AXIS);

         /*
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(sp);
        layout.setAutoCreateGaps(false);
        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addComponent(bdown, javax.swing.GroupLayout.PREFERRED_SIZE, bup.getPreferredSize().width, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, txt.getPreferredSize().width, Short.MAX_VALUE)
                .addComponent(bup, javax.swing.GroupLayout.PREFERRED_SIZE, bup.getPreferredSize().width, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(bdown, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        sp.setLayout(layout);
        */
        
        return true;
      }

       public static void setHorizontalSpinners(java.awt.Container container)
       {
            for (java.awt.Component c:container.getComponents())
            {
                if (c instanceof javax.swing.JSpinner)
                    setHorizontalSpinner((javax.swing.JSpinner)c);
                else if (c instanceof java.awt.Container)
                    setHorizontalSpinners((java.awt.Container)c);
            }
        }
    
    
    final Console console;

    public Tomaz() {
        initComponents();
        console = new Console() {
            protected void onConsoleCommand(String name, String[] pars, String trimming) throws Exception {
                switch (name) {
                    case "test":
                        System.out.println("OK");
                        break;
                }
            }
        };
        setHorizontalSpinners(this);
        setHorizontalSpinner(spinner);
    }

    @Override
    public void onStart() {
        super.onStart();
        console.start();
    }

    @Override
    public void onStop() {
        super.onStart();
        console.stop();
    }

    //Overridable callbacks
    @Override
    public void onInitialize(int runCount) {

    }

    @Override
    public void onStateChange(State state, State former) {

    }

    @Override
    public void onExecutedFile(String fileName, Object result) {
    }

    //Callback to perform update - in event thread
    @Override
    protected void doUpdate() {
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        spinner = new javax.swing.JSpinner();

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSpinner1.setModel(new javax.swing.SpinnerDateModel());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jButton1)
                        .addGap(117, 117, 117)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //showSettingsEditor(false);
        //showSettingsEditor(true);
        Config cfg = new ProviderConfig();
        try {
            cfg.load("~/test.cfg");
        } catch (IOException ex) {
            Logger.getLogger(Tomaz.class.getName()).log(Level.SEVERE, null, ex);
        }
        showConfigEditor(cfg, true);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JSpinner spinner;
    // End of variables declaration//GEN-END:variables
}
