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