Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015 by Peter Spirtes, Richard Scheines, Joseph //
// Ramsey, and Clark Glymour. //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetradapp.editor;
import edu.cmu.tetrad.graph.*;
import edu.cmu.tetrad.util.TetradSerializable;
import edu.cmu.tetradapp.model.GraphSelectionWrapper;
import edu.cmu.tetradapp.ui.DualListPanel;
import edu.cmu.tetradapp.ui.PaddingPanel;
import edu.cmu.tetradapp.util.DesktopController;
import edu.cmu.tetradapp.util.ImageUtils;
import edu.cmu.tetradapp.util.IntTextField;
import edu.cmu.tetradapp.util.WatchedProcess;
import edu.cmu.tetradapp.workbench.DisplayEdge;
import edu.cmu.tetradapp.workbench.DisplayNode;
import edu.cmu.tetradapp.workbench.GraphWorkbench;
import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.*;
/**
* Lets the user select a subgraph of a possible large graph and display it.
*
* @author josephramsey
* @author Zhou Yuan
*/
public class GraphSelectionEditor extends JPanel implements GraphEditable, TripleClassifier {
private static final long serialVersionUID = 2754618060275627122L;
private final GraphEditorOptionsPanel graphEditorOptionsPanel;
private final JComboBox graphTypeCombo = new JComboBox<>();
private final HelpSet helpSet;
/**
* Holds the graphs.
*/
private final GraphSelectionWrapper wrapper;
private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
private final List workbenches = new ArrayList<>();
// Need this initialization. Don't delete.
private JPanel workbenchScrollsPanel = new JPanel();
private GraphPropertiesAction graphAction;
private Map> layoutGraph;
private int prevSelected = 0;
/**
* Constructs a graph selection editor.
*
* @throws NullPointerException if wrapper is null.
*/
public GraphSelectionEditor(GraphSelectionWrapper wrapper) {
if (wrapper == null) {
throw new NullPointerException("The regression wrapper is required.");
}
this.wrapper = wrapper;
if (layoutGraph == null) {
layoutGraph = new HashMap<>();
}
// Initialize helpSet - Zhou
String helpHS = "/docs/javahelp/TetradHelp.hs";
try {
URL url = this.getClass().getResource(helpHS);
this.helpSet = new HelpSet(null, url);
} catch (Exception ee) {
System.out.println("HelpSet " + ee.getMessage());
System.out.println("HelpSet " + helpHS + " not found");
throw new IllegalArgumentException();
}
setLayout(new BorderLayout());
// Must before calling setSelectedGraphType()
graphEditorOptionsPanel = new GraphEditorOptionsPanel(wrapper);
// Select the graph type if graph wrapper has the type info
setSelectedGraphType(wrapper.getType());
// Graph panel on right
workbenchScrollsPanel = workbenchScrollsPanel(wrapper);
// splitPane contains subgraph setting options on left and graph on right
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setLeftComponent(new PaddingPanel(graphEditorOptionsPanel));
splitPane.setRightComponent(new PaddingPanel(workbenchScrollsPanel));
splitPane.setDividerLocation(383);
// Bottom panel contains "Graph It" button and info on edge types
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
// "Graph It" button
JButton executeButton = new JButton("Graph It!");
executeButton.addActionListener(e -> {
class MyWatchedProcess extends WatchedProcess {
public void watch() {
GraphWorkbench workbench = getWorkbench();
List displayNodes = workbench.getSelectedNodes();
List newSelected = new ArrayList<>();
for (DisplayNode node : displayNodes) {
newSelected.add(node.getModelNode());
}
if (!newSelected.isEmpty()) {
graphEditorOptionsPanel.setSelected(newSelected);
}
tabbedPaneGraphs(wrapper);
}
}
;
new MyWatchedProcess();
});
workbenchScrollsPanel.validate();
// Add to buttonPanel
buttonPanel.add(executeButton);
// Info button added by Zhou to show edge types
JLabel infoLabel = new JLabel("More information on graph edge types and colorings");
infoLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
// Info button added by Zhou to show edge types
JButton infoBtn = new JButton(new ImageIcon(ImageUtils.getImage(this, "info.png")));
infoBtn.setBorder(new EmptyBorder(0, 0, 0, 0));
// Clock info button to show edge types instructions - Zhou
infoBtn.addActionListener(e -> {
helpSet.setHomeID("graph_edge_types");
HelpBroker broker = helpSet.createHelpBroker();
ActionListener listener = new CSH.DisplayHelpFromSource(broker);
listener.actionPerformed(e);
});
// Add to buttonPanel
buttonPanel.add(infoLabel);
buttonPanel.add(infoBtn);
// Add top level componments to container
add(createTopMenuBar(), BorderLayout.NORTH);
add(splitPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
graphEditorOptionsPanel.reset();
}
// Top menu bar, contains "Save As" and "Graph"
private JMenuBar createTopMenuBar() {
JMenuBar menuBar = new JMenuBar();
// Add the save options - Zhou
JMenu saveMenu = createSaveMenu(this);
menuBar.add(saveMenu);
// Add the graph options
JMenu graphMenu = createGraphMenu();
menuBar.add(graphMenu);
return menuBar;
}
// Graph type selection
private void graphTypeSelection() {
for (GraphSelectionWrapper.Type type : GraphSelectionWrapper.Type.values()) {
graphTypeCombo.addItem(type);
}
graphTypeCombo.setSelectedItem(wrapper.getType());
graphTypeCombo.addActionListener(e -> {
GraphSelectionWrapper.Type selectedItem = (GraphSelectionWrapper.Type) graphTypeCombo.getSelectedItem();
assert selectedItem != null;
wrapper.setType(selectedItem);
setSelectedGraphType(selectedItem);
});
}
private void setSelectedGraphType(GraphSelectionWrapper.Type type) {
if (type == GraphSelectionWrapper.Type.Subgraph) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Adjacents) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Adjacents_of_Adjacents) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Adjacents_of_Adjacents_of_Adjacents) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Pag_Y_Structures) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Markov_Blankets) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Y_Structures) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Pag_Y_Structures) {
graphEditorOptionsPanel.setNLabel("");
}
if (type == GraphSelectionWrapper.Type.Treks) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Trek_Edges) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Paths) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Path_Edges) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Directed_Paths) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Directed_Path_Edges) {
graphEditorOptionsPanel.setNLabel("Path Length");
}
if (type == GraphSelectionWrapper.Type.Indegree) {
graphEditorOptionsPanel.setNLabel("Indegree");
}
if (type == GraphSelectionWrapper.Type.Out_Degree) {
graphEditorOptionsPanel.setNLabel("Outdegree");
}
if (type == GraphSelectionWrapper.Type.Degree) {
graphEditorOptionsPanel.setNLabel("");
}
}
// Create scroll pane for each graph
private JPanel workbenchScrollsPanel(GraphSelectionWrapper wrapper) {
List workbenchScrolls = new ArrayList<>();
List graphs = wrapper.getGraphs();
for (int i = 0; i < graphs.size(); i++) {
GraphWorkbench workbench = new GraphWorkbench();
workbenches.add(workbench);
workbench.addPropertyChangeListener(evt -> {
if ("modelChanged".equals(evt.getPropertyName())) {
firePropertyChange("modelChanged", null, null);
}
});
JScrollPane workbenchScroll = new JScrollPane(workbench);
workbenchScroll.setPreferredSize(new Dimension(520, 560));
workbenchScrolls.add(workbenchScroll);
}
for (int i = 0; i < workbenchScrolls.size(); i++) {
tabbedPane.add("" + (i + 1), workbenchScrolls.get(i));
}
tabbedPane.addChangeListener(e -> {
if (e.getSource() instanceof JTabbedPane) {
JTabbedPane panel = (JTabbedPane) e.getSource();
int selectedIndex = panel.getSelectedIndex();
selectedIndex = selectedIndex == -1 ? 0 : selectedIndex;
graphAction.setGraph(wrapper.getGraphs().get(selectedIndex), getWorkbench());
}
});
// Show graph in each tabbed pane
tabbedPaneGraphs(wrapper);
// Make the tabbedPane auto resize - Zhou
workbenchScrollsPanel.setLayout(new BorderLayout());
workbenchScrollsPanel.add(tabbedPane, BorderLayout.CENTER);
workbenchScrollsPanel.validate();
return workbenchScrollsPanel;
}
private void tabbedPaneGraphs(GraphSelectionWrapper wrapper) {
wrapper.calculateSelection();
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Graph selection = wrapper.getSelectionGraph(i);
if (!layoutGraph.isEmpty()) {
for (Node node : selection.getNodes()) {
List center = layoutGraph.get(node.getName());
if (center != null) {
node.setCenter(center.get(0), center.get(1));
}
}
}
GraphWorkbench workbench = getWorkbench(i);
workbench.setGraph(selection);
List selected = wrapper.getSelectedVariables();
for (Node node : selected) {
if (wrapper.getHighlightInEditor().contains(node)
&& workbench.getGraph().containsNode(node)) {
workbench.selectNode(node);
}
}
}
}
/**
* File save menu - Zhou
*/
private JMenu createSaveMenu(GraphEditable editable) {
JMenu save = new JMenu("Save As");
save.add(new SaveGraph(editable, "Graph XML...", SaveGraph.Type.xml));
save.add(new SaveGraph(editable, "Graph Text...", SaveGraph.Type.text));
save.add(new SaveGraph(editable, "Graph Json...", SaveGraph.Type.json));
save.add(new SaveGraph(editable, "R...", SaveGraph.Type.r));
save.add(new SaveGraph(editable, "Dot...", SaveGraph.Type.dot));
return save;
}
private JMenu createGraphMenu() {
JMenu graph = new JMenu("Graph");
graphAction = new GraphPropertiesAction(getWorkbench());
graph.add(graphAction);
graph.add(new PathsAction(getWorkbench()));
UnderliningsAction underliningsAction = new UnderliningsAction(getWorkbench());
graph.add(underliningsAction);
return graph;
}
/**
* Sets the name of this editor.
*/
@Override
public void setName(String name) {
String oldName = getName();
super.setName(name);
this.firePropertyChange("name", oldName, getName());
}
@Override
public List getSelectedModelComponents() {
List selectedComponents
= getWorkbench().getSelectedComponents();
List selectedModelComponents
= new ArrayList<>();
for (Component comp : selectedComponents) {
if (comp instanceof DisplayNode) {
selectedModelComponents.add(
((DisplayNode) comp).getModelNode());
} else if (comp instanceof DisplayEdge) {
selectedModelComponents.add(
((DisplayEdge) comp).getModelEdge());
}
}
return selectedModelComponents;
}
@Override
public void pasteSubsession(List