KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > editor > DjenericObjectEditorProxy


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.editor;
31
32 import java.util.ArrayList JavaDoc;
33
34 import javax.swing.DefaultComboBoxModel JavaDoc;
35 import javax.swing.JComboBox JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JTextField JavaDoc;
39
40 import com.genimen.djeneric.language.Messages;
41 import com.genimen.djeneric.repository.DjDomainValue;
42 import com.genimen.djeneric.repository.DjObject;
43 import com.genimen.djeneric.repository.DjProperty;
44 import com.genimen.djeneric.repository.DjSession;
45 import com.genimen.djeneric.repository.exceptions.DjenericException;
46 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
47 import com.genimen.djeneric.tools.specifier.components.DjBindable;
48 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
49
50 public class DjenericObjectEditorProxy
51 {
52   private ObjectViewer _viewer;
53
54   private ArrayList JavaDoc _allBinders = new ArrayList JavaDoc();
55
56   public DjenericObjectEditorProxy(ObjectViewer forViewer)
57   {
58     _viewer = forViewer;
59   }
60
61   public void setEnabled(boolean b)
62   {
63     for (int i = 0; i < _allBinders.size(); i++)
64     {
65       DjBindable pb = (DjBindable) _allBinders.get(i);
66       JComponent JavaDoc comp = pb.getFocussableComponent();
67       if (comp instanceof JTextField JavaDoc)
68       {
69         JTextField JavaDoc fld = (JTextField JavaDoc) comp;
70         fld.setEditable(b);
71       }
72       else
73       {
74         comp.setEnabled(b && pb.isComponentWritable());
75       }
76     }
77
78   }
79
80   public boolean isEnabled()
81   {
82     for (int i = 0; i < _allBinders.size(); i++)
83     {
84       DjBindable pb = (DjBindable) _allBinders.get(i);
85       if (pb.getFocussableComponent().isEnabled())
86       {
87         return true;
88       }
89     }
90     return false;
91   }
92
93   public void synchronize()
94   {
95     for (int i = 0; i < _allBinders.size(); i++)
96     {
97       DjBindable pb = (DjBindable) _allBinders.get(i);
98       try
99       {
100         pb.synchronize();
101       }
102       catch (DjenericException x)
103       {
104         _viewer.setStatusMessage(x);
105       }
106     }
107   }
108
109   public void clear()
110   {
111     for (int i = 0; i < _allBinders.size(); i++)
112     {
113       DjBindable pb = (DjBindable) _allBinders.get(i);
114       try
115       {
116         pb.clear();
117       }
118       catch (DjenericException x)
119       {
120         _viewer.setStatusMessage(x);
121       }
122     }
123     synchronize();
124
125   }
126
127   public void applyEditorsToProxy() throws DjenericException
128   {
129     for (int i = 0; i < _allBinders.size(); i++)
130     {
131       DjBindable pb = (DjBindable) _allBinders.get(i);
132       pb.apply();
133     }
134     _viewer.setStatusMessage(Messages.getString("global.Ok"));
135   }
136
137   protected ArrayList JavaDoc getBinders()
138   {
139     return _allBinders;
140   }
141
142   public void unbindEditor(DjBindable comp)
143   {
144     getBinders().remove(comp);
145   }
146
147   public void unbindEditors()
148   {
149     getBinders().clear();
150   }
151
152   public void updateLookupModel(DjSession session, String JavaDoc propertyName) throws DjenericException
153   {
154     for (int i = 0; i < _allBinders.size(); i++)
155     {
156       DjBindable ppb = (DjBindable) (getBinders().get(i));
157
158       if (ppb.getPropertyName().equals(propertyName)) updateModelForBinder(session, ppb);
159     }
160   }
161
162   public void updateAllLookupModels(DjSession session) throws DjenericException
163   {
164     if (session == null) return;
165     for (int i = 0; i < _allBinders.size(); i++)
166     {
167       DjBindable ppb = (DjBindable) (getBinders().get(i));
168       updateModelForBinder(session, ppb);
169     }
170   }
171
172   public void updateAllRestrictedModels(DjSession session) throws DjenericException
173   {
174     for (int i = 0; i < _allBinders.size(); i++)
175     {
176       DjBindable ppb = (DjBindable) (getBinders().get(i));
177       DjProperty prop = _viewer.getExtent().getProperty(ppb.getPropertyName());
178       if (prop.isPartOfRestrictedPath()) updateModelForBinder(session, ppb);
179     }
180   }
181
182   protected void updateModelForBinder(DjSession session, DjBindable ppb) throws DjenericException,
183       ObjectNotDefinedException
184   {
185     DjDomainValue[] validValues = null;
186     JComponent JavaDoc comp = ppb.getFocussableComponent();
187
188     if (comp instanceof JComboBox JavaDoc || comp instanceof JList JavaDoc)
189     {
190       validValues = getValidValuesForProperty(session, ppb.getPropertyName());
191     }
192
193     if (validValues != null)
194     {
195       if (comp instanceof JComboBox JavaDoc)
196       {
197         JComboBox JavaDoc cbb = (JComboBox JavaDoc) comp;
198         if (cbb.isEditable())
199         {
200           Object JavaDoc obj = cbb.getEditor().getItem();
201           cbb.setModel(new DefaultComboBoxModel JavaDoc(validValues));
202           cbb.setSelectedIndex(-1);
203           cbb.getEditor().setItem(obj);
204         }
205         else
206         {
207           Object JavaDoc obj = cbb.getSelectedItem();
208           cbb.setModel(new DefaultComboBoxModel JavaDoc(validValues));
209           if (obj != null)
210           {
211             cbb.setSelectedIndex(-1);
212             cbb.setSelectedItem(obj);
213           }
214           else
215           {
216             cbb.setSelectedIndex(-1);
217           }
218         }
219       }
220     }
221   }
222
223   public void bindEditor(DjSession session, DjBindable comp, String JavaDoc propertyName)
224   {
225     // Make sure we don't bind the component more than once
226
unbindEditor(comp);
227     getBinders().add(comp);
228   }
229
230   private DjDomainValue[] getValidValuesForProperty(DjSession session, String JavaDoc propertyName) throws DjenericException,
231       ObjectNotDefinedException
232   {
233     DjDomainValue[] validValues;
234     DjObject obj = _viewer.getSelectedValue();
235     if (obj != null)
236     {
237       validValues = obj.getValidValues(propertyName);
238     }
239     else
240     {
241       DjProperty prop = _viewer.getExtent().getProperty(propertyName);
242       validValues = prop.getValidValues(session);
243     }
244     return validValues;
245   }
246
247   public void requestFocus()
248   {
249     ArrayList JavaDoc binders = getBinders();
250     if (binders.size() > 0)
251     {
252       DjBindable ppb = (DjBindable) (getBinders().get(0));
253       ppb.getFocussableComponent().requestFocus();
254     }
255   }
256
257   public void requestFocus(String JavaDoc propertyName)
258   {
259     ArrayList JavaDoc binders = getBinders();
260     for (int i = 0; i < binders.size(); i++)
261     {
262       DjBindable ppb = (DjBindable) (getBinders().get(i));
263       if (ppb.getPropertyName().equals(propertyName))
264       {
265         ppb.getFocussableComponent().requestFocus();
266         break;
267       }
268     }
269
270   }
271
272 }
Popular Tags