KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > dso > DSOField


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.dso;
5
6 import com.tc.admin.AdminClient;
7 import com.tc.admin.ConnectionContext;
8 import com.tc.object.ObjectID;
9 import com.tc.objectserver.mgmt.ManagedObjectFacade;
10
11 import java.beans.PropertyChangeEvent JavaDoc;
12
13 public class DSOField extends DSOObject {
14   private String JavaDoc m_name;
15   private boolean m_isPrimitive;
16   private boolean m_isMap;
17   private boolean m_isList;
18   private boolean m_isSet;
19   private boolean m_isArray;
20   private String JavaDoc m_type;
21   private Object JavaDoc m_value;
22   private String JavaDoc[] m_fieldNames;
23   private DSOObject[] m_fields;
24   private String JavaDoc m_label;
25   private boolean m_isCycle;
26   private DSOObject m_cycleRoot;
27
28   public DSOField(
29     ConnectionContext cc,
30     String JavaDoc name,
31     boolean isPrimitive,
32     String JavaDoc type,
33     Object JavaDoc value,
34     DSOObject parent)
35   {
36     super(cc, parent);
37
38     m_name = name;
39     m_isPrimitive = isPrimitive;
40     m_type = type;
41     m_value = value;
42
43     initFields();
44     updateLabel();
45   }
46
47   public Object JavaDoc getFacade() {
48     return m_value instanceof ManagedObjectFacade ? (ManagedObjectFacade)m_value : null;
49   }
50   
51   protected void updateLabel() {
52     String JavaDoc prefix = m_name;
53
54     if(m_type != null) {
55       prefix += " (" + m_type + ")";
56     }
57     
58     m_label = prefix;
59
60     if(isPrimitive() || m_value == null || m_value instanceof String JavaDoc) {
61       m_label = prefix + "=" + m_value;
62     }
63     else if(isCollection()) {
64       ManagedObjectFacade mof = (ManagedObjectFacade)m_value;
65       m_label += " [" + mof.getFacadeSize() + "/" + mof.getTrueObjectSize() + "]";
66     }
67   }
68   
69   public void initFields() {
70     if(!m_isPrimitive) {
71       if(m_value != null && m_value instanceof ManagedObjectFacade) {
72         ManagedObjectFacade mof = (ManagedObjectFacade)m_value;
73
74         m_isMap = mof.isMap();
75         m_isList = mof.isList();
76         m_isSet = mof.isSet();
77         m_isArray = mof.isArray();
78         m_isCycle = isCycle(mof);
79       }
80
81       m_fields = new DSOObject[getFieldCount()];
82     }
83   }
84
85   public String JavaDoc getName() {
86     return m_name;
87   }
88
89   public boolean isPrimitive() {
90     return m_isPrimitive;
91   }
92
93   public boolean isArray() {
94     return m_isArray;
95   }
96
97   public boolean isCollection() {
98     return isMap() || isList() || isSet();
99   }
100   
101   public boolean isMap() {
102     return m_isMap;
103   }
104
105   public boolean isList() {
106     return m_isList;
107   }
108
109   public boolean isCycle() {
110     return m_isCycle;
111   }
112   
113   public DSOObject getCycleRoot() {
114     return m_cycleRoot;
115   }
116   
117   public boolean isSet() {
118     return m_isSet;
119   }
120
121   public String JavaDoc getType() {
122     return m_type;
123   }
124
125   public Object JavaDoc getValue() {
126     return m_value;
127   }
128
129   public String JavaDoc[] getFieldNames() {
130     if(m_fieldNames == null) {
131       if(m_value != null && m_value instanceof ManagedObjectFacade) {
132         ManagedObjectFacade facade = (ManagedObjectFacade)m_value;
133         m_fieldNames = RootsHelper.getHelper().trimFields(facade.getFields());
134       }
135       else {
136         m_fieldNames = new String JavaDoc[]{};
137       }
138     }
139     
140     return m_fieldNames;
141   }
142
143   public String JavaDoc getFieldName(int index) {
144     String JavaDoc[] names = getFieldNames();
145     return names != null ? names[index] : null;
146   }
147
148   public DSOObject getField(int index) {
149     DSOObject result = null;
150
151     if(m_fields == null) {
152       m_fields = new DSOObject[getFieldCount()];
153     }
154     
155     if(m_fields[index] == null) {
156       ManagedObjectFacade mof = (ManagedObjectFacade)getValue();
157       String JavaDoc field = getFieldName(index);
158
159       try {
160         result = createField(field, mof.getFieldValue(field));
161       } catch(Throwable JavaDoc t) {
162         //t.printStackTrace();
163
}
164     }
165
166     m_fields[index] = result;
167
168     return result;
169   }
170
171   public int getFieldCount() {
172     return m_isCycle ? 0 : getFieldNames().length;
173   }
174
175   public String JavaDoc toString() {
176     return m_label;
177   }
178
179   public boolean isValid() {
180     boolean result = true;
181
182     if(m_value != null && m_value instanceof ManagedObjectFacade) {
183       ManagedObjectFacade mof = (ManagedObjectFacade)m_value;
184       ObjectID id = mof.getObjectId();
185
186       try {
187         m_value = DSOHelper.getHelper().lookupFacade(m_cc, id, m_batchSize);
188         result = (m_value != null);
189       } catch(Exception JavaDoc e) {
190         result = false;
191       }
192     }
193
194     return result;
195   }
196   
197   protected void updateFacade() throws Exception JavaDoc {
198     if(m_value != null && m_value instanceof ManagedObjectFacade) {
199       ManagedObjectFacade mof = (ManagedObjectFacade)m_value;
200       ObjectID id = mof.getObjectId();
201
202       m_value = DSOHelper.getHelper().lookupFacade(m_cc, id, m_batchSize);
203     }
204   }
205   
206   public boolean isCycle(ManagedObjectFacade mof) {
207     ObjectID oid = mof.getObjectId();
208     DSOObject parent = getParent();
209     
210     while(parent != null) {
211       Object JavaDoc facade = parent.getFacade();
212         
213       if(facade instanceof ManagedObjectFacade) {
214         ObjectID parentOID = ((ManagedObjectFacade)facade).getObjectId();
215         
216         if(oid.equals(parentOID)) {
217           m_cycleRoot = parent;
218           return true;
219         }
220       }
221       parent = parent.getParent();
222     }
223     
224     return false;
225   }
226   
227   public void refresh() {
228     try {
229       updateFacade();
230       updateLabel();
231       m_fieldNames = null;
232       m_fields = null;
233       
234       m_changeHelper.firePropertyChange(
235         new PropertyChangeEvent JavaDoc(this, null, null, null));
236     } catch(Exception JavaDoc e) {
237       AdminClient.getContext().log(e);
238     }
239   }
240
241   public void accept(DSOObjectVisitor visitor) {
242     visitor.visitDSOField(this);
243   }
244 }
245
Popular Tags