/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phonetalks.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.PriorityQueue;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import phonetalks.entities.calls.PhoneCall;
import phonetalks.entities.calls.SubscriberCallPair;
import phonetalks.entities.collections.CardIndex;
import phonetalks.entities.subscriber.Subscriber;
import phonetalks.entities.subscriber.SubscriberEntry;
import phonetalks.io.BinaryFileIndexSaver;
/**
*
* @author Платон
*/
public class PhoneTalksUI extends JFrame {
//MVC-part
JPanel mainPanel;
JPanel menuPanel;
JPanel infoPanel;
JPanel controlPanel;
JMenuBar menu = new JMenuBar();
//info part
JTextArea overviewArea;
JTextArea infoArea;
//control part
JPanel addCallPanel;
JPanel remCallPanel;
JPanel addSubPanel;
JPanel remSubPanel;
JPanel getCallsPanel;
JPanel findPanel;
//add call
JTextField numberFromArea = new JTextField();
JTextField numberToArea = new JTextField();
JTextField durationArea = new JTextField();
JTextField dateArea = new JTextField();
JButton addCallButton = new JButton("Add Call");
//remove call
JTextField numberFromAreaRem = new JTextField();
JTextField numberToAreaRem = new JTextField();
JButton remCallButton = new JButton("Remove Call");
//add subsc
JTextField addSubFirst = new JTextField();
JTextField addSubLast = new JTextField();
JTextField addSubPatr = new JTextField();
JTextField addSubNumber = new JTextField();
JButton addSubButton = new JButton("Add Subscriber");
//rem subcsriber
JTextField remSubName = new JTextField();
JTextField remSubNumber = new JTextField();
JButton remSubButton = new JButton("Remove Subscriber");
//get all calls
JTextField getCallsField = new JTextField();
JButton getCallsButton = new JButton("Get All Calls");
//find by number
JTextField findField = new JTextField();
JButton findButton = new JButton("Find by Number");
//data part
private CardIndex base = new CardIndex();
private BinaryFileIndexSaver saver = new BinaryFileIndexSaver("writehere.bin", "out.bin");
//helpers
final String EOL = "\n\r";
public PhoneTalksUI() {
initUI();
}
public void initUI() {
setSize(640, 600);
setMinimumSize(new Dimension(640, 600));
setResizable(true);
setTitle("PhoneTalksUI");
initControl();
initInfo();
initMenu();
menuPanel = new JPanel();
menuPanel.setToolTipText("Menu panel");
mainPanel = new JPanel();
add(mainPanel, BorderLayout.CENTER);
add(menuPanel, BorderLayout.NORTH);
mainPanel.setLayout(new GridLayout(1, 2));
mainPanel.add(infoPanel);
mainPanel.add(controlPanel);
setJMenuBar(menu);
}
public void initInfo() {
infoPanel = new JPanel();
Border infoBorder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Info Panel");
infoPanel.setBorder(infoBorder);
infoPanel.setLayout(new GridLayout(2, 1));
//init areas
overviewArea = new JTextArea();
overviewArea.setEditable(false);
JScrollPane overviewPane = new JScrollPane(overviewArea);
infoPanel.add(overviewPane);
infoArea = new JTextArea();
infoArea.setEditable(false);
JScrollPane infoPane = new JScrollPane(infoArea);
infoPanel.add(infoPane);
}
public void initControl() {
controlPanel = new JPanel();
Border infoBorder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Control Panel");
controlPanel.setBorder(infoBorder);
controlPanel.setLayout(new GridLayout(6, 1, 3, 3));
initAddCall();
initRemCall();
initAddSub();
initRemSub();
initFindCalls();
initFind();
controlPanel.add(addCallPanel);
controlPanel.add(remCallPanel);
controlPanel.add(addSubPanel);
controlPanel.add(remSubPanel);
controlPanel.add(getCallsPanel);
controlPanel.add(findPanel);
}
public void initAddCall() {
addCallPanel = new JPanel();
addCallPanel.setLayout(new GridLayout(5, 2));
addCallPanel.add(new JLabel("from number: ", SwingConstants.LEFT));
addCallPanel.add(numberFromArea);
addCallPanel.add(new JLabel("to number: ", SwingConstants.LEFT));
addCallPanel.add(numberToArea);
addCallPanel.add(new JLabel("duartion: ", SwingConstants.LEFT));
addCallPanel.add(durationArea);
addCallPanel.add(new JLabel("date: ", SwingConstants.LEFT));
addCallPanel.add(dateArea);
addCallPanel.add(new JLabel("press to add call: ", SwingConstants.LEFT));
addCallPanel.add(addCallButton);
addCallButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addCall();
}
});
addCallPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void addCall() {
try {
PhoneCall temp = new PhoneCall(numberToArea.getText(), Integer.valueOf(durationArea.getText()));
base.addCall(numberFromArea.getText(), temp);
printIndex();
printInfo("Call added");
} catch (Exception e) {
printInfo("Error while adding call. Check your input.");
}
}
public void initRemCall() {
remCallPanel = new JPanel();
remCallPanel.setLayout(new GridLayout(3, 2, 5, 7));
remCallPanel.add(new JLabel("from number: ", SwingConstants.LEFT));
remCallPanel.add(numberFromAreaRem);
remCallPanel.add(new JLabel("to number: ", SwingConstants.LEFT));
remCallPanel.add(numberToAreaRem);
remCallPanel.add(new JLabel("press to remove call: ", SwingConstants.LEFT));
remCallPanel.add(remCallButton);
remCallButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remCall();
}
});
remCallPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void remCall() {
throw new UnsupportedOperationException();
}
public void initAddSub() {
addSubPanel = new JPanel();
addSubPanel.setLayout(new GridLayout(5, 2));
addSubPanel.add(new JLabel("first name: ", SwingConstants.LEFT));
addSubPanel.add(addSubFirst);
addSubPanel.add(new JLabel("last name: ", SwingConstants.LEFT));
addSubPanel.add(addSubLast);
addSubPanel.add(new JLabel("patronymic: ", SwingConstants.LEFT));
addSubPanel.add(addSubPatr);
addSubPanel.add(new JLabel("number: ", SwingConstants.LEFT));
addSubPanel.add(addSubNumber);
addSubPanel.add(new JLabel("press to add subscriber: ", SwingConstants.LEFT));
addSubPanel.add(addSubButton);
addSubButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addSub();
}
});
addSubPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void addSub() {
try {
Subscriber s = new Subscriber(addSubFirst.getText(), addSubPatr.getText(),
addSubLast.getText());
SubscriberEntry entry = new SubscriberEntry(s, addSubNumber.getText());
base.addEntry(entry);
printInfo("Subscriber added");
printIndex();
} catch (Exception e) {
printInfo("Error while adding subscriber. Check it!");
}
}
//<editor-fold>
public void initRemSub() {
remSubPanel = new JPanel();
remSubPanel.setLayout(new GridLayout(3, 2, 5, 7));
remSubPanel.add(new JLabel("subscriber name: ", SwingConstants.LEFT));
remSubPanel.add(remSubName);
remSubPanel.add(new JLabel("subscriber's number: ", SwingConstants.LEFT));
remSubPanel.add(remSubNumber);
remSubPanel.add(new JLabel("press to remove subscriber: ", SwingConstants.LEFT));
remSubPanel.add(remSubButton);
remSubButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remSub();
}
});
remSubPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void remSub() {
throw new UnsupportedOperationException();
}
public void initFindCalls() {
getCallsPanel = new JPanel();
getCallsPanel.setLayout(new GridLayout(3, 2, 5, 7));
getCallsPanel.add(new JLabel("for number: ", SwingConstants.LEFT));
getCallsPanel.add(getCallsField);
getCallsPanel.add(new JLabel("press to get calls: ", SwingConstants.LEFT));
getCallsPanel.add(getCallsButton);
getCallsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getCalls();
}
});
getCallsPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void getCalls() {
try {
ArrayList<SubscriberCallPair> list = base.getAllCalls(getCallsField.getText());
String message = list.size() == 0 ? "No call" : "Calls are:";
printInfo(message);
for (SubscriberCallPair pair : list) {
printInfo(pair.toString());
}
} catch (Exception e) {
printInfo("Stop getting at me and check yourself!");
}
}
public void initFind() {
findPanel = new JPanel();
findPanel.setLayout(new GridLayout(3, 2, 5, 7));
findPanel.add(new JLabel("number is: ", SwingConstants.LEFT));
findPanel.add(findField);
findPanel.add(new JLabel("press to find'em: ", SwingConstants.LEFT));
findPanel.add(findButton);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
findByNumber();
}
});
findPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
}
public void findByNumber() {
try {
SubscriberEntry entry = base.findByNumber(findField.getText());
String message = entry == null ? "No chance" : "Subscriber is: " + entry.getSubscriber();
printInfo(message);
} catch (Exception e) {
printInfo("No way. Erorr.");
}
}
//</editor-fold>
void initMenu() {
JMenu fileMenu = new JMenu("File");
JMenu aboutMenu = new JMenu("About");
JMenuItem openItem = new JMenuItem("Open", new ImageIcon("open.gif"));
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
open();
}
});
JMenuItem saveItem = new JMenuItem("Save", new ImageIcon("save.gif"));
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
save();
}
});
JMenuItem exitItem = new JMenuItem("Exit", new ImageIcon("exit.gif"));
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
JMenuItem aboutItem = new JMenuItem("About", new ImageIcon("about.gif"));
aboutMenu.add(aboutItem);
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, (Object) "Version 0.1a by DKunin", "About This App",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon("about.gif"));
}
});
menu.add(fileMenu);
menu.add(aboutMenu);
}
public void open() {
base = saver.readIndex();
printIndex();
printInfo("Index's been opened from file");
}
public void save() {
saver.writeIndex(base);
printInfo("Index's been saved to file");
}
private void printIndex() {
overviewArea.setText("");
if (base == null) {
return;
}
StringBuilder sb = new StringBuilder();
String step = " ";
for (PriorityQueue<SubscriberEntry> entrys : base.values()) {
for (SubscriberEntry entry : entrys) {
sb.append(entry.getSubscriber().toString() + entry.getPhoneNumber() + EOL);
for (PhoneCall call : entry) {
sb.append(step + call.toString() + EOL);
}
}
}
overviewArea.append(sb.toString());
}
public void printInfo(String message) {
infoArea.append(message + EOL);
}
public static void main(String[] fuckoff) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
PhoneTalksUI frame = new PhoneTalksUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
}