$darkmode
DENOPTIM
GUIPrepare.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.BorderLayout;
22import java.awt.event.ActionEvent;
23import java.awt.event.ActionListener;
24import java.io.File;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.util.ArrayList;
28
29import javax.swing.BoxLayout;
30import javax.swing.JButton;
31import javax.swing.JLabel;
32import javax.swing.JOptionPane;
33import javax.swing.JPanel;
34import javax.swing.JTabbedPane;
35import javax.swing.JTextField;
36import javax.swing.SwingConstants;
37import javax.swing.UIManager;
38
39import denoptim.constants.DENOPTIMConstants;
40import denoptim.exception.DENOPTIMException;
41import denoptim.files.FileFormat;
42import denoptim.files.FileUtils;
43import denoptim.main.Main.RunType;
44import denoptim.programs.combinatorial.FragSpaceExplorer;
45import denoptim.programs.denovo.GARunner;
46import denoptim.programs.fitnessevaluator.FitnessRunner;
47import denoptim.task.ProgramTask;
48import denoptim.task.StaticTaskManager;
49
58public class GUIPrepare extends GUICardPanel
59{
60
64 private static final long serialVersionUID = 6481647840284906676L;
65
69 protected JTabbedPane tabbedPane;
70
74 public ArrayList<IParametersForm> allParams;
75
79 public GUIPrepare(GUIMainPanel mainPanel, String newPanelName)
80 {
81 super(mainPanel, newPanelName);
82 super.setLayout(new BorderLayout());
83 this.allParams = new ArrayList<IParametersForm>();
84 initialize();
85 }
86
90 private void initialize()
91 {
92
93 // Parameters for the various components are divided in TABs
94 tabbedPane = new JTabbedPane(JTabbedPane.TOP);
95 super.add(tabbedPane, BorderLayout.CENTER);
96
97 // Buttons go below the tabs
98 ButtonsBar commandsPane = new ButtonsBar();
99 super.add(commandsPane, BorderLayout.SOUTH);
100
101 JButton btnLoadParams = new JButton("Load Parameters");
102 // Adding an Icon overwrites the fontsize, no matter the setFont
103 /*
104 JButton btnLoadParams = new JButton("Load Parameters",
105 UIManager.getIcon("FileView.directoryIcon"));
106 */
107 btnLoadParams.setToolTipText("<html>Reads a DENOPTIM parameter file,"
108 + "<br>and imports parameters into the form.</html>");
109 btnLoadParams.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 File inFile = GUIFileOpener.pickFile(btnLoadParams);
112 if (inFile == null || inFile.getAbsolutePath().equals(""))
113 {
114 return;
115 }
116
118
119 for (IParametersForm p : allParams)
120 {
121 p.setUnsavedChanges(false);
122 }
123 }
124 });
125 commandsPane.add(btnLoadParams);
126
127 JButton btnSaveParams = new JButton("Save Parameters");
128 // Adding an Icon overwrites the fontsize, no matter the setFont
129 /*
130 JButton btnSaveParams = new JButton("Save Parameters",
131 UIManager.getIcon("FileView.hardDriveIcon"));
132 */
133 btnSaveParams.setToolTipText("<html>Write all parameters to file."
134 + "<br>This will produce a DENOPTIM parameter file.</html>");
135 btnSaveParams.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e) {
137 File outFile = GUIFileOpener.pickFileForSaving(btnSaveParams);
138 printAllParamsToFile(outFile);
139 if (outFile!=null)
140 FileUtils.addToRecentFiles(outFile, getFileFormat("PARAMS"));
141 }
142 });
143 commandsPane.add(btnSaveParams);
144
145 /*
146 //TODO
147 JButton btnValidate = new JButton("Validate Parameters",
148 btnValidate.setToolTipText("Check the correctness of the parameters");
149 btnValidate.addActionListener(new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 //TODO
152 getNonImplementedError();
153 }
154 });
155 commandsPane.add(btnValidate);
156 */
157
158
159 JButton btnRun = new JButton("Run now...");//,
160 // UIManager.getIcon("Menu.arrowIcon"));
161 // Using the arrowIcon causes problems with adoptopenjdk-1.8
162 // due to casting of the JButton into a JMenuItem. This could be
163 // due to the fact that the arrow icon is meant for a menu.
164 btnRun.addActionListener(new ActionListener() {
165 public void actionPerformed(ActionEvent e) {
166 String msg = "<html><body width='%1s'><p>Running a DENOPTIM "
167 + "experiment from the graphical user interface "
168 + "(GUI) makes it dependent on "
169 + "the GUI itself. Therefore, if the GUI is closed "
170 + "or shut down, "
171 + "the experiment will be terminated as well.</p>"
172 + "<p>To avoid this, consider running your experiment "
173 + "as a batch process disconnected from the GUI.</p>"
174 + "<br>";
176 msg = msg + "<p>Continue?</p></body></html>";
177 //TODO: add capability of running in the background
178 String[] options = new String[]{"Yes", "Cancel"};
179 int res = JOptionPane.showOptionDialog(btnRun,
180 String.format(msg, 450),
181 "WARNING",
182 JOptionPane.DEFAULT_OPTION,
183 JOptionPane.QUESTION_MESSAGE,
184 UIManager.getIcon("OptionPane.warningIcon"),
185 options,
186 options[1]);
187 switch (res)
188 {
189 case 0:
190 String location = "unknownLocation";
191 try {
192 File wrkSpace = prepareWorkSpace();
193 File paramFile = instantiateParametersFile(wrkSpace);
194 if (printAllParamsToFile(paramFile))
195 {
196 ProgramTask task = buildProgramTask(paramFile,
197 wrkSpace);
199 } else {
200 throw new DENOPTIMException("Failed to make "
201 + "parameter file '" + paramFile + "'");
202 }
203 location = wrkSpace.getAbsolutePath();
204 } catch (DENOPTIMException e1) {
205 JOptionPane.showMessageDialog(btnRun,
206 "Could not start task. " + e1.getMessage()
207 + ". " + e1.getCause().getMessage(),
208 "ERROR",
209 JOptionPane.ERROR_MESSAGE);
210 return;
211 }
212
213 JOptionPane.showMessageDialog(btnRun,
214 "<html>Experiment submitted!<br>"
215 + "See under " + location+"<br>"
216 + "or 'File -&gt; Open Recent'</html>",
217 "Submitted",
218 JOptionPane.INFORMATION_MESSAGE);
219 break;
220
221 case 1:
222 break;
223 }
224 }
225 });
226 commandsPane.add(btnRun);
227
228
229 JButton btnCanc = new JButton("Close Tab");
230 // Adding the icon overrites font size no matter setFont
231 /*
232 JButton btnCanc = new JButton("Close Tab",
233 UIManager.getIcon("FileView.fileIcon"));
234 */
235 btnCanc.setToolTipText("Closes this tab.");
236 btnCanc.addActionListener(new removeCardActionListener(this));
237 commandsPane.add(btnCanc);
238
239 JButton btnHelp = new JButton("?");
240 btnHelp.setToolTipText("Help");
241 btnHelp.addActionListener(new ActionListener() {
242 public void actionPerformed(ActionEvent e) {
243 String txt = "<html><body width='%1s'>"
244 + "<p>This tab allows to create, inspect, and edit "
245 + "parameter used as input for DENOPTIM experiments. "
246 + "These parameters are then collected into an input "
247 + "file for DENOPTIM.</p>"
248 + "<br>"
249 + "<p>Hover over buttons and parameter fields to get "
250 + "informations on a specific parameter.</p></html>";
251 JOptionPane.showMessageDialog(btnHelp,
252 String.format(txt, 350),
253 "Tips",
254 JOptionPane.PLAIN_MESSAGE);
255 }
256 });
257 commandsPane.add(btnHelp);
258 }
259
260//------------------------------------------------------------------------------
261
262 private FileFormat getFileFormat(String string)
263 {
264
265 if (this instanceof GUIPrepareGARun)
266 {
267 switch(string)
268 {
269 case "PARAMS":
270 return FileFormat.GA_PARAM;
271 case "RUN":
272 return FileFormat.GA_RUN;
273 default:
274 throw new IllegalArgumentException("BUG: GUIPrepare"
275 + "subclasses must "
276 + "declare what kind of recent file to store. "
277 + "Current declaration is not valid. Report this "
278 + "to the development team.");
279 }
280 } else if (this instanceof GUIPrepareFSERun)
281 {
282 switch(string)
283 {
284 case "PARAMS":
285 return FileFormat.FSE_PARAM;
286 case "RUN":
287 return FileFormat.FSE_PARAM;
288 default:
289 throw new IllegalArgumentException("BUG: GUIPrepare"
290 + "subclasses must "
291 + "declare what kind of recent file to store. "
292 + "Current declaration is not valid. Report this "
293 + "to the development team.");
294 }
295 } else if (this instanceof GUIPrepareFitnessRunner)
296 {
297 switch(string)
298 {
299 case "PARAMS":
300 return FileFormat.FSE_PARAM;
301 case "RUN":
302 return FileFormat.FSE_PARAM;
303 default:
304 throw new IllegalArgumentException("BUG: GUIPrepare"
305 + "subclasses must "
306 + "declare what kind of recent file to store. "
307 + "Current declaration is not valid. Report this "
308 + "to the development team.");
309 }
310 }
311 return null;
312 }
313
314//------------------------------------------------------------------------------
315
317 {
318 RunType baseName =null;
319 if (this instanceof GUIPrepareGARun)
320 {
321 baseName = RunType.GA;
322 } else if (this instanceof GUIPrepareFSERun)
323 {
324 baseName = RunType.FSE;
325 } else if (this instanceof GUIPrepareFitnessRunner)
326 {
327 baseName = RunType.FIT;
328 }
329 return baseName;
330 }
331
332//------------------------------------------------------------------------------
333
334 private File instantiateParametersFile(File wrkSpace)
335 {
336 String baseName = getAchronimFromClass() + ".params";
337 File paramFile = new File (wrkSpace.getAbsolutePath()
338 + System.getProperty("file.separator") + baseName);
339 return paramFile;
340 }
341
342//------------------------------------------------------------------------------
343
349 private boolean printAllParamsToFile(File outFile)
350 {
351 StringBuilder sb = new StringBuilder();
352 for (IParametersForm p : allParams)
353 {
354 try
355 {
356 p.putParametersToString(sb);
357 }
358 catch (Exception e1)
359 {
360 JOptionPane.showMessageDialog(this,
361 e1.getMessage(),
362 "Error",
363 JOptionPane.ERROR_MESSAGE,
364 UIManager.getIcon("OptionPane.errorIcon"));
365 return false;
366 }
367 }
368
369 // It might be coming from a JOptionPane, which might return null
370 // upon user's attempt to cancel the printing task.
371 if (outFile == null)
372 {
373 return false;
374 }
375
376 try
377 {
378 FileWriter fw = new FileWriter(outFile);
379 fw.write(sb.toString());
380 fw.close();
381
382 for (IParametersForm p : allParams)
383 {
384 p.setUnsavedChanges(false);
385 }
386 }
387 catch (IOException io)
388 {
389 JOptionPane.showMessageDialog(this,
390 "Could not write to '" + outFile + "'!.",
391 "Error",
392 JOptionPane.PLAIN_MESSAGE,
393 UIManager.getIcon("OptionPane.errorIcon"));
394 return false;
395 }
396 return true;
397 }
398
399//------------------------------------------------------------------------------
400
408 private ProgramTask buildProgramTask(File configFile, File workDir)
409 throws DENOPTIMException
410 {
411 ProgramTask task = null;
412 if (this instanceof GUIPrepareGARun)
413 {
414 task = new GARunner(configFile, workDir);
415 } else if (this instanceof GUIPrepareFSERun)
416 {
417 task = new FragSpaceExplorer(configFile, workDir);
418 } else if (this instanceof GUIPrepareFitnessRunner)
419 {
420 task = new FitnessRunner(configFile, workDir);
421 }
422 return task;
423 }
424
425//------------------------------------------------------------------------------
426
428 {
429 String baseName = getAchronimFromClass() + "_run";
430 File parent = new File(GUIPreferences.tmpSpace);
431 File wrkSpace = FileUtils.getAvailableFileName(parent, baseName);
432 FileUtils.createDirectory(wrkSpace.getAbsolutePath());
433 return wrkSpace;
434 }
435//------------------------------------------------------------------------------
436
438 {
439 for (IParametersForm p : allParams)
440 {
441 try
442 {
443 p.importParametersFromDenoptimParamsFile(
444 file.getAbsolutePath());
445 }
446 catch (Exception e1)
447 {
448 if (e1.getMessage().equals("")
449 || e1.getMessage() == null)
450 {
451 e1.printStackTrace();
452 JOptionPane.showMessageDialog(this,
453 "<html>Exception occurred while importing"
454 + "parameters.<br>Please, report this to "
455 + "the DENOPTIM team.</html>",
456 "Error",
457 JOptionPane.ERROR_MESSAGE,
458 UIManager.getIcon("OptionPane.errorIcon"));
459 }
460 else
461 {
462 JOptionPane.showMessageDialog(this,
463 e1.getMessage(),
464 "Error",
465 JOptionPane.ERROR_MESSAGE,
466 UIManager.getIcon("OptionPane.errorIcon"));
467 }
468 }
469 }
470 }
471
472//-----------------------------------------------------------------------------
473
480 public boolean hasUnsavedChanges()
481 {
482 boolean res = false;
483 for (IParametersForm p : allParams)
484 {
485 if (p.hasUnsavedChanges())
486 {
487 res = true;
488 break;
489 }
490 }
491 return res;
492 }
493
494//-----------------------------------------------------------------------------
495
496}
static File getAvailableFileName(File parent, String baseName)
Define a filename that can be used, i.e., is still available, because no other file with the same pat...
Definition: FileUtils.java:729
static boolean createDirectory(String fileName)
Creates a directory.
Definition: FileUtils.java:231
static void addToRecentFiles(String fileName, FileFormat ff)
Appends an entry to the list of recent files.
Definition: FileUtils.java:67
Standardised horizontal bar with padded components, which are meant to be JButtons.
Definition: ButtonsBar.java:36
Component add(Component comp)
Definition: ButtonsBar.java:53
Remove the card from the deck of cards and takes care of removing also the entry in the list of activ...
Class of GUI panels meant to occupy one card in the deck-of-cards layout of the main panel.
GUIMainPanel mainPanel
The main panel (cards deck)
File opener for DENOPTIM GUI.
static File pickFile(Component parent)
static File pickFileForSaving(Component parent)
The main panel is a deck of cards that occupies all the GUI frame.
The collection of tunable preferences.
static String tmpSpace
Readable/writable space for tmp files.
Master form containing all sub-forms that need to be filled to define the input parameters for FragSp...
Form that allows to test the configuration of a fitness provider.
Master form containing all sub-forms that need to be filled to define the input parameters for Denopt...
Class representing the general structure of a form including a specific set of parameter collections.
Definition: GUIPrepare.java:59
FileFormat getFileFormat(String string)
RunType getAchronimFromClass()
void importParametersFromDenoptimParamsFile(File file)
GUIPrepare(GUIMainPanel mainPanel, String newPanelName)
Constructor.
Definition: GUIPrepare.java:79
boolean hasUnsavedChanges()
Check whether any of the parameter forms (i.e., a tab) in this list of tabs has unsaved changes.
static final long serialVersionUID
Version UID.
Definition: GUIPrepare.java:64
void initialize()
Initialize the panel with tabbedPane and buttons.
Definition: GUIPrepare.java:90
File instantiateParametersFile(File wrkSpace)
ProgramTask buildProgramTask(File configFile, File workDir)
The type of main to run is determined by which subclass calls this method.
JTabbedPane tabbedPane
Parameters for the various components are divided in TABs.
Definition: GUIPrepare.java:69
ArrayList< IParametersForm > allParams
Storage of parameters.
Definition: GUIPrepare.java:74
boolean printAllParamsToFile(File outFile)
Combinatorial exploration of the fragment space.
Programs that runs de novo design by a genetic algorithm.
Definition: GARunner.java:40
Task structure for any of the main programs in the denoptim project, such as genetic algorithm and co...
Manager for tasks submitted by the GUI.
File formats identified by DENOPTIM.
Definition: FileFormat.java:32
Types of runs that can be requested to the DENOPTIM Main class.
Definition: Main.java:62
GA
Run the genetic algorithm with DenoptimGA.
Definition: Main.java:71
FIT
stand-alone fitness evaluation
Definition: Main.java:82
FSE
Run a combinatorial generation of candidates with FragSpaceExplorer.
Definition: Main.java:77
Interface for parameter forms.