/
SessionEditor.java
190 lines (158 loc) · 6.72 KB
/
SessionEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
///////////////////////////////////////////////////////////////////////////////
// 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, 2022 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.app;
import edu.cmu.tetrad.graph.Edge;
import edu.cmu.tetrad.graph.Node;
import edu.cmu.tetrad.session.Session;
import edu.cmu.tetradapp.editor.SaveComponentImage;
import edu.cmu.tetradapp.model.SessionWrapper;
import edu.cmu.tetradapp.util.SessionEditorIndirectRef;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
/**
* Displays a toolbar and workbench for editing Session's.
*
* @author josephramsey
* @see SessionEditorWorkbench
* @see SessionEditorToolbar
*/
public final class SessionEditor extends JComponent
implements SessionEditorIndirectRef {
/**
* The toolbar displayed on the left-hand side of the session editor.
*/
private SessionEditorToolbar toolbar;
/**
* The workbench displayed on the right-hand side of the session editor.
*/
private SessionEditorWorkbench workbench;
/**
* Constucts a session editor with the given name.
*/
public SessionEditor(String name) {
this(name, null);
}
/**
* Constructs a new session editor. A session editor consists of a session
* workbench and a session toolbar.
*
* @param name The name of the session. This is used for saving out
*/
public SessionEditor(String name, SessionEditorWorkbench workbench) {
setName(name);
if (workbench == null) {
Session session = new Session(name);
SessionWrapper wrapper = new SessionWrapper(session);
workbench = new SessionEditorWorkbench(wrapper);
}
workbench.setName(name);
setWorkbench(workbench);
setToolbar(new SessionEditorToolbar(workbench));
JScrollPane workbenchScroll = new JScrollPane(workbench);
setLayout(new BorderLayout());
add(workbenchScroll, BorderLayout.CENTER);
add(getToolbar(), BorderLayout.WEST);
workbench.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if ("name".equals(propertyName)) {
firePropertyChange("name", e.getOldValue(),
e.getNewValue());
}
}
});
}
/**
* @return the session workbench.
*/
public SessionEditorWorkbench getSessionWorkbench() {
return getWorkbench();
}
/**
* @return a list of all the SessionNodeWrappers (TetradNodes) and
* SessionNodeEdges that are model components for the respective
* SessionNodes and SessionEdges selected in the workbench. Note that the
* workbench, not the SessionEditorNodes themselves, keeps track of the
* selection.
*/
public List getSelectedModelComponents() {
List selectedComponents = getWorkbench().getSelectedComponents();
List selectedModelComponents = new ArrayList();
for (Object comp : selectedComponents) {
if (comp instanceof SessionEditorNode) {
SessionEditorNode editorNode = (SessionEditorNode) comp;
Node modelNode = editorNode.getModelNode();
selectedModelComponents.add(modelNode);
} else if (comp instanceof SessionEditorEdge) {
SessionEditorEdge editorEdge = (SessionEditorEdge) comp;
Edge modelEdge = getWorkbench().getModelEdge(editorEdge);
selectedModelComponents.add(modelEdge);
}
}
return selectedModelComponents;
}
/**
* Pastes a consistent list of model nodes into the workbench. Note that the
* responsivity of the toolbar to events needs to be turned off during this
* operation.
*
* @param sessionElements the list of model nodes.
* @param point the upper left corner of the first node.
*/
public void pasteSubsession(List sessionElements, Point point) {
getToolbar().setRespondingToEvents(false);
getWorkbench().pasteSubsession(sessionElements, point);
getToolbar().setRespondingToEvents(true);
}
/**
* Sets the name of the session editor.
*/
public void setName(String name) {
if (name == null) {
throw new NullPointerException("Name must not be null.");
}
super.setName(name);
}
private SessionEditorToolbar getToolbar() {
return this.toolbar;
}
private void setToolbar(SessionEditorToolbar toolbar) {
this.toolbar = toolbar;
}
private SessionEditorWorkbench getWorkbench() {
return this.workbench;
}
private void setWorkbench(SessionEditorWorkbench workbench) {
this.workbench = workbench;
}
public void saveSessionImage() {
Action action = new SaveComponentImage(this.workbench, "Save Session Image...");
action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Save"));
}
public void firePropertyChange(String s, Object o, String name) {
super.firePropertyChange(s, o, name);
}
}