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 005 package graphlab.graph.ui; 006 007 import graphlab.platform.core.BlackBoard; 008 009 import javax.swing.*; 010 import javax.swing.event.ChangeEvent; 011 import javax.swing.event.ChangeListener; 012 import java.util.HashMap; 013 014 /** 015 * @author Azin Azadi 016 */ 017 public class GTabbedPane { 018 019 /** 020 * provides a tabbed UI Interface for editing multiple graphs in multiple tabs 021 * 022 * @author azin azadi 023 */ 024 public static final String NAME = "GTabbedPane"; 025 public static final String CURRENT_COMPONENT = "GTabbedPane current component"; 026 027 public JTabbedPane jtp; 028 public BlackBoard blackboard; 029 030 public GTabbedPane(BlackBoard b) { 031 blackboard = b; 032 jtp = new JTabbedPane(); 033 // jtp.setDoubleBuffered(true); 034 jtp.setBorder(null); 035 jtp.setOpaque(false); 036 // jtp.setBackground(Color.white); 037 jtp.addChangeListener(new ChangeListener() { 038 public void stateChanged(ChangeEvent e) { 039 reTab(); 040 } 041 }); 042 043 b.setData(GTabbedPane.NAME, this); 044 } 045 046 protected void reTab() { 047 JComponent sc = (JComponent) jtp.getSelectedComponent(); 048 blackboard.setData(CURRENT_COMPONENT, sc); 049 } 050 051 public JTabbedPane getTabedPane() { 052 return jtp; 053 } 054 055 /** 056 * adds a tab to the tabbed pane, if addHelper=true, the added component will be a 057 * GSplitedPane which have a GHTMLPageComponent on it's top as a helper 058 * 059 * @param title 060 * @param component 061 * @param addHelper 062 */ 063 public JComponent addComponent(String title, JComponent component, boolean addHelper) { 064 if (addHelper) 065 component = new GSplitedPane(new GHTMLPageComponent(blackboard), component); 066 jtp.addTab(title, component); 067 jtp.setSelectedComponent(component); 068 jtp.setTabComponentAt(jtp.getTabCount() - 1, new graphlab.graph.old.ButtonTabComponent(jtp)); 069 return component; 070 } 071 072 public HashMap<Class, Class<? extends JComponent>> supportedType = new HashMap<Class, Class<? extends JComponent>>(); 073 074 public void add(Object o, String label) { 075 if (supportedType.containsKey(o.getClass())) { 076 try { 077 JComponent c = (JComponent) supportedType.get(o.getClass()).getConstructors()[0].newInstance(o); 078 jtp.addTab(label, c); 079 jtp.setSelectedComponent(c); 080 } catch (Exception e) { 081 e.printStackTrace(); 082 } 083 } else System.err.println("Error in type"); 084 } 085 086 public void registerType(Class clazz, Class<? extends JComponent> jcclazz) { 087 supportedType.put(clazz, jcclazz); 088 } 089 }