Package net.laubenberger.tyr.module.test

Source Code of net.laubenberger.tyr.module.test.DialogDb$ActionClose

/*
* Copyright (c) 2011 by Stefan Laubenberger.
*
* Test is free software: you can redistribute it and/or modify
* it under the terms of the General Public License v2.0.
*
* Test is distributed together with Tyr in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details:
* <http://www.gnu.org/licenses>
*
* This distribution is available at:
* <http://code.google.com/p/tyr/>
* <http://dev.laubenberger.net/tyr/>
*
* Contact information:
* Stefan Laubenberger
* Bullingerstrasse 53
* CH-8004 Zuerich
*
* <http://www.laubenberger.net>
*
* <laubenberger@gmail.com>
*/
package net.laubenberger.tyr.module.test;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import net.laubenberger.bogatyr.helper.HelperLog;
import net.laubenberger.bogatyr.helper.HelperMap;
import net.laubenberger.bogatyr.helper.HelperString;
import net.laubenberger.bogatyr.service.localizer.Localizer;
import net.laubenberger.bogatyr.view.swing.ActionAbstract;
import net.laubenberger.bogatyr.view.swing.Button;
import net.laubenberger.bogatyr.view.swing.Dialog;
import net.laubenberger.bogatyr.view.swing.Group;
import net.laubenberger.bogatyr.view.swing.Panel;
import net.laubenberger.bogatyr.view.swing.Table;
import net.laubenberger.bogatyr.view.swing.factory.FormatFactory;
import net.laubenberger.bogatyr.view.swing.pane.PaneScroll;
import net.laubenberger.tyr.model.Icon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Dialog for the db entries.
*
* @author Stefan Laubenberger
* @version 0.9.0 (20110214)
* @since 0.0.1
*/
public class DialogDb extends Dialog {
  private static final long serialVersionUID = -963238792665511943L;

  private static final Logger log = LoggerFactory.getLogger(DialogDb.class);

  private static final String TABLE = "RUNCOUNTER"; //$NON-NLS-1$

  final Test test;
  final Localizer localizer;

  private final Map<Integer, String> entries = new HashMap<Integer, String>();

  public DialogDb(final Test test) {
    super(test.getOwner());
    if (log.isTraceEnabled()) log.trace(HelperLog.constructor(test));

    this.test = test;
    localizer = test.getModel().getLocalizer();

    setupDb();
  }

  {
    // setAlwaysOnTop(true);
    // setResizable(false);
    // setMinimumSize(new Dimension(300, 150));
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  }

  /*
   * Private methods
   */

  private void setupDb() {
    PreparedStatement stmt;

    String statement = "SELECT  count(*) " + "FROM information_schema.system_tables " //$NON-NLS-1$ //$NON-NLS-2$
        + "WHERE table_schem = 'PUBLIC' AND table_name = '" + TABLE + "'"; //$NON-NLS-1$ //$NON-NLS-2$

    try {
      stmt = test.getModuleDb().getConnection().prepareStatement(statement);

      final ResultSet rs = stmt.executeQuery();

      rs.next();
      if (0 == rs.getInt(1)) {
        statement = "CREATE CACHED TABLE " + TABLE + " (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY," + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            // "text VARCHAR(255) NOT NULL," +
            "created TIMESTAMP NOT NULL)"; //$NON-NLS-1$

        try {
          test.getModuleDb().execute(statement);
        } catch (Exception ex) {
          log.error("Could not create db", ex); //$NON-NLS-1$
        }
      }
    } catch (SQLException ex) {
      log.error("Could not select data from db", ex); //$NON-NLS-1$
    } catch (Exception ex) {
      log.error("Could not connect to db", ex); //$NON-NLS-1$
    }

    statement = "INSERT INTO " + TABLE + " VALUES (null, CURRENT_TIMESTAMP)"; //$NON-NLS-1$ //$NON-NLS-2$

    try {
      test.getModuleDb().execute(statement);
    } catch (Exception ex) {
      log.error("Could not insert data into db", ex); //$NON-NLS-1$
    }

    final DateFormat df = FormatFactory.createDateFormat(FormatFactory.PATTERN_DATE_ISO8601);

    statement = "SELECT * FROM " + TABLE; //$NON-NLS-1$
    try {
      stmt = test.getModuleDb().getConnection().prepareStatement(statement);

      final ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        entries.put(rs.getInt(1), df.format(rs.getTimestamp(2)));
      }
    } catch (SQLException ex) {
      log.error("Could not select data from db", ex); //$NON-NLS-1$
    } catch (Exception ex) {
      log.error("Could not connect to db", ex); //$NON-NLS-1$
    }

    // String statement = "UPDATE " + TABLE_ADVISORY +
    // " SET start = '1900-01-01' WHERE start = '0000-00-00'";
    // executeUpdate(statement);
  }

  private void createLayout() {
    getContentPane().removeAll();

    setTitle(test.getModel().getName() + HelperString.COLON + HelperString.SPACE
        + localizer.getValue(HelperResource.RES_ACTION_DATA));

    final Panel panel = new Panel(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));

    final JTable table = new Table(HelperMap.toArray(entries), new String[] { "id", "created" }) { //$NON-NLS-1$//$NON-NLS-2$
      private static final long serialVersionUID = 1327561378424760584L;

      @Override
      public boolean isCellEditable(final int rowIndex, final int colIndex) {
        return false;
      }
    };

    // final JTextArea ta = new TextArea();
    // ta.setEditable(false);
    // ta.append(entries);
    final JScrollPane scroll = new PaneScroll(table);

    panel.add(scroll, BorderLayout.CENTER);

    final Group group = new Group(new Insets(0, 0, 0, 0), new Button(new ActionClose()));
    panel.add(group, BorderLayout.SOUTH);

    getContentPane().add(panel);
//    pack();
  }

  /*
   * Overridden methods
   */

  @Override
  public void createAndShowGUI() {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart());
   
    createLayout();
    pack();

    test.getDialogs().add(this);

    super.createAndShowGUI();
 
    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void clearAndHide() {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart());
   
    test.getDialogs().remove(this);

    super.clearAndHide();
 
    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  /*
   * Inner classes
   */

  private class ActionClose extends ActionAbstract {
    private static final long serialVersionUID = 3521248435384827073L;

    public ActionClose() {
      super(localizer.getValue(HelperResource.RES_ACTION_CLOSE), test.getCallback().getScaledIcon(Icon.OK),
          localizer.getTooltip(HelperResource.RES_ACTION_CLOSE), localizer
              .getMnemonic(HelperResource.RES_ACTION_CLOSE));
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
      clearAndHide();
    }
  }
}
TOP

Related Classes of net.laubenberger.tyr.module.test.DialogDb$ActionClose

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.