-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPathwayTableModel.java
More file actions
280 lines (250 loc) · 8.18 KB
/
Copy pathPathwayTableModel.java
File metadata and controls
280 lines (250 loc) · 8.18 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
* PathVisio, a tool for data visualization and analysis using biological pathways
* Copyright 2006-2022 BiGCaT Bioinformatics, WikiPathways
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package org.pathvisio.gui.handler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import org.pathvisio.core.ApplicationEvent;
import org.pathvisio.core.Engine.ApplicationEventListener;
import org.pathvisio.core.view.model.VPathwayModel;
import org.pathvisio.core.view.model.VPathwayObject;
import org.pathvisio.core.view.model.SelectionBox.SelectionEvent;
import org.pathvisio.core.view.model.SelectionBox.SelectionListener;
import org.pathvisio.core.view.model.VPathwayElement;
import org.pathvisio.gui.SwingEngine;
import org.pathvisio.libgpml.model.PathwayElement;
import org.pathvisio.libgpml.model.PathwayObject;
import org.pathvisio.libgpml.model.PathwayObjectEvent;
import org.pathvisio.libgpml.model.PathwayObjectListener;
import org.pathvisio.libgpml.prop.StaticProperty;
/**
* The model for the table in the Properties side panel. Each row corresponds to
* a Property, the first column is the name of the property and the second
* column its value.
*
* This will pass through the properties for zero, one or many selected
* PathwayElements. If many are selected, the subset of shared Properties is
* used as row set.
*/
public class PathwayTableModel extends AbstractTableModel
implements SelectionListener, PathwayObjectListener, ApplicationEventListener {
private JTable table;
final private Collection<PathwayObject> input;
final private Map<Object, PropertyView> propertyValues;
final private List<PropertyView> shownProperties;
private SwingEngine swingEngine;
public PathwayTableModel(SwingEngine swingEngine) {
input = new HashSet<PathwayObject>();
propertyValues = new HashMap<Object, PropertyView>();
shownProperties = new ArrayList<PropertyView>();
this.swingEngine = swingEngine;
swingEngine.getEngine().addApplicationEventListener(this);
VPathwayModel vp = swingEngine.getEngine().getActiveVPathwayModel();
if (vp != null)
vp.addSelectionListener(this);
}
public void setTable(JTable table) {
this.table = table;
}
private void reset() {
stopEditing();
for (PathwayObject e : input) {
// System.err.println("Removed " + e);
e.removeListener(this);
}
propertyValues.clear();
shownProperties.clear();
input.clear();
refresh(true);
}
private void removeInput(PathwayElement pwElm) {
stopEditing();
// System.err.println("Input removed");
input.remove(pwElm);
if (input.size() > 0) {
updatePropertyCounts(pwElm, true);
pwElm.removeListener(this);
refresh(true);
} else {
reset();
}
}
private void stopEditing() {
if (table != null && table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
}
}
private void addInput(PathwayElement pwElm) {
stopEditing();
// System.err.println("Input added");
input.add(pwElm);
updatePropertyCounts(pwElm, false);
pwElm.addListener(this);
refresh(true);
}
protected void refresh() {
refresh(false);
}
public void refresh(boolean propertyCount) {
if (propertyCount) {
updateShownProperties();
}
refreshPropertyValues();
fireTableDataChanged();
}
/**
* Add/remove properties to/from the table model.
*
* @param e the PathwayElement with the properties of interest
* @param remove true if the PathwayElement's visible properties should be
* removed from the table model, false if it should be added to
* the table model
*/
public void updatePropertyCounts(PathwayElement e, boolean remove) {
for (Object o : PropertyDisplayManager.getVisiblePropertyKeys(e)) {
PropertyView tp = propertyValues.get(o);
if (tp == null) {
propertyValues.put(o, tp = new PropertyView(swingEngine.getEngine().getActiveVPathwayModel(), o));
}
if (remove) {
tp.removeElement(e);
} else {
tp.addElement(e);
}
}
}
protected void updateShownProperties() {
for (PropertyView tp : propertyValues.values()) {
boolean shown = shownProperties.contains(tp);
if (tp.elementCount() == input.size()) {
// System.err.println("\tadding " + tp + " from shown");
if (!shown)
shownProperties.add(tp);
} else {
// System.err.println("\tremoving " + tp + " from shown");
shownProperties.remove(tp);
}
Collections.sort(shownProperties);
}
}
protected void refreshPropertyValues() {
for (PropertyView p : shownProperties) {
p.refreshValue();
}
}
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return shownProperties.size();
}
public PropertyView getPropertyAt(int row) {
return shownProperties.get(row);
}
public Object getValueAt(int rowIndex, int columnIndex) {
PropertyView p = getPropertyAt(rowIndex);
if (columnIndex == 0)
return p.getName();
else
return p.getValue();
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex != 0) {
PropertyView p = getPropertyAt(rowIndex);
p.setValue(aValue);
p.refreshValue();//refreshing cached value
fireTableCellUpdated(rowIndex, columnIndex); // Fix: add this line to notify the table that the cell value has changed
}
swingEngine.getEngine().getActiveVPathwayModel().redraw();
}
public String getColumnName(int column) {
if (column == 0)
return "Property";
return "Value";
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
// do not allow editing of pathway authors for now TODO
if (getPropertyAt(rowIndex).getType() == StaticProperty.AUTHOR) {
return false;
}
// cannot directly edit aliasRef
if (getPropertyAt(rowIndex).getType() == StaticProperty.ALIASREF) {
return false;
}
return columnIndex == 1 && swingEngine.getEngine().hasVPathwayModel()
&& swingEngine.getEngine().getActiveVPathwayModel().isEditMode();
}
public void selectionEvent(SelectionEvent e) {
switch (e.type) {
case SelectionEvent.OBJECT_ADDED:
// System.err.println("OBJECT ADDED");
if (e.affectedObject instanceof VPathwayObject)
addInput(((VPathwayElement) e.affectedObject).getPathwayObject());
break;
case SelectionEvent.OBJECT_REMOVED:
// System.err.println("OBJECT REMOVED");
if (e.affectedObject instanceof VPathwayObject)
removeInput(((VPathwayElement) e.affectedObject).getPathwayObject());
break;
case SelectionEvent.SELECTION_CLEARED:
// System.err.println("CLEARED");
reset();
break;
}
}
public TableCellRenderer getCellRenderer(int row, int column) {
PropertyView propHandler = getPropertyAt(row);
if (propHandler != null) {
if (column == 0) {
return propHandler.getLabelRenderer();
} else {
return propHandler.getCellRenderer();
}
}
return null;
}
public TableCellEditor getCellEditor(int row, int column) {
if (column != 0) {
PropertyView tp = getPropertyAt(row);
if (tp != null)
return tp.getCellEditor(swingEngine);
}
return null;
}
public void gmmlObjectModified(PathwayObjectEvent e) {
refresh();
}
public void applicationEvent(ApplicationEvent e) {
switch (e.getType()) {
case VPATHWAY_CREATED:
((VPathwayModel) e.getSource()).addSelectionListener(this);
break;
case VPATHWAY_DISPOSED:
((VPathwayModel) e.getSource()).removeSelectionListener(this);
reset(); // clear selected set
break;
}
}
}