Package trackerModule.sim.map

Source Code of trackerModule.sim.map.MapTablePane

package trackerModule.sim.map;

import groundTruthModule.datastructure.DataBase;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.AbstractTableModel;

import trackerModule.core.datastructure.TDB;
import trackerModule.core.datastructure.Unit;
import trackerModule.core.rulestructure.IMessageObject;
import trackerModule.core.rulestructure.RDB;
import trackerModule.sim.tracker.World;
/**
* The Class MapTablePane.
*/
public class MapTablePane extends JPanel implements IMessageObject {

  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 7882225041908399387L;

  /** The data model w. */
  AbstractTableModel dataModelW;
 
  /** The data model e. */
  AbstractTableModel dataModelE;
 
  /** The cur unit. */
  Unit curUnit = null;
 
  /** The selected unit. */
  public Unit selUnit = null;
 
  /** The parent dialog. */
  MapFrame parentDlg;
 
  /** The query button. */
  private JButton queryButton;
 
  /** The query button mt. */
  private JButton queryButtonMT;
 
  /** The query button ws. */
  private JButton queryButtonWS;
 
  /** The lowered border. */
  public Border loweredBorder = new CompoundBorder(new SoftBevelBorder(
      SoftBevelBorder.LOWERED), new EmptyBorder(5, 5, 5, 5));
 
  /** The Preferred size_ w. */
  int PreferredSize_W = 950;
 
  /** The Preferred size_ h. */
  int PreferredSize_H = 50;

  /** The batch query button. */
  private JButton batchQueryButton;
 
  /** The is to save world by time. */
  private boolean isToSaveWorldByTime = false;
 
  /** The times to save world. */
  private int timesToSaveWorld[] = {5,30,50};
 
  /**
   * Instantiates a new map table pane.
   *
   * @param p the Map Frame
   */
  public MapTablePane(MapFrame p) {
    super();
    parentDlg = p;
    RDB.This().addMessageObjectForTime(this);

    JPanel p1 = createVerticalPanel(false);
    JPanel p2 = createVerticalPanel(false);
    JPanel p3 = this.createHorizontalPanel(false);
   
    JComponent j1 = createWorldTable();
    JComponent j2 = createEntityTable();

    p2.add(j1, BorderLayout.NORTH);
    p2.add(j2, BorderLayout.SOUTH)
       
     JButton j3 = createQueryButton();
     JButton j4 = createQueryButtonMT();
     JButton j5 = createQueryButtonWS();
     JButton j6 = createQueryButtonBatchArea();
     p3.add(j3);
     p3.add(j4);
     p3.add(j5);
     p3.add(j6);
    
    
    p1.add(p3, BorderLayout.NORTH);
    p1.add(p2, BorderLayout.SOUTH);
   
    add(p1);
  }

 

  /**
   * Creates the world table.
   *
   * @return the java component
   */
  public JComponent createWorldTable(){
   
    // Create a model of the data.
    dataModelW = new AbstractTableModel() {

      public int getColumnCount() {
        return 6;
      }

      public int getRowCount() {
        return 1;
      }

      public Object getValueAt(int row, int col) {
        if( row == 0 && col == 0 ){
          if (isToSaveWorldByTime) {
            saveWorldByTime();
          }
          return RDB.This().getRealTime();
        }else
        if( row == 0 && col == 1 ){
          return World.This().NumberOfVisiableFishingShips;
        }else
        if( row == 0 && col == 2 ){
          return World.This().NumberOfVisiableMerchantShips;
        }
       
        return null;
      }

      public String getColumnName(int col) {
        if(col == 0 ){
          return "Time";
        }else
        if(col == 1 ){
          return "NumberOfFishingShips";
        }else
        if(col == 2 ){
          return "NumberOfMerchantShips";
        }
       
        return "";
      }
    };

    // Create the table
    JTable tableView;
    JScrollPane scrollPane;
    tableView = new JTable(dataModelW);
    tableView.setRowHeight(16);

    scrollPane = new JScrollPane(tableView);
    scrollPane.setPreferredSize(new Dimension( PreferredSize_W, PreferredSize_H));
    return scrollPane;
  }
 
