KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > swing > DualTextEntryDialog


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.swing;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import javax.swing.*;
25
26 import org.openharmonise.vfs.context.*;
27 import org.openharmonise.vfs.gui.*;
28
29 /**
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class DualTextEntryDialog extends JDialog implements KeyListener, LayoutManager, ActionListener, ContextListener {
36
37     private JButton m_okButton = null;
38     private JButton m_cancelButton = null;
39     
40     private JTextField m_textfield1 = null;
41     private JLabel m_caption1 = null;
42     
43     private JTextField m_textfield2 = null;
44     private JLabel m_caption2 = null;
45     
46     private JLabel m_label = null;
47
48     /**
49      *
50      */

51     private void setup() {
52         ContextHandler.getInstance().addListener(ContextType.CONTEXT_APP_FOCUS, this);
53             
54         this.setResizable(false);
55         
56         this.getContentPane().setLayout(this);
57         
58         this.setSize(400,170);
59         int x = this.getGraphicsConfiguration().getBounds().width/2-this.getSize().width/2;
60         int y = this.getGraphicsConfiguration().getBounds().height/2-this.getSize().height/2;
61         
62         this.setLocation(x, y);
63
64         this.m_textfield1 = new JTextField();
65         this.m_textfield1.addKeyListener(this);
66         this.getContentPane().add(this.m_textfield1);
67         
68         this.m_caption1 = new JLabel();
69         this.getContentPane().add(this.m_caption1);
70
71         this.m_textfield2 = new JTextField();
72         this.m_textfield2.addKeyListener(this);
73         this.getContentPane().add(this.m_textfield2);
74         
75         this.m_caption2 = new JLabel();
76         this.getContentPane().add(this.m_caption2);
77
78         String JavaDoc fontName = "Dialog";
79         int fontSize = 11;
80         Font font = new Font(fontName, Font.PLAIN, fontSize);
81         this.getContentPane().setBackground(new Color(224,224,224));
82         
83         m_okButton = new JButton("OK");
84         m_okButton.setActionCommand("OK");
85         m_okButton.setFont(font);
86         m_okButton.addActionListener(this);
87         this.getContentPane().add(m_okButton);
88         
89         m_cancelButton = new JButton("Cancel");
90         m_cancelButton.setActionCommand("CANCEL");
91         m_cancelButton.setFont(font);
92         m_cancelButton.addActionListener(this);
93         this.getContentPane().add(m_cancelButton);
94     }
95     
96     public void setLabelText(String JavaDoc sLabel) {
97         String JavaDoc fontName = "Dialog";
98         int fontSize = 11;
99         Font font = new Font(fontName, Font.PLAIN, fontSize);
100         
101         this.m_label = new JLabel(sLabel);
102         this.m_label.setFont(font);
103         this.getContentPane().add(this.m_label);
104         this.setSize(300,190);
105     }
106     
107     public void setLabelColor(Color color) {
108         if(this.m_label!=null) {
109             this.m_label.setForeground(color);
110         }
111     }
112     
113     public static void main(String JavaDoc[] args) {
114
115         JFrame frame = new JFrame();
116         frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
117         
118         SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Rename");
119         dialog.show();
120         
121         dialog = new SingleTextEntryDialog(frame, "Warning");
122         dialog.setLabelText("Some label text sd fsd ds fsd fs dfds dsfs dfs fds dfsd fds fsd sd sd .");
123         dialog.show();
124     }
125
126     /**
127      * @param arg0
128      * @throws java.awt.HeadlessException
129      */

130     private DualTextEntryDialog(Frame arg0) throws HeadlessException {
131         super(arg0, true);
132     }
133     
134     /**
135      * @throws java.awt.HeadlessException
136      */

137     private DualTextEntryDialog() throws HeadlessException {
138         super();
139     }
140
141     /**
142      * @param arg0
143      * @throws java.awt.HeadlessException
144      */

145     private DualTextEntryDialog(Dialog arg0) throws HeadlessException {
146         super(arg0);
147     }
148
149     /**
150      * @param arg0
151      * @param arg1
152      * @throws java.awt.HeadlessException
153      */

154     private DualTextEntryDialog(Dialog arg0, boolean arg1)
155         throws HeadlessException {
156         super(arg0, arg1);
157     }
158
159     /**
160      * @param arg0
161      * @param arg1
162      * @throws java.awt.HeadlessException
163      */

164     private DualTextEntryDialog(Frame arg0, boolean arg1)
165         throws HeadlessException {
166         super(arg0, arg1);
167     }
168
169     /**
170      * @param arg0
171      * @param arg1
172      * @throws java.awt.HeadlessException
173      */

174     private DualTextEntryDialog(Dialog arg0, String JavaDoc arg1)
175         throws HeadlessException {
176         super(arg0, arg1);
177     }
178
179     /**
180      * @param arg0
181      * @param arg1
182      * @param arg2
183      * @throws java.awt.HeadlessException
184      */

