001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.swing;
020:
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dialog;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.Frame;
028: import java.awt.GraphicsConfiguration;
029: import java.awt.HeadlessException;
030: import java.awt.LayoutManager;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.KeyEvent;
034: import java.awt.event.KeyListener;
035:
036: import javax.swing.ImageIcon;
037: import javax.swing.JButton;
038: import javax.swing.JDialog;
039: import javax.swing.JFrame;
040: import javax.swing.JLabel;
041: import javax.swing.JTextField;
042:
043: import org.openharmonise.vfs.context.*;
044: import org.openharmonise.vfs.gui.*;
045:
046: /**
047: * @author Matthew Large
048: * @version $Revision: 1.1 $
049: *
050: */
051: public class SingleTextEntryDialog extends JDialog implements
052: KeyListener, LayoutManager, ActionListener, ContextListener {
053:
054: private JButton m_okButton = null;
055: private JButton m_cancelButton = null;
056:
057: private JTextField m_textfield = null;
058:
059: private JLabel m_label = null;
060:
061: /**
062: *
063: */
064: private void setup() {
065: ContextHandler.getInstance().addListener(
066: ContextType.CONTEXT_APP_FOCUS, this );
067:
068: this .setResizable(false);
069:
070: this .getContentPane().setLayout(this );
071:
072: this .setSize(300, 100);
073: int x = this .getGraphicsConfiguration().getBounds().width / 2
074: - this .getSize().width / 2;
075: int y = this .getGraphicsConfiguration().getBounds().height / 2
076: - this .getSize().height / 2;
077:
078: this .setLocation(x, y);
079:
080: this .m_textfield = new JTextField();
081: this .m_textfield.addKeyListener(this );
082: this .getContentPane().add(this .m_textfield);
083:
084: String fontName = "Dialog";
085: int fontSize = 11;
086: Font font = new Font(fontName, Font.PLAIN, fontSize);
087: this .getContentPane().setBackground(new Color(224, 224, 224));
088:
089: m_okButton = new JButton("OK");
090: m_okButton.setActionCommand("OK");
091: m_okButton.setFont(font);
092: m_okButton.addActionListener(this );
093: this .getContentPane().add(m_okButton);
094:
095: m_cancelButton = new JButton("Cancel");
096: m_cancelButton.setActionCommand("CANCEL");
097: m_cancelButton.setFont(font);
098: m_cancelButton.addActionListener(this );
099: this .getContentPane().add(m_cancelButton);
100: }
101:
102: public void setLabelText(String sLabel) {
103: String fontName = "Dialog";
104: int fontSize = 11;
105: Font font = new Font(fontName, Font.PLAIN, fontSize);
106:
107: this .m_label = new JLabel(sLabel);
108: this .m_label.setFont(font);
109: this .getContentPane().add(this .m_label);
110: this .setSize(300, 120);
111: }
112:
113: public void setLabelColor(Color color) {
114: if (this .m_label != null) {
115: this .m_label.setForeground(color);
116: }
117: }
118:
119: public static void main(String[] args) {
120:
121: JFrame frame = new JFrame();
122: frame.setIconImage(((ImageIcon) IconManager.getInstance()
123: .getIcon("32-sim-logo.gif")).getImage());
124:
125: SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame,
126: "Rename");
127: dialog.show();
128:
129: dialog = new SingleTextEntryDialog(frame, "Warning");
130: dialog
131: .setLabelText("Some label text sd fsd ds fsd fs dfds dsfs dfs fds dfsd fds fsd sd sd .");
132: dialog.show();
133: }
134:
135: /**
136: * @param arg0
137: * @throws java.awt.HeadlessException
138: */
139: private SingleTextEntryDialog(Frame arg0) throws HeadlessException {
140: super (arg0, true);
141: }
142:
143: /**
144: * @throws java.awt.HeadlessException
145: */
146: private SingleTextEntryDialog() throws HeadlessException {
147: super ();
148: }
149:
150: /**
151: * @param arg0
152: * @throws java.awt.HeadlessException
153: */
154: private SingleTextEntryDialog(Dialog arg0) throws HeadlessException {
155: super (arg0);
156: }
157:
158: /**
159: * @param arg0
160: * @param arg1
161: * @throws java.awt.HeadlessException
162: */
163: private SingleTextEntryDialog(Dialog arg0, boolean arg1)
164: throws HeadlessException {
165: super (arg0, arg1);
166: }
167:
168: /**
169: * @param arg0
170: * @param arg1
171: * @throws java.awt.HeadlessException
172: */
173: private SingleTextEntryDialog(Frame arg0, boolean arg1)
174: throws HeadlessException {
175: super (arg0, arg1);
176: }
177:
178: /**
179: * @param arg0
180: * @param arg1
181: * @throws java.awt.HeadlessException
182: */
183: private SingleTextEntryDialog(Dialog arg0, String arg1)
184: throws HeadlessException {
185: super (arg0, arg1);
186: }
187:
188: /**
189: * @param arg0
190: * @param arg1
191: * @param arg2
192: * @throws java.awt.HeadlessException
193: */
194: private SingleTextEntryDialog(Dialog arg0, String arg1, boolean arg2)
195: throws HeadlessException {
196: super (arg0, arg1, arg2);
197: }
198:
199: /**
200: * @param arg0
201: * @param arg1
202: * @throws java.awt.HeadlessException
203: */
204: public SingleTextEntryDialog(Frame arg0, String arg1)
205: throws HeadlessException {
206: super (arg0, arg1, true);
207: this .setup();
208: }
209:
210: /**
211: * @param arg0
212: * @param arg1
213: * @param arg2
214: * @throws java.awt.HeadlessException
215: */
216: private SingleTextEntryDialog(Frame arg0, String arg1, boolean arg2)
217: throws HeadlessException {
218: super (arg0, arg1, arg2);
219: }
220:
221: /**
222: * @param arg0
223: * @param arg1
224: * @param arg2
225: * @param arg3
226: * @throws java.awt.HeadlessException
227: */
228: private SingleTextEntryDialog(Dialog arg0, String arg1,
229: boolean arg2, GraphicsConfiguration arg3)
230: throws HeadlessException {
231: super (arg0, arg1, arg2, arg3);
232: }
233:
234: /**
235: * @param arg0
236: * @param arg1
237: * @param arg2
238: * @param arg3
239: */
240: private SingleTextEntryDialog(Frame arg0, String arg1,
241: boolean arg2, GraphicsConfiguration arg3) {
242: super (arg0, arg1, arg2, arg3);
243: }
244:
245: /* (non-Javadoc)
246: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
247: */
248: public void removeLayoutComponent(Component arg0) {
249:
250: }
251:
252: /* (non-Javadoc)
253: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
254: */
255: public void layoutContainer(Container arg0) {
256: int nHeight = 10;
257:
258: if (this .m_label != null) {
259: this .m_label.setLocation(10, nHeight);
260: this .m_label.setSize(280, 20);
261: nHeight = nHeight + 20;
262: }
263:
264: m_textfield.setLocation(10, nHeight);
265: m_textfield.setSize(280, 20);
266: nHeight = nHeight + 30;
267:
268: m_okButton.setLocation(130, nHeight);
269: m_okButton.setSize(70, 20);
270:
271: m_cancelButton.setLocation(210, nHeight);
272: m_cancelButton.setSize(70, 20);
273: }
274:
275: /* (non-Javadoc)
276: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
277: */
278: public void addLayoutComponent(String arg0, Component arg1) {
279:
280: }
281:
282: /* (non-Javadoc)
283: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
284: */
285: public Dimension minimumLayoutSize(Container arg0) {
286: return this .getSize();
287: }
288:
289: /* (non-Javadoc)
290: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
291: */
292: public Dimension preferredLayoutSize(Container arg0) {
293: return this .getSize();
294: }
295:
296: /* (non-Javadoc)
297: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
298: */
299: public void actionPerformed(ActionEvent ae) {
300: if (ae.getActionCommand().equals("OK")) {
301: // NO-OP
302: } else if (ae.getActionCommand().equals("CANCEL")) {
303: this .setTextValue("");
304: }
305: ContextHandler.getInstance().removeListener(
306: ContextType.CONTEXT_APP_FOCUS, this );
307: this .hide();
308: }
309:
310: /**
311: * @param string
312: */
313: public void setTextValue(String string) {
314: this .m_textfield.setText(string);
315: }
316:
317: public String getTextValue() {
318: return this .m_textfield.getText().trim();
319: }
320:
321: /* (non-Javadoc)
322: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
323: */
324: public void keyPressed(KeyEvent ke) {
325: if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
326: this .hide();
327: }
328: }
329:
330: /* (non-Javadoc)
331: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
332: */
333: public void keyReleased(KeyEvent arg0) {
334: }
335:
336: /* (non-Javadoc)
337: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
338: */
339: public void keyTyped(KeyEvent arg0) {
340: }
341:
342: /* (non-Javadoc)
343: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
344: */
345: public void contextMessage(ContextEvent ce) {
346: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_APP_FOCUS) {
347: this.toFront();
348: }
349: }
350:
351: }
|