KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > mailchecking > AbstractMailCheckingAction


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.mailchecking;
18
19 import java.awt.event.ActionEvent JavaDoc;
20
21 import javax.swing.Timer JavaDoc;
22
23 import org.columa.core.config.IDefaultItem;
24 import org.columba.core.config.DefaultItem;
25 import org.columba.core.connectionstate.ConnectionStateImpl;
26 import org.columba.core.gui.action.AbstractColumbaAction;
27 import org.columba.core.xml.XmlElement;
28 import org.columba.mail.config.AccountItem;
29
30 /**
31  * For each account there exists one check action.
32  * <p>
33  * It keeps a reference to the check action, which is used by the
34  * <b>File->Receive New Messages </b> menu.
35  *
36  * @author fdietz
37  */

38 public abstract class AbstractMailCheckingAction extends AbstractColumbaAction {
39     private final static int ONE_SECOND = 1000;
40
41     /**
42      * account item
43      */

44     private AccountItem accountItem;
45     private Timer JavaDoc timer;
46
47     public AbstractMailCheckingAction(AccountItem accountItem) {
48         super(null, null);
49
50         this.accountItem = accountItem;
51
52         createName();
53
54         restartTimer();
55     }
56
57     private void createName() {
58         // generate label for menuitem
59
String JavaDoc name = accountItem.getName();
60         String JavaDoc address = accountItem.getIdentity().getAddress()
61                 .getMailAddress();
62         String JavaDoc menuItemName = name + " (" + address + ")";
63
64         putValue(AbstractColumbaAction.NAME, menuItemName);
65     }
66
67     public void restartTimer() {
68         // recreate name of menuitem
69
createName();
70
71         IDefaultItem item = null;
72
73         if (accountItem.isPopAccount()) {
74             XmlElement e = accountItem.getRoot().getElement("popserver");
75             item = new DefaultItem(e);
76         } else {
77             XmlElement e = accountItem.getRoot().getElement("imapserver");
78             item = new DefaultItem(e);
79         }
80
81         if (item.getBoolean("enable_mailcheck")) {
82             int interval = item.getIntegerWithDefault("mailcheck_interval", 10);
83
84             timer = new Timer JavaDoc(ONE_SECOND * interval * 60, this);
85             timer.restart();
86         } else {
87             if (timer != null) {
88                 timer.stop();
89                 timer = null;
90             }
91         }
92     }
93
94     /**
95      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
96      */

97     public void actionPerformed(ActionEvent JavaDoc arg0) {
98         Object JavaDoc source = arg0.getSource();
99
100         if (source.equals(timer)) {
101             // timer action
102

103             // only do checking if we are in online state
104
if (ConnectionStateImpl.getInstance().isOnline())
105                 check();
106             
107         } else {
108             // menuitem/toolbar button action
109
check();
110         }
111     }
112
113     /**
114      * Check for new messages.
115      * <p>
116      * Subclasses should implement this method.
117      *
118      */

119     public abstract void check();
120
121     /**
122      * @return
123      */

124     public AccountItem getAccountItem() {
125         return accountItem;
126     }
127
128     /**
129      * @return
130      */

131     public abstract boolean isCheckAll();
132 }
Popular Tags