/* 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.event.*;
import com.acelet.lib.*;
import com.acelet.s.*;
import com.acelet.s.chore.*;
import com.acelet.s.scheduler.ColorCellRenderer;
public class HistoryTablePanel extends TablePanel {
public static File exportDir = new File(System.getProperty("user.dir"));
long from;
long to;
Vector workingChoreVector;
Vector theTableDataVector;
JLabel periodLabel;
JButton exportButton;
JButton previewButton;
JButton detailButton;
JButton closeButton;
public HistoryTablePanel(long from, long to) throws Exception {
super(800, 300);
this.from = from;
this.to = to;
helpTopic = "WatchdogWindows.HistoryTablePanel";
ShowHelp.setHelpTopic(this, helpTopic);
}
public void actionPerformed(ActionEvent event) {
try {
Object source = event.getSource();
if (source == closeButton) {
close();
} else if (source == exportButton) {
export();
} else if (source == previewButton) {
preview();
} else if (source == detailButton) {
detail();
} else {
super.actionPerformed(event);
}
} catch(Exception ex) {
InfoBox.exhibit(this, InfoBox.OK, ex);
}
}
protected void cleanup() {
}
void detail() throws Exception {
int[] selectedIndexes = checkOneSelected();
if (selectedIndexes == null)
return;
int row = getOriginalIndex();
WorkingChore workingChore = (WorkingChore) workingChoreVector.elementAt(row);
HistoryChorePanel historyChorePanel = new HistoryChorePanel(workingChore);
if (historyChorePanel.getReady() == false)
return;
String title = Phrase.get("TX_HISTORY_TASK");
new CommonDlg(Globals.masterWindow, title, historyChorePanel);
}
void export() throws Exception {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(Phrase.get("TX_EXPORT"));
chooser.setCurrentDirectory(exportDir);
chooser.setApproveButtonText(Phrase.get("TX_EXPORT"));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setApproveButtonToolTipText(Phrase.get("TX_EXPORT"));
String fileName = null;
int retval = chooser.showDialog(this, Phrase.get("TX_EXPORT"));
if(retval == JFileChooser.APPROVE_OPTION) {
File theFile = chooser.getSelectedFile();
if(theFile != null) {
fileName = theFile.getAbsolutePath();
File aFile = new File(fileName);
if (aFile.exists()) {
int answer = InfoBox.exhibit(null, fileName + ": " +
Phrase.get("TX_FILE_EXISTS") + "\n" +
Phrase.get("TX_OVERWRITE") + "?",
Phrase.get("TX_WARNING"), InfoBox.YES | InfoBox.NO);
if (answer != InfoBox.YES)
return;
}
exportDir = chooser.getCurrentDirectory();
}
} else
return;
String extension = Kit.getFileExtension(fileName);
if (extension == null)
fileName += ".html";
HistoryStandardPage standardPage = new HistoryStandardPage(from, to,
tableDataVector, columnNameVector);
String htmlText = standardPage.getHtmlText();
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileName));
writer.write(htmlText);
writer.newLine();
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}
protected Vector getColumnNameVector() {
Vector columnNames = new Vector();
columnNames.add(Phrase.get("TX_NAME"));
columnNames.add(Phrase.get("TX_REAL_HOST"));
columnNames.add(Phrase.get("TX_WORKING_STATUS"));
columnNames.add(Phrase.get("TX_LAST_RUN_TIME"));
columnNames.add(Phrase.get("TX_BRIEF"));
columnNames.add(Phrase.get("TX_LAST_RESULT"));
return columnNames;
}
protected String getPropertyNameForColumns() {
return "HistoryPropertiesColumns";
}
protected String getPropertyNameForSize() {
return "HistoryPropertiesSize";
}
public boolean getReady() throws Exception {
if (super.getReady() == false)
return false;
statusTextField.setVisible(false);
return true;
}
protected SuperProperties getSuperProperties() throws Exception {
return new WatchdogProperties();
}
protected boolean inlayComponents() throws Exception {
if (super.inlayComponents() == false)
return false;
table.setDefaultRenderer(Object.class, new ColorCellRenderer(Phrase.get("TX_WORKING_STATUS")));
return true;
}
protected JMenuBar makeMenuBar() {
String period = Common.makeFromToString(from, to);
periodLabel = new JLabel(Phrase.get("TX_PERIOD") + ": " + period);
LoadFile loadFile = new LoadFile();
ImageIcon exportIcon = loadFile.receiveImageIcon("export.gif");
ImageIcon previewIcon = loadFile.receiveImageIcon("preview.gif");
ImageIcon detailIcon = loadFile.receiveImageIcon("detail.gif");
ImageIcon closeIcon = loadFile.receiveImageIcon("close.gif");
detailButton = new JButton(Phrase.get("TX_DETAIL"), detailIcon);
detailButton.addActionListener(this);
detailButton.setMargin(buttonInsets);
exportButton = new JButton(Phrase.get("TX_EXPORT"), exportIcon);
exportButton.addActionListener(this);
exportButton.setMargin(buttonInsets);
previewButton = new JButton(Phrase.get("TX_PREVIEW"), previewIcon);
previewButton.addActionListener(this);
previewButton.setMargin(buttonInsets);
closeButton = new JButton(Phrase.get("TX_CLOSE"), closeIcon);
closeButton.addActionListener(this);
closeButton.setMargin(buttonInsets);
menuBar = new JMenuBar();
menuBar.add(Box.createHorizontalStrut(7));
menuBar.add(periodLabel);
menuBar.add(Box.createHorizontalStrut(10));
menuBar.add(detailButton);
menuBar.add(closeButton);
return menuBar;
}
Vector makeRowVector(WorkingChore workingChore) throws Exception {
Vector rowVector = new Vector();
rowVector.add(workingChore.name);
rowVector.add(workingChore.realHost);
rowVector.add(ChoreText.translateStatus(workingChore.status));
String lastRunTimeString = "";
if (workingChore.lastRunTime != Chore.NOT_SPECIFIED)
lastRunTimeString =
Common.makeTimestampLikeString(workingChore.lastRunTime);
rowVector.add(lastRunTimeString);
rowVector.add(workingChore.choreBrief);
rowVector.add(workingChore.lastResult);
return rowVector;
}
Vector makeTableDataVector(Vector workingChoreVector) {
Vector vector = new Vector();
for (int i = 0; i < workingChoreVector.size(); i++) {
try {
WorkingChore workingChore = null;
Object object = workingChoreVector.elementAt(i);
if (object instanceof WorkingChore) {
workingChore = (WorkingChore) object;
Vector rowVector = makeRowVector(workingChore);
vector.add(rowVector);
} else
System.out.println("History Table Panel error 0312131545:" + object.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
return vector;
}
protected boolean prepareData() throws Exception {
workingChoreVector = Api.selectAllWorkingChores(from, to);
theTableDataVector = makeTableDataVector(workingChoreVector);
tableDataVector.clear();
tableDataVector.addAll(theTableDataVector);
return true;
}
void preview() throws Exception {
HistoryStandardPage standardPage = new HistoryStandardPage(from, to,
tableDataVector, columnNameVector);
String htmlText = standardPage.getHtmlText();
if (htmlText.trim().length() == 0) {
InfoBox.exhibit(this, Phrase.get("ER_EMPTY_DATA"), Phrase.get("TX_ERROR"), InfoBox.OK);
return;
}
PreviewPanel previewPanel = new PreviewPanel(htmlText);
String title = Phrase.get("TX_PREVIEW");
new CommonDlg(Globals.masterWindow, title, previewPanel);
}
public void refresh() {
try {
} catch (Exception ex) {
InfoBox.exhibit(this, InfoBox.OK, ex);
return;
}
tableModel.fireTableChanged(new TableModelEvent(tableModel));
}
}