Source Code Cross Referenced for JLabeledChoice.java in  » Testing » jakarta-jmeter » org » apache » jorphan » gui » 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 » Testing » jakarta jmeter » org.apache.jorphan.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:
019:        package org.apache.jorphan.gui;
020:
021:        import java.awt.Insets;
022:        import java.awt.event.ActionEvent;
023:        import java.awt.event.ActionListener;
024:        import java.awt.event.ItemEvent;
025:        import java.awt.event.ItemListener;
026:        import java.util.ArrayList;
027:        import java.util.LinkedList;
028:        import java.util.List;
029:
030:        import javax.swing.BorderFactory;
031:        import javax.swing.JButton;
032:        import javax.swing.JComboBox;
033:        import javax.swing.JLabel;
034:        import javax.swing.JPanel;
035:        import javax.swing.event.ChangeEvent;
036:        import javax.swing.event.ChangeListener;
037:
038:        public class JLabeledChoice extends JPanel implements  JLabeledField {
039:            private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
040:
041:            private final JLabel mLabel = new JLabel();
042:
043:            private final JComboBox choiceList;
044:
045:            // Maybe move to vector if MT problems occur
046:            private final ArrayList mChangeListeners = new ArrayList(3);
047:
048:            private JButton delete, add;
049:
050:            /**
051:             * Default constructor, The label and the Text field are left empty.
052:             */
053:            public JLabeledChoice() {
054:                super ();
055:                choiceList = new JComboBox();
056:                init();
057:            }
058:
059:            public JLabeledChoice(String pLabel, boolean editable) {
060:                super ();
061:                choiceList = new JComboBox();
062:                mLabel.setText(pLabel);
063:                choiceList.setEditable(editable);
064:                init();
065:            }
066:
067:            /**
068:             * Constructs a non-edittable combo-box with the label displaying the passed text.
069:             * 
070:             * @param pLabel - the text to display in the label.
071:             * @param items - the items to display in the Combo box
072:             */
073:            public JLabeledChoice(String pLabel, String[] items) {
074:                this (pLabel, items, false);
075:            }
076:
077:            /**
078:             * Constructs a combo-box with the label displaying the passed text.
079:             * 
080:             * @param pLabel - the text to display in the label.
081:             * @param items - the items to display in the Combo box
082:             * @param editable - if true, then Add and Delete buttons are created.
083:             * 
084:             */
085:            public JLabeledChoice(String pLabel, String[] items,
086:                    boolean editable) {
087:                super ();
088:                mLabel.setText(pLabel);
089:                choiceList = new JComboBox(items);
090:                choiceList.setEditable(editable);
091:                init();
092:            }
093:
094:            public List getComponentList() {
095:                List comps = new LinkedList();
096:                comps.add(mLabel);
097:                comps.add(choiceList);
098:                return comps;
099:            }
100:
101:            public void setEditable(boolean editable) {
102:                choiceList.setEditable(editable);
103:            }
104:
105:            public void addValue(String item) {
106:                choiceList.addItem(item);
107:            }
108:
109:            public void setValues(String[] items) {
110:                choiceList.removeAllItems();
111:                for (int i = 0; i < items.length; i++) {
112:                    choiceList.addItem(items[i]);
113:                }
114:            }
115:
116:            /**
117:             * Initialises all of the components on this panel.
118:             */
119:            private void init() {
120:                /*
121:                 * if(choiceList.isEditable()) { choiceList.addActionListener(new
122:                 * ComboListener()); }
123:                 */
124:                choiceList.setBorder(BorderFactory.createLoweredBevelBorder());
125:                // Register the handler for focus listening. This handler will
126:                // only notify the registered when the text changes from when
127:                // the focus is gained to when it is lost.
128:                choiceList.addItemListener(new ItemListener() {
129:                    /**
130:                     * Callback method when the focus to the Text Field component is
131:                     * lost.
132:                     * 
133:                     * @param e
134:                     *            The focus event that occured.
135:                     */
136:                    public void itemStateChanged(ItemEvent e) {
137:                        if (e.getStateChange() == ItemEvent.SELECTED) {
138:                            notifyChangeListeners();
139:                        }
140:                    }
141:                });
142:
143:                // Add the sub components
144:                this .add(mLabel);
145:                this .add(choiceList);
146:                if (choiceList.isEditable()) {
147:                    add = new JButton("Add");
148:                    add.setMargin(new Insets(1, 1, 1, 1));
149:                    add.addActionListener(new AddListener());
150:                    this .add(add);
151:                    delete = new JButton("Del");
152:                    delete.setMargin(new Insets(1, 1, 1, 1));
153:                    delete.addActionListener(new DeleteListener());
154:                    this .add(delete);
155:                }
156:
157:            }
158:
159:            /**
160:             * Set the text displayed in the label.
161:             * 
162:             * @param pLabel
163:             *            The new label text.
164:             */
165:            public void setLabel(String pLabel) {
166:                mLabel.setText(pLabel);
167:            }
168:
169:            /**
170:             * Set the text displayed in the Text Field.
171:             * 
172:             * @param pText
173:             *            The new text to display in the text field.
174:             */
175:            public void setText(String pText) {
176:                choiceList.setSelectedItem(pText);
177:            }
178:
179:            public void setSelectedIndex(int index) {
180:                choiceList.setSelectedIndex(index);
181:            }
182:
183:            /**
184:             * Returns the text in the Text Field.
185:             * 
186:             * @return The text in the Text Field. Never returns null.
187:             */
188:            public String getText() {
189:                Object item = choiceList.getSelectedItem();
190:                if (item == null) {
191:                    return "";
192:                } else {
193:                    return (String) item;
194:                }
195:            }
196:
197:            public int getSelectedIndex() {
198:                return choiceList.getSelectedIndex();
199:            }
200:
201:            public Object[] getSelectedItems() {
202:                Object list[] = choiceList.getSelectedObjects();
203:                if (list == null) {
204:                    return EMPTY_OBJECT_ARRAY;
205:                }
206:                return list;
207:            }
208:
209:            public String[] getItems() {
210:                String[] items = new String[choiceList.getItemCount()];
211:                for (int i = 0; i < items.length; i++) {
212:                    items[i] = (String) choiceList.getItemAt(i);
213:                }
214:                return items;
215:            }
216:
217:            /**
218:             * Returns the text of the label.
219:             * 
220:             * @return The text of the label.
221:             */
222:            public String getLabel() {
223:                return mLabel.getText();
224:            }
225:
226:            /**
227:             * Adds a change listener, that will be notified when the text in the text
228:             * field is changed. The ChangeEvent that will be passed to registered
229:             * listeners will contain this object as the source, allowing the new text
230:             * to be extracted using the {@link #getText() getText} method.
231:             * 
232:             * @param pChangeListener
233:             *            The listener to add
234:             */
235:            public void addChangeListener(ChangeListener pChangeListener) {
236:                mChangeListeners.add(pChangeListener);
237:            }
238:
239:            /**
240:             * Removes a change listener.
241:             * 
242:             * @param pChangeListener
243:             *            The change listener to remove.
244:             */
245:            public void removeChangeListener(ChangeListener pChangeListener) {
246:                mChangeListeners.remove(pChangeListener);
247:            }
248:
249:            /**
250:             * Notify all registered change listeners that the text in the text field
251:             * has changed.
252:             */
253:            private void notifyChangeListeners() {
254:                ChangeEvent ce = new ChangeEvent(this );
255:                for (int index = 0; index < mChangeListeners.size(); index++) {
256:                    ((ChangeListener) mChangeListeners.get(index))
257:                            .stateChanged(ce);
258:                }
259:            }
260:
261:            private class AddListener implements  ActionListener {
262:
263:                public void actionPerformed(ActionEvent e) {
264:                    Object item = choiceList.getSelectedItem();
265:                    int index = choiceList.getSelectedIndex();
266:                    if (!item.equals(choiceList.getItemAt(index))) {
267:                        choiceList.addItem(item);
268:                    }
269:                    choiceList.setSelectedItem(item);
270:                    notifyChangeListeners();
271:                }
272:            }
273:
274:            private class DeleteListener implements  ActionListener {
275:
276:                public void actionPerformed(ActionEvent e) {
277:                    if (choiceList.getItemCount() > 1) {
278:                        choiceList.removeItemAt(choiceList.getSelectedIndex());
279:                        notifyChangeListeners();
280:                    }
281:                }
282:            }
283:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.