📄 lookup.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.io.*;
import java.util.*;
import javax.swing.*;
import org.compiere.util.*;
/**
* Base Class for MLookup, MLocator.
* as well as for MLocation, MAccount (only single value)
* Maintains selectable data as NamePairs in ArrayList
* The objects itself may be shared by the lookup implementation (ususally HashMap)
*
* @author Jorg Janke
* @version $Id: Lookup.java,v 1.3 2003/02/23 19:44:05 jjanke Exp $
*/
public abstract class Lookup extends AbstractListModel
implements MutableComboBoxModel, Serializable
{
/**
* Lookup
*/
public Lookup()
{
} // Lookup
/** The List */
protected volatile ArrayList p_data = new ArrayList();
/** The Selected Item */
private volatile Object m_selectedObject;
/** Temporary Data */
private Object[] m_tempData = null;
/** The Worker to load data async */
private Worker m_worker = null;
/** Log Level */
private static final int s_ll = 8;
/*************************************************************************/
/**
* Set the value of the selected item. The selected item may be null.
* <p>
* @param anObject The combo box value or null for no selection.
*/
public void setSelectedItem(Object anObject)
{
if ((m_selectedObject != null && !m_selectedObject.equals( anObject ))
|| m_selectedObject == null && anObject != null)
{
if (p_data.contains(anObject) || anObject == null)
{
m_selectedObject = anObject;
// Log.trace(s_ll, "Lookup.setSelectedItem", anObject);
}
else
{
m_selectedObject = null;
Log.trace(s_ll, "Lookup.setSelectedItem - Set to NULL");
}
// if (m_worker == null || !m_worker.isAlive())
fireContentsChanged(this, -1, -1);
}
} // setSelectedItem
/**
* Return previously selected Item
* @return value
*/
public Object getSelectedItem()
{
return m_selectedObject;
} // getSelectedItem
/**
* Get Size of Model
* @return size
*/
public int getSize()
{
return p_data.size();
} // getSize
/**
* Get Element at Index
* @param index index
* @return value
*/
public Object getElementAt (int index)
{
return p_data.get(index);
} // getElementAt
/**
* Returns the index-position of the specified object in the list.
*
* @param anObject object
* @return an int representing the index position, where 0 is
* the first position
*/
public int getIndexOf (Object anObject)
{
return p_data.indexOf(anObject);
} // getIndexOf
/**
* Add Element at the end
* @param anObject object
*/
public void addElement (Object anObject)
{
p_data.add(anObject);
fireIntervalAdded (this, p_data.size()-1, p_data.size()-1);
if (p_data.size() == 1 && m_selectedObject == null && anObject != null)
setSelectedItem (anObject);
} // addElement
/**
* Insert Element At
* @param anObject object
* @param index index
*/
public void insertElementAt (Object anObject, int index)
{
p_data.add (index, anObject);
fireIntervalAdded (this, index, index);
} // insertElementAt
/**
* Remove Item at index
* @param index index
*/
public void removeElementAt (int index)
{
if (getElementAt(index) == m_selectedObject)
{
if (index == 0)
setSelectedItem (getSize() == 1 ? null : getElementAt( index + 1 ));
else
setSelectedItem (getElementAt (index - 1));
}
p_data.remove(index);
fireIntervalRemoved (this, index, index);
} // removeElementAt
/**
* Remove Item
* @param anObject object
*/
public void removeElement (Object anObject)
{
int index = p_data.indexOf (anObject);
if (index != -1)
removeElementAt(index);
} // removeItem
/**
* Empties the list.
*/
public void removeAllElements()
{
if (p_data.size() > 0)
{
int firstIndex = 0;
int lastIndex = p_data.size() - 1;
p_data.clear();
m_selectedObject = null;
fireIntervalRemoved (this, firstIndex, lastIndex);
}
} // removeAllElements
/*************************************************************************/
/**
* Put Value
* @param key key
* @param value value
*/
public void put (String key, String value)
{
NamePair pp = new ValueNamePair (key, value);
addElement(pp);
} // put
/**
* Put Value
* @param key key
* @param value value
*/
public void put (int key, String value)
{
NamePair pp = new KeyNamePair (key, value);
addElement(pp);
} // put
/**
* Fill ComboBox with lookup data (async using Worker).
* - try to maintain selected item
* @param mandatory has mandatory data only (i.e. no "null" selection)
* @param onlyValidated only validated
* @param onlyActive onlt active
* @param temporary save current values - restore via fillComboBox (true)
*/
public void fillComboBox (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary)
{
// Save current data
if (temporary)
{
int size = p_data.size();
m_tempData = new Object[size];
// We need to do a deep copy, so store it in Array
p_data.toArray(m_tempData);
// for (int i = 0; i < size; i++)
// m_tempData[i] = p_data.get(i);
}
// We have an active Worker, so ignore
if (m_worker != null && m_worker.isAlive())
{
Log.trace(Log.l1_User, "Lookup.fillComboBox", "Worker already active - ignored");
return;
}
//
m_worker = new Worker (mandatory, onlyValidated, onlyActive);
m_worker.run(); // NOT async
} // fillComboBox
/**
* Fill ComboBox with old saved data (if exists) or all data available
* @param restore if true, use saved data - else fill it with all data
*/
public void fillComboBox (boolean restore)
{
if (restore && m_tempData != null)
{
Object obj = m_selectedObject;
p_data.clear();
// restore old data
p_data = new ArrayList(m_tempData.length);
for (int i = 0; i < m_tempData.length; i++)
p_data.add(m_tempData[i]);
m_tempData = null;
// if nothing selected, select first
if (obj == null && p_data.size() > 0)
obj = p_data.get(0);
setSelectedItem(obj);
fireContentsChanged(this, 0, p_data.size());
return;
}
fillComboBox(false, false, false, false);
} // fillComboBox
/*************************************************************************/
/**
* Get Display of Key Value
* @param key key
* @return String
*/
public abstract String getDisplay (Object key);
/**
* Get Object of Key Value
* @param key key
* @return Object or null
*/
public abstract NamePair get (Object key);
/**
* Fill ComboBox with Data (Value/KeyNamePair)
* @param mandatory has mandatory data only (i.e. no "null" selection)
* @param onlyValidated only validated
* @param onlyActive only active
* @return ArrayList
*/
public abstract ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive);
/**
* Get underlying fully qualified Table.Column Name
* @return column name
*/
public abstract String getColumnName();
/**
* The Lookup contains the key
* @param key key
* @return true if contains key
*/
public abstract boolean containsKey (Object key);
/*************************************************************************/
/**
* Refresh Values - default implementation
* @return size
*/
public int refresh()
{
return 0;
} // refresh
/**
* Is Validated - default implementation
* @return true if validated
*/
public boolean isValidated()
{
return true;
} // isValidated
/**
* Get dynamic Validation SQL (none)
* @return validation
*/
public String getValidation()
{
return "";
} // getValidation
/**
* Has Inactive records - default implementation
* @return true if inactive
*/
public boolean hasInactive()
{
return false;
}
/**
* Get Zoom - default implementation
* @return Zoom Window
*/
public int getZoom()
{
return 0;
} // getZoom
/**
* Get Zoom Query String - default implementation
* @return Zoom Query
*/
public MQuery getZoomQuery()
{
return null;
} // getZoomQuery
/**
* Get Data Direct from Table - default implementation
* @param key key
* @param saveInCache save in cache
* @return value
*/
public NamePair getDirect (Object key, boolean saveInCache)
{
return get (key);
} // getDirect
/**
* Dispose - clear items w/o firing events
*/
protected void dispose()
{
while (m_worker != null && m_worker.isAlive())
m_worker.interrupt();
m_worker = null;
//
p_data.clear();
p_data = null;
m_selectedObject = null;
m_tempData = null;
} // dispose
/**
* Wait until async Load Complete
*/
public void loadComplete()
{
} // loadComplete
/*************************************************************************/
/**
* Lookup Worker loads p_data when available
*/
class Worker extends Thread
{
/**
* Constructor
* @param mandatory mandatory
* @param onlyValidated only validated
* @param onlyActive only active
*/
Worker (boolean mandatory, boolean onlyValidated, boolean onlyActive)
{
l_mandatory = mandatory;
l_onlyValidated = onlyValidated;
l_onlyActive = onlyActive;
} // Worker
private long m_startTime = System.currentTimeMillis();
private boolean l_mandatory, l_onlyValidated, l_onlyActive;
/**
* The Worker
*/
public void run()
{
Object obj = m_selectedObject;
p_data.clear();
if (this.interrupted())
return;
// may cause delay
p_data = getData (l_mandatory, l_onlyValidated, l_onlyActive);
if (this.interrupted())
return;
// Selected Object changed
if (obj != m_selectedObject)
{
Log.trace(s_ll, "Lookup.run", "SelectedValue Changed=" + obj + "->" + m_selectedObject);
obj = m_selectedObject;
}
// if nothing selected & mandatory, select first
if (obj == null && l_mandatory && p_data.size() > 0)
{
obj = p_data.get(0);
m_selectedObject = obj;
Log.trace(s_ll, "Lookup.run", "SelectedValue SetToFirst=" + obj);
// fireContentsChanged(this, -1, -1);
}
fireContentsChanged(this, 0, p_data.size());
Log.trace(Log.l6_Database, "Lookup.run", toString() + " ms=" + String.valueOf(System.currentTimeMillis()-m_startTime));
} // run
} // Worker
} // Lookup
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -