$darkmode
DENOPTIM
GUIPreferencesDialog.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2020 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.gui;
20
21import java.awt.Component;
22import java.awt.Dimension;
23import java.awt.FlowLayout;
24import java.awt.event.ActionEvent;
25import java.awt.event.ActionListener;
26import java.util.Arrays;
27
28import javax.swing.BoxLayout;
29import javax.swing.GroupLayout;
30import javax.swing.JButton;
31import javax.swing.JComboBox;
32import javax.swing.JLabel;
33import javax.swing.JOptionPane;
34import javax.swing.JPanel;
35import javax.swing.JRadioButton;
36import javax.swing.JScrollPane;
37import javax.swing.JSeparator;
38import javax.swing.JTable;
39import javax.swing.JTextField;
40import javax.swing.SwingConstants;
41import javax.swing.UIManager;
42import javax.swing.table.DefaultTableModel;
43
44import denoptim.exception.DENOPTIMException;
45import denoptim.files.FileUtils;
46import denoptim.gui.GUIPreferences.SMITo3DEngine;
47
49{
50
54 private static final long serialVersionUID = -1416475901274128714L;
55
59 final Dimension fileLabelSize = new Dimension(250,28);
60
64 final int preferredHeight =
65 (int) (new JTextField()).getPreferredSize().getHeight();
66
70 final Dimension strFieldSize = new Dimension(75,preferredHeight);
71
72 private JPanel centralPanel;
73
74 private String namGraphTxtSize = "Font size for graph labels";
75 private JPanel pnlGraphTxtSize;
76 private JLabel lblGraphTxtSize;
77 private JTextField txtGraphTxtSize;
78
79 private String namGraphNodeSize = "Size of graph nodes";
80 private JPanel pnlGraphNodeSize;
81 private JLabel lblGraphNodeSize;
82 private JTextField txtGraphNodeSize;
83
84 private String namChartPointSize = "Size of points in evolution chart";
85 private JPanel pnlChartPointSize;
86 private JLabel lblChartPointSize;
87 private JTextField txtChartPointSize;
88
89 private String namEvoChartLegend = "Display legend in evolution chart";
90 private JPanel pnlEvoChartLegend;
91 private JRadioButton rcbEvoChartLegend;
92
93 private String namMntChartLegend = "Display legend in monitor chart";
94 private JPanel pnlMntChartLegend;
95 private JRadioButton rcbMntChartLegend;
96
97 private JPanel linePropTags;
98 private JLabel lblPropTags;
99 private DefaultTableModel tabModPropTags;
100 private JTable tabPropTags;
101 private JButton btnPropTagsInsert;
102 private JButton btnPropTagsCleanup;
103
104 private String namTmpSpace = "Folder for tmp files";
105 private JPanel pnlTmpSpace;
106 private JLabel lblTmpSpace;
107 private JTextField txtTmpSpace;
108
109 private String namSMILESTo3D = "SMILES-to-3D converer";
110 private JPanel pnlSMILESTo3D;
111 private JComboBox<SMITo3DEngine> cmbSMILESTo3D;
112 private JLabel lblSMILESTo3D;
113
114
115 private boolean inputIsOK = true;
116
117//------------------------------------------------------------------------------
118
123 @SuppressWarnings("serial")
124 public GUIPreferencesDialog(Component refForPlacement)
125 {
126 super(refForPlacement);
127 setTitle("Preferences");
128 centralPanel = new JPanel();
129 JScrollPane scrollablePane = new JScrollPane(centralPanel);
130 centralPanel.setLayout(new BoxLayout(
131 centralPanel, SwingConstants.VERTICAL));
132
133 JPanel titleGeneral = new JPanel(new FlowLayout(FlowLayout.LEFT));
134 titleGeneral.add(new JLabel("<html><b>General</b></html>"));
135 centralPanel.add(titleGeneral);
136
137 pnlTmpSpace = new JPanel(new FlowLayout(FlowLayout.LEFT));
138 lblTmpSpace = new JLabel(namTmpSpace + ":");
139 txtTmpSpace = new JTextField();
140 txtTmpSpace.setPreferredSize(fileLabelSize);
145
146 centralPanel.add(new JSeparator());
147
148 JPanel titleFragments = new JPanel(new FlowLayout(FlowLayout.LEFT));
149 titleFragments.add(new JLabel("<html><b>Handling of fragments</b></html>"));
150 centralPanel.add(titleFragments);
151
152 pnlSMILESTo3D = new JPanel(new FlowLayout(FlowLayout.LEFT));
153 lblSMILESTo3D = new JLabel(namSMILESTo3D + ": "
154 + GUIPreferences.smiTo3dResolver + " - Change to ");
155 cmbSMILESTo3D = new JComboBox<SMITo3DEngine>(SMITo3DEngine.values());
156 cmbSMILESTo3D.addActionListener(new ActionListener() {
157 @Override
158 public void actionPerformed(ActionEvent e) {
159 GUIPreferences.smiTo3dResolver = (SMITo3DEngine)
160 cmbSMILESTo3D.getSelectedItem();
161 lblSMILESTo3D.setText(namSMILESTo3D + ": "
162 + GUIPreferences.smiTo3dResolver + " - Change to ");
163 }
164 });
168
169 centralPanel.add(new JSeparator());
170
171 JPanel titleGraphViewer = new JPanel(new FlowLayout(FlowLayout.LEFT));
172 titleGraphViewer.add(new JLabel("<html><b>Graph visualization</b></html>"));
173 centralPanel.add(titleGraphViewer);
174
175 pnlGraphTxtSize = new JPanel(new FlowLayout(FlowLayout.LEFT));
176 lblGraphTxtSize = new JLabel(namGraphTxtSize + ":");
177 txtGraphTxtSize = new JTextField();
178 txtGraphTxtSize.setPreferredSize(strFieldSize);
183
184 pnlGraphNodeSize = new JPanel(new FlowLayout(FlowLayout.LEFT));
185 lblGraphNodeSize = new JLabel(namGraphNodeSize + ":");
186 txtGraphNodeSize = new JTextField();
187 txtGraphNodeSize.setPreferredSize(strFieldSize);
192
193 centralPanel.add(new JSeparator());
194
195 JPanel titleMolViewer = new JPanel(new FlowLayout(FlowLayout.LEFT));
196 titleMolViewer.add(new JLabel("<html><b>Candidate item visualization</b></html>"));
197 centralPanel.add(titleMolViewer);
198
199 String toolTipPropTags = "</html>Customizes the list of properties displayed togetther with the chemical representation of an item.</html>";
200 linePropTags = new JPanel(new FlowLayout(FlowLayout.LEFT));
201 lblPropTags = new JLabel("Additional properties to display:", SwingConstants.LEFT);
202 lblPropTags.setPreferredSize(fileLabelSize);
203 lblPropTags.setToolTipText(toolTipPropTags);
204 tabModPropTags = new DefaultTableModel() {
205 @Override
206 public boolean isCellEditable(int row, int column) {
207 return false;
208 }
209 };
210 tabModPropTags.setColumnCount(1);
211 for (String propName : GUIPreferences.chosenSDFTags)
212 {
213 tabModPropTags.addRow(new Object[]{propName});
214 }
215 tabPropTags = new JTable(tabModPropTags);
216 tabPropTags.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
217 btnPropTagsInsert = new JButton("Add Property Name");
218 btnPropTagsInsert.setToolTipText("Click to add the reference name or SDF tag of the desired property.");
219 btnPropTagsInsert.addActionListener(new ActionListener(){
220 public void actionPerformed(ActionEvent e){
221 String propName = (String)JOptionPane.showInputDialog(
223 "Specify the reference name or SDF tag of the "
224 + "desired property",
225 "Specify Property Name",
226 JOptionPane.PLAIN_MESSAGE);
227
228 if ((propName != null) && (propName.length() > 0)
229 && !GUIPreferences.chosenSDFTags.contains(propName))
230 {
231 tabModPropTags.addRow(new Object[]{propName});
232 GUIPreferences.chosenSDFTags.add(propName);
233 }
234 }
235 });
236 btnPropTagsCleanup = new JButton("Remove Selected");
237 btnPropTagsCleanup.setToolTipText("Remove all selected entries from the list.");
238 btnPropTagsCleanup.addActionListener(new ActionListener(){
239 public void actionPerformed(ActionEvent e){
240 if (tabPropTags.getRowCount() > 0)
241 {
242 if (tabPropTags.getSelectedRowCount() > 0)
243 {
244 int selectedRowIds[] = tabPropTags.getSelectedRows();
245 Arrays.sort(selectedRowIds);
246 for (int i=(selectedRowIds.length-1); i>-1; i--)
247 {
248 String val = tabModPropTags.getValueAt(
249 selectedRowIds[i], 0).toString();
250 GUIPreferences.chosenSDFTags.remove(val);
251 tabModPropTags.removeRow(selectedRowIds[i]);
252 }
253 }
254 }
255 }
256 });
257 GroupLayout grpLyoPropTags = new GroupLayout(linePropTags);
258 linePropTags.setLayout(grpLyoPropTags);
259 grpLyoPropTags.setAutoCreateGaps(true);
260 grpLyoPropTags.setAutoCreateContainerGaps(true);
261 grpLyoPropTags.setHorizontalGroup(grpLyoPropTags.createSequentialGroup()
262 .addComponent(lblPropTags)
263 .addGroup(grpLyoPropTags.createParallelGroup()
264 .addGroup(grpLyoPropTags.createSequentialGroup()
265 .addComponent(btnPropTagsInsert)
266 .addComponent(btnPropTagsCleanup))
267 .addComponent(tabPropTags))
268 );
269 grpLyoPropTags.setVerticalGroup(grpLyoPropTags.createParallelGroup(
270 GroupLayout.Alignment.LEADING)
271 .addComponent(lblPropTags)
272 .addGroup(grpLyoPropTags.createSequentialGroup()
273 .addGroup(grpLyoPropTags.createParallelGroup()
274 .addComponent(btnPropTagsInsert)
275 .addComponent(btnPropTagsCleanup))
276 .addComponent(tabPropTags))
277 );
279
280 centralPanel.add(new JSeparator());
281
282 JPanel titleEvolutionPlots = new JPanel(new FlowLayout(FlowLayout.LEFT));
283 titleEvolutionPlots.add(new JLabel("<html><b>Evolution run plots</b></html>"));
284 centralPanel.add(titleEvolutionPlots);
285
286 pnlChartPointSize = new JPanel(new FlowLayout(FlowLayout.LEFT));
287 lblChartPointSize = new JLabel(namChartPointSize + ":");
288 txtChartPointSize = new JTextField();
289 txtChartPointSize.setPreferredSize(strFieldSize);
294
295 pnlEvoChartLegend = new JPanel(new FlowLayout(FlowLayout.LEFT));
296 rcbEvoChartLegend = new JRadioButton(namEvoChartLegend);
300
301 pnlMntChartLegend = new JPanel(new FlowLayout(FlowLayout.LEFT));
302 rcbMntChartLegend = new JRadioButton(namMntChartLegend);
306
307 // Customize the buttons of the modal dialog
308 this.btnDone.setText("Save");
309 this.btnDone.setToolTipText("Save the values and close dialog");
310 this.btnDone.addActionListener(new ActionListener() {
311
312 @Override
313 public void actionPerformed(ActionEvent e) {
314 checkInput();
315 if (inputIsOK)
316 {
317 storeValues();
318 close();
319 }
320 }
321 });
322
323 this.btnCanc.setEnabled(false);
324 this.btnCanc.setVisible(false);
325
326 super.addToCentralPane(scrollablePane);
327 }
328
329//-----------------------------------------------------------------------------
330
331 private void checkInput()
332 {
333 inputIsOK = true; //resetting results from previous attempts
338 }
339
340//-----------------------------------------------------------------------------
341
342 private void mustParseToInt(JTextField field, String name)
343 {
344 try {
345 Integer.parseInt(field.getText());
346 } catch (Exception e) {
347 inputIsOK = false;
348 JOptionPane.showMessageDialog(this,
349 "<html>Unacceptable value for '" + name + "'<br>"
350 + "<br>The value should be an integer.</html>",
351 "Error",
352 JOptionPane.ERROR_MESSAGE,
353 UIManager.getIcon("OptionPane.errorIcon"));
354 }
355 }
356
357//-----------------------------------------------------------------------------
358
359 private void pathMustBeReadableWritable(JTextField field, String name)
360 {
361 String testFileName = field.getText()
362 + System.getProperty("file.separator")
363 + "test";
364 if (!FileUtils.canWriteAndReadTo(testFileName))
365 {
366 inputIsOK = false;
367 JOptionPane.showMessageDialog(this,
368 "<html>Unacceptable value for '" + name + "'<br>"
369 + "<br>The pathname should be readable and writable.</html>",
370 "Error",
371 JOptionPane.ERROR_MESSAGE,
372 UIManager.getIcon("OptionPane.errorIcon"));
373 }
374 try {
375 FileUtils.deleteFile(testFileName);
376 } catch (DENOPTIMException e) {
377 e.printStackTrace();
378 }
379 }
380
381//-----------------------------------------------------------------------------
382
383 private void storeValues()
384 {
385 GUIPreferences.graphLabelFontSize =
386 Integer.parseInt(txtGraphTxtSize.getText());
387 GUIPreferences.graphNodeSize =
388 Integer.parseInt(txtGraphNodeSize.getText());
389 GUIPreferences.chartPointSize =
390 Integer.parseInt(txtChartPointSize.getText());
391 GUIPreferences.tmpSpace = txtTmpSpace.getText();
392 GUIPreferences.showLegenInEvolutionPlot = rcbEvoChartLegend.isSelected();
393 GUIPreferences.showLegenInMonitorPlot = rcbMntChartLegend.isSelected();
394 }
395}
static boolean canWriteAndReadTo(String pathName)
Check whether we can write and read to a given pathname.
Definition: FileUtils.java:185
static void deleteFile(String fileName)
Delete the file.
Definition: FileUtils.java:256
JButton btnCanc
The button that is used to close the dialog without processing any input.
JButton btnDone
The button that is used to launch the processing of the data given to the open dialog,...
void close()
Closes the dialog window.
static final long serialVersionUID
Version UID.
final Dimension fileLabelSize
Default sizes for file pathname labels.
void mustParseToInt(JTextField field, String name)
void pathMustBeReadableWritable(JTextField field, String name)
final Dimension strFieldSize
Default sizes for short pathname fields (i.e., string or number)
final int preferredHeight
Default text field height.
JComboBox< SMITo3DEngine > cmbSMILESTo3D
The collection of tunable preferences.
static String tmpSpace
Readable/writable space for tmp files.
static boolean showLegenInMonitorPlot
Choice of displaying legend in monitor plot.
static int graphNodeSize
Graph visualization: size of nodes.
static boolean showLegenInEvolutionPlot
Choice of displaying legend in evolution plot.
static int graphLabelFontSize
Graph visualization: font size of labels.
static TreeSet< String > chosenSDFTags
MolecularViewer: list of SDF tags specifying which properties to display.
static int chartPointSize
Evolutionary Inspector: size of points.
static SMITo3DEngine smiTo3dResolver
Selects the engine used to do SMILES-to-3D conversion.
Available engines used to do SMILES-to-3D conversion.