$darkmode
DENOPTIM
GUIModalDialog.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
21
22import java.awt.BorderLayout;
23import java.awt.Component;
24import java.awt.Frame;
25import java.awt.event.ActionEvent;
26import java.awt.event.ActionListener;
27import java.awt.event.WindowEvent;
28
29import javax.swing.JButton;
30import javax.swing.JComponent;
31import javax.swing.JDialog;
32import javax.swing.JPanel;
33
34public class GUIModalDialog extends JDialog
35{
36
40 private static final long serialVersionUID = 8562693062507200623L;
41
46 protected JButton btnDone;
47
52 protected JButton btnCanc;
53
58 protected JButton btnExtra;
59
60 private JPanel pnlControls;
61
65 protected Object result = null;
66
67//------------------------------------------------------------------------------
68
74 public GUIModalDialog(Component refForPlacement)
75 {
76 this(refForPlacement,false);
77 }
78
79//------------------------------------------------------------------------------
80
88 public GUIModalDialog(Component refForPlacement, boolean useExtraButton)
89 {
90 //To avoid conflict when having multiple modal dialogs open.
91 super(new Frame());
92 setLocationRelativeTo(refForPlacement);
93 initialize(useExtraButton);
94 }
95
96//------------------------------------------------------------------------------
97
98 private void initialize(boolean useExtraButton)
99 {
100 this.setModal(true);
101 this.setLayout(new BorderLayout());
102 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
103
104 btnDone = new JButton("Done");
105 btnDone.setToolTipText("Processes data and closes dialog");
106 btnDone.addActionListener(new ActionListener() {
107
108 @Override
109 public void actionPerformed(ActionEvent e) {
110 close();
111 }
112 });
113
114 btnCanc = new JButton("Cancel");
115 btnCanc.setToolTipText("Exit dialog.");
116 btnCanc.addActionListener(new ActionListener() {
117
118 @Override
119 public void actionPerformed(ActionEvent e) {
120 result = null;
121 close();
122 }
123 });
124
125 pnlControls = new JPanel();
126 if (useExtraButton)
127 {
128 btnExtra = new JButton("Extra");
130 }
131 pnlControls.add(btnDone);
132 pnlControls.add(btnCanc);
133 this.add(pnlControls, BorderLayout.SOUTH);
134 }
135
136//-------------------------------------------------------------------------
137
142 public Object showDialog()
143 {
144 pack();
145 setVisible(true);
146 return result;
147 }
148
149//-------------------------------------------------------------------------
150
155 public void addToNorthPane(JComponent comp)
156 {
157 this.add(comp, BorderLayout.NORTH);
158 }
159
160//-------------------------------------------------------------------------
161
166 public void addToCentralPane(JComponent comp)
167 {
168 this.add(comp, BorderLayout.CENTER);
169 }
170
171//-------------------------------------------------------------------------
172
176 protected void close()
177 {
178 GUIModalDialog.this.setVisible(false);
179 GUIModalDialog.this.dispatchEvent(new WindowEvent(
180 GUIModalDialog.this, WindowEvent.WINDOW_CLOSING));
181 dispose();
182 }
183
184//-------------------------------------------------------------------------
185}
GUIModalDialog(Component refForPlacement, boolean useExtraButton)
Constructor.
JButton btnCanc
The button that is used to close the dialog without processing any input.
static final long serialVersionUID
Version UID.
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 initialize(boolean useExtraButton)
Object showDialog()
Shows the dialog and restrains the modality to it, until the dialog gets closed.
GUIModalDialog(Component refForPlacement)
Constructor.
void addToNorthPane(JComponent comp)
Adds a component to the topmost part of this dialog frame.
void close()
Closes the dialog window.