Package com.acelet.s.watchdog

Source Code of com.acelet.s.watchdog.ChorePanel

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.s.watchdog;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import com.acelet.lib.*;
import com.acelet.s.*;
import com.acelet.s.chore.*;
import com.acelet.s.scheduler.SetJobPanel;

public class ChorePanel extends CommonPanel {
  public static int MODE_ADD = 1;
  public static int MODE_EDIT = 2;
  public static int MODE_VIEW = 3;

  int mode;

  Chore chore;
  Chore tmpChore;
  boolean changed = false;
 
  JTextField nameTF;
  JTextField clubTF;

  JTextField jobTF;
  JButton setJobButton;

  JTextField periodTF;
  JButton setPeriodButton;

  JTextField triggerTF;
  JButton setTriggerButton;

  JTextField statusTF;
  JTextField hostnameTF;

  JTextField descriptionTF;
  JTextField commentsTF;

  JTextField creatorTF;
  JTextField alarmEmailTF;
  JTextField modifiedAtTF;

  JPanel jobPanel;
  JPanel scheduleTermPanel;

  public ChorePanel(Chore chore, int mode) throws Exception {
    this.chore = chore;
    this.mode = mode;

    if (chore == null)
      this.chore = new Chore()

    tmpChore = (Chore) this.chore.clone();

    String helpTopic = "WatchdogWindows.TaskPanel";
    ShowHelp.setHelpTopic(this, helpTopic);

    fldSize = 30;
  }

  public void actionPerformed(ActionEvent event) {
    try {
      Object source = event.getSource();
      if(source == setJobButton) {
        setJob();
      } else if (source == setPeriodButton) {
        setPeriod();
      } else if (source == setTriggerButton) {
        setTrigger();
      } else
        super.actionPerformed(event);
    } catch (Throwable ex) {
      InfoBox.exhibit(this, InfoBox.OK, ex);
    }
  }

  protected boolean makeMainPanel() throws Exception {
    LoadFile loadFile = new LoadFile();
    ImageIcon browseIcon = loadFile.receiveImageIcon("_define.gif");

    JLabel nameLabel = new JLabel(Phrase.get("TX_NAME"), JLabel.RIGHT);
    JLabel clubLabel = new JLabel(Phrase.get("TX_GROUP"), JLabel.RIGHT);
    JLabel jobLabel = new JLabel(Phrase.get("TX_JOB"), JLabel.RIGHT);
    JLabel periodLabel = new JLabel(Phrase.get("TX_PERIOD"), JLabel.RIGHT);
    JLabel triggerLabel = new JLabel(Phrase.get("TX_TRIGGER"), JLabel.RIGHT);
    JLabel statusLabel = new JLabel(Phrase.get("TX_STATUS"), JLabel.RIGHT);
    JLabel hostnameLabel = new JLabel(Phrase.get("TX_DESIRED_HOST"), JLabel.RIGHT);
    JLabel descriptionLabel = new JLabel(Phrase.get("TX_DESCRIPTION"), JLabel.RIGHT);
    JLabel commentsLabel = new JLabel(Phrase.get("TX_COMMENTS"), JLabel.RIGHT);
    JLabel creatorLabel = new JLabel(Phrase.get("TX_CREATOR"), JLabel.RIGHT);
    JLabel alarmEmailLabel = new JLabel(Phrase.get("TX_ALARM_EMAIL"), JLabel.RIGHT);
    JLabel modifiedAtLabel = new JLabel(Phrase.get("TX_MODIFIED_AT"), JLabel.RIGHT);

    nameTF = new JTextField(20);
    clubTF = new JTextField(15);

    jobTF = new JTextField(fldSize);
    jobTF.setEditable(false);
    setJobButton = new JButton(Phrase.get("TX_SET_JOB"), browseIcon);
    setJobButton.addActionListener(this);
   

    periodTF = new JTextField(fldSize);
    periodTF.setEditable(false);
    setPeriodButton = new JButton(Phrase.get("TX_SET_PERIOD"), browseIcon);
    setPeriodButton.addActionListener(this);
   

    triggerTF = new JTextField(fldSize);
    triggerTF.setEditable(false);
    setTriggerButton = new JButton(Phrase.get("TX_SET_TRIGGER"), browseIcon);
    setTriggerButton.addActionListener(this);
   

    statusTF = new JTextField(10);
    statusTF.setEditable(false);

    hostnameTF = new JTextField(25);

    descriptionTF = new JTextField(fldSize);
    commentsTF = new JTextField(fldSize);

    creatorTF = new JTextField(10);
    creatorTF.setEditable(false);

    alarmEmailTF = new JTextField(10);

    modifiedAtTF = new JTextField(15);
    modifiedAtTF.setEditable(false);

    JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    namePanel.add(nameTF);
    namePanel.add(new JLabel("   "));
    namePanel.add(clubLabel);
    namePanel.add(new JLabel("   "));
    namePanel.add(clubTF);

    JPanel creatorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    creatorPanel.add(creatorTF);
    creatorPanel.add(new JLabel("   "));
    creatorPanel.add(modifiedAtLabel);
    creatorPanel.add(new JLabel(" "));
    creatorPanel.add(modifiedAtTF);

    int nn = 0;
    makeGrid(nn++, nameLabel, namePanel);
    makeGrid(nn++, jobLabel, jobTF, setJobButton);
    makeGrid(nn++, triggerLabel, triggerTF, setTriggerButton);
    makeGrid(nn++, periodLabel, periodTF, setPeriodButton);
    makeGrid(nn++, statusLabel, statusTF, GridBagConstraints.NONE);
    makeGrid(nn++, hostnameLabel, hostnameTF, GridBagConstraints.NONE);
    makeGrid(nn++, descriptionLabel, descriptionTF);
    makeGrid(nn++, commentsLabel, commentsTF);
    makeGrid(nn++, alarmEmailLabel, alarmEmailTF);
    makeGrid(nn++, creatorLabel, creatorPanel);
    return true;
  }

  String makePeriod() {
    String fromText = "";
    String toText = "";
    if (tmpChore.fromPeriod != tmpChore.NOT_SPECIFIED)
      fromText = Common.makeTimestampLikeString(tmpChore.fromPeriod);
    if (tmpChore.toPeriod != Long.MAX_VALUE)
      toText = Common.makeTimestampLikeString(tmpChore.toPeriod);
    if (fromText.length() > 0 || toText.length() > 0)
      return fromText + " - " + toText;
    else
      return "";
  }

  protected boolean prepareData() throws Exception {
    return true;
  }

  void setJob() throws Exception {
    String title = Phrase.get("TX_SET_JOB");
    SetJobPanel setJobPanel = new SetJobPanel(tmpChore);
    if (setJobPanel.getReady() == false)
      return;
    new CommonDlg(this, title, setJobPanel);

    if (setJobPanel.getAnswer() == setJobPanel.OK) {
      String jobString = new XmlingToString(tmpChore.getJob()).toString();
      jobTF.setText(jobString);
      jobTF.setSelectionStart(0);
      jobTF.setSelectionEnd(0);
    }
  }

  void setPeriod() throws Exception {
    String title = Phrase.get("TX_SET_PERIOD");
    PeriodPanel periodPanel = new PeriodPanel(tmpChore.fromPeriod, tmpChore.toPeriod);

    if (periodPanel.getReady() == false)
      return;
    new CommonDlg(this, title, periodPanel);

    if (periodPanel.getAnswer() == periodPanel.OK) {
      tmpChore.fromPeriod = periodPanel.getFrom();
      if (tmpChore.fromPeriod == 0)
        tmpChore.fromPeriod = Chore.NOT_SPECIFIED;

      tmpChore.toPeriod = periodPanel.getTo();

      String fromText = "";
      if (tmpChore.fromPeriod > 0)
        fromText = Common.makeTimestampLikeString(tmpChore.fromPeriod).trim();
      String toText = "";
      if (tmpChore.toPeriod < Long.MAX_VALUE)
        toText = Common.makeTimestampLikeString(tmpChore.toPeriod).trim();
      String text = "";
      if (fromText.length() > 0 || toText.length() > 0)
        text = fromText + " " + Phrase.get("TX_TO") + " " + toText;
      periodTF.setText(text);
      periodTF.setSelectionStart(0);
      periodTF.setSelectionEnd(0);

      if (periodPanel.getTo() <= System.currentTimeMillis())
        InfoBox.exhibit(this, Phrase.get("ER_PERIOD_IN_HISTORY"),
          Phrase.get("TX_ERROR"), InfoBox.OK, null, "Errors." + "ER_PERIOD_IN_HISTORY");
    }
  }

  void setTrigger() throws Exception {
    String title = Phrase.get("TX_SET_TRIGGER");
    SetTriggerPanel setTriggerPanel = new SetTriggerPanel(tmpChore);

    if (setTriggerPanel.getReady() == false)
      return;
    new CommonDlg(this, title, setTriggerPanel);

    if (setTriggerPanel.getAnswer() == setTriggerPanel.OK) {
      String text = new XmlingToString(tmpChore.getTriggerObject()).toString();
      triggerTF.setText(text);
      triggerTF.setSelectionStart(0);
      triggerTF.setSelectionEnd(0);
    }
  }

