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.graph.ui; 005 006 import graphlab.graph.JGraph; 007 import graphlab.graph.atributeset.GraphAttrSet; 008 import graphlab.graph.atributeset.GraphNotifiableAttrSet; 009 import graphlab.graph.event.GraphSelectData; 010 import graphlab.graph.graph.AbstractGraphRenderer; 011 import graphlab.graph.graph.FastRenderer; 012 import graphlab.graph.graph.GraphModel; 013 import graphlab.platform.attribute.AttributeListener; 014 import graphlab.platform.core.BlackBoard; 015 016 import javax.swing.*; 017 import javax.swing.event.ChangeEvent; 018 import javax.swing.event.ChangeListener; 019 import java.awt.*; 020 021 /** 022 * provides a tabbed UI Interface for editing multiple graphs in multiple tabs 023 * 024 * @author azin azadi 025 */ 026 public class GTabbedGraphPane extends GTabbedPane { 027 public static final String NAME = "GTabbedGraphPane"; 028 029 private static int lastGraphIndex; 030 031 public static GTabbedGraphPane getCurrentGTabbedGraphPane(BlackBoard b) { 032 return b.getData(GTabbedGraphPane.NAME); 033 } 034 035 public GTabbedGraphPane(BlackBoard b) { 036 super(b); 037 jtp.addChangeListener(new ChangeListener() { 038 public void stateChanged(ChangeEvent e) { 039 reTab(); 040 } 041 }); 042 b.setData(GTabbedGraphPane.NAME, this); 043 } 044 045 public static Boolean defaultDirectedChoice = false; 046 047 protected void reTab() { 048 super.reTab(); 049 JComponent sc = (JComponent) jtp.getSelectedComponent(); 050 051 if (sc instanceof GSplitedPane) 052 sc = ((GSplitedPane) sc).main; 053 if (sc instanceof JGraph) { 054 GraphModel graph = ((JGraph) sc).getGraph(); 055 AbstractGraphRenderer graphV = ((JGraph) sc).getGraphView(); 056 //set the graph that all actions are work with to the selected graph 057 blackboard.setData(GraphAttrSet.name, graph); 058 blackboard.setData(AbstractGraphRenderer.EVENT_KEY, graphV); 059 GraphSelectData c = new GraphSelectData(); 060 c.g = graph; 061 blackboard.setData(GraphSelectData.EVENT_KEY, c); 062 } 063 } 064 065 /** 066 * create a tab for the given graph, the name of tab will be "G" + graph label 067 * 068 * @param g graphmodel 069 */ 070 public void addGraph(GraphModel g) { 071 AbstractGraphRenderer v = new FastRenderer(g, blackboard); 072 final JGraph c = new JGraph(g, v);// JGraph.getNewComponent(blackboard); 073 074 if (g.getLabel() == null) 075 g.setLabel("G" + String.valueOf(lastGraphIndex++)); 076 077 final JComponent gsp = addComponent(g.getLabel(), c, true); 078 079 080 new GraphNotifiableAttrSet(g).addAttributeListener(new AttributeListener() { 081 JComponent cc = gsp; 082 083 public void attributeUpdated(String name, Object oldVal, Object newVal) { 084 if (name.equals(GraphAttrSet.LABEL)) 085 updateTitle((String) newVal, cc); 086 } 087 }); 088 } 089 // 090 // public void addComponent(String title, JComponent compoenent) { 091 // jtp.add(title, new JScrollPane(compoenent)); 092 093 // } 094 095 private void updateTitle(String newVal, JComponent c) { 096 for (int i = 0; i < jtp.getTabCount(); i++) { 097 if (jtp.getComponentAt(i) == c) { 098 jtp.setTitleAt(i, newVal + ""); 099 if (jtp.getTabComponentAt(i) instanceof ButtonTabComponent) { 100 ButtonTabComponent buttonTabComponent = (ButtonTabComponent) jtp.getTabComponentAt(i); 101 JLabel l = buttonTabComponent.label; 102 l.setText(l.getText()); 103 l.validate(); 104 c.validate(); 105 c.doLayout(); 106 jtp.doLayout(); 107 jtp.validate(); 108 } 109 } 110 } 111 } 112 113 public Component getComponent(BlackBoard b) { 114 b.setData(NAME, this); 115 return jtp; 116 } 117 118 //------------------------------------------------------------------------------- 119 /** 120 * shows a message as a notification to the user, It will hide when the hideNotificationMessage is called 121 * and the prv message will be shown, 122 * if formatIt=true, an html formatting will be applied to the message to make it nicer, do it if you don't 123 * pass a complete html message 124 * 125 * @param message 126 * @param b 127 * @param formatIt 128 */ 129 public static void showNotificationMessage(String message, BlackBoard b, boolean formatIt) { 130 if (formatIt) message = htmlFormat(message); 131 GHTMLPageComponent c = getCurrentGHTGhtmlPageComponent(b); 132 if (c == null) JOptionPane.showMessageDialog(null, message); 133 else 134 c.showNotificationMessage(message); 135 } 136 137 /** 138 * shows a message to the user, It will hide when the showNotificationMessage is called, and show again 139 * when the hideNotificationMessage is called 140 * if formatIt=true, an html formatting will be applied to the message to make it nicer, do it if you don't 141 * pass a complete html message 142 */ 143 public static void setMessage(String message, BlackBoard b, boolean formatIt) { 144 if (formatIt) message = htmlFormat(message); 145 GHTMLPageComponent c = getCurrentGHTGhtmlPageComponent(b); 146 if (c == null) JOptionPane.showMessageDialog(null, message); 147 else 148 getCurrentGHTGhtmlPageComponent(b).setMessage(message); 149 } 150 151 /** 152 * hides the previously showing message 153 */ 154 public static void hideNotificationMessage(BlackBoard b) { 155 GHTMLPageComponent pc = getCurrentGHTGhtmlPageComponent(b); 156 if (pc != null) 157 pc.hideNotificationMessage(); 158 } 159 160 /** 161 * shows a message and hide it after a given time 162 */ 163 public static void showTimeNotificationMessage(String message, final BlackBoard b, final long timeMillis, boolean formatIt) { 164 showNotificationMessage(message, b, formatIt); 165 new Thread() { 166 public void run() { 167 try { 168 Thread.sleep(timeMillis + 10000); 169 hideNotificationMessage(b); 170 } catch (InterruptedException e) { 171 e.printStackTrace(); 172 } 173 } 174 }.start(); 175 176 } 177 178 static String htmlFormat(String message) { 179 return "<HTML><BODY><CENTER><B>" + message + "</B></CENTER></BODY></HTML>"; 180 } 181 182 //------------------------------------------------ 183 /** 184 * returns the current GHTMLPageComponent which is used in the top of a tab, 185 * if there is not any of them returns null 186 * 187 * @param b 188 * @return 189 * @see GTabbedPane 190 */ 191 public static GHTMLPageComponent getCurrentGHTGhtmlPageComponent(BlackBoard b) { 192 Object o = b.getData(CURRENT_COMPONENT); 193 if (o instanceof GSplitedPane) { 194 GSplitedPane g = (GSplitedPane) o; 195 return g.helper; 196 } 197 return null; 198 199 } 200 } 201