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