  /**
   * Creates the entity table.
   *
   * @return the java component
   */
  public JComponent createEntityTable(){
 
    // Create a model of the data.
    dataModelE = new AbstractTableModel() {
      public int getColumnCount() {
        if (selUnit != null) {
          return 15;// hide some information //selUnit.list.size();
        } else
          return 0;
      }

      public int getRowCount() {
        if (selUnit != null)
          return 1;
        else
          return 0;
      }

      public Object getValueAt(int row, int col) {
        if (selUnit != null) {
          return selUnit.list.get(col).getDataByObject();
        } else {
          // curUnit.getRow(row).getName();
        }

        return null;
      }

      public String getColumnName(int col) {
        if (selUnit != null) {
          return selUnit.list.get(col).getName();
        }
        return "";
      }

      public boolean isCellEditable(int row, int col) {
        return col != 5;
      }

      public void setValueAt(Object aValue, int row, int col) {
        // curUnit.set(row, col, (String)aValue);
      }
    };

    // Create the table
    JTable tableView;
    JScrollPane scrollPane;
    tableView = new JTable(dataModelE);
    tableView.setRowHeight(16);

    scrollPane = new JScrollPane(tableView);
    scrollPane.setPreferredSize(new Dimension(PreferredSize_W, PreferredSize_H));
    return scrollPane;
  }

  /**
   * Creates the query button.
   *
   * @return the java button
   */
  private JButton createQueryButton() {
    queryButton = new JButton("Query");
    queryButton.putClientProperty("Local Query", Boolean.TRUE);
    return queryButton;
  }
 
  /**
   * Creates the query button mt.
   *
   * @return the java button
   */
  private JButton createQueryButtonMT() {
    queryButtonMT = new JButton("Area Query");
    queryButtonMT.putClientProperty("Area Query", Boolean.TRUE);
    return queryButtonMT;
  }
 
  /**
   * Creates the query button batch area.
   *
   * @return the java button
   */
  private JButton createQueryButtonBatchArea() {
    batchQueryButton = new JButton("Batch Area Query");
     batchQueryButton.setToolTipText("It will do an area query for all available ships in batch.");
     batchQueryButton.putClientProperty("Batch Area Query", Boolean.TRUE);
     return batchQueryButton;
  }
 
  /**
   * Creates the query button ws.
   *
   * @return the java button
   */
  private JButton createQueryButtonWS() {
    queryButtonWS = new JButton("Web Server Query");
    queryButtonWS.putClientProperty("Web Server Query", Boolean.TRUE);
    return queryButtonWS;
  }
 
  /**
   * Adds the query button action listener.
   *
   * @param al the Action Listener
   */
  public void addQueryButtonActionListener(ActionListener al) {
    queryButton.addActionListener(al);
  }
   
  /**
   * Adds the query button mt action listener.
   *
   * @param al the Action Listener
   */
  public void addQueryButtonMTActionListener(ActionListener al) {
    queryButtonMT.addActionListener(al);
  }
 
  /**
   * Adds the query button ws action listener.
   *
   * @param al the Action Listener
   */
  public void addQueryButtonWSActionListener(ActionListener al) {
    queryButtonWS.addActionListener(al);
  }

  /**
   * Initialize the table.
   *
   * @param tUnitP the current unit
   * @param tUnitC the selected unit
   */
  public void initTable(Unit tUnitP, Unit tUnitC) {
    curUnit = tUnitP;
    selUnit = tUnitC;
    dataModelW.fireTableStructureChanged();
    dataModelE.fireTableStructureChanged();
  }

  /* (non-Javadoc)
   * @see trackerModule.core.rulestructure.IMessageObject#onUpdated(java.lang.Object)
   */
  public void onUpdated(Object o) {
    initTable(curUnit, selUnit);

  }

  // GUI
  /**
   * Creates the button.
   *
   * @return the java button
   */
  public JButton createButton() {
    JButton b = new JButton();

    b.putClientProperty("displayActionText", Boolean.TRUE);

    return b;
  }

  /**
   * Creates the button.
   *
   * @param a the action
   * @return the java button
   */
  public JButton createButton(Action a) {
    JButton b = new JButton();

    b.putClientProperty("displayActionText", Boolean.TRUE);
    b.setAction(a);

    return b;
  }

  /**
   * Creates the radio button.
   *
   * @param a the action
   * @return the java radio button
   */
  public JRadioButton createRadioButton(Action a) {
    JRadioButton b = new JRadioButton();

    b.putClientProperty("displayActionText", Boolean.TRUE);
    b.setAction(a);

    return b;
  }

