JAL-1503 update version in GPL header
[jalview.git] / src / jalview / gui / JalviewDialog.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3  * Copyright (C) 2014 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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import jalview.util.MessageManager;
22
23 import java.awt.Container;
24 import java.awt.Rectangle;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29
30 import javax.swing.JButton;
31 import javax.swing.JDialog;
32 import javax.swing.JPanel;
33
34 /**
35  * Boilerplate dialog class. Implements basic functionality necessary for model
36  * blocking/non-blocking dialogs with an OK and Cancel button ready to add to
37  * the content pane.
38  * 
39  * @author jimp
40  * 
41  */
42 public abstract class JalviewDialog extends JPanel
43 {
44
45   protected JDialog frame;
46
47   protected JButton ok = new JButton();
48
49   protected JButton cancel = new JButton();
50
51   boolean block = false;
52
53   public void waitForInput()
54   {
55     if (!block)
56     {
57       new Thread(new Runnable()
58       {
59
60         public void run()
61         {
62           frame.setVisible(true);
63         }
64
65       }).start();
66     }
67     else
68     {
69       frame.setVisible(true);
70     }
71   }
72
73   protected void initDialogFrame(Container content, boolean modal,
74           boolean block, String title, int width, int height)
75   {
76
77     frame = new JDialog(Desktop.instance, modal);
78     frame.setTitle(title);
79     if (Desktop.instance != null)
80     {
81       Rectangle deskr = Desktop.instance.getBounds();
82       frame.setBounds(new Rectangle((int) (deskr.getCenterX() - width / 2),
83               (int) (deskr.getCenterY() - height / 2), width, height));
84     }
85     else
86     {
87       frame.setSize(width, height);
88     }
89     frame.setContentPane(content);
90     this.block = block;
91
92     ok.setOpaque(false);
93     ok.setText(MessageManager.getString("action.ok"));
94     ok.addActionListener(new ActionListener()
95     {
96       public void actionPerformed(ActionEvent e)
97       {
98         okPressed();
99         closeDialog();
100       }
101     });
102     cancel.setOpaque(false);
103     cancel.setText(MessageManager.getString("action.cancel"));
104     cancel.addActionListener(new ActionListener()
105     {
106       public void actionPerformed(ActionEvent e)
107       {
108         cancelPressed();
109         closeDialog();
110       }
111     });
112     frame.addWindowListener(new WindowListener()
113     {
114
115       @Override
116       public void windowOpened(WindowEvent e)
117       {
118         // TODO Auto-generated method stub
119
120       }
121
122       @Override
123       public void windowIconified(WindowEvent e)
124       {
125         // TODO Auto-generated method stub
126
127       }
128
129       @Override
130       public void windowDeiconified(WindowEvent e)
131       {
132         // TODO Auto-generated method stub
133
134       }
135
136       @Override
137       public void windowDeactivated(WindowEvent e)
138       {
139         // TODO Auto-generated method stub
140
141       }
142
143       @Override
144       public void windowClosing(WindowEvent e)
145       {
146         // user has cancelled the dialog
147         closeDialog();
148       }
149
150       @Override
151       public void windowClosed(WindowEvent e)
152       {
153       }
154
155       @Override
156       public void windowActivated(WindowEvent e)
157       {
158         // TODO Auto-generated method stub
159
160       }
161     });
162   }
163
164   /**
165    * clean up and raise the 'dialog closed' event by calling raiseClosed
166    */
167   protected void closeDialog()
168   {
169     try
170     {
171       frame.dispose();
172       raiseClosed();
173     } catch (Exception ex)
174     {
175     }
176   }
177
178   protected abstract void raiseClosed();
179
180   protected abstract void okPressed();
181
182   protected abstract void cancelPressed();
183
184 }