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.components.gmenu; 005 006 007 import graphlab.platform.core.BlackBoard; 008 import graphlab.ui.actions.UIEventData; 009 010 import javax.swing.*; 011 import java.awt.event.ActionEvent; 012 import java.awt.event.ActionListener; 013 014 /** 015 * this is the same as JMenuItem with the diffrece that it sends its events to the blackboard automatically. 016 * see GButton for more details. 017 * 018 * @author Azin Azadi 019 */ 020 //todo: like GButton 021 public class GMenuItem extends JMenuItem implements ActionListener { 022 023 /** 024 * 025 */ 026 private static final long serialVersionUID = 6309834468678258546L; 027 028 public GMenuItem(String label, String action, BlackBoard b) { 029 super(label); 030 blackboard = b; 031 addActionListener(this); 032 // add(new JButton(" ...")); 033 // validate(); 034 //----------- 035 t = new UIEventData(); 036 t.action = action; 037 } 038 039 public GMenuItem(String label, String action, BlackBoard b, String accelerator, int index) { 040 //icons R ! supported yet. now it is important to just work correctly 041 super(label); 042 blackboard = b; 043 addActionListener(this); 044 //----------- 045 t = new UIEventData(); 046 t.action = action; 047 KeyBoardShortCut shortcut = KeyBoardShortCutProvider.registerKeyBoardShortcut(accelerator, label, index); 048 if (shortcut != null) { 049 if (!shortcut.isAccelerator()) { 050 setMnemonic(shortcut.getKeyMnemonic()); 051 setDisplayedMnemonicIndex(shortcut.getKeyWordIndex()); 052 053 } else { 054 setAccelerator(KeyStroke.getKeyStroke(shortcut.getKeyEvent(), shortcut.getKeyModifiers())); 055 setDisplayedMnemonicIndex(shortcut.getKeyWordIndex()); 056 setMnemonic(shortcut.getKeyMnemonic()); 057 } 058 } 059 // setLayout(new BorderLayout(10,0)); 060 // 061 // JButton pref = new JButton("Prefs"); 062 // pref.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){ 063 // t.eventType=0; 064 // blackboard.listen4Event(UIEventData.event(""), t);}}); 065 // add(pref, BorderLayout.EAST); 066 067 // validate(); 068 069 070 } 071 072 public String toString() { 073 return getText(); 074 } 075 076 public void actionPerformed(ActionEvent e) { 077 blackboard.setData(UIEventData.name(""), t); 078 } 079 080 BlackBoard blackboard; 081 UIEventData t; 082 }