/**
* automatic sites registrator
* course project of NSTU students Copyright (C)
* 2012 students of gr. ZF-927(a)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed 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.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.border.EmptyBorder;
public class ProgressWindow extends JDialog {
public ProgressWindow(JFrame parent)
{
super(parent, true);
initComponents();
makeLayout();
}
private void initComponents ()
{
dialogPane = new JPanel();
contentPanel = new JPanel();
scrollPane = new JScrollPane();
logArea = new JTextArea();
logArea.setEditable(false);
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
progressLabel = new JLabel("�������� ��������� �����������");
buttonBar = new JPanel();
button = new JButton("������");
button.addActionListener(new buttonHandler());
finish = false;
}
private void makeLayout ()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setLayout(new BorderLayout());
scrollPane.setViewportView(logArea);
GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
contentPanel.setLayout(contentPanelLayout);
GroupLayout.ParallelGroup parallelGroup = contentPanelLayout.createParallelGroup();
parallelGroup.addComponent(scrollPane, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE);
parallelGroup.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE);
parallelGroup.addComponent(progressLabel, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE);
contentPanelLayout.setHorizontalGroup(parallelGroup);
GroupLayout.SequentialGroup sequentialGroup = contentPanelLayout.createSequentialGroup();
sequentialGroup.addComponent(progressLabel);
sequentialGroup.addGap(2, 2, 2);
sequentialGroup.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE);
sequentialGroup.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
sequentialGroup.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE);
contentPanelLayout.setVerticalGroup(contentPanelLayout.createParallelGroup().addGroup(sequentialGroup));
dialogPane.add(contentPanel, BorderLayout.CENTER);
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
GridBagLayout buttonLayout = new GridBagLayout();
buttonLayout.columnWidths = new int[] {0, 85, 80};
buttonLayout.columnWeights = new double[] {1.0, 0.0, 0.0};
buttonBar.setLayout(buttonLayout);
buttonBar.add(button, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
dialogPane.add(buttonBar, BorderLayout.SOUTH);
contentPane.add(dialogPane, BorderLayout.CENTER);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(getOwner());
}
public void write (String msg)
{
logArea.append(msg);
}
public void setProgress (int pr)
{
progressBar.setValue(pr);
progressBar.setString(Integer.toString(pr) + "%");
}
public boolean executeResult ()
{
return finish;
}
public void finished (List<TaskStatus> statuses)
{
final String goodMsg = "����������� ������ �������!\n";
final String badMsg = "�� ����� ����������� ��������� ������,\n" +
"�� ������� ���������� ����������� � �������� ��������:\n";
final String noInet = "���������� � ���������� �����������\n" +
"��������� ����������� � ���������\n" +
"� ��������� �������\n";
String message = goodMsg;
if (statuses.size() != 0) {
for (TaskStatus status : statuses) {
if (status.hasError()) {
if (message == goodMsg) {
message = badMsg;
}
message += status.name() + "\n";
}
}
} else {
message = noInet;
}
if (message != goodMsg && message != noInet) {
Calendar c = Calendar.getInstance();
String fileName = c.get(c.DAY_OF_MONTH) + "-" + c.get(c.MONTH) + "-" + c.get(c.YEAR) + "-log.txt";
if (Logger.flashErrors(statuses, fileName)) {
message += "������ ��� � ��������: " + fileName;
} else {
message += "�� ������� ������� ��� � ��������";
}
}
message += "\n������� \"OK\" ��� ������.";
logArea.append(message);
button.setText("OK");
finish = true;
}
private class buttonHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
dispose();
System.gc();
}
}
private JPanel dialogPane;
private JPanel contentPanel;
private JScrollPane scrollPane;
private JTextArea logArea;
private JProgressBar progressBar;
private JLabel progressLabel;
private JPanel buttonBar;
private JButton button;
private boolean finish;
}