$darkmode
DENOPTIM
GUIVertexSelector.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.event.ActionEvent;
24import java.awt.event.ActionListener;
25import java.util.ArrayList;
26import java.util.List;
27
28import javax.swing.JLabel;
29import javax.swing.JOptionPane;
30import javax.swing.JPanel;
31import javax.swing.JSpinner;
32import javax.swing.SpinnerNumberModel;
33import javax.swing.UIManager;
34import javax.swing.event.ChangeEvent;
35import javax.swing.event.ChangeListener;
36
37import denoptim.graph.Vertex;
38
39
49{
53 private static final long serialVersionUID = 912850110991449553L;
54
58 private List<Vertex> vertexLibrary =
59 new ArrayList<Vertex>();
60
64 private Vertex vertex;
65
69 private int currVrtxIdx = 0;
70
74 private int currApIdx = -1;
75
77 protected JPanel ctrlPane;
78 private JPanel navigPanel;
79 private JPanel navigPanel2;
80
81 private JSpinner navigSpinner;
82 private JLabel totalVerticesLabel;
85
86 private boolean enforceAPSelection = false;
87
91 public static final String PRESELECTEDAPSFIELD = "pre-SelectedAPs";
92
96 public static final String PRESELECTEDAPSFIELDSEP = "pre-SelectedAPs";
97
98//-----------------------------------------------------------------------------
99
105 public GUIVertexSelector(Component refForPlacement, boolean use3rd)
106 {
107 super(refForPlacement, use3rd);
108
109 // The viewer with Jmol and APtable (not editable)
110 vertexViewer = new VertexViewPanel(false);
112
113 this.setTitle("Select Vertex and AP");
114
115 // Controls for navigating the vertices list
116 ctrlPane = new JPanel();
117 ctrlPane.setVisible(true);
118
119 // NB: avoid GroupLayout because it interferes with Jmol viewer and causes exception
120
121 navigPanel = new JPanel();
122 navigPanel2 = new JPanel();
123 JLabel navigationLabel1 = new JLabel("vertex # ");
124 JLabel navigationLabel2 = new JLabel("Current library size: ");
125 totalVerticesLabel = new JLabel("0");
126
127 navigSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 0, 1));
128 navigSpinner.setToolTipText("Move to vertex number # in the currently "
129 + "loaded library.");
130 navigSpinner.setPreferredSize(new Dimension(75,20));
131 navigSpinner.addChangeListener(vrtxSpinnerListener);
132 navigPanel.add(navigationLabel1);
134 ctrlPane.add(navigPanel);
135
136 navigPanel2.add(navigationLabel2);
140
141 if (use3rd)
142 {
143 this.btnExtra.setText("Select current vertex");
144 this.btnExtra.setToolTipText("<html>Selects this fragment. "
145 + "Multiple selections are allowed before hitting the "
146 + "'Done' button.</html>");
147 this.btnExtra.addActionListener(new ActionListener() {
148
149 @Override
150 public void actionPerformed(ActionEvent e) {
151 ArrayList<Integer> ids = vertexViewer.getSelectedAPIDs();
152 if (ids.size() > 0)
153 {
154 // WARNING: we take only the first, if many are selected.
155 currApIdx = ids.get(0);
156 }
157 else
158 {
159 if (vertex.getNumberOfAPs()==1)
160 {
161 currApIdx=0;
162 }
163 else if (enforceAPSelection)
164 {
165 JOptionPane.showMessageDialog(btnDone,"<html>"
166 + "No attachment point (AP) selected.<br>"
167 + "Please select an AP in the table."
168 + "</html>",
169 "Error",
170 JOptionPane.PLAIN_MESSAGE,
171 UIManager.getIcon("OptionPane.errorIcon"));
172 return;
173 }
174 }
176 }
177 });
178 } else {
179 this.btnDone.setText("Select current vertex");
180 this.btnDone.setToolTipText("<html>Process the currently displayed "
181 + "vertex<br>and the currently selected AP, "
182 + "if any.</html>");
183 this.btnDone.removeActionListener(
184 this.btnDone.getActionListeners()[0]);
185 this.btnDone.addActionListener(new ActionListener() {
186
187 @Override
188 public void actionPerformed(ActionEvent e) {
189 ArrayList<Integer> ids = vertexViewer.getSelectedAPIDs();
190 if (ids.size() > 0)
191 {
192 // WARNING: we take only the first, if many are selected.
193 currApIdx = ids.get(0);
194 }
195 else
196 {
197 if (vertex.getNumberOfAPs()==1)
198 {
199 currApIdx=0;
200 }
201 else if (enforceAPSelection)
202 {
203 JOptionPane.showMessageDialog(btnDone,"<html>"
204 + "No attachment point (AP) selected.<br>"
205 + "Please select an AP in the table."
206 + "</html>",
207 "Error",
208 JOptionPane.PLAIN_MESSAGE,
209 UIManager.getIcon("OptionPane.errorIcon"));
210 return;
211 }
212 }
215 close();
216 }
217 });
218 }
219
220 //This is key in allowing to render the graph according to the size of
221 // the panels.
222 pack();
223 }
224
225//-----------------------------------------------------------------------------
226
234 public void load(List<Vertex> fragments, int initialId)
235 {
236// this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
237 vertexLibrary = fragments;
238 currVrtxIdx = initialId;
241 // this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
242 }
243
244//-----------------------------------------------------------------------------
245
246 @SuppressWarnings("unchecked")
247 private void appendToResult(int vrtxId, int apId)
248 {
249 if (result == null)
250 {
251 result = new ArrayList<ArrayList<Integer>>();
252 }
253 boolean alreadySelected = false;
254 for (ArrayList<Integer> p :
255 ((ArrayList<ArrayList<Integer>>)result))
256 {
257 if ((p.get(0) == vrtxId)
258 && (p.get(1) == apId))
259 {
260 alreadySelected = true;
261 break;
262 }
263 }
264 if (!alreadySelected)
265 {
266 ArrayList<Integer> pair = new ArrayList<Integer>();
267 pair.add(vrtxId);
268 pair.add(apId);
269 ((ArrayList<ArrayList<Integer>>)result).add(pair);
270 }
271 }
272
273//-----------------------------------------------------------------------------
274
280 public void setRequireApSelection(boolean enforced)
281 {
282 this.enforceAPSelection = enforced;
283 }
284
285//-----------------------------------------------------------------------------
286
295 {
296 if (vertexLibrary == null)
297 {
298 JOptionPane.showMessageDialog(this,
299 "No list of vertices loaded.",
300 "Error",
301 JOptionPane.PLAIN_MESSAGE,
302 UIManager.getIcon("OptionPane.errorIcon"));
303 return;
304 }
305
309 }
310
311//-----------------------------------------------------------------------------
312
314 {
315 navigSpinner.setModel(new SpinnerNumberModel(currVrtxIdx+1, 1,
316 vertexLibrary.size(), 1));
317 totalVerticesLabel.setText(Integer.toString(vertexLibrary.size()));
318 }
319
320//-----------------------------------------------------------------------------
321
322 private void clearCurrentSystem()
323 {
324 // Get rid of currently loaded mol
325 vertex = null;
326
327 // Clear viewer?
328 // No, its clears upon loading of a new system.
329 // The exception (i.e., removal of the last vertex) is dealt with by
330 // submitting "zap" only in that occasion.
331
333 }
334
335//-----------------------------------------------------------------------------
336
337 private class VrtxSpinnerChangeEvent implements ChangeListener
338 {
339 private boolean inEnabled = true;
340
342
343 @Override
344 public void stateChanged(ChangeEvent event)
345 {
346 if (!inEnabled)
347 {
348 return;
349 }
350
352
353 //NB here we convert from 1-based index in GUI to 0-based index
354 currVrtxIdx = ((Integer) navigSpinner.getValue()).intValue() - 1;
356
358 }
359 }
360
361//-----------------------------------------------------------------------------
362
363 private void activateTabEditsListener(boolean var)
364 {
366 }
367
368//-----------------------------------------------------------------------------
369
370}
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
void addToCentralPane(JComponent comp)
Adds a component to the central part of this dialog frame.
JButton btnExtra
The button that can be used for any action that does not close the dialog.
JButton btnDone
The button that is used to launch the processing of the data given to the open dialog,...
Object result
The result to be returned once the dialog is closed.
void addToNorthPane(JComponent comp)
Adds a component to the topmost part of this dialog frame.
void close()
Closes the dialog window.
A modal dialog with a viewer that understands the different types of DENOPTIM vertex and allows to se...
final VrtxSpinnerChangeEvent vrtxSpinnerListener
void load(List< Vertex > fragments, int initialId)
Load the list of vertexes to choose from.
void appendToResult(int vrtxId, int apId)
void activateTabEditsListener(boolean var)
void setRequireApSelection(boolean enforced)
Allows to control whether confirming the selection of a vertex without having selected an attachment ...
int currApIdx
The index of the selected AP [0-(n-1)].
static final String PRESELECTEDAPSFIELD
Property used to pre-select APs.
static final long serialVersionUID
Version UID.
void loadCurrentVrtxIdxToViewer()
Loads the vertices corresponding to the field index.
int currVrtxIdx
The index of the currently loaded vertex [0–(n-1)}.
GUIVertexSelector(Component refForPlacement, boolean use3rd)
Constructor for an empty modal panel meant for selection of vertexes.
Vertex vertex
The currently loaded vertex.
static final String PRESELECTEDAPSFIELDSEP
Separator in property used to pre-select APs.
List< Vertex > vertexLibrary
The currently loaded list of vertices.
A panel for visualizing vertices.
void clearCurrentSystem()
Removes the currently visualized molecule and AP table.
ArrayList< Integer > getSelectedAPIDs()
Identifies which attachment points are selected in the currently active viewer.
void activateTabEditsListener(boolean var)
Allows to activate and deactivate the listener.
void loadVertexToViewer(Vertex v)
Loads the given vertex to this viewer.