JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / JalviewDialog.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 java.awt.Container;
22 import java.awt.Rectangle;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.WindowEvent;
26 import java.awt.event.WindowListener;
27
28 import javax.swing.JButton;
29 import javax.swing.JDialog;
30 import javax.swing.JPanel;
31
32 /**
33  * Boilerplate dialog class. Implements basic functionality necessary for model
34  * blocking/non-blocking dialogs with an OK and Cancel button ready to add to
35  * the content pane.
36  * 
37  * @author jimp
38  * 
39  */
40 public abstract class JalviewDialog extends JPanel
41 {
42
43   protected JDialog frame;
44
45   protected JButton ok = new JButton();
46
47   protected JButton cancel = new JButton();
48
49   boolean block = false;
50
51   public void waitForInput()
52   {
53     if (!block)
54     {
55       new Thread(new Runnable()
56       {
57
58         public void run()
59         {
60           frame.setVisible(true);
61         }
62
63       }).start();
64     }
65     else
66     {
67       frame.setVisible(true);
68     }
69   }
70
71   protected void initDialogFrame(Container content, boolean modal,
72           boolean block, String title, int width, int height)
73   {
74
75     frame = new JDialog(Desktop.instance, modal);
76     frame.setTitle(title);
77     if (Desktop.instance != null)
78     {
79       Rectangle deskr = Desktop.instance.getBounds();
80       frame.setBounds(new Rectangle((int) (deskr.getCenterX() - width / 2),
81               (int) (deskr.getCenterY() - height / 2), width, height));
82     }
83     else
84     {
85       frame.setSize(width, height);
86     }
87     frame.setContentPane(content);
88     this.block = block;
89
90     ok.setOpaque(false);
91     ok.setText("OK");
92     ok.addActionListener(new ActionListener()
93     {
94       public void actionPerformed(ActionEvent e)
95       {
96         okPressed();
97         closeDialog();
98       }
99     });
100     cancel.setOpaque(false);
101     cancel.setText("Cancel");
102     cancel.addActionListener(new ActionListener()
103     {
104       public void actionPerformed(ActionEvent e)
105       {
106         cancelPressed();
107         closeDialog();
108       }
109     });
110     frame.addWindowListener(new WindowListener()
111     {
112
113       @Override
114       public void windowOpened(WindowEvent e)
115       {
116         // TODO Auto-generated method stub
117
118       }
119
120       @Override
121       public void windowIconified(WindowEvent e)
122       {
123         // TODO Auto-generated method stub
124
125       }
126
127       @Override
128       public void windowDeiconified(WindowEvent e)
129       {
130         // TODO Auto-generated method stub
131
132       }
133
134       @Override
135       public void windowDeactivated(WindowEvent e)
136       {
137         // TODO Auto-generated method stub
138
139       }
140
141       @Override
142       public void windowClosing(WindowEvent e)
143       {
144         // user has cancelled the dialog
145         closeDialog();
146       }
147
148       @Override
149       public void windowClosed(WindowEvent e)
150       {
151       }
152
153       @Override
154       public void windowActivated(WindowEvent e)
155       {
156         // TODO Auto-generated method stub
157
158       }
159     });
160   }
161
162   /**
163    * clean up and raise the 'dialog closed' event by calling raiseClosed
164    */
165   protected void closeDialog()
166   {
167     try
168     {
169       frame.dispose();
170       raiseClosed();
171     } catch (Exception ex)
172     {
173     }
174   }
175
176   protected abstract void raiseClosed();
177
178   protected abstract void okPressed();
179
180   protected abstract void cancelPressed();
181
182 }