KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > Lookup


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.io.*;
17 import java.util.*;
18 import javax.swing.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Base Class for MLookup, MLocator.
24  * as well as for MLocation, MAccount (only single value)
25  * Maintains selectable data as NamePairs in ArrayList
26  * The objects itself may be shared by the lookup implementation (ususally HashMap)
27  *
28  * @author Jorg Janke
29  * @version $Id: Lookup.java,v 1.11 2003/11/02 07:49:56 jjanke Exp $
30  */

31 public abstract class Lookup extends AbstractListModel
32     implements MutableComboBoxModel, Serializable
33 {
34     /**
35      * Lookup
36      */

37     public Lookup()
38     {
39     } // Lookup
40

41     /** The List */
42     protected volatile ArrayList p_data = new ArrayList();
43
44     /** The Selected Item */
45     private volatile Object JavaDoc m_selectedObject;
46
47     /** Temporary Data */
48     private Object JavaDoc[] m_tempData = null;
49
50     /** The Worker to load data async */
51     private Worker m_worker = null;
52
53     /** Logger */
54     protected Logger log = Logger.getCLogger(getClass());
55
56     /*************************************************************************/
57
58     /**
59      * Set the value of the selected item. The selected item may be null.
60      * <p>
61      * @param anObject The combo box value or null for no selection.
62      */

63     public void setSelectedItem(Object JavaDoc anObject)
64     {
65         if ((m_selectedObject != null && !m_selectedObject.equals( anObject ))
66             || m_selectedObject == null && anObject != null)
67         {
68             if (p_data.contains(anObject) || anObject == null)
69             {
70                 m_selectedObject = anObject;
71             // Log.trace(s_ll, "Lookup.setSelectedItem", anObject);
72
}
73             else
74             {
75                 m_selectedObject = null;
76                 log.debug("setSelectedItem - Set to NULL");
77             }
78         // if (m_worker == null || !m_worker.isAlive())
79
fireContentsChanged(this, -1, -1);
80         }
81     } // setSelectedItem
82

83     /**
84      * Return previously selected Item
85      * @return value
86      */

87     public Object JavaDoc getSelectedItem()
88     {
89         return m_selectedObject;
90     } // getSelectedItem
91

92     /**
93      * Get Size of Model
94      * @return size
95      */

96     public int getSize()
97     {
98         return p_data.size();
99     } // getSize
100

101     /**
102      * Get Element at Index
103      * @param index index
104      * @return value
105      */

106     public Object JavaDoc getElementAt (int index)
107     {
108         return p_data.get(index);
109     } // getElementAt
110

111     /**
112      * Returns the index-position of the specified object in the list.
113      *
114      * @param anObject object
115      * @return an int representing the index position, where 0 is
116      * the first position
117      */

118     public int getIndexOf (Object JavaDoc anObject)
119     {
120         return p_data.indexOf(anObject);
121     } // getIndexOf
122

123     /**
124      * Add Element at the end
125      * @param anObject object
126      */

127     public void addElement (Object JavaDoc anObject)
128     {
129         p_data.add(anObject);
130         fireIntervalAdded (this, p_data.size()-1, p_data.size()-1);
131         if (p_data.size() == 1 && m_selectedObject == null && anObject != null)
132             setSelectedItem (anObject);
133     } // addElement
134

135     /**
136      * Insert Element At
137      * @param anObject object
138      * @param index index
139      */

140     public void insertElementAt (Object JavaDoc anObject, int index)
141     {
142         p_data.add (index, anObject);
143         fireIntervalAdded (this, index, index);
144     } // insertElementAt
145

146     /**
147      * Remove Item at index
148      * @param index index
149      */

150     public void removeElementAt (int index)
151     {
152         if (getElementAt(index) == m_selectedObject)
153         {
154             if (index == 0)
155                 setSelectedItem (getSize() == 1 ? null : getElementAt( index + 1 ));
156             else
157                 setSelectedItem (getElementAt (index - 1));
158         }
159         p_data.remove(index);
160         fireIntervalRemoved (this, index, index);
161     } // removeElementAt
162

163     /**
164      * Remove Item
165      * @param anObject object
166      */

167     public void removeElement (Object JavaDoc anObject)
168     {
169         int index = p_data.indexOf (anObject);
170         if (index != -1)
171             removeElementAt(index);
172     } // removeItem
173

174     /**
175      * Empties the list.
176      */

177     public void removeAllElements()
178     {
179         if (p_data.size() > 0)
180         {
181             int firstIndex = 0;
182             int lastIndex = p_data.size() - 1;
183             p_data.clear();
184             m_selectedObject = null;
185             fireIntervalRemoved (this, firstIndex, lastIndex);
186         }
187     } // removeAllElements
188

189     /*************************************************************************/
190
191     /**
192      * Put Value
193      * @param key key
194      * @param value value
195      */

196     public void put (String JavaDoc key, String JavaDoc value)
197     {
198         NamePair pp = new ValueNamePair (key, value);
199         addElement(pp);
200     } // put
201

202     /**
203      * Put Value
204      * @param key key
205      * @param value value
206      */

207     public void put (int key, String JavaDoc value)
208     {
209         NamePair pp = new KeyNamePair (key, value);
210         addElement(pp);
211     } // put
212

213     /**
214      * Fill ComboBox with lookup data (async using Worker).
215      * - try to maintain selected item
216      * @param mandatory has mandatory data only (i.e. no "null" selection)
217      * @param onlyValidated only validated
218      * @param onlyActive onlt active
219      * @param temporary save current values - restore via fillComboBox (true)
220      */

221     public void fillComboBox (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary)
222     {
223         // Save current data
224
if (temporary)
225         {
226             int size = p_data.size();
227             m_tempData = new Object JavaDoc[size];
228             // We need to do a deep copy, so store it in Array
229
p_data.toArray(m_tempData);
230         // for (int i = 0; i < size; i++)
231
// m_tempData[i] = p_data.get(i);
232
}
233
234         // We have an active Worker, so ignore
235
if (m_worker != null && m_worker.isAlive())
236         {
237             log.warn ("fillComboBox - Worker already active - ignored");
238             return;
239         }
240         // Worker calls getData()
241
m_worker = new Worker (mandatory, onlyValidated, onlyActive, temporary);
242         m_worker.run(); // NOT async
243
// if (p_data.size() == 0)
244
// System.out.println(this);
245
} // fillComboBox
246

247     /**
248      * Fill ComboBox with old saved data (if exists) or all data available
249      * @param restore if true, use saved data - else fill it with all data
250      */

251     public void fillComboBox (boolean restore)
252     {
253         if (restore && m_tempData != null)
254         {
255             Object JavaDoc obj = m_selectedObject;
256             p_data.clear();
257             // restore old data
258
p_data = new ArrayList(m_tempData.length);
259             for (int i = 0; i < m_tempData.length; i++)
260                 p_data.add(m_tempData[i]);
261             m_tempData = null;
262
263             // if nothing selected, select first
264
if (obj == null && p_data.size() > 0)
265                 obj = p_data.get(0);
266             setSelectedItem(obj);
267             fireContentsChanged(this, 0, p_data.size());
268             return;
269         }
270         fillComboBox(false, false, false, false);
271     } // fillComboBox
272

273     /*************************************************************************/
274
275     /**
276      * Get Display of Key Value
277      * @param key key
278      * @return String
279      */

280     public abstract String JavaDoc getDisplay (Object JavaDoc key);
281
282     /**
283      * Get Object of Key Value
284      * @param key key
285      * @return Object or null
286      */

287     public abstract NamePair get (Object JavaDoc key);
288
289
290     /**
291      * Fill ComboBox with Data (Value/KeyNamePair)
292      * @param mandatory has mandatory data only (i.e. no "null" selection)
293      * @param onlyValidated only validated
294      * @param onlyActive only active
295      * @param temporary force load for temporary display
296      * @return ArrayList
297      */

298     public abstract ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary);
299
300     /**
301      * Get underlying fully qualified Table.Column Name.
302      * Used for VLookup.actionButton (Zoom)
303      * @return column name
304      */

