KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > ListMapping


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ListMapping.java
21  *
22  * Created on January 9, 2004, 3:05 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.util.List JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30
31 /** Class that associates a list with a calculated string. This is so a field of
32  * a combobox or table that actually represents an entire list can have a nice
33  * text field displayed. The default toString() on ArrayList is not sufficient.
34  * This class could be generalized to Collection if necessary, though additional
35  * typed accessors would be necessary (e.g. valueAsList(), valueAsCollection(),
36  * etc.) It is expected that the underlying list may be changed during this
37  * object's lifetime, and between calls to toString().
38  *
39  * @author Peter Williams
40  * @version %I%, %G%
41  */

42 public class ListMapping {
43     
44     // Standard resource bundle to use for non-property list fields
45
private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
46         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); // NOI18N
47

48     private static final String JavaDoc formatPattern = bundle.getString("LBL_SizeOfListText"); // NOI18N
49

50     private List JavaDoc theList;
51     private String JavaDoc displayText;
52     private int listSize;
53
54     public ListMapping(List JavaDoc l) {
55         theList = l;
56         displayText = null;
57         listSize = 0;
58     }
59
60     public String JavaDoc toString() {
61         if(textOutOfDate()) {
62             buildDisplayText();
63         }
64         
65         return displayText;
66     }
67     
68     private void buildDisplayText() {
69         listSize = (theList != null) ? theList.size() : 0;
70         Object JavaDoc [] args = { new Integer JavaDoc(listSize) };
71         displayText = MessageFormat.format(formatPattern, args);
72     }
73     
74     private boolean textOutOfDate() {
75         // Rebuild display Text if text is null or if size of list has changed.
76
if(displayText == null) {
77             return true;
78         }
79         
80         int newListSize = 0;
81         if(theList != null) {
82             newListSize = theList.size();
83         }
84         
85         if(listSize != newListSize) {
86             return true;
87         }
88         
89         return false;
90     }
91
92     public List JavaDoc getList() {
93         return theList;
94     }
95 }
96
Popular Tags