Sitter och försöker skriva ett pop3 program i java. Det kommer säkert att komma frågor :)
Hittade en bra pop3-klass så det är inga problem, men att få ihop skiten med ett vettigt gränssnitt, lite loggar och tjoffa runt det i en databas kommer nog att generera en och annan fråg!
------------------
The computer is mightier than the pen, the sword, and usually, the programmer.
/*
* PropertyHandler.java
*
* Created on den 9 januari 2002, 10:40
*/
package lime.util;
/**
* This is a code used mainly for internal educational use.
* Feel free to use it as you wich but please state the origin
* of the code.
* <p>
* The author will not answer any questions about the code by mail.
* Questions can be asked at <a href="http://program.webforum.nu/"> WebForum</a>
*
* @author lime@enea.se
*/
public class PropertyHandler {
/** Creates a new instance of PropertyHandler */
public PropertyHandler() {
}
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
new PropertyHandlerFrame();
}
}
PropertyHandlerFrame.java
/*
* PropertyHandlerFrame.java
*
* Created on den 9 januari 2002, 09:59
*/
package lime.util;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.util.*;
import lime.util.tools.*;
/**
* This is a code used mainly for internal educational use.
* Feel free to use it as you wich but please state the origin
* of the code.
* <p>
* The author will not answer any questions about the code by mail.
* Questions can be asked at <a href="http://program.webforum.nu/"> WebForum</a>
* <p>
* All code is written in NetBeans IDE 3.3, downloadable for free at [url="http://www.netbeans.org."]www.netbeans.org.[/url]
* <p>
* This class it the GUI-class for the example of the Property class.
* <p>
* @author lime@enea.se
*/
public class PropertyHandlerFrame extends javax.swing.JFrame {
/** Creates new form PropertyHandler */
public PropertyHandlerFrame() {
initComponents();
this.show();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
keyList = new javax.swing.JList();
valueList = new javax.swing.JList();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
cutMenuItem = new javax.swing.JMenuItem();
copyMenuItem = new javax.swing.JMenuItem();
pasteMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
addMenuItem = new javax.swing.JMenuItem();
editMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
contentsMenuItem = new javax.swing.JMenuItem();
aboutMenuItem = new javax.swing.JMenuItem();
setForeground(java.awt.Color.black);
setBackground(new java.awt.Color(204, 255, 255));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setPreferredSize(new java.awt.Dimension(300, 200));
jPanel1.setMinimumSize(new java.awt.Dimension(200, 200));
keyList.setBorder(new javax.swing.border.TitledBorder("Key"));
keyList.setMinimumSize(new java.awt.Dimension(100, 100));
jPanel1.add(keyList);
valueList.setBorder(new javax.swing.border.TitledBorder("Value"));
valueList.setMinimumSize(new java.awt.Dimension(100, 100));
valueList.setEnabled(false);
jPanel1.add(valueList);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
fileMenu.setText("File");
openMenuItem.setText("Open");
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuItemActionPerformed(evt);
}
});
fileMenu.add(openMenuItem);
saveMenuItem.setText("Save");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
fileMenu.add(saveMenuItem);
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.setText("Edit");
cutMenuItem.setText("Cut");
editMenu.add(cutMenuItem);
copyMenuItem.setText("Copy");
editMenu.add(copyMenuItem);
pasteMenuItem.setText("Paste");
editMenu.add(pasteMenuItem);
deleteMenuItem.setText("Delete Property");
deleteMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteMenuItemActionPerformed(evt);
}
});
editMenu.add(deleteMenuItem);
addMenuItem.setText("Add Property");
addMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addMenuItemActionPerformed(evt);
}
});
editMenu.add(addMenuItem);
editMenuItem.setText("Edit Property");
editMenu.add(editMenuItem);
menuBar.add(editMenu);
helpMenu.setText("Help");
contentsMenuItem.setText("Contents");
helpMenu.add(contentsMenuItem);
aboutMenuItem.setText("About");
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
pack();
}
/**
* This is the method that opens the .lprop file and restores the properties.
* @param evt The ActionEvent from the Menu.
**/
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser filechooser = new JFileChooser();
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("lprop");
filter.setDescription("Lime Property files");
filechooser.addChoosableFileFilter(filter);
int selected = filechooser.showOpenDialog(null);
switch(selected) {
case JFileChooser.APPROVE_OPTION:
File selectedFile = filechooser.getSelectedFile();
File directory = filechooser.getCurrentDirectory();
try{
props.load(new FileInputStream(selectedFile));
}
catch (java.io.IOException ioe) { ioe.printStackTrace(); System.exit(-1);}
Enumeration keys = props.keys();
keyVector.clear();
valueVector.clear();
while(keys.hasMoreElements()) {
String keyTemp = (String) keys.nextElement();
String valTemp = props.getProperty(keyTemp);
keyVector.add(keyTemp);
valueVector.add(valTemp);
}
updateLists();
case JFileChooser.CANCEL_OPTION:
break;
default:
break;
}
}
/**
* This is the method that saves the parameters to the .lprop file.
* @param evt The ActionEvent from the Menu.
**/
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser filechooser = new JFileChooser();
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("lprop");
filter.setDescription("Lime Property files");
filechooser.addChoosableFileFilter(filter);
int selected = filechooser.showSaveDialog(null);
switch (selected) {
case JFileChooser.APPROVE_OPTION:
File selectedFile = filechooser.getSelectedFile();
File directory = filechooser.getCurrentDirectory();
if (selectedFile != null) {
// Save the props
try {
props.store( new FileOutputStream(selectedFile), "Lime props");
}
catch (java.io.IOException ioe) { ioe.printStackTrace(); System.exit(-1);}
} // if
break;
case JFileChooser.CANCEL_OPTION:
break;
default:
break;
} // switch
}
private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
String selectedKey = (String) keyList.getSelectedValue();
if (selectedKey != null) {
String valueFromProps = props.getProperty(selectedKey);
String[] message = new String[3];
message[0] = "Are you sure that you want to delete";
message[1] = "the key \""+ selectedKey + "\"";
message[2] = "with value \"" + valueFromProps + "\"?";
int result = JOptionPane.showConfirmDialog(this,message,"Confirm Delete",JOptionPane.OK_CANCEL_OPTION );
if (result == JOptionPane.YES_OPTION) {
props.remove(selectedKey);
valueVector.remove(valueFromProps);
keyVector.remove(selectedKey);
updateLists();
}
}
}
private void addMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
String key = null;
String value = null;
key = JOptionPane.showInputDialog(this,"Enter the property key:","Property input: Key 1(2)", JOptionPane.OK_CANCEL_OPTION);
if(key != null){
value = JOptionPane.showInputDialog(this,"Enter the property value:","Property input: Value 2(2)", JOptionPane.OK_CANCEL_OPTION);
}
if ((value != null) && (key != null) && (value != "") && (key != "")){
props.setProperty(key,value);
keyVector.add(key);
valueVector.add(value);
updateLists();
}
}
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
private void updateLists() {
keyList.setListData(keyVector);
valueList.setListData(valueVector);
}
// Variables declaration - do not modify
private javax.swing.JList valueList;
private javax.swing.JMenuItem cutMenuItem;
private javax.swing.JMenuItem addMenuItem;
private javax.swing.JMenuItem pasteMenuItem;
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenuItem saveMenuItem;
private javax.swing.JMenu helpMenu;
private javax.swing.JMenuItem editMenuItem;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem contentsMenuItem;
private javax.swing.JMenu editMenu;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenuItem copyMenuItem;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenu fileMenu;
private javax.swing.JList keyList;
private javax.swing.JMenuItem openMenuItem;
// End of variables declaration
private static java.util.Properties props = new java.util.Properties();
private Vector keyVector = new Vector(50);
private Vector valueVector = new Vector(50);
}
ExtensionFileFilter.java
/*
* ExtensionFileFilter.java
*
* Created on den 9 januari 2002, 14:11
*/
package lime.util.tools;
/**
* A file filter that matches file name extensions.
* These is to be a filter like this in the standard JFC
* classes, making this obsolete.
*
* @author ovli
*/
public class ExtensionFileFilter extends javax.swing.filechooser.FileFilter {
/**
* The description for this file filter.
*/
private String description;
/**
* The extensions that match this filter.
*/
private java.util.Hashtable filters = null;
/** Creates a new instance of ExtensionFileFilter */
public ExtensionFileFilter() {
filters = new java.util.Hashtable();
setDescription("undefined");
}
/**
* Add the given extension to the list of extensions
* to match. The extensions should not have a '.' in it.
* <p>
* @param extension The extension to add to the list of matching extensions.
**/
public void addExtension(String extension) {
filters.put(extension.toLowerCase(), this);
}
/**
* @return The description of this file filter.
**/
public String getDescription() {
return description;
}
/**
* Set the description of this file filter. This
* will be displayed in the file chooser's filter
* selection box.
* <p>
* @param d The file filter's description.
**/
public void setDescription(String d) {
description = d;
}
/**
* The file maches if its extension is contained in our filter hashtable.
* <p>
* @param f The File to test for matching the filter criteria.
* @return True if given File matches the filter criteria.
* False otherwise.
**/
public boolean accept(java.io.File f) {
if (f != null) {
if (f.isDirectory() ) {
return true;
}
String extension = getExtension(f);
if (extension != null && filters.get (extension) != null){
return true;
}
}
return false;
}
/**
* Determine the extension of the given File.
* <p>
* @param f The File whose extension is desired.
* @return The extension of the given File, in lower case.
**/
private String getExtension(java.io.File f) {
if (f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if (i>0 && i<filename.length() -1 ) {
return filename.substring(i+1).toLowerCase();
};
}
return null;
}
}
------------------
Praeterea conseo microsoftenem esse delendam.
- modernt latinskt ordspråk
275 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849