Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
JalviewDialog | 45 | 30 | 10 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
7 | * Jalview is free software: you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation, either version 3 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * Jalview is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty | |
14 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | * PURPOSE. See the GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Jalview. If not, see <http://www.gnu.org/licenses/>. | |
19 | * The Jalview Authors are detailed in the 'AUTHORS' file. | |
20 | */ | |
21 | package jalview.gui; | |
22 | ||
23 | import jalview.util.MessageManager; | |
24 | ||
25 | import java.awt.Container; | |
26 | import java.awt.Dimension; | |
27 | import java.awt.Rectangle; | |
28 | import java.awt.event.ActionEvent; | |
29 | import java.awt.event.ActionListener; | |
30 | import java.awt.event.WindowAdapter; | |
31 | import java.awt.event.WindowEvent; | |
32 | ||
33 | import javax.swing.JButton; | |
34 | import javax.swing.JDialog; | |
35 | import javax.swing.JPanel; | |
36 | ||
37 | /** | |
38 | * Boilerplate dialog class. Implements basic functionality necessary for model | |
39 | * blocking/non-blocking dialogs with an OK and Cancel button ready to add to | |
40 | * the content pane. | |
41 | * | |
42 | * @author jimp | |
43 | * | |
44 | */ | |
45 | public abstract class JalviewDialog extends JPanel | |
46 | { | |
47 | ||
48 | protected JDialog frame; | |
49 | ||
50 | protected JButton ok = new JButton(); | |
51 | ||
52 | protected JButton cancel = new JButton(); | |
53 | ||
54 | boolean block = false; | |
55 | ||
56 | 9 | public void waitForInput() |
57 | { | |
58 | 9 | if (!block) |
59 | { | |
60 | 9 | new Thread(new Runnable() |
61 | { | |
62 | ||
63 | 9 | @Override |
64 | public void run() | |
65 | { | |
66 | 9 | frame.setVisible(true); |
67 | } | |
68 | ||
69 | }).start(); | |
70 | } | |
71 | else | |
72 | { | |
73 | 0 | frame.setVisible(true); |
74 | } | |
75 | } | |
76 | ||
77 | 9 | protected void initDialogFrame(Container content, boolean modal, |
78 | boolean block, String title, int width, int height) | |
79 | { | |
80 | ||
81 | 9 | frame = new JDialog(Desktop.instance, modal); |
82 | 9 | frame.setTitle(title); |
83 | 9 | if (Desktop.instance != null) |
84 | { | |
85 | 9 | Rectangle deskr = Desktop.instance.getBounds(); |
86 | 9 | frame.setBounds(new Rectangle((int) (deskr.getCenterX() - width / 2), |
87 | (int) (deskr.getCenterY() - height / 2), width, height)); | |
88 | } | |
89 | else | |
90 | { | |
91 | 0 | frame.setSize(width, height); |
92 | } | |
93 | 9 | int minWidth = width - 100; |
94 | 9 | int minHeight = height - 100; |
95 | 9 | frame.setMinimumSize(new Dimension(minWidth, minHeight)); |
96 | 9 | frame.setContentPane(content); |
97 | 9 | this.block = block; |
98 | ||
99 | 9 | ok.setOpaque(false); |
100 | 9 | ok.setText(MessageManager.getString("action.ok")); |
101 | 9 | ok.addActionListener(new ActionListener() |
102 | { | |
103 | 0 | @Override |
104 | public void actionPerformed(ActionEvent e) | |
105 | { | |
106 | 0 | okPressed(); |
107 | 0 | closeDialog(); |
108 | } | |
109 | }); | |
110 | 9 | cancel.setOpaque(false); |
111 | 9 | cancel.setText(MessageManager.getString("action.cancel")); |
112 | 9 | cancel.addActionListener(new ActionListener() |
113 | { | |
114 | 0 | @Override |
115 | public void actionPerformed(ActionEvent e) | |
116 | { | |
117 | 0 | cancelPressed(); |
118 | 0 | closeDialog(); |
119 | } | |
120 | }); | |
121 | 9 | frame.addWindowListener(new WindowAdapter() |
122 | { | |
123 | 0 | @Override |
124 | public void windowClosing(WindowEvent e) | |
125 | { | |
126 | // user has cancelled the dialog | |
127 | 0 | closeDialog(); |
128 | } | |
129 | }); | |
130 | } | |
131 | ||
132 | /** | |
133 | * clean up and raise the 'dialog closed' event by calling raiseClosed | |
134 | */ | |
135 | 0 | protected void closeDialog() |
136 | { | |
137 | 0 | try |
138 | { | |
139 | 0 | raiseClosed(); |
140 | 0 | frame.dispose(); |
141 | } catch (Exception ex) | |
142 | { | |
143 | } | |
144 | } | |
145 | ||
146 | protected abstract void raiseClosed(); | |
147 | ||
148 | protected abstract void okPressed(); | |
149 | ||
150 | protected abstract void cancelPressed(); | |
151 | ||
152 | } |