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