  /**
   * Creates the horizontal panel.
   *
   * @param threeD the three
   * @return the java panel
   */
  public JPanel createHorizontalPanel(boolean threeD) {
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.setAlignmentY(TOP_ALIGNMENT);
    p.setAlignmentX(LEFT_ALIGNMENT);
    if (threeD) {
      p.setBorder(loweredBorder);
    }
    return p;
  }

  /**
   * Creates the vertical panel.
   *
   * @param threeD the three 
   * @return the java panel
   */
  public JPanel createVerticalPanel(boolean threeD) {
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    p.setAlignmentY(TOP_ALIGNMENT);
    p.setAlignmentX(LEFT_ALIGNMENT);
    if (threeD) {
      p.setBorder(loweredBorder);
    }
    return p;
  }



  /**
   * Gets the batch query button.
   *
   * @return the batchQueryButton
   */
  public JButton getBatchQueryButton() {
    return batchQueryButton;
  }



  /**
   * Sets the batch query button.
   *
   * @param batchQueryButton the batchQueryButton to set
   */
  public void setBatchQueryButton(JButton batchQueryButton) {
    this.batchQueryButton = batchQueryButton;
  }
 
  /**
   * This is called when {@link #isToSaveWorldByTime()} is true
   * and the timestamps declared in {@link #getTimesToSaveWorld()}
   * are reached.
   */
  protected void saveWorldByTime() {
    // variable to check whether it is to save now, and if so, what time is it now (in hours)
    int momentToSave = -1;    // negative means false (not to save now)
    for (int time : getTimesToSaveWorld()) {
      if ((RDB.This().getTime()/60 == time// hour matches
          && (RDB.This().getTime()%60 == 0)){  // minutes is zero (exact hour match)
        momentToSave = time;
        break;
      }
    }
    if (momentToSave > 0) {
      // pause world
      RDB.This().stop();
     
      String strfile = DataBase.getInstance().getShips().size() + "Ships"
        + DataBase.getInstance().getOrganizations().size() + "Orgs"
        + DataBase.getInstance().getPeople().size() + "Persons"
        + "Time" + momentToSave;
     
      World.This().save(strfile+".map");
      System.out.println("Saved! [map]: " + strfile+".map");
      DataBase.getInstance().save(strfile+".sdb")
      System.out.println("Saved! [sdb]: " + strfile+".sdb");
      TDB.This().save(strfile+".tdb");  
      System.out.println("Saved! [tdb]: " + strfile+".tdb");
      RDB.This().save(strfile+".rdb");   
      System.out.println("Saved! [rdb]: " + strfile+".rdb");
     
      System.gc();
      RDB.This().start();
    }
  }



  /**
   * If true, {@link #saveWorldByTime()} will be called when
   * timestamps declared in {@link #getTimesToSaveWorld()} are
   * reached.
   * @return the isToSaveWorldByTime
   */
  public boolean isToSaveWorldByTime() {
    return isToSaveWorldByTime;
  }



  /**
   * If true, {@link #saveWorldByTime()} will be called when
   * timestamps declared in {@link #getTimesToSaveWorld()} are
   * reached.
   * @param isToSaveWorldByTime the isToSaveWorldByTime to set
   */
  public void setToSaveWorldByTime(boolean isToSaveWorldByTime) {
    this.isToSaveWorldByTime = isToSaveWorldByTime;
  }



  /**
   * Indicates when {@link #saveWorldByTime()} will be called.
   * This value represents the hour in the simulated world.
   * That is, {@link RDB#getTime()} / 60.
   * @return the timesToSaveWorld
   */
  public int[] getTimesToSaveWorld() {
    return timesToSaveWorld;
  }



  /**
   * Indicates when {@link #saveWorldByTime()} will be called.
   * This value represents the hour in the simulated world.
   * That is, {@link RDB#getTime()} / 60.
   * @param timesToSaveWorld the timesToSaveWorld to set
   */
  public void setTimesToSaveWorld(int[] timesToSaveWorld) {
    this.timesToSaveWorld = timesToSaveWorld;
  }
}
TOP

Related Classes of trackerModule.sim.map.MapTablePane

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.