185     private DualTextEntryDialog(Dialog arg0, String JavaDoc arg1, boolean arg2)
186         throws HeadlessException {
187         super(arg0, arg1, arg2);
188     }
189
190     /**
191      * @param arg0
192      * @param arg1
193      * @throws java.awt.HeadlessException
194      */

195     public DualTextEntryDialog(Frame arg0, String JavaDoc arg1)
196         throws HeadlessException {
197         super(arg0, arg1, true);
198         this.setup();
199     }
200
201     /**
202      * @param arg0
203      * @param arg1
204      * @param arg2
205      * @throws java.awt.HeadlessException
206      */

207     private DualTextEntryDialog(Frame arg0, String JavaDoc arg1, boolean arg2)
208         throws HeadlessException {
209         super(arg0, arg1, arg2);
210     }
211
212     /**
213      * @param arg0
214      * @param arg1
215      * @param arg2
216      * @param arg3
217      * @throws java.awt.HeadlessException
218      */

219     private DualTextEntryDialog(
220         Dialog arg0,
221         String JavaDoc arg1,
222         boolean arg2,
223         GraphicsConfiguration arg3)
224         throws HeadlessException {
225         super(arg0, arg1, arg2, arg3);
226     }
227
228     /**
229      * @param arg0
230      * @param arg1
231      * @param arg2
232      * @param arg3
233      */

234     private DualTextEntryDialog(
235         Frame arg0,
236         String JavaDoc arg1,
237         boolean arg2,
238         GraphicsConfiguration arg3) {
239         super(arg0, arg1, arg2, arg3);
240     }
241
242     /* (non-Javadoc)
243      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
244      */

245     public void removeLayoutComponent(Component arg0) {
246         
247     }
248
249     /* (non-Javadoc)
250      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
251      */

252     public void layoutContainer(Container arg0) {
253         int nHeight = 10;
254         
255         if(this.m_label!=null) {
256             this.m_label.setLocation(10, nHeight);
257             this.m_label.setSize(280,20);
258             nHeight = nHeight+20;
259         }
260         
261         m_caption1.setLocation(10, nHeight);
262         m_caption1.setSize(280,20);
263         nHeight = nHeight+20;
264         
265         m_textfield1.setLocation(10, nHeight);
266         m_textfield1.setSize(280,20);
267         nHeight = nHeight+20;
268         
269         m_caption2.setLocation(10, nHeight);
270         m_caption2.setSize(280,20);
271         nHeight = nHeight+20;
272         
273         m_textfield2.setLocation(10, nHeight);
274         m_textfield2.setSize(280,20);
275         nHeight = nHeight+30;
276
277         m_okButton.setLocation(130, nHeight);
278         m_okButton.setSize(70,20);
279         
280         m_cancelButton.setLocation(210, nHeight);
281         m_cancelButton.setSize(70,20);
282     }
283
284     /* (non-Javadoc)
285      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
286      */

287     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
288         
289     }
290
291     /* (non-Javadoc)
292      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
293      */

294     public Dimension minimumLayoutSize(Container arg0) {
295         return this.getSize();
296     }
297
298     /* (non-Javadoc)
299      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
300      */

301     public Dimension preferredLayoutSize(Container arg0) {
302         return this.getSize();
303     }
304
305     /* (non-Javadoc)
306      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
307      */

308     public void actionPerformed(ActionEvent ae) {
309         if(ae.getActionCommand().equals("OK")) {
310             // NO-OP
311
} else if(ae.getActionCommand().equals("CANCEL")) {
312             this.setText1Value("");
313             this.setText2Value("");
314         }
315         ContextHandler.getInstance().removeListener(ContextType.CONTEXT_APP_FOCUS, this);
316         this.hide();
317     }
318     
319     public void setCaption1(String JavaDoc string) {
320         this.m_caption1.setText(string);
321     }
322     
323     public void setCaption2(String JavaDoc string) {
324         this.m_caption2.setText(string);
325     }
326
327     /**
328      * @param string
329      */

330     public void setText2Value(String JavaDoc string) {
331         this.m_textfield2.setText(string);
332     }
333     
334     public String JavaDoc getText2Value() {
335         return this.m_textfield2.getText().trim();
336     }
337
338     /**
339      * @param string
340      */

341     public void setText1Value(String JavaDoc string) {
342         this.m_textfield1.setText(string);
343     }
344     
345     public String JavaDoc getText1Value() {
346         return this.m_textfield1.getText().trim();
347     }
348
349     /* (non-Javadoc)
350      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
351      */

352     public void keyPressed(KeyEvent ke) {
353         if(ke.getKeyCode()==KeyEvent.VK_ENTER) {
354             this.hide();
355         }
356     }
357
358     /* (non-Javadoc)
359      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
360      */

361     public void keyReleased(KeyEvent arg0) {
362     }
363
364     /* (non-Javadoc)
365      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
366      */

367     public void keyTyped(KeyEvent arg0) {
368     }
369
370     /* (non-Javadoc)
371      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
372      */

373     public void contextMessage(ContextEvent ce) {
374         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_APP_FOCUS) {
375             this.toFront();
376         }
377     }
378
379 }
380
Popular Tags