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; 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 import java.net.URL; 014 015 /** 016 * This is class is the child of JButton which is using the blackboard to pass the action of itself. 017 * it is specially passing the actions to the log which UIEventHandler will be map it to its corresponding action 018 * I used this buttons in the GToolBar 019 * User: Azin Azadi 020 */ 021 public class GButton extends JButton implements ActionListener { 022 /** 023 * 024 */ 025 private static final long serialVersionUID = 938792224562609943L; 026 BlackBoard blackboard; 027 UIEventData t; 028 029 /** 030 * action is the name of the class which the UIEventHandler will map the button to. 031 */ 032 public GButton(String label, URL iconURL, BlackBoard b, String action) { 033 super(); 034 blackboard = b; 035 ImageIcon icon = null; 036 if (iconURL != null) 037 icon = new ImageIcon(iconURL); 038 System.out.println(iconURL + ""); 039 setLabelAndIcon(label, icon); 040 addActionListener(this); 041 //------------- 042 t = new UIEventData(); 043 t.action = action; 044 } 045 046 /** 047 * action is the name of the class which the UIEventHandler will map the button to. 048 */ 049 public GButton(String label, String iconFileName, BlackBoard b, String action) { 050 super(); 051 blackboard = b; 052 ImageIcon icon = loadIcon(iconFileName); 053 setLabelAndIcon(label, icon); 054 addActionListener(this); 055 //------------- 056 t = new UIEventData(); 057 t.action = action; 058 } 059 060 private ImageIcon loadIcon(String iconFileName) { 061 ImageIcon icon = null; 062 if (iconFileName != null && !iconFileName.equals("")) { 063 icon = new ImageIcon(iconFileName); 064 if (icon.getIconWidth() < 1) { 065 //solving the problem between GraphLab and GraphLabDebugger 066 if (iconFileName.charAt(0) == '/') 067 iconFileName = iconFileName.substring(1); 068 else 069 iconFileName = iconFileName + "/"; 070 icon = new ImageIcon(iconFileName); 071 072 } 073 } 074 075 return icon; 076 } 077 078 //set the icon and text of the button according to input 079 private void setLabelAndIcon(String label, ImageIcon icon) { 080 if (icon != null && icon.getIconWidth() > 1) { 081 setIcon(icon); 082 setToolTipText(label); 083 } else { 084 setText(label); 085 } 086 } 087 088 public void actionPerformed(ActionEvent e) { 089 blackboard.setData(UIEventData.name(""), t); 090 } 091 }