Source Code Cross Referenced for JTextFieldComponent.java in  » XML-UI » SwingML » org » swingml » component » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » SwingML » org.swingml.component 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * SwingML Copyright (C) 2002 SwingML Team
003:         * 
004:         * This library is free software; you can redistribute it and/or modify it under
005:         * the terms of the GNU Lesser General Public License as published by the Free
006:         * Software Foundation; either version 2 of the License, or (at your option) any
007:         * later version.
008:         * 
009:         * This library is distributed in the hope that it will be useful, but WITHOUT
010:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011:         * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012:         * details.
013:         * 
014:         * You should have received a copy of the GNU Lesser General Public License
015:         * along with this library; if not, write to the Free Software Foundation, Inc.,
016:         * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017:         * 
018:         * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com> 
019:         *               Farid Ibrahim <faridibrahim@lycos.com>
020:         * 
021:         */
022:
023:        package org.swingml.component;
024:
025:        import java.awt.datatransfer.*;
026:        import java.awt.event.*;
027:        import java.util.*;
028:
029:        import javax.swing.*;
030:        import javax.swing.event.*;
031:
032:        import org.swingml.*;
033:        import org.swingml.event.*;
034:        import org.swingml.model.*;
035:
036:        public class JTextFieldComponent extends JTextField implements 
037:                XMLTranslatable, DocumentListener, FocusListener,
038:                MouseListener, KeyListener, ISwingMLTextContainer {
039:
040:            private class ContextMenu extends JPopupMenu implements 
041:                    ActionListener {
042:
043:                private JTextFieldComponent m_textField = null;
044:                private final String MENU_COPY = "Copy";
045:                private final String MENU_CUT = "Cut";
046:                private final String MENU_DELETE = "Delete";
047:                private final String MENU_PASTE = "Paste";
048:                private final String MENU_SELECT_ALL = "Select All";
049:
050:                private final String MENU_SEPARATOR = "-";
051:
052:                public ContextMenu(final JTextFieldComponent textField) {
053:                    this .m_textField = textField;
054:
055:                    String items[] = { MENU_CUT, MENU_COPY, MENU_PASTE,
056:                            MENU_DELETE, MENU_SEPARATOR, MENU_SELECT_ALL };
057:
058:                    for (int i = 0; i < items.length; i++) {
059:                        if (items[i].equalsIgnoreCase(MENU_SEPARATOR)) {
060:                            javax.swing.JSeparator separator = new JSeparator();
061:                            this .add(separator);
062:                        } else {
063:                            JMenuItem menuItem = new JMenuItem(items[i]);
064:                            menuItem.addActionListener(this );
065:
066:                            boolean isEnabled = ((items[i]
067:                                    .equalsIgnoreCase(MENU_CUT)
068:                                    || items[i].equalsIgnoreCase(MENU_COPY) || items[i]
069:                                    .equalsIgnoreCase(MENU_DELETE))
070:                                    && (this .m_textField.getSelectedText() == null || this .m_textField
071:                                            .getSelectedText().length() == 0) ? false
072:                                    : true);
073:
074:                            // This entire block is used just to determine whether or
075:                            // not the
076:                            // data in the clipboard is available for pasting into a
077:                            // text field.
078:                            if (items[i].equalsIgnoreCase(MENU_PASTE)) {
079:                                try {
080:                                    Clipboard clipBoard = this .m_textField
081:                                            .getToolkit().getSystemClipboard();
082:
083:                                    if (clipBoard != null) {
084:                                        Transferable clipObject = clipBoard
085:                                                .getContents(null);
086:                                        Object realData = clipObject
087:                                                .getTransferData(DataFlavor.stringFlavor);
088:                                        isEnabled = (realData != null);
089:                                    }
090:                                } catch (UnsupportedFlavorException e) {
091:                                    isEnabled = false;
092:                                } catch (Exception e) {
093:                                    // Swallow the exception.
094:                                }
095:                            }
096:                            // Finally set the enabled status of the menu item.
097:                            menuItem.setEnabled(isEnabled);
098:                            this .add(menuItem);
099:                        }
100:                    }
101:                }
102:
103:                public void actionPerformed(final ActionEvent event) {
104:                    if (event.getActionCommand().equalsIgnoreCase(MENU_CUT)) {
105:                        this .m_textField.cut();
106:                    } else if (event.getActionCommand().equalsIgnoreCase(
107:                            MENU_COPY)) {
108:                        this .m_textField.copy();
109:                    } else if (event.getActionCommand().equalsIgnoreCase(
110:                            MENU_PASTE)) {
111:                        this .m_textField.paste();
112:
113:                    } else if (event.getActionCommand().equalsIgnoreCase(
114:                            MENU_DELETE)) {
115:                        if (this .m_textField.getSelectedText() != null
116:                                && this .m_textField.getSelectedText().length() > 0) {
117:                            StringBuffer text = new StringBuffer(
118:                                    this .m_textField.getText());
119:
120:                            text.replace(this .m_textField.getSelectionStart(),
121:                                    this .m_textField.getSelectionEnd(), "");
122:                            this .m_textField.setText(text.toString());
123:                        }
124:                    } else if (event.getActionCommand().equalsIgnoreCase(
125:                            MENU_SELECT_ALL)) {
126:                        this .m_textField.requestFocus();
127:                        this .m_textField.selectAll();
128:                    }
129:                }
130:            }
131:
132:            private EventHandler eventHandler = EventHandler.getInstance();
133:            private JTextFieldModel model;
134:
135:            public JTextFieldComponent(final JTextFieldModel theModel) {
136:                this .model = theModel;
137:                setVisible(theModel.isVisible());
138:                setName(theModel.getName());
139:                setText(theModel.getText());
140:                setColumns(Integer.parseInt(theModel.getCols()));
141:                setEditable(theModel.isEditable());
142:                getDocument().addDocumentListener(this );
143:                addFocusListener(this );
144:                setToolTipText(theModel.getTooltip());
145:                addMouseListener(this );
146:                List listeners = theModel.getListeners();
147:                for (int i = 0; i < listeners.size(); i++) {
148:                    ListenerModel lm = (ListenerModel) listeners.get(i);
149:                    if (lm.getEvent().indexOf("KeyListener") != -1) {
150:                        addKeyListener(this );
151:                        break;
152:                    }
153:                }
154:                if (theModel.getForeground() != null) {
155:                    setForeground(theModel.getForeground());
156:                }
157:
158:                if (theModel.getBackground() != null) {
159:                    setBackground(theModel.getBackground());
160:                }
161:
162:                setEnabled(theModel.isEnabled());
163:
164:                if (null != theModel.getReturnButton()) {
165:                    addKeyListener(new ReturnKeyAdapter(theModel
166:                            .getReturnButton()));
167:                }
168:            }
169:
170:            public void changedUpdate(final DocumentEvent aEvt) {
171:                this .eventHandler.handleEvent(this .getModel(),
172:                        Constants.CHANGED_UPDATE, this );
173:            }
174:
175:            public void focusGained(final FocusEvent aEvt) {
176:                this .eventHandler.handleEvent(this .getModel(),
177:                        Constants.FOCUS_GAINED, this );
178:                if (!aEvt.isTemporary()
179:                        && aEvt.getID() == FocusEvent.FOCUS_GAINED) {
180:                    internalFocusGained();
181:                }
182:            }
183:
184:            public void focusLost(final FocusEvent aEvt) {
185:                this .eventHandler.handleEvent(this .getModel(),
186:                        Constants.FOCUS_LOST, this );
187:            }
188:
189:            public JTextFieldModel getModel() {
190:                return this .model;
191:            }
192:
193:            public String getXMLValue() {
194:                return super .getText();
195:            }
196:
197:            public void insertUpdate(final DocumentEvent aEvt) {
198:                this .eventHandler.handleEvent(this .getModel(),
199:                        Constants.INSERT_UPDATE, this );
200:            }
201:
202:            private void internalFocusGained() {
203:                if (model.shouldAutoSelectOnFocus()) {
204:                    selectAll();
205:                }
206:            }
207:
208:            /**
209:             * Don't allow focus if the field is not editable
210:             */
211:            public boolean isFocusable() {
212:                return isEditable();
213:            }
214:
215:            public void keyPressed(KeyEvent e) {
216:            }
217:
218:            public void keyReleased(KeyEvent e) {
219:                this .eventHandler.handleEvent(this .getModel(),
220:                        Constants.KEY_RELEASED, this );
221:            }
222:
223:            public void keyTyped(KeyEvent e) {
224:            }
225:
226:            public void mouseClicked(final MouseEvent aEvt) {
227:                possiblePopup(aEvt);
228:                if (!hasFocus()) {
229:                    internalFocusGained();
230:                }
231:            }
232:
233:            public void mouseEntered(final MouseEvent aEvt) {
234:            }
235:
236:            public void mouseExited(final MouseEvent aEvt) {
237:            }
238:
239:            public void mousePressed(final MouseEvent aEvt) {
240:                possiblePopup(aEvt);
241:            }
242:
243:            public void mouseReleased(final MouseEvent aEvt) {
244:                possiblePopup(aEvt);
245:            }
246:
247:            public void possiblePopup(final MouseEvent aEvt) {
248:                if (aEvt.isPopupTrigger()) {
249:                    ContextMenu thePopup = new ContextMenu(this );
250:                    thePopup
251:                            .show(aEvt.getComponent(), aEvt.getX(), aEvt.getY());
252:                }
253:            }
254:
255:            public void removeUpdate(final DocumentEvent aEvt) {
256:                this.eventHandler.handleEvent(this.getModel(),
257:                        Constants.REMOVE_UPDATE, this);
258:            }
259:
260:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.