305     public abstract String JavaDoc getColumnName();
306
307     /**
308      * The Lookup contains the key
309      * @param key key
310      * @return true if contains key
311      */

312     public abstract boolean containsKey (Object JavaDoc key);
313
314     /*************************************************************************/
315
316     /**
317      * Refresh Values - default implementation
318      * @return size
319      */

320     public int refresh()
321     {
322         return 0;
323     } // refresh
324

325     /**
326      * Is Validated - default implementation
327      * @return true if validated
328      */

329     public boolean isValidated()
330     {
331         return true;
332     } // isValidated
333

334     /**
335      * Get dynamic Validation SQL (none)
336      * @return validation
337      */

338     public String JavaDoc getValidation()
339     {
340         return "";
341     } // getValidation
342

343     /**
344      * Has Inactive records - default implementation
345      * @return true if inactive
346      */

347     public boolean hasInactive()
348     {
349         return false;
350     }
351
352     /**
353      * Get Zoom - default implementation
354      * @return Zoom Window
355      */

356     public int getZoom()
357     {
358         return 0;
359     } // getZoom
360

361     /**
362      * Get Zoom - default implementation
363      * @param isSOTrx SO
364      * @return Zoom Window
365      */

366     public int getZoom(String JavaDoc isSOTrx)
367     {
368         return 0;
369     } // getZoom
370

