View Javadoc

1   package uk.ac.roe.antigen.dialogs;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.awt.FlowLayout;
6   import java.awt.GridBagConstraints;
7   import java.awt.GridBagLayout;
8   import java.awt.Insets;
9   import java.awt.Point;
10  import java.awt.Toolkit;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  
14  import javax.swing.BorderFactory;
15  import javax.swing.BoxLayout;
16  import javax.swing.ImageIcon;
17  import javax.swing.JButton;
18  import javax.swing.JFrame;
19  import javax.swing.JPanel;
20  import javax.swing.JTextArea;
21  import javax.swing.JTextField;
22  import javax.swing.border.BevelBorder;
23  import javax.swing.border.TitledBorder;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.input.InputHandler;
27  import org.apache.tools.ant.input.InputRequest;
28  
29  import uk.ac.roe.antigen.utils.Config;
30  public abstract class AbstractFilePropertyInputHandler extends javax.swing.JDialog implements InputHandler {
31  	JTextField propertyValueTxt;
32  	private JPanel jPanel1;
33  	private JButton browseBtn;
34  	private JPanel jPanel4;
35  	private JTextArea propertyNameLbl;
36  	private JPanel jPanel3;
37  	private JButton okBtn;
38  	private JPanel jPanel2;
39  
40  
41  	
42  	public AbstractFilePropertyInputHandler(JFrame frame) {
43  		super(frame);
44  		initGUI();
45  	}
46  	
47  	private void initGUI() {
48  		try {
49              setUndecorated(true);
50  			setSize(600, 180);
51  			{
52  				jPanel2 = new JPanel();
53  				this.getContentPane().add(jPanel2, BorderLayout.CENTER);
54  				BorderLayout jPanel2Layout = new BorderLayout();
55  				jPanel2.setLayout(jPanel2Layout);
56  				{
57  					jPanel3 = new JPanel();
58  					jPanel2.add(jPanel3, BorderLayout.SOUTH);
59  					FlowLayout jPanel3Layout = new FlowLayout();
60  					jPanel3.setOpaque(false);
61  					jPanel3.setLayout(jPanel3Layout);
62  					{
63  						okBtn = new JButton();
64  						jPanel3.add(okBtn);
65  						okBtn.setText("Next");
66  						okBtn.setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/rightarrow.png")));
67  						okBtn.addActionListener(new ActionListener() {
68  							public void actionPerformed(ActionEvent evt) {
69  								okPressed = true;
70  							}
71  						});
72  					}
73  				}
74  				{
75  					jPanel4 = new JPanel();
76  					BoxLayout jPanel4Layout = new BoxLayout(jPanel4, BoxLayout.PAGE_AXIS);
77  					String borderTitle = Config.getProperty("antigen.dialog.bordertitle","antigen");
78  					jPanel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.RAISED, null, null), borderTitle, TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("Eurostile",0,12), new java.awt.Color(0,0,255)));
79  					jPanel4.setLayout(jPanel4Layout);
80  					jPanel2.add(jPanel4, BorderLayout.CENTER);
81  					{
82  						propertyNameLbl = new JTextArea();
83  						jPanel4.add(propertyNameLbl);
84  						propertyNameLbl.setText("message  very long very long very long very long very long very long very long very long very long very long very long very long very long very long very long very long very long");
85  						propertyNameLbl.setFont(new java.awt.Font("Dialoginput",0,12));
86  						propertyNameLbl.setEditable(false);
87  						propertyNameLbl.setOpaque(false);
88                          propertyNameLbl.setLineWrap(true);
89                          propertyNameLbl.setWrapStyleWord(true);
90  					}
91  					{
92                          propertyValueTxt = new JTextField();
93  
94                          propertyValueTxt.setText("");
95                          propertyValueTxt.setBorder(BorderFactory
96                              .createBevelBorder(
97                                  BevelBorder.LOWERED,
98                                  null,
99                                  null,
100                                 null,
101                                 null));
102                         propertyValueTxt
103                             .addActionListener(new ActionListener() {
104                                 public void actionPerformed(ActionEvent evt) {
105                                     okPressed = true;
106                                 }
107                             });
108 
109                         browseBtn = new JButton();
110 
111                         browseBtn.setText("Browse...");
112 
113                         browseBtn.addActionListener(getBrowseButtonListener());
114                         
115 						jPanel1 = new JPanel();
116 						jPanel4.add(jPanel1);
117                         
118                         GridBagLayout gridbag = new GridBagLayout();
119                         GridBagConstraints c = new GridBagConstraints();
120                         
121                         jPanel1.setLayout(gridbag);
122 
123                         c.insets = new Insets(2,2,2,2);
124                         c.fill = GridBagConstraints.HORIZONTAL;
125                         c.weightx = 1.0;
126                        // c.weighty = 1.0;
127                         c.gridx = 0;
128                         c.gridwidth = 1;
129                         gridbag.setConstraints(propertyValueTxt,c);
130                         jPanel1.add(propertyValueTxt);
131                         
132                         c.fill = GridBagConstraints.NONE;
133                         c.weightx = 0.0;
134                         c.gridx = 1;
135                         c.gridwidth =   1;
136                         gridbag.setConstraints(browseBtn,c);
137                         jPanel1.add(browseBtn);
138                         
139                         						
140 					}
141 				}
142 			}
143 		} catch (Exception e) {
144 			e.printStackTrace();
145 		}
146         this.setLocation(getDialogLocation().width, getDialogLocation().height);
147 	}
148 
149     /***
150 	 * @return
151 	 */
152 	protected abstract ActionListener getBrowseButtonListener();
153 
154 	boolean okPressed = false;
155 
156 	/* (non-Javadoc)
157 	 * @see org.apache.tools.ant.input.InputHandler#handleInput(org.apache.tools.ant.input.InputRequest)
158 	 */
159 	public void handleInput(InputRequest arg0) throws BuildException {
160         okPressed = false;
161 		this.setVisible(true);
162         propertyNameLbl.setText(arg0.getPrompt().trim());
163         propertyValueTxt.setText(arg0.getDefault());
164 		while (!okPressed) {
165 			try {
166 				Thread.sleep(100);
167             } catch (InterruptedException ie) {}
168         }
169         arg0.setInput(propertyValueTxt.getText());
170         this.setVisible(false);
171         this.dispose(); //That's me done
172 	}
173 
174 	/***
175 	 * Calculates the desired location of the dialog on the screen.
176 	 * By default it is in the center of the screen.
177 	 *
178 	 * @return the desired location of the dialog on the screen
179 	 */
180 	private Dimension getDialogLocation() {
181         //@TODO refactor to a superclass?
182 	    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
183 	    Dimension dlgInputDim = this.getSize();
184         Dimension splashDim = getOwner().getSize();
185         Point splashLoc = getOwner().getLocationOnScreen();
186         
187 	    int dlgInputX = (int)
188 	        ((screenDim.getWidth() - dlgInputDim.getWidth()) / 2);
189 	    int dlgInputY = (int)
190 	        splashLoc.y+splashDim.height;
191 	    return new Dimension(dlgInputX, dlgInputY);
192 	}
193 
194 }