  protected boolean transferData(int direction) throws Exception {
    switch(direction) {
    case TRANSFER_FROM_DLG:
     

      String name = nameTF.getText().trim();
      if (name.length() == 0) {
        InfoBox.exhibit(this, Phrase.get("ER_DATA") + ": " + Phrase.get("TX_NAME"),
          Phrase.get("TX_ERROR"), InfoBox.OK, null, "Errors." + "ER_DATA");
        return false;
      }
      tmpChore.name = name;

      tmpChore.club = clubTF.getText().trim();

      if (tmpChore.getJob() == null) {
        InfoBox.exhibit(this,
          Phrase.get("ER_DATA") + ": " + Phrase.get("TX_JOB"),
          Phrase.get("TX_ERROR"),
          InfoBox.OK, null, "Errors." + "ER_DATA");
        return false;
      }

      if (tmpChore.getTriggerObject() == null) {
        InfoBox.exhibit(this,
          Phrase.get("ER_DATA") + ": " + Phrase.get("TX_TRIGGER"),
          Phrase.get("TX_ERROR"),
          InfoBox.OK, null, "Errors." + "ER_DATA");
        return false;
      }

      tmpChore.hostname = hostnameTF.getText().trim();
      tmpChore.description = descriptionTF.getText();
      tmpChore.comments = commentsTF.getText();
      tmpChore.alarmEmail = alarmEmailTF.getText().trim();
      tmpChore.lastTriggerTime = System.currentTimeMillis();  

      if (tmpChore.alarmEmail.length() > 0) {
        if (MailServerData.getMailHostname() == null) {
          InfoBox.exhibit(this, Phrase.get("ER_MAIL_SERVER_NOT_SET"),
            Phrase.get("TX_ERROR"), InfoBox.OK, null,
           "Errors." + "ER_MAIL_SERVER_NOT_SET");

          MailServerPanel mailServerPanel = new MailServerPanel();
          mailServerPanel.getReady();
          new CommonDlg(this, Phrase.get("TX_MAIL_SERVER"), mailServerPanel);
         
        }
      }

      long newModifiedAt = System.currentTimeMillis();

      if (chore.equals(tmpChore) == false) {
        changed = true;
        chore.copy(tmpChore);

        try {
          if (mode == MODE_ADD) {
            Api.insertChore(chore);
          } else {
            chore.status = Chore.STATUS_STANDBY;
            if (Api.updateChore(chore, newModifiedAt) == 0) {
              InfoBox.exhibit(this,
                Phrase.get("ER_UPDATE_FAILED_ROW_MODIFIED"),
                Phrase.get("TX_ERROR"), InfoBox.OK, null,
                "Errors." + "ER_UPDATE_FAILED_ROW_MODIFIED");
              return false;
            }
          }
        } catch (SQLException sqlException) {
          String sqlState = sqlException.getSQLState();
          if (sqlState != null && sqlState.startsWith("23")) {
            InfoBox.exhibit(this, Phrase.get("ER_NAME_IS_TAKEN"),
                Phrase.get("TX_ERROR"), InfoBox.OK, null,
                "Errors." + "ER_NAME_IS_TAKEN");
          } else {
            InfoBox.exhibit(this, InfoBox.OK, sqlException);
            return false;
          }
        }

        try {
          Watchdogging.updateTimerTask(chore);
        } catch (ClassCastException cce) {
          InfoBox.exhibit(this, Phrase.get("ER_TASK_JOB_TYPE"),
                Phrase.get("TX_ERROR"), InfoBox.OK, cce,
                "Errors." + "ER_TASK_JOB_TYPE");
          return false;
        }
      } else {
        if (chore.status == Chore.STATUS_ERROR) {
          chore.status = Chore.STATUS_STANDBY;
          Api.updateChore(chore, System.currentTimeMillis());
        }
      }
      break;
    case TRANSFER_TO_DLG:
      nameTF.setText(tmpChore.name);
      clubTF.setText(tmpChore.club);

      if (tmpChore.getJob() != null) {
        String jobString = new XmlingToString(tmpChore.getJob()).toString();
        jobTF.setText(jobString);
        jobTF.setSelectionStart(0);
        jobTF.setSelectionEnd(0);
      }

      periodTF.setText(makePeriod());
      periodTF.setSelectionStart(0);
      periodTF.setSelectionEnd(0);

      String triggerObject = tmpChore.getTriggerObject();
      if (triggerObject != null) {
        String text = new XmlingToString(tmpChore.getTriggerObject()).toString();
        triggerTF.setText(text);
        triggerTF.setSelectionStart(0);
        triggerTF.setSelectionEnd(0);
      }

      statusTF.setText(ChoreText.translateStatus(tmpChore.status));
      hostnameTF.setText(tmpChore.hostname);

      creatorTF.setText(tmpChore.getCreator());
      alarmEmailTF.setText(tmpChore.getAlarmEmail());
      descriptionTF.setText(tmpChore.description);
      commentsTF.setText(tmpChore.comments);

      modifiedAtTF.setText(new java.sql.Timestamp(tmpChore.modifiedAt).toString());
      break;
    }
    return true;
  }

}
TOP

Related Classes of com.acelet.s.watchdog.ChorePanel

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.