/**
* 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.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MainWindow extends JFrame {
public MainWindow()
{
super("Automatic sites registrator");
initComponents();
makeLayout();
}
private void initComponents ()
{
menuBar = new JMenuBar();
menuAbout = new JMenu("Help");
menuItemAutors = new JMenuItem("About");
menuItemAutors.addActionListener(new menuItemAutorsHandler());
keyLabel = new JLabel("Key");
keyLabel.setEnabled(false);
urlLabel = new JLabel("URL");
keyTextField = new JTextField();
keyTextField.setEditable(false);
urlTextField = new JTextField();
// urlTextField.setText("http://");
urlTextField.setText("http://url.test");
handCaptcha = new JRadioButton("������ ���� captcha", true);
handCaptcha.setActionCommand(handCaptchaActionCommand);
handCaptcha.addActionListener(new radioCaptchaHandler());
autoCaptcha = new JRadioButton("�������������� ���� captcha");
autoCaptcha.setActionCommand(autoCaptchaActionCommand);
autoCaptcha.addActionListener(new radioCaptchaHandler());
dialogPane = new JPanel();
contentPanel = new JPanel();
startButton = new JButton("Start");
startButton.addActionListener(new startHandler());
exitButton = new JButton("Exit");
exitButton.addActionListener(new exitHandler());
searchers = new HashMap<String, JCheckBox>();
chosenSearchers = new Vector<String>();
for (String name : searcherNames) {
searchers.put(name, new JCheckBox(name, true));
searchers.get(name).setActionCommand(name);
searchers.get(name).addActionListener(new setSearcher());
chosenSearchers.add(name);
}
radioButtons = new ButtonGroup();
radioButtons.add(handCaptcha);
radioButtons.add(autoCaptcha);
}
private void makeLayout ()
{
menuAbout.add(menuItemAutors);
menuBar.add(menuAbout);
setJMenuBar(menuBar);
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
contentPanel.setLayout(new GridBagLayout());
Container contentPane = getContentPane();
GridBagConstraints constraints = new GridBagConstraints();
Insets insets = new Insets(0, 0, 5, 5);
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = insets;
contentPanel.add(urlLabel, constraints);
constraints.gridx = 3;
constraints.gridwidth = 9;
contentPanel.add(urlTextField, constraints);
constraints.gridx = 12;
constraints.gridwidth = 5;
contentPanel.add(searchers.get(searcherNames[0]), constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 4;
contentPanel.add(keyLabel, constraints);
constraints.gridx = 3;
constraints.gridy = 1;
constraints.gridwidth = 9;
constraints.gridheight = 1;
contentPanel.add(keyTextField, constraints);
constraints.gridx = 12;
constraints.gridwidth = 4;
contentPanel.add(searchers.get(searcherNames[1]), constraints);
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 7;
contentPanel.add(handCaptcha, constraints);
constraints.gridx = 12;
constraints.gridwidth = 4;
contentPanel.add(searchers.get(searcherNames[2]), constraints);
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 8;
contentPanel.add(autoCaptcha, constraints);
constraints.gridx = 12;
constraints.gridwidth = 4;
contentPanel.add(searchers.get(searcherNames[3]), constraints);
constraints.gridy = 4;
contentPanel.add(searchers.get(searcherNames[4]), constraints);
constraints.gridx = 1;
constraints.gridy = 7;
constraints.gridwidth = 7;
constraints.insets.bottom = 0;
contentPanel.add(startButton, constraints);
constraints.gridx = 8;
constraints.gridwidth = 3;
contentPanel.add(exitButton, constraints);
dialogPane.add(contentPanel, BorderLayout.CENTER);
contentPane.add(dialogPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 250);
setResizable(false);
setLocationRelativeTo(getOwner());
}
private void startRegistration ()
{
RegistratorLauncher.launch(this);
}
private class menuItemAutorsHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
String message = "<html>" +
"<body style=\"background-color:#f0f0f0\">" +
"<p>Automatic sites registrator pre-alpha " +
"version<br>" +
"��������� �������� � ������ ��������� ������� �� �������� \"���\"<br>" +
"��������� �������� open source � ���������������� �� �������� " +
"<b><a href=\"http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\">GNU GPL v2</a></b><br>" +
"����� �������: <b><a href=\"http://code.google.com/p/automatic-sites-registrator\">" +
"http://code.google.com/p/automatic-sites-registrator</a></b><br>" +
"������ ���������:<br>" +
"������� �.�. - ��-927<br>" +
"�������� �.�. - ��-927�<br>" +
"���� �.�. - ��-927�</p>" +
"<br>" +
"This software is distributed under <b><a href=\"http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\">GNU GPL v2</a></b><br>" +
"The program was developed under the course project on the subject of \"Object-oriented programming\"<br>" +
"Address of project on Google-code: <b><a href=\"http://code.google.com/p/automatic-sites-registrator\">" +
"http://code.google.com/p/automatic-sites-registrator</a></b><br>" +
"Autors: Karasev R.A., Filonov R.I., Yutsis A.V.</body></html>";
final JEditorPane editorPane = new JEditorPane("text/html", message);
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate (HyperlinkEvent e)
{
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
}
});
try {
BufferedImage logo = ImageIO.read(new File("logo.gif"));
JOptionPane.showMessageDialog(null,
editorPane, "About", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(logo));
} catch (IOException ignored) {
JOptionPane.showMessageDialog(null,
message, "About", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class setSearcher implements ActionListener {
public void actionPerformed (ActionEvent e)
{
String name = e.getActionCommand();
if (searchers.get(name).isSelected()) {
if (!chosenSearchers.contains(name)) {
chosenSearchers.add(name);
}
} else {
if (chosenSearchers.contains(name)) {
chosenSearchers.remove(name);
}
}
}
}
private class exitHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
private class radioCaptchaHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals(autoCaptchaActionCommand)) {
keyLabel.setEnabled(true);
keyTextField.setEditable(true);
keyTextField.setText("������� ����");
keyTextField.requestFocusInWindow();
keyTextField.selectAll();
} else {
keyLabel.setEnabled(false);
keyTextField.setEditable(false);
keyTextField.setText("");
}
}
}
private class startHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
String url = urlTextField.getText();
String captchaMethod = radioButtons.getSelection().getActionCommand();
String errorMessage = null;
String antigateKey = null;
if (url.isEmpty()) {
errorMessage = "���� URL �� ���������";
}
if (chosenSearchers.isEmpty()) {
errorMessage = "�� ������� �� ����� ��������� �������";
}
if (!Validator.isValidUrl(url)) {
errorMessage = "��������� URL ������������!";
urlTextField.requestFocusInWindow();
urlTextField.selectAll();
}
if (captchaMethod.equals(autoCaptchaActionCommand)) {
antigateKey = keyTextField.getText();
if (!Validator.isValidAGKey(antigateKey) || antigateKey.isEmpty()) {
errorMessage = "������ ������������ ����";
keyTextField.requestFocusInWindow();
keyTextField.selectAll();
}
keyTextField.requestFocusInWindow();
keyTextField.selectAll();
}
if (errorMessage != null) {
JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.WARNING_MESSAGE);
return;
}
String message = new String("<html><body>"
+
"<b style=\"color:#0000FF\">URL</b>:<font style=\"color:#008080\"> "
+ url
+ "</font><br>"
+
"<b style=\"color:#0000FF\">����� ����� captcha</b>: <font style=\"color:#008080\">"
+
(captchaMethod.equals(handCaptchaActionCommand) ? "������" : "��������������")
+ "</font><br>"
+
(captchaMethod.equals(autoCaptchaActionCommand) ? "<b style=\"color:#0000FF\">���� antigate</b>: <font style=\"color:#008080\">"
+ antigateKey + "</font><br>" : "") +
"<b style=\"color:#0000FF\">��������� ��������� �������</b>: <font style=\"color:#008080\">");
for (int i = 0; i < chosenSearchers.size(); i++) {
message += chosenSearchers.get(i);
if (i < chosenSearchers.size() - 1) {
message += ", ";
}
}
message += "</font><br><br>���� ��� �����, ������� \"OK\" ��� ������ �����������</body></html>";
int answer = JOptionPane.showConfirmDialog(null, message, "Info",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
UserData.registerUrl(url);
UserData.registerSearchers(chosenSearchers);
UserData.registerCaptchaMethod(captchaMethod);
if (antigateKey != null) {
UserData.registerAgKey(antigateKey);
}
startRegistration();
}
}
}
private JMenuBar menuBar;
private JMenu menuAbout;
private JMenuItem menuItemAutors;
private JPanel dialogPane;
private JPanel contentPanel;
private JLabel keyLabel;
private JLabel urlLabel;
private JTextField keyTextField;
private JTextField urlTextField;
private JRadioButton handCaptcha;
private JRadioButton autoCaptcha;
private JButton startButton;
private JButton exitButton;
private ButtonGroup radioButtons;
private Map<String, JCheckBox> searchers;
private Vector<String> chosenSearchers;
private final String[] searcherNames = {"Mail", "Yandex", "Rambler", "Yahoo", "Bing"};
private final String handCaptchaActionCommand = new String("handCaptcha");
private final String autoCaptchaActionCommand = new String("autoCaptcha");
}