refactored popup dialog boilerplate code to own class
[jalview.git] / src / jalview / gui / JalviewDialog.java
1 package jalview.gui;
2
3 import java.awt.Container;
4 import java.awt.Rectangle;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 import javax.swing.JButton;
9 import javax.swing.JDialog;
10 import javax.swing.JPanel;
11
12 /**
13  * Boilerplate dialog class. Implements basic functionality necessary for model blocking/non-blocking dialogs
14  * with an OK and Cancel button ready to add to the content pane.
15  * @author jimp
16  *
17  */
18 public abstract class JalviewDialog extends JPanel
19 {
20
21   protected JDialog frame;
22   protected JButton ok = new JButton();
23   protected JButton cancel = new JButton();
24   boolean block=false;
25   
26   public void waitForInput()
27   {
28     if (!block)
29     {
30       new Thread(new Runnable()
31       {
32
33         public void run()
34         {
35           frame.show();
36         }
37
38       }).start();
39     }
40     else
41     {
42       frame.show();
43     }
44   }
45
46   protected void initDialogFrame(Container content,
47           boolean modal, boolean block, String title, int width, int height)
48   {
49     
50     frame = new JDialog(Desktop.instance, true);
51     frame.setTitle(title);
52     if (Desktop.instance!=null)
53     {
54     Rectangle deskr = Desktop.instance.getBounds();
55     frame.setBounds(new Rectangle((int) (deskr.getCenterX() - width/2),
56             (int) (deskr.getCenterY() - height/2), width, height));
57     } else {
58       frame.setSize(width,height);
59     }
60     frame.setContentPane(content);
61     this.block = block;
62     
63     ok.setOpaque(false);
64     ok.setText("OK");
65     ok.addActionListener(new ActionListener()
66     {
67       public void actionPerformed(ActionEvent e)
68       {
69         okPressed();
70         closeDialog();
71       }
72     });
73     cancel.setOpaque(false);
74     cancel.setText("Cancel");
75     cancel.addActionListener(new ActionListener()
76     {
77       public void actionPerformed(ActionEvent e)
78       {
79         cancelPressed();
80         closeDialog();
81       }
82     });
83
84   }
85   /**
86    * clean up and raise the 'dialog closed' event by calling raiseClosed
87    */
88   protected void closeDialog()
89   {
90     try
91     {
92       frame.dispose();
93       raiseClosed();
94     } catch (Exception ex)
95     {
96     }
97   }
98   
99   protected abstract void raiseClosed();
100   protected abstract void okPressed();
101   protected abstract void cancelPressed();
102   
103 }