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