JAL-3253 jalview.bin.Instance handles all singleton instances -
[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.bin.Instance;
24 import jalview.util.MessageManager;
25
26 import java.awt.Container;
27 import java.awt.Dimension;
28 import java.awt.Rectangle;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33
34 import javax.swing.JButton;
35 import javax.swing.JDialog;
36 import javax.swing.JPanel;
37
38 /**
39  * Boilerplate dialog class. Implements basic functionality necessary for model
40  * blocking/non-blocking dialogs with an OK and Cancel button ready to add to
41  * the content pane.
42  * 
43  * @author jimp
44  * 
45  */
46 public abstract class JalviewDialog extends JPanel
47 {
48
49   protected JDialog frame;
50
51   protected JButton ok = new JButton();
52
53   protected JButton cancel = new JButton();
54
55   boolean block = false;
56
57   public void waitForInput()
58   {
59     if (!block)
60     {
61       new Thread(new Runnable()
62       {
63
64         @Override
65         public void run()
66         {
67           frame.setVisible(true);
68         }
69
70       }).start();
71     }
72     else
73     {
74       frame.setVisible(true);
75     }
76   }
77
78   protected void initDialogFrame(Container content, boolean modal,
79           boolean block, String title, int width, int height)
80   {
81
82     frame = new JDialog(Instance.getDesktop(), modal);
83     frame.setTitle(title);
84     if (Instance.getDesktop() != null)
85     {
86       Rectangle deskr = Instance.getDesktop().getBounds();
87       frame.setBounds(new Rectangle((int) (deskr.getCenterX() - width / 2),
88               (int) (deskr.getCenterY() - height / 2), width, height));
89     }
90     else
91     {
92       frame.setSize(width, height);
93     }
94     int minWidth = width - 100;
95     int minHeight = height - 100;
96     frame.setMinimumSize(new Dimension(minWidth, minHeight));
97     frame.setContentPane(content);
98     this.block = block;
99
100     ok.setOpaque(false);
101     ok.setText(MessageManager.getString("action.ok"));
102     ok.addActionListener(new ActionListener()
103     {
104       @Override
105       public void actionPerformed(ActionEvent e)
106       {
107         okPressed();
108         closeDialog();
109       }
110     });
111     cancel.setOpaque(false);
112     cancel.setText(MessageManager.getString("action.cancel"));
113     cancel.addActionListener(new ActionListener()
114     {
115       @Override
116       public void actionPerformed(ActionEvent e)
117       {
118         cancelPressed();
119         closeDialog();
120       }
121     });
122     frame.addWindowListener(new WindowAdapter()
123     {
124       @Override
125       public void windowClosing(WindowEvent e)
126       {
127         // user has cancelled the dialog
128         closeDialog();
129       }
130     });
131   }
132
133   /**
134    * clean up and raise the 'dialog closed' event by calling raiseClosed
135    */
136   protected void closeDialog()
137   {
138     try
139     {
140       raiseClosed();
141       frame.dispose();
142     } catch (Exception ex)
143     {
144     }
145   }
146
147   protected abstract void raiseClosed();
148
149   protected abstract void okPressed();
150
151   protected abstract void cancelPressed();
152
153 }