| 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 |
package dr.app.beauti.operatorspanel; |
| 27 |
|
| 28 |
import dr.app.beauti.BeautiFrame; |
| 29 |
import dr.app.beauti.BeautiPanel; |
| 30 |
import dr.app.beauti.options.BeautiOptions; |
| 31 |
import dr.app.beauti.options.Operator; |
| 32 |
import dr.app.beauti.types.OperatorSetType; |
| 33 |
import dr.app.beauti.util.PanelUtils; |
| 34 |
import dr.app.gui.table.RealNumberCellEditor; |
| 35 |
import jam.framework.Exportable; |
| 36 |
import jam.table.HeaderRenderer; |
| 37 |
import jam.table.TableRenderer; |
| 38 |
|
| 39 |
import javax.swing.*; |
| 40 |
import javax.swing.plaf.BorderUIResource; |
| 41 |
import javax.swing.table.AbstractTableModel; |
| 42 |
import java.awt.*; |
| 43 |
import java.awt.event.ActionEvent; |
| 44 |
import java.util.*; |
| 45 |
import java.util.List; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class OperatorsPanel extends BeautiPanel implements Exportable { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private static final long serialVersionUID = -3456667023451785854L; |
| 58 |
JScrollPane scrollPane = new JScrollPane(); |
| 59 |
JTable operatorTable = null; |
| 60 |
OperatorTableModel operatorTableModel = null; |
| 61 |
|
| 62 |
JCheckBox autoOptimizeCheck = null; |
| 63 |
|
| 64 |
JComboBox operatorSetCombo = new JComboBox(new OperatorSetType[] { |
| 65 |
OperatorSetType.DEFAULT, |
| 66 |
OperatorSetType.FIXED_TREE_TOPOLOGY, |
| 67 |
OperatorSetType.NEW_TREE_MIX, |
| 68 |
OperatorSetType.ADAPTIVE_MULTIVARIATE, |
| 69 |
OperatorSetType.CUSTOM |
| 70 |
}); |
| 71 |
|
| 72 |
public List<Operator> operators = new ArrayList<Operator>(); |
| 73 |
|
| 74 |
private BeautiOptions options; |
| 75 |
BeautiFrame frame = null; |
| 76 |
|
| 77 |
public OperatorsPanel(BeautiFrame parent) { |
| 78 |
|
| 79 |
this.frame = parent; |
| 80 |
|
| 81 |
operatorTableModel = new OperatorTableModel(); |
| 82 |
operatorTable = new JTable(operatorTableModel); |
| 83 |
|
| 84 |
operatorTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); |
| 85 |
operatorTable.getTableHeader().setReorderingAllowed(false); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
operatorTable.getColumnModel().getColumn(0).setMinWidth(40); |
| 90 |
operatorTable.getColumnModel().getColumn(0).setMaxWidth(40); |
| 91 |
|
| 92 |
operatorTable.getColumnModel().getColumn(1).setCellRenderer( |
| 93 |
new OperatorTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 94 |
operatorTable.getColumnModel().getColumn(1).setMinWidth(200); |
| 95 |
|
| 96 |
operatorTable.getColumnModel().getColumn(2).setCellRenderer( |
| 97 |
new OperatorTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 98 |
operatorTable.getColumnModel().getColumn(2).setMinWidth(140); |
| 99 |
|
| 100 |
operatorTable.getColumnModel().getColumn(3).setCellRenderer( |
| 101 |
new OperatorTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 102 |
operatorTable.getColumnModel().getColumn(3).setCellEditor( |
| 103 |
new RealNumberCellEditor(0, Double.POSITIVE_INFINITY)); |
| 104 |
operatorTable.getColumnModel().getColumn(3).setMinWidth(40); |
| 105 |
|
| 106 |
operatorTable.getColumnModel().getColumn(4).setCellRenderer( |
| 107 |
new OperatorTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 108 |
operatorTable.getColumnModel().getColumn(4).setCellEditor( |
| 109 |
new RealNumberCellEditor(0, Double.MAX_VALUE)); |
| 110 |
operatorTable.getColumnModel().getColumn(4).setMinWidth(40); |
| 111 |
|
| 112 |
operatorTable.getColumnModel().getColumn(5).setCellRenderer( |
| 113 |
new OperatorTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 114 |
operatorTable.getColumnModel().getColumn(5).setMinWidth(380); |
| 115 |
|
| 116 |
scrollPane = new JScrollPane(operatorTable, |
| 117 |
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
| 118 |
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 119 |
|
| 120 |
scrollPane.setOpaque(false); |
| 121 |
|
| 122 |
autoOptimizeCheck = new JCheckBox("Auto Optimize"); |
| 123 |
autoOptimizeCheck.setToolTipText("<html>This option will attempt to tune the operators<br>" + |
| 124 |
"to maximum efficiency. Turn off to tune the<br>" + |
| 125 |
"operators manually.</html>"); |
| 126 |
autoOptimizeCheck.setOpaque(false); |
| 127 |
autoOptimizeCheck.addActionListener(new java.awt.event.ActionListener() { |
| 128 |
public void actionPerformed(ActionEvent e) { |
| 129 |
options.autoOptimize = autoOptimizeCheck.isSelected(); |
| 130 |
if (!autoOptimizeCheck.isSelected()) options.operatorAnalysis = true; |
| 131 |
frame.setDirty(); |
| 132 |
} |
| 133 |
}); |
| 134 |
|
| 135 |
JToolBar toolBar1 = new JToolBar(); |
| 136 |
toolBar1.setFloatable(false); |
| 137 |
toolBar1.setOpaque(false); |
| 138 |
toolBar1.setBorder(BorderFactory.createEmptyBorder()); |
| 139 |
toolBar1.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0)); |
| 140 |
toolBar1.add(autoOptimizeCheck); |
| 141 |
toolBar1.add(new JToolBar.Separator(new Dimension(12, 12))); |
| 142 |
final JLabel label = new JLabel("Operator mix: "); |
| 143 |
toolBar1.add(label); |
| 144 |
PanelUtils.setupComponent(operatorSetCombo); |
| 145 |
toolBar1.add(operatorSetCombo); |
| 146 |
|
| 147 |
setOpaque(false); |
| 148 |
setLayout(new BorderLayout(0, 0)); |
| 149 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12))); |
| 150 |
add(toolBar1, BorderLayout.NORTH); |
| 151 |
add(scrollPane, BorderLayout.CENTER); |
| 152 |
|
| 153 |
|
| 154 |
operatorSetCombo.addItemListener( |
| 155 |
new java.awt.event.ItemListener() { |
| 156 |
public void itemStateChanged(java.awt.event.ItemEvent ev) { |
| 157 |
options.operatorSetType = (OperatorSetType)operatorSetCombo.getSelectedItem(); |
| 158 |
operators = options.selectOperators(); |
| 159 |
operatorTableModel.fireTableDataChanged(); |
| 160 |
operatorsChanged(); |
| 161 |
} |
| 162 |
} |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
public final void operatorsChanged() { |
| 167 |
frame.setDirty(); |
| 168 |
} |
| 169 |
|
| 170 |
public void setOptions(BeautiOptions options) { |
| 171 |
this.options = options; |
| 172 |
|
| 173 |
operatorSetCombo.setSelectedItem(options.operatorSetType); |
| 174 |
|
| 175 |
autoOptimizeCheck.setSelected(options.autoOptimize); |
| 176 |
operators = options.selectOperators(); |
| 177 |
operatorTableModel.fireTableDataChanged(); |
| 178 |
} |
| 179 |
|
| 180 |
public void getOptions(BeautiOptions options) { |
| 181 |
} |
| 182 |
|
| 183 |
public JComponent getExportableComponent() { |
| 184 |
return operatorTable; |
| 185 |
} |
| 186 |
|
| 187 |
class OperatorTableModel extends AbstractTableModel { |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private static final long serialVersionUID = -575580804476182225L; |
| 193 |
String[] columnNames = {"In use", "Operates on", "Type", "Tuning", "Weight", "Description"}; |
| 194 |
|
| 195 |
public OperatorTableModel() { |
| 196 |
} |
| 197 |
|
| 198 |
public int getColumnCount() { |
| 199 |
return columnNames.length; |
| 200 |
} |
| 201 |
|
| 202 |
public int getRowCount() { |
| 203 |
return operators.size(); |
| 204 |
} |
| 205 |
|
| 206 |
public Object getValueAt(int row, int col) { |
| 207 |
Operator op = operators.get(row); |
| 208 |
switch (col) { |
| 209 |
case 0: |
| 210 |
return op.isUsed(); |
| 211 |
case 1: |
| 212 |
return op.getName(); |
| 213 |
case 2: |
| 214 |
return op.getOperatorType(); |
| 215 |
case 3: |
| 216 |
if (op.isTunable()) { |
| 217 |
return op.getTuning(); |
| 218 |
} else { |
| 219 |
return "n/a"; |
| 220 |
} |
| 221 |
case 4: |
| 222 |
return op.getWeight(); |
| 223 |
case 5: |
| 224 |
return op.getDescription(); |
| 225 |
} |
| 226 |
return null; |
| 227 |
} |
| 228 |
|
| 229 |
public void setValueAt(Object aValue, int row, int col) { |
| 230 |
Operator op = operators.get(row); |
| 231 |
switch (col) { |
| 232 |
case 0: |
| 233 |
op.setUsed((Boolean) aValue); |
| 234 |
operatorSetCombo.setSelectedItem(OperatorSetType.CUSTOM); |
| 235 |
break; |
| 236 |
case 3: |
| 237 |
op.setTuning((Double) aValue); |
| 238 |
break; |
| 239 |
case 4: |
| 240 |
op.setWeight((Double) aValue); |
| 241 |
break; |
| 242 |
} |
| 243 |
operatorsChanged(); |
| 244 |
} |
| 245 |
|
| 246 |
public String getColumnName(int column) { |
| 247 |
return columnNames[column]; |
| 248 |
} |
| 249 |
|
| 250 |
public Class getColumnClass(int c) { |
| 251 |
return getValueAt(0, c).getClass(); |
| 252 |
} |
| 253 |
|
| 254 |
public boolean isCellEditable(int row, int col) { |
| 255 |
boolean editable; |
| 256 |
|
| 257 |
Operator op = operators.get(row); |
| 258 |
|
| 259 |
switch (col) { |
| 260 |
case 0: |
| 261 |
|
| 262 |
editable = !op.isParameterFixed(); |
| 263 |
break; |
| 264 |
case 3: |
| 265 |
editable = op.isUsed() && op.isTunable(); |
| 266 |
break; |
| 267 |
case 4: |
| 268 |
editable = op.isUsed(); |
| 269 |
break; |
| 270 |
default: |
| 271 |
editable = false; |
| 272 |
} |
| 273 |
|
| 274 |
return editable; |
| 275 |
} |
| 276 |
|
| 277 |
public String toString() { |
| 278 |
StringBuffer buffer = new StringBuffer(); |
| 279 |
|
| 280 |
buffer.append(getColumnName(0)); |
| 281 |
for (int j = 1; j < getColumnCount(); j++) { |
| 282 |
buffer.append("\t"); |
| 283 |
buffer.append(getColumnName(j)); |
| 284 |
} |
| 285 |
buffer.append("\n"); |
| 286 |
|
| 287 |
for (int i = 0; i < getRowCount(); i++) { |
| 288 |
buffer.append(getValueAt(i, 0)); |
| 289 |
for (int j = 1; j < getColumnCount(); j++) { |
| 290 |
buffer.append("\t"); |
| 291 |
buffer.append(getValueAt(i, j)); |
| 292 |
} |
| 293 |
buffer.append("\n"); |
| 294 |
} |
| 295 |
|
| 296 |
return buffer.toString(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
class OperatorTableCellRenderer extends TableRenderer { |
| 301 |
|
| 302 |
public OperatorTableCellRenderer(int alignment, Insets insets) { |
| 303 |
super(alignment, insets); |
| 304 |
} |
| 305 |
|
| 306 |
public Component getTableCellRendererComponent(JTable aTable, |
| 307 |
Object value, |
| 308 |
boolean aIsSelected, |
| 309 |
boolean aHasFocus, |
| 310 |
int aRow, int aColumn) { |
| 311 |
|
| 312 |
if (value == null) return this; |
| 313 |
|
| 314 |
Component renderer = super.getTableCellRendererComponent(aTable, |
| 315 |
value, |
| 316 |
aIsSelected, |
| 317 |
aHasFocus, |
| 318 |
aRow, aColumn); |
| 319 |
|
| 320 |
Operator op = operators.get(aRow); |
| 321 |
if (!op.isUsed() && aColumn > 0) { |
| 322 |
renderer.setForeground(Color.gray); |
| 323 |
} else { |
| 324 |
renderer.setForeground(Color.black); |
| 325 |
} |
| 326 |
if (op.isParameterFixed()) { |
| 327 |
setToolTipText( |
| 328 |
"This parameter is set to a fixed value. To turn \r" + |
| 329 |
"this move on, select a prior in the Priors tab"); |
| 330 |
} |
| 331 |
|
| 332 |
return this; |
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
} |