Package com.ronald.gantengtimesheet.ui.config

Source Code of com.ronald.gantengtimesheet.ui.config.ConfigurationComponent

package com.ronald.gantengtimesheet.ui.config;

import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.GroupLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;

import com.ronald.gantengtimesheet.db.ConfigEntity;
import com.ronald.gantengtimesheet.db.ConfigEntityCollection;
import com.ronald.gantengtimesheet.ui.util.NumberDocument;

@SuppressWarnings("serial")
public class ConfigurationComponent extends JXPanel {
  private static ConfigurationComponent instance;
    public static synchronized ConfigurationComponent getInstance() {
      if (instance == null) {
        instance = new ConfigurationComponent();
      }
      return instance;
    }
    private ConfigurationComponent() {
      setName("Configuration");
      addComponents();
      addListeners();
    }
   
    JXLabel lblDefaultRate = new JXLabel("Default Rate");
    JTextField txtDefaultRate = new JTextField(15);
    JXLabel lblDefaultReportDuration = new JXLabel("Default Report Duration");
    JTextField txtDefaultReportDuration = new JTextField(15);
    JXLabel lblFrom = new JXLabel("From");
    JTextArea txtFrom = new JTextArea(7,45);
    JScrollPane fromPane = new JScrollPane(txtFrom);
    JXLabel lblTo = new JXLabel("To");
    JTextArea txtTo = new JTextArea(7,45);
    JScrollPane toPane = new JScrollPane(txtTo);
    JXLabel lblNotes = new JXLabel("Notes");
    JTextArea txtNotes = new JTextArea(4,45);
    JScrollPane notesPane = new JScrollPane(txtNotes);

    {
      txtDefaultRate.setName(ConfigEntityCollection.Config.DEFAULT_RATE.name);
      txtDefaultRate.setDocument(new NumberDocument(false, Double.class));

      txtDefaultReportDuration.setName(ConfigEntityCollection.Config.DEFAULT_REPORT_DURATION.name);
      txtDefaultReportDuration.setDocument(new NumberDocument(false, Integer.class));
     
      txtFrom.setName(ConfigEntityCollection.Config.FROM.name);
      txtTo.setName(ConfigEntityCollection.Config.TO.name);
      txtNotes.setName(ConfigEntityCollection.Config.NOTES.name);
    }
   
    private void addComponents() {
      setLayout(new FlowLayout(FlowLayout.LEFT));
      final JXPanel panel = new JXPanel();
      final GroupLayout groupLayout = new GroupLayout(panel);
      groupLayout.setAutoCreateGaps(true);
      groupLayout.setHorizontalGroup(
              groupLayout.createSequentialGroup()
              .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                      .addComponent(lblDefaultRate)
                      .addComponent(lblDefaultReportDuration)
                      .addComponent(lblFrom)
                      .addComponent(lblTo)
                      .addComponent(lblNotes))
              .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                      .addComponent(txtDefaultRate)
                      .addComponent(txtDefaultReportDuration)
                      .addComponent(fromPane)
                      .addComponent(toPane)
                      .addComponent(notesPane))
      );
     
      groupLayout.setVerticalGroup(
              groupLayout.createSequentialGroup()
                      .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                              .addComponent(lblDefaultRate).addComponent(txtDefaultRate))
                      .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                              .addComponent(lblDefaultReportDuration).addComponent(txtDefaultReportDuration))
                      .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                              .addComponent(lblFrom).addComponent(fromPane))
                      .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                              .addComponent(lblTo).addComponent(toPane))
                        .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNotes).addComponent(notesPane)));
     
      panel.setLayout(groupLayout);
      add(panel);
     
      // get value from config
      final ConfigEntityCollection config = ConfigEntityCollection.getInstance();
      txtDefaultRate.setText(config.getConfigEntity(txtDefaultRate.getName()).getValue());
      txtDefaultReportDuration.setText(config.getConfigEntity(txtDefaultReportDuration.getName()).getValue());
      txtFrom.setText(config.getConfigEntity(txtFrom.getName()).getValue());
      txtTo.setText(config.getConfigEntity(txtTo.getName()).getValue());
      txtNotes.setText(config.getConfigEntity(txtNotes.getName()).getValue());
    }
  private void addListeners() {
    txtDefaultRate.addFocusListener(focusAdapter);
    txtDefaultReportDuration.addFocusListener(focusAdapter);
    txtFrom.addFocusListener(focusAdapter);
    txtTo.addFocusListener(focusAdapter);
    txtNotes.addFocusListener(focusAdapter);
    }
  private final FocusListener focusAdapter = new FocusListener() {
    @Override
        public void focusLost(final FocusEvent e) {
      if (!(e.getSource() instanceof JTextComponent)) {
        return;
      }
      final JTextComponent src = (JTextComponent) e.getSource();
      final ConfigEntity ce = ConfigEntityCollection.getInstance().getConfigEntity(src.getName());
      if (ce != null ) {
        ce.setValue(src.getText());
        ce.save();
      }
        }

    @Override
        public void focusGained(final FocusEvent e) {
      final JTextComponent src = (JTextComponent) e.getSource();
      src.selectAll();
        }
   
  };
}
TOP

Related Classes of com.ronald.gantengtimesheet.ui.config.ConfigurationComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.