001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.ui.actions; 005 006 007 import graphlab.platform.core.AbstractAction; 008 import graphlab.platform.core.BlackBoard; 009 import graphlab.platform.core.Listener; 010 import graphlab.ui.UIUtils; 011 012 import java.util.HashMap; 013 import java.util.HashSet; 014 015 /** 016 * Maps the events generated by menues and toolbars to their Matching Action 017 * 018 * @author azin azadi 019 */ 020 /*ie seri az eventa faghat kafie ke enable shan (mese add vertex). 021 // ie seri az eventa daran addListener mikonan ta kar konan (mese loadfile). 022 // mishe dar har soorat enablesh kard, badesh too blackboard gasht did 023 // chizi dare addListener mikone ia na. (ba action), age addListener mikard ie event 024 // barash tolid konim. 025 //--- 026 //agar bekhaim oonaii ro ke mese load file hasan ie event barashoon befresim 027 // pas baiad ie standardi baraie esme logi ke tooie black board daran dashte bashim 028 // khob chon baiad befahmim ke aghajoon oon dare tooie che logi goosh mikone 029 // hala mishe esmesho az tooie xml khoond ia az rooie action fahmid. man be shakhse action ro 030 // tarjih midam 031 //.. 032 // man oon standard ro UIEventData.name(action) entekhab mikonam 033 */ 034 public class UIEventHandler extends AbstractAction { 035 public static final String ACTIONS_MAP = "blackboard action -> actions hashmap"; 036 public static final String CONF = "blackboard : Configuration of program"; 037 038 /** 039 * constructor 040 * 041 * @param bb the blackboard of the action 042 */ 043 public UIEventHandler(BlackBoard bb) { 044 super(bb); 045 listen4Event(UIUtils.getUIEventKey("")); 046 } 047 048 public void performAction(String eventName, Object value) { 049 UIEventData _ = blackboard.getData(UIEventData.name("")); 050 String id = _.action; 051 if (id != null) { 052 enableAction(id); 053 sendEventToAction(id, _); 054 } 055 } 056 057 private void enableAction(String id) { 058 AbstractAction action = getAction(id); 059 if (action != null) 060 action.enable(); 061 else { 062 HashSet<Listener> listeners = blackboard.getListeners(UIUtils.getUIEventKey(id)); 063 if (listeners == null) 064 System.err.println("Can't find action for id = " + id); 065 } 066 } 067 068 /** 069 * fetches the target action from blackboard 070 * 071 * @param id 072 */ 073 private AbstractAction getAction(String id) { 074 //the action map is put in the blackboard in UIHandlerImpl class, at endActions() method. 075 HashMap<String, AbstractAction> actionsmap = blackboard.getData(ACTIONS_MAP); 076 return actionsmap.get(id); 077 } 078 079 /** 080 * it first check that is there exists any actions that addListener for the event? (is there any log with the name 081 * registered by UIEventData.name(id) in blackboard?) 082 * and if the answer is true it sends the event to log. 083 * 084 * @param id 085 * @param uiEventData 086 */ 087 private void sendEventToAction(String id, UIEventData uiEventData) { 088 // if (blackboard.contains(UIEventData.name(id))) 089 blackboard.setData(UIEventData.name(id), uiEventData); 090 } 091 }