⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addressbookmanager.java

📁 java图形化界面编程 功能和记事本通讯录相似 记录通讯人的姓名和联系方法等信息并提供查询
💻 JAVA
字号:
package elegate.cn.edu.nju;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;
import java.util.Collection;
import java.util.Properties;
import java.awt.*;


/**
 * A simple address book manager
 * @author Elegate,elegate@gmail.com
 * @author cs department of NJU
 */
public class AddressBookManager extends JFrame
{
	

	private static final long serialVersionUID = 1L;
	private static final int WIDTH=800;
	private static final int HEIGHT=700;
	public  static Properties DEFAULT_PROPERTIES=new Properties();;
	private AddressBookModel addressBookModel;
	private AddressEntryDialog addressEntryDialog;
	private DevelopDocumentViewer docViewer=null;
	private AddressBookPanel addressBookPanel;
	private Properties properties=null;
	private PreferenceEditor preferenceEditor;

	//initialize block
	{
		DEFAULT_PROPERTIES.put
		("look","javax.swing.plaf.metal.MetalLookAndFeel");
		
		DEFAULT_PROPERTIES.put("path","config"
				+File.separator+"data"
				+File.separator+"addresses");
		DEFAULT_PROPERTIES.put("psw","");
	}

	public AddressBookManager()
	{
		super("Address Book Manager");
		properties=new Properties(DEFAULT_PROPERTIES);
		readFromFile();
		
		try
		{
			UIManager.setLookAndFeel(properties.getProperty("look"));
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		JMenu mnuFile=new JMenu("File");
		mnuFile.setMnemonic('F');
		JMenuItem mnuAddAddressEntry=new JMenuItem("Add Address Entry");
		mnuAddAddressEntry.setMnemonic('A');
		mnuAddAddressEntry.setAccelerator(KeyStroke.getKeyStroke
				(KeyEvent.VK_A,InputEvent.CTRL_MASK));
		mnuAddAddressEntry.addActionListener(new MnuAddAddressEntryL());
		mnuAddAddressEntry.setToolTipText("Click to add an address entry");
		
		JMenuItem mnuAddGroup=new JMenuItem("Add group");
		mnuAddGroup.setMnemonic('G');
		mnuAddGroup.addActionListener(new MnuAddGroupL());
		mnuAddGroup.setAccelerator(KeyStroke.getKeyStroke
				(KeyEvent.VK_G,InputEvent.CTRL_MASK));
		mnuAddGroup.setToolTipText("Click to add a group");
		
		JMenuItem mnuRemoveGroup=new JMenuItem("Remove Group");
		mnuRemoveGroup.setMnemonic('R');
		mnuRemoveGroup.setAccelerator
		(KeyStroke.getKeyStroke
				(KeyEvent.VK_R,InputEvent.CTRL_MASK));
		mnuRemoveGroup.addActionListener(new MnuRemoveGroupL());
		mnuRemoveGroup.setToolTipText("Click to remove a group");
		
		JMenuItem mnuExit=new JMenuItem("Exit");
		mnuExit.setMnemonic('X');
		mnuExit.addActionListener(new MnuExitL());
		mnuExit.setAccelerator(KeyStroke.getKeyStroke
				(KeyEvent.VK_X,InputEvent.CTRL_MASK));
		mnuExit.setToolTipText("Click to quit the program");
		
		JMenu mnuEdit=new JMenu("Edit");
		mnuEdit.setMnemonic('E');
		JMenuItem mnuSelectAll=new JMenuItem("Select all");
		mnuSelectAll.setMnemonic('S');
		mnuSelectAll.addActionListener(new MnuSelectAllL());
		mnuSelectAll.setToolTipText("Select all the entries displaying");
		JMenuItem mnuDeselectAll=new JMenuItem("Deselect all");
		mnuDeselectAll.setMnemonic('D');
		mnuDeselectAll.setToolTipText("Deselect all the selected entries");
		mnuDeselectAll.addActionListener(new MnuDeselectAllL());
		JCheckBoxMenuItem mnuShowAllColumns=new JCheckBoxMenuItem("Show all columns",false);
		mnuShowAllColumns.setMnemonic('A');
		mnuShowAllColumns.setToolTipText("Click to view all the infomation of the address entry");
		mnuShowAllColumns.addActionListener(new MnuShowAllColumnsL());
		
		JMenu mnuConfig=new JMenu("Config");
		mnuConfig.setMnemonic('C');
		mnuConfig.setToolTipText("Config the program");
		JMenuItem mnuPreference=new JMenuItem("Preference...");
		mnuPreference.setMnemonic('P');
		mnuPreference.addActionListener(new MnuPreferenceL());
		mnuPreference.setToolTipText("Click to set your preference");
		
		
		JMenu mnuHelp=new JMenu("Help");
		mnuHelp.setMnemonic('H');
		JMenuItem mnuHelpContents=new JMenuItem("Help contents");
		mnuHelpContents.setMnemonic('H');
		mnuHelpContents.addActionListener(new MnuHelpContentsL());
		mnuHelpContents.setToolTipText("Click to get hlep");
		
		JMenuItem mnuDoc=new JMenuItem("Develop Document");
		mnuDoc.setMnemonic('D');
		mnuDoc.setToolTipText("Click to view the develop document of this program");
		mnuDoc.addActionListener(new MnuDevelopDocumentL());
		JMenuItem mnuAbout=new JMenuItem("About");
		mnuAbout.setMnemonic('A');
		mnuAbout.addActionListener(new MnuAboutL());
		
		mnuFile.add(mnuAddAddressEntry);
		mnuFile.add(mnuAddGroup);
		mnuFile.add(mnuRemoveGroup);
		mnuFile.add(mnuExit);
		
		mnuEdit.add(mnuSelectAll);
		mnuEdit.add(mnuDeselectAll);
		mnuEdit.add(mnuShowAllColumns);
		
		mnuHelp.add(mnuHelpContents);
		mnuHelp.add(mnuDoc);
		mnuHelp.add(mnuAbout);
		mnuConfig.add(mnuPreference);
		
		JMenuBar mnuBar=new JMenuBar();
		mnuBar.add(mnuFile);
		mnuBar.add(mnuEdit);
		mnuBar.add(mnuConfig);
		mnuBar.add(mnuHelp);
		this.setJMenuBar(mnuBar);
		
		addressBookPanel=new AddressBookPanel(addressBookModel);
		
		this.getContentPane().add(addressBookPanel);
		this.pack();
		this.setSize(WIDTH,HEIGHT);
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((d.width-WIDTH)/2,(d.height-HEIGHT)/2);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				setVisible(false);
				saveToFile();
				dispose();
				System.exit(0);
			}
		});
		preferenceEditor=new PreferenceEditor(this,this.properties);
		this.addressEntryDialog=new AddressEntryDialog(this,this.addressBookModel);
	}
	/**
	 * read address file from the local file system
	 *
	 */
	private void readFromFile()
	{
		try
		{
			FileInputStream in=new FileInputStream
			("config/data/AddressBookManager.properties");
			properties.load(in);
			in.close();
			String psw=properties.getProperty("psw");
			if(psw.length()>0)
			{
				PasswordInputEditor pswEditor=new PasswordInputEditor
				("Password Protection","Input your protection password");
				pswEditor.setVisible(true);
				String str=null;
				char[] pswChar=pswEditor.getPassword();
				if(pswChar!=null)
					str=String.valueOf(pswChar);
				if(str==null)
				{
					System.exit(0);
				}
				if(!psw.equals(Encryption.arrayEncrypt(str)))
				{
					JOptionPane.showMessageDialog(this
							,"Incorrect passwrod"
							,"Warnig",JOptionPane.ERROR_MESSAGE);
					System.exit(0);
				}
			}
	
			File file=new File(properties.getProperty("path"));
			if(file.exists())
			{
				ObjectInputStream input=new ObjectInputStream
				(new FileInputStream(file));
				addressBookModel=(AddressBookModel)input.readObject();
			}
			else
				this.addressBookModel=new AddressBookModel();
		}
		catch(Exception e)
		{
			addressBookModel=new AddressBookModel();
			e.printStackTrace();
		}
	}
	/**
	 * save address file to the local file system
	 *
	 */
	private void saveToFile()
	{
		try
		{
			FileOutputStream out=new FileOutputStream(properties.getProperty("path","config/data/Addresses"));
			ObjectOutputStream output=new ObjectOutputStream(out);
			output.writeObject(this.addressBookModel);
			output.flush();
			output.close();
			FileOutputStream outputConfig=new FileOutputStream
			("config"+File.separator+"data"+File.separator+"AddressBookManager.properties");
			properties.store(outputConfig,"Address Book Manager Properties");	
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(AddressBookManager.this,e.toString()
					,"Error",JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	
/**
 *  menu remove group's listener
 *  @author Elegate,elegate@gmail.com
 *  @author cs department of NJU
 */
	class MnuRemoveGroupL implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Collection<GroupEntry> collection=addressBookModel.getAllGroupEntries();
			if(collection.size()==1)
			{
				JOptionPane.showMessageDialog
				(AddressBookManager.this
						,"There's no groups to remove"
						,"Warning",JOptionPane.ERROR_MESSAGE);
				return;
			}
			Object[] groups=new Object[collection.size()-1];
			int index=0;
			for(GroupEntry ele:collection)
			{
				if(!ele.getGroupName().equals("All"))
					groups[index++]=ele;
			}
			Object selectedValue=JOptionPane.showInputDialog
			(AddressBookManager.this
					,"Selecte a group to remove"
					,"Remove group",JOptionPane.PLAIN_MESSAGE
					,null,groups,groups[0]);
			if(selectedValue==null)
				return;
			addressBookModel.removeGroupEntry((GroupEntry)selectedValue);
			addressBookPanel.updateView();
		}
	}
	/**
	 * menu select all's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuSelectAllL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			addressBookPanel.selectAllEntries();
		}
		
	}
	/**
	 * menu deselect all's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuDeselectAllL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			addressBookPanel.deselectAllEntries();
		}
		
	}
	/**
	 * menu show all columns' listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuShowAllColumnsL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			JCheckBoxMenuItem mnuShowAllColumns=
				(JCheckBoxMenuItem)e.getSource();
			if(mnuShowAllColumns.getState())
			{
				addressBookPanel.showAllColumns();
			}
			else
			{
				addressBookPanel.hideColumns();
			}
		}
		
	}
	/**
	 * menu preference's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuPreferenceL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			preferenceEditor.setVisible(true);
			Properties p=preferenceEditor.getProperties();
			if(p==null)
				return;
			properties=p;
		}
	}
	/**
	 * menu help contents' listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuHelpContentsL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			new NotePad("config"+File.separator
					+"help"+File.separator
					+"help.txt").setVisible(true);
		}
		
	}
	/**
	 * menu develop document's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuDevelopDocumentL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			if(docViewer==null)
			{
				docViewer=new DevelopDocumentViewer("doc"
						+File.separator+"index.html");
			}
			else
			{
				docViewer.setVisible(true);
			}

		}
		
	}
	/**
	 * menu about's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuAboutL implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JOptionPane.showMessageDialog(AddressBookManager.this
					,"Address book manager by Elegate,elgate@gmail.com"
					,"About",JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
	
	class MnuExitL implements ActionListener
	{

		public void actionPerformed(ActionEvent e) 
		{
			setVisible(false);
			saveToFile();
			dispose();
			System.exit(0);
		}
		
	}
	/**
	 * menu add address entry's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuAddAddressEntryL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			addressEntryDialog.showAddressEntryDialog();
			AddressEntry addressEntry=
				addressEntryDialog.getInputAddressEntry();
			if(addressEntry==null)
				return;
			if(!addressBookModel.addAddressEntry(addressEntry))
			{
				JOptionPane.showMessageDialog(
						AddressBookManager.this
						,"Add address entry failed"
						,"Warning",JOptionPane.ERROR_MESSAGE);
				return;
			}
			addressBookPanel.updateView();
		}
	}
	/**
	 * menu add group's listener
	 * @author Elegate,elegate@gmail.com
	 * @author cs department of NJU
	 */
	class MnuAddGroupL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			String str=JOptionPane.showInputDialog(AddressBookManager.this
					,"Input new group name:","Add a new group"
					,JOptionPane.PLAIN_MESSAGE);
			if(str==null)
				return;
			if(str.length()==0)
			{
				JOptionPane.showMessageDialog(AddressBookManager.this
					,"Illegal group name","Warning"
					,JOptionPane.WARNING_MESSAGE);
				return;
			}
			boolean b=addressBookModel.addGroupEntry(str);
			if(!b)
			{
				JOptionPane.showMessageDialog(AddressBookManager.this
						,"The group has existed","Warning",JOptionPane.WARNING_MESSAGE);
			}
			else
			{
				addressBookPanel.updateView();
			}
		}
		
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -