001: // Space4J(TM) - Object Persistence in RAM
002: // Copyright (C) 2003 Eduardo Rodrigues
003: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
004:
005: package org.space4j.demos.phonebook;
006:
007: import java.awt.Container;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.awt.event.WindowEvent;
011: import java.awt.event.WindowListener;
012: import java.io.IOException;
013: import java.net.UnknownHostException;
014: import java.util.ArrayList;
015: import java.util.Iterator;
016:
017: import javax.swing.JButton;
018: import javax.swing.JFrame;
019: import javax.swing.JLabel;
020: import javax.swing.JOptionPane;
021: import javax.swing.JScrollPane;
022: import javax.swing.JTable;
023: import javax.swing.JTextField;
024:
025: import org.space4j.CommandException;
026: import org.space4j.LoggerException;
027:
028: /**
029: * This is a simple application demonstrating how Space4J can be easily used as a data storage system.<br>
030: * Is is as simple as it can be.
031: * java org.space4j.demos.phonebook.PhoneBookSwing
032: * Swing demo for PhoneBook Space4J prevalence System
033: * ToDo: Implement any layout manager? I think it isn't necessary since it is a demo app
034: */
035: public class PhoneBookSwing extends JFrame implements ActionListener,
036: WindowListener {
037:
038: JButton listButton = new JButton("List"), findButton = new JButton(
039: "Find"), addButton = new JButton("Add"),
040: snapButton = new JButton("Snapshot"),
041: removeButton = new JButton("Remove");
042: JTextField nameField = new JTextField(),
043: phoneField = new JTextField();
044: JTable records = new JTable(new PhoneBookTableModel(
045: new ArrayList(), new ArrayList()));
046: JScrollPane pane = new JScrollPane(records);
047: PhoneBook book;
048:
049: public PhoneBookSwing() {
050:
051: setResizable(false); //See ToDo above
052: setSize(300, 440); //See ToDo above
053: addWindowListener(this );
054: positionComponents();
055: setTitle("Space4J PhoneBook Demo App");
056: try {
057: book = new PhoneBook();
058: } catch (UnknownHostException e) {
059: e.printStackTrace();
060: } catch (LoggerException e) {
061: e.printStackTrace();
062: } catch (CommandException e) {
063: e.printStackTrace();
064: } catch (IOException e) {
065: e.printStackTrace();
066: } catch (ClassNotFoundException e) {
067: e.printStackTrace();
068: }
069: show();
070: refresh(true);
071:
072: }
073:
074: /**
075: * NO LAYOUT MANAGER
076: * Since this is a demo app, I didn't use any layou manager
077: * Feel yourself free to add a convenient layout manager
078: * See ToDo above
079: */
080: private void positionComponents() {
081:
082: getContentPane().setLayout(null); //See ToDo above
083:
084: Container container = getContentPane();
085: JLabel nameLabel = new JLabel("Name:"), phoneLabel = new JLabel(
086: "Phone:");
087: container.add(nameLabel);
088: container.add(phoneLabel);
089:
090: nameLabel.setBounds(5, 5, 100, 25);
091: container.add(nameField);
092: nameField.setBounds(70, 5, 220, 25);
093:
094: phoneLabel.setBounds(5, 32, 100, 25);
095: container.add(phoneField);
096: phoneField.setBounds(70, 32, 220, 25);
097:
098: container.add(addButton);
099: addButton.setBounds(45, 70, 100, 25);
100: addButton.addActionListener(this );
101:
102: container.add(findButton);
103: findButton.setBounds(150, 70, 100, 25);
104: findButton.addActionListener(this );
105:
106: container.add(listButton);
107: listButton.setBounds(45, 97, 100, 25);
108: listButton.addActionListener(this );
109:
110: container.add(snapButton);
111: snapButton.setBounds(150, 97, 100, 25);
112: snapButton.addActionListener(this );
113:
114: container.add(pane);
115: pane.setBounds(5, 130, 285, 240);
116:
117: container.add(removeButton);
118: removeButton.setBounds(90, 380, 100, 25);
119: removeButton.addActionListener(this );
120:
121: }
122:
123: public void actionPerformed(ActionEvent e) {
124:
125: Object source = e.getSource();
126:
127: if (source == listButton) {
128:
129: refresh(true);
130:
131: } else if (source == findButton) {
132:
133: String name = nameField.getText();
134: String tel = book.getNumber(name);
135: if (tel != null) {
136: ArrayList list = new ArrayList();
137: list.add(name);
138: ArrayList tels = new ArrayList();
139: tels.add(tel);
140: PhoneBookTableModel model = new PhoneBookTableModel(
141: list, tels);
142: records.setModel(model);
143: } else {
144: JOptionPane.showMessageDialog(this , "Nothing found!");
145: }
146:
147: } else if (source == addButton) {
148:
149: String name = nameField.getText();
150: String tel = phoneField.getText();
151: if (!name.trim().equals("") && !tel.trim().equals("")) {
152: try {
153: book.addNumber(name, tel);
154: } catch (CommandException e1) {
155: e1.printStackTrace();
156: } catch (LoggerException e1) {
157: e1.printStackTrace();
158: }
159: JOptionPane.showMessageDialog(this , name + ": " + tel
160: + " added!");
161: refresh(false);
162: }
163: } else if (source == snapButton) {
164: try {
165: book.executeSnapshot();
166: JOptionPane.showMessageDialog(this , "Snapshot taken!");
167: return;
168: } catch (LoggerException e1) {
169: e1.printStackTrace();
170: }
171: JOptionPane.showMessageDialog(this ,
172: "Error taking Snapshot!");
173:
174: } else if (source == removeButton) {
175:
176: String name = (String) records.getModel().getValueAt(
177: records.getSelectedRow(), 0);
178: try {
179: if (book.delNumber(name)) {
180: JOptionPane
181: .showMessageDialog(this , "Item removed!");
182: refresh(false);
183: } else {
184: JOptionPane.showMessageDialog(this , "Not found!");
185: }
186:
187: } catch (CommandException e1) {
188: e1.printStackTrace();
189: } catch (LoggerException e1) {
190: e1.printStackTrace();
191: }
192: }
193: }
194:
195: private void refresh(boolean showMessage) {
196: ArrayList list = book.getNames();
197: if (list == null || list.size() == 0) {
198: PhoneBookTableModel model = new PhoneBookTableModel(
199: new ArrayList(), new ArrayList());
200: records.setModel(model);
201: if (showMessage)
202: JOptionPane.showMessageDialog(this ,
203: "No entries in phone book!");
204: } else {
205: Iterator iter = list.iterator();
206: ArrayList tels = new ArrayList();
207: while (iter.hasNext()) {
208: String key = (String) iter.next();
209: String value = book.getNumber(key);
210: tels.add(value);
211: }
212: PhoneBookTableModel model = new PhoneBookTableModel(list,
213: tels);
214: records.setModel(model);
215: }
216: }
217:
218: public void windowActivated(WindowEvent e) {
219: }
220:
221: public void windowClosed(WindowEvent e) {
222: }
223:
224: public void windowClosing(WindowEvent e) {
225: System.exit(0);
226: }
227:
228: public void windowDeactivated(WindowEvent e) {
229: }
230:
231: public void windowDeiconified(WindowEvent e) {
232: }
233:
234: public void windowIconified(WindowEvent e) {
235: }
236:
237: public void windowOpened(WindowEvent e) {
238: }
239:
240: public static void main(String[] args) {
241: PhoneBookSwing phoneFrame = new PhoneBookSwing();
242: }
243:
244: }
|