TrustGrapher
r52
A playabale simulator for modelling trust between agents
|
00001 00002 package cu.trustGrapher; 00003 00004 import cu.trustGrapher.loading.*; 00005 import cu.trustGrapher.eventplayer.*; 00006 import cu.trustGrapher.visualizer.*; 00007 import cu.repsystestbed.entities.Agent; 00008 import cu.repsystestbed.graphs.TestbedEdge; 00009 00010 import edu.uci.ics.jung.algorithms.layout.*; 00011 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse; 00012 00013 import java.awt.*; 00014 import java.awt.event.*; 00015 import javax.swing.*; 00016 00017 import java.util.List; 00018 import java.util.LinkedList; 00019 00020 import aohara.utilities.*; 00021 import cu.trustGrapher.loading.GraphLoader; 00022 import cu.trustGrapher.graphs.SimAbstractGraph; 00023 00032 public class TrustGrapher extends JFrame { 00033 00034 public static final int CURRENT_REVISION = 52; 00035 public static final int TABBED = 0, GRID = 1; //View types 00036 public static final int DEFWIDTH = 1360, DEFHEIGHT = 768; //default size for the swing graphic components 00037 protected TrustMenuBar menuBar; 00038 protected List<GraphViewer> viewers; //Each of the viewers is a component which displays a graph 00039 protected List<SimAbstractGraph> graphs; //A list of the the graph pairs attached to the viewers 00040 protected ViewerPopupMenu popupMenu; //The popup menu that is shown when a viewer is right-clicked 00041 protected PropertyManager config; //The Property Manager that contains all of the saved algorithmLoader for the applet and algorithm loader 00042 protected Container graphsPanel; //This container contains all of the TrustGraphViewers as components 00043 protected EventPlayer eventThread; //Plays through the list of events and updates the graphs and its' listeners 00044 00046 00049 public TrustGrapher() { 00050 String configPath = getClass().getResource("").getPath(); 00051 configPath = configPath.replace("file:", ""); 00052 if (configPath.contains(".jar")) { //if this class is in a jar, save the properties file next to it 00053 configPath = configPath.substring(0, configPath.indexOf("!")) + "TrustGrapher.properties"; 00054 } else { //Otherwise, save it in the project root 00055 configPath = "TrustGrapher.properties"; 00056 } 00057 config = new PropertyManager(configPath); 00058 initComponents(); 00059 enableMenu(false); 00060 setVisible(true); 00061 startAlgorithmLoader(); 00062 enableMenu(true); 00063 } 00064 00066 public boolean graphsLoaded() { 00067 return eventThread != null && viewers.get(0) != null && graphsPanel != null; 00068 } 00069 00073 public List<SimAbstractGraph> getGraphs() { 00074 return graphs; 00075 } 00076 00082 public Integer getViewType() { 00083 String s = config.getProperty("viewType"); 00084 try { 00085 return Integer.parseInt(s); 00086 } catch (NumberFormatException ex) { 00087 return TABBED; //The default view if the property can't be read or doesn't exist 00088 } 00089 } 00090 00094 public PropertyManager getPropertyManager() { 00095 return config; 00096 } 00097 00104 public List<GraphViewer> getVisibleViewers() { 00105 if (graphsPanel instanceof JTabbedPane) { 00106 List<GraphViewer> visibleViewers = new java.util.LinkedList<GraphViewer>(); 00107 visibleViewers.add((GraphViewer) ((JTabbedPane) graphsPanel).getSelectedComponent()); 00108 return visibleViewers; 00109 } else { 00110 return viewers; 00111 } 00112 } 00113 00114 public EventPlayer getEventPlayer() { 00115 return eventThread; 00116 } 00117 00124 public TrustMenuBar getTrustMenuBar() { 00125 return menuBar; 00126 } 00127 00129 00135 public void algorithmsLoaded(List<GraphConfig> graphConfigs) { 00136 //If a log path has been selected by the Algortihm Loader 00137 if (config.containsKey(AlgorithmLoader.LOG_PATH)) { 00138 //If there are any events currently loaded, pause the simulator 00139 if (graphsLoaded()) { 00140 eventThread.pause(); 00141 } 00142 00143 //Build graphs based on graphConfigs 00144 graphs = GraphLoader.loadGraphs(graphConfigs); 00145 00146 //Begin reading the log and performing graphConstructionEvents 00147 LogReader.startReader(this, new java.io.File(config.getProperty(AlgorithmLoader.LOG_PATH)), new AreWeThereYet(this)); 00148 } else { 00149 ChatterBox.alert("No log was loaded, so no action will be taken."); 00150 } 00151 } 00152 00157 public void startAlgorithmLoader() { 00158 AlgorithmLoader.run(this, config); 00159 } 00160 00165 public void enableMenu(boolean enabled) { 00166 for (Component menu : getJMenuBar().getComponents()) { 00167 menu.setEnabled(enabled); 00168 } 00169 } 00170 00172 00175 private void initComponents() { 00176 //Initialize frame 00177 setTitle("TrustGrapher r" + CURRENT_REVISION + " - Written by Andrew O'Hara"); 00178 getContentPane().setFont(new Font("Arial", Font.PLAIN, 12)); 00179 getContentPane().setBounds(0, 0, DEFWIDTH, DEFHEIGHT); 00180 setPreferredSize(new Dimension(DEFWIDTH, DEFHEIGHT)); 00181 setExtendedState(JFrame.MAXIMIZED_BOTH); 00182 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 00183 00184 this.menuBar = new TrustMenuBar(this); 00185 setJMenuBar(menuBar.getJMenuBar()); 00186 } 00187 00195 public void startGraph(List<TrustLogEvent> events) { 00196 //Creates a new graphsPanel depending on the view type 00197 graphsPanel = (getViewType() == GRID) ? new JPanel(new GridLayout(2, 3)) : new JTabbedPane(JTabbedPane.TOP); 00198 graphsPanel.setBackground(Color.LIGHT_GRAY); 00199 00200 //Create viewer listeners and popup menu 00201 DefaultModalGraphMouse<Agent, TestbedEdge> gm = new DefaultModalGraphMouse<Agent, TestbedEdge>(); 00202 ViewerListener listener = new ViewerListener(); 00203 popupMenu = new ViewerPopupMenu(gm, listener); 00204 00205 //Create the Visualization Viewers 00206 viewers = new LinkedList<GraphViewer>(); 00207 for (SimAbstractGraph graph : graphs) { 00208 if (graph.isDisplayed()) { 00209 //Sets the initial layout of the graph. The graphs must have already had their construction events processed, or the graphs will have a random layout 00210 AbstractLayout<Agent, TestbedEdge> layout = new FRLayout<Agent, TestbedEdge>(graph.getReferenceGraph()); 00211 layout.setInitializer(new VertexPlacer(new Dimension (DEFWIDTH / 3, DEFHEIGHT /2))); 00212 //Creates the new GraphViewer 00213 GraphViewer viewer = new GraphViewer(layout, gm, listener, graph); 00214 if (graphsPanel instanceof JPanel) { //If the graphsPanel is set for grid view 00215 viewer.setBorder(BorderFactory.createTitledBorder(graph.getDisplayName())); 00216 ((JPanel) graphsPanel).add(viewer); 00217 } else { //Othwerwise, the graphsPanel is set for Tabbed view, which is the default. 00218 ((JTabbedPane) graphsPanel).addTab(graph.getDisplayName(), viewer); 00219 } 00220 layout.lock(true); //Locking the layout will prevent the graph entities from moving around 00221 viewers.add(viewer); 00222 } 00223 } 00224 00225 //Create the eventThread and its listener panels for the simulator 00226 eventThread = new EventPlayer(this, events); 00227 eventThread.addEventPlayerListener(new PlaybackPanel(eventThread)); 00228 eventThread.addEventPlayerListener(new LogPanel(eventThread)); 00229 00230 //Create the mainPane, add the graphsPanel and playbackPanel, and add the mainPane to the content pane 00231 getContentPane().removeAll(); //Necessary to removeAll since there might already be existing viewers present 00232 JSplitPane secondaryPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, graphsPanel, eventThread.getPlaybackPanel()); 00233 JSplitPane primaryPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, secondaryPane, eventThread.getLogPanel()); 00234 primaryPane.setResizeWeight(1); 00235 secondaryPane.setResizeWeight(1); //Means the size of the playbackPanel will set the position of the divider 00236 primaryPane.setDividerSize(3); 00237 secondaryPane.setDividerSize(3); 00238 getContentPane().add(primaryPane); //The main pane includes the graphsPanel and the playbackPanel 00239 validate(); 00240 } 00241 00243 00248 private class ViewerListener extends MouseAdapter implements ActionListener { 00249 00250 @Override 00254 public void mousePressed(MouseEvent mouseClick) { 00255 if (SwingUtilities.isRightMouseButton(mouseClick)) { 00256 popupMenu.showPopupMenu((GraphViewer) mouseClick.getComponent()); 00257 } 00258 } 00259 00264 public void actionPerformed(ActionEvent buttonEvent) { 00265 popupMenu.popupMenuEvent(((AbstractButton) buttonEvent.getSource()).getText()); 00266 } 00267 } 00268 00270 00273 public static void main(String[] args) { 00274 TrustGrapher myApp = new TrustGrapher(); 00275 } 00276 }