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