371     /**
372      * Get Zoom Query String - default implementation
373      * @return Zoom Query
374      */

375     public MQuery getZoomQuery()
376     {
377         return null;
378     } // getZoomQuery
379

380     /**
381      * Get Data Direct from Table.
382      * Default implementation - does not requery
383      * @param key key
384      * @param saveInCache save in cache for r/w
385      * @param cacheLocal cache locally for r/o
386      * @return value
387      */

388     public NamePair getDirect (Object JavaDoc key, boolean saveInCache, boolean cacheLocal)
389     {
390         return get (key);
391     } // getDirect
392

393     /**
394      * Dispose - clear items w/o firing events
395      */

396     public void dispose()
397     {
398         while (m_worker != null && m_worker.isAlive())
399             m_worker.interrupt();
400         m_worker = null;
401         //
402
if (p_data != null)
403             p_data.clear();
404         p_data = null;
405         m_selectedObject = null;
406         m_tempData = null;
407     } // dispose
408

409     /**
410      * Wait until async Load Complete
411      */

412     public void loadComplete()
413     {
414     } // loadComplete
415

416     /*************************************************************************/
417
418     /**
419      * Lookup Worker loads p_data when available
420      */

421     class Worker extends Thread JavaDoc
422     {
423         /**
424          * Constructor
425          * @param mandatory mandatory
426          * @param onlyValidated only validated
427          * @param onlyActive only active
428          * @param temporary force load for temporary display
429          */

430         Worker (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary)
431         {
432             l_mandatory = mandatory;
433             l_onlyValidated = onlyValidated;
434             l_onlyActive = onlyActive;
435             l_temporary = temporary;
436         } // Worker
437

438         private long m_startTime = System.currentTimeMillis();
439         private boolean l_mandatory, l_onlyValidated, l_onlyActive, l_temporary;
440
441         /**
442          * The Worker
443          */

444         public void run()
445         {
446             Object JavaDoc obj = m_selectedObject;
447             p_data.clear();
448             if (interrupted())
449                 return;
450
451             // may cause delay
452
p_data = getData (l_mandatory, l_onlyValidated, l_onlyActive, l_temporary);
453             if (interrupted())
454                 return;
455
456             // Selected Object changed
457
if (obj != m_selectedObject)
458             {
459                 if (Log.isTraceLevel(7))
460                     log.debug("Worker.run - SelectedValue Changed=" + obj + "->" + m_selectedObject);
461                 obj = m_selectedObject;
462             }
463
464             // if nothing selected & mandatory, select first
465
if (obj == null && l_mandatory && p_data.size() > 0)
466             {
467                 obj = p_data.get(0);
468                 m_selectedObject = obj;
469                 if (Log.isTraceLevel(7))
470                     log.debug("Worker.run - SelectedValue SetToFirst=" + obj);
471             // fireContentsChanged(this, -1, -1);
472
}
473             fireContentsChanged(this, 0, p_data.size());
474             log.debug("Worker.run - Count=" + p_data.size() + " ms=" + String.valueOf(System.currentTimeMillis()-m_startTime));
475         } // run
476
} // Worker
477

478 } // Lookup
479
Popular Tags