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: package org.columba.mail.gui.tree.action;
017:
018: import java.util.Observable;
019: import java.util.Observer;
020:
021: import org.columa.core.config.IDefaultItem;
022: import org.columba.api.gui.frame.IFrameMediator;
023: import org.columba.api.selection.ISelectionListener;
024: import org.columba.api.selection.SelectionChangedEvent;
025: import org.columba.core.config.DefaultItem;
026: import org.columba.core.gui.action.AbstractColumbaAction;
027: import org.columba.core.xml.XmlElement;
028: import org.columba.mail.config.IFolderItem;
029: import org.columba.mail.config.MailConfig;
030: import org.columba.mail.folder.IMailFolder;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.mail.gui.frame.MailFrameMediator;
033: import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
034:
035: /**
036: * @author redsolo
037: */
038: public abstract class AbstractMoveFolderAction extends
039: AbstractColumbaAction implements ISelectionListener, Observer {
040:
041: private IMailFolder lastSelectedFolder;
042:
043: /**
044: * @param frameMediator
045: * the frame controller.
046: * @param name
047: * name of action.
048: */
049: public AbstractMoveFolderAction(IFrameMediator frameMediator,
050: String name) {
051: super (frameMediator, name);
052:
053: ((MailFrameMediator) frameMediator)
054: .registerTreeSelectionListener(this );
055: registerSortingObserver();
056: }
057:
058: /**
059: * Returns true if the action enables a folder with the specified index.
060: * Implementing methods can rely on that the lastSelectedFolder is not null.
061: *
062: * @param folderIndex
063: * the index of the folder.
064: * @return true if it should enable the action; false otherwise.
065: */
066: protected abstract boolean isActionEnabledByIndex(int folderIndex);
067:
068: /**
069: * Register for notifications when the Tree node changes.
070: */
071: private void registerSortingObserver() {
072: XmlElement sortElement = MailConfig.getInstance()
073: .get("options").getElement("/options/gui/tree/sorting");
074: if (sortElement == null) {
075: XmlElement treeElement = MailConfig.getInstance().get(
076: "options").getElement("/options/gui/tree");
077: if (treeElement == null) {
078: treeElement = MailConfig.getInstance().get("options")
079: .getElement("/options/gui").addSubElement(
080: "tree");
081: }
082: sortElement = treeElement.addSubElement("sorting");
083: }
084: sortElement.addObserver(this );
085: }
086:
087: /** {@inheritDoc} */
088: public void update(Observable o, Object arg) {
089: enableAction();
090: }
091:
092: /**
093: * Enables or disables the action.
094: */
095: private void enableAction() {
096: XmlElement sortElement = MailConfig.getInstance()
097: .get("options").getElement("/options/gui/tree/sorting");
098:
099: IDefaultItem item = new DefaultItem(sortElement);
100: boolean sorted = item.getBoolean("sorted");
101: if (sorted) {
102: setEnabled(false);
103: } else {
104: reenableActionFromSelectedFolder();
105: }
106: }
107:
108: /**
109: * Enables or disables the action based on the last selected folder.
110: */
111: private void reenableActionFromSelectedFolder() {
112: if ((lastSelectedFolder == null)
113: || (lastSelectedFolder.getParent() == null)) {
114: setEnabled(false);
115: } else {
116: IFolderItem item = lastSelectedFolder.getConfiguration();
117:
118: if (item.getString("property", "accessrights").equals(
119: "user")) {
120: int index = lastSelectedFolder.getParent().getIndex(
121: lastSelectedFolder);
122:
123: setEnabled(isActionEnabledByIndex(index));
124: } else {
125: setEnabled(false);
126: }
127: }
128: }
129:
130: /**
131: * @see org.columba.core.gui.util.ISelectionListener#connectionChanged(org.columba.core.gui.util.SelectionChangedEvent)
132: */
133: public void selectionChanged(SelectionChangedEvent e) {
134: if (((TreeSelectionChangedEvent) e).getSelected().length > 0) {
135: IMailFolder folder = ((TreeSelectionChangedEvent) e)
136: .getSelected()[0];
137: if ((folder != null) && folder instanceof IMailbox) {
138: lastSelectedFolder = folder;
139: } else {
140: lastSelectedFolder = null;
141: }
142: enableAction();
143: } else {
144: setEnabled(false);
145: }
146: }
147:
148: /**
149: * @return Returns the last selected folder.
150: */
151: protected IMailFolder getLastSelectedFolder() {
152: return lastSelectedFolder;
153: }
154: }
|