001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.mailchecking;
018:
019: import java.awt.event.ActionEvent;
020:
021: import javax.swing.Timer;
022:
023: import org.columa.core.config.IDefaultItem;
024: import org.columba.core.config.DefaultItem;
025: import org.columba.core.connectionstate.ConnectionStateImpl;
026: import org.columba.core.gui.action.AbstractColumbaAction;
027: import org.columba.core.xml.XmlElement;
028: import org.columba.mail.config.AccountItem;
029:
030: /**
031: * For each account there exists one check action.
032: * <p>
033: * It keeps a reference to the check action, which is used by the
034: * <b>File->Receive New Messages </b> menu.
035: *
036: * @author fdietz
037: */
038: public abstract class AbstractMailCheckingAction extends
039: AbstractColumbaAction {
040: private final static int ONE_SECOND = 1000;
041:
042: /**
043: * account item
044: */
045: private AccountItem accountItem;
046: private Timer timer;
047:
048: public AbstractMailCheckingAction(AccountItem accountItem) {
049: super (null, null);
050:
051: this .accountItem = accountItem;
052:
053: createName();
054:
055: restartTimer();
056: }
057:
058: private void createName() {
059: // generate label for menuitem
060: String name = accountItem.getName();
061: String address = accountItem.getIdentity().getAddress()
062: .getMailAddress();
063: String menuItemName = name + " (" + address + ")";
064:
065: putValue(AbstractColumbaAction.NAME, menuItemName);
066: }
067:
068: public void restartTimer() {
069: // recreate name of menuitem
070: createName();
071:
072: IDefaultItem item = null;
073:
074: if (accountItem.isPopAccount()) {
075: XmlElement e = accountItem.getRoot()
076: .getElement("popserver");
077: item = new DefaultItem(e);
078: } else {
079: XmlElement e = accountItem.getRoot().getElement(
080: "imapserver");
081: item = new DefaultItem(e);
082: }
083:
084: if (item.getBoolean("enable_mailcheck")) {
085: int interval = item.getIntegerWithDefault(
086: "mailcheck_interval", 10);
087:
088: timer = new Timer(ONE_SECOND * interval * 60, this );
089: timer.restart();
090: } else {
091: if (timer != null) {
092: timer.stop();
093: timer = null;
094: }
095: }
096: }
097:
098: /**
099: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
100: */
101: public void actionPerformed(ActionEvent arg0) {
102: Object source = arg0.getSource();
103:
104: if (source.equals(timer)) {
105: // timer action
106:
107: // only do checking if we are in online state
108: if (ConnectionStateImpl.getInstance().isOnline())
109: check();
110:
111: } else {
112: // menuitem/toolbar button action
113: check();
114: }
115: }
116:
117: /**
118: * Check for new messages.
119: * <p>
120: * Subclasses should implement this method.
121: *
122: */
123: public abstract void check();
124:
125: /**
126: * @return
127: */
128: public AccountItem getAccountItem() {
129: return accountItem;
130: }
131:
132: /**
133: * @return
134: */
135: public abstract boolean isCheckAll();
136: }
|