⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtable.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			m_loader.close();
		m_open = true;
		//
		m_changed = false;
		m_rowChanged = -1;
		return true;
	}	//	open

	/**
	 *  Wait until async loader of Table and Lookup Fields is complete
	 *  Used for performance tests
	 */
	public void loadComplete()
	{
		//  Wait for loader
		if (m_loader != null)
		{
			if (m_loader.isAlive())
			{
				try
				{
					m_loader.join();
				}
				catch (InterruptedException ie)
				{
					Log.error("MTable.loadComplete - join interrupted", ie);
				}
			}
		}
		//  wait for field lookup loaders
		for (int i = 0; i < m_fields.size(); i++)
		{
			MField field = (MField)m_fields.get(i);
			field.lookupLoadComplete();
		}
	}   //  loadComplete

	/**
	 *  Is Loading
	 *  @return true if loading
	 */
	public boolean isLoading()
	{
		if (m_loader != null && m_loader.isAlive())
			return true;
		return false;
	}   //  isLoading

	/**
	 *	Is it open?
	 *  @return true if opened
	 */
	public boolean isOpen()
	{
		return m_open;
	}	//	isOpen

	/**
	 *	Close Resultset
	 *  @param finalCall final call
	 */
	public void close (boolean finalCall)
	{
		if (!m_open)
			return;
		Log.trace(Log.l4_Data, "MTable.close - final=" + finalCall);

		//  remove listeners
		if (finalCall)
		{
			m_dataStatusListeners.clear();
			EventListener evl[] = m_tableModelListeners.getListeners(TableModelListener.class);
			for (int i = 0; i < evl.length; i++)
				m_tableModelListeners.remove(TableModelListener.class, evl[i]);
			VetoableChangeListener vcl[] = m_vetoableChangeSupport.getVetoableChangeListeners();
			for (int i = 0; i < vcl.length; i++)
				m_vetoableChangeSupport.removeVetoableChangeListener(vcl[i]);
		}

		//	Stop loader
		while (m_loader != null && m_loader.isAlive())
		{
			Log.trace(Log.l5_DData, "Interrupting Loader");
			m_loader.interrupt();
			try
			{
				Thread.currentThread().sleep(200);		//	.2 second
			}
			catch (InterruptedException ie)
			{}
		}

		if (!m_inserting)
			dataSave(true);

		if (m_buffer != null)
			m_buffer.clear();
		m_buffer = null;
		if (m_sort != null)
			m_sort.clear();
		m_sort = null;

		if (finalCall)
			dispose();

		//  Fields are disposed from MTab
		Log.trace(Log.l4_Data, "MTable.close - complete");
		m_open = false;
	}	//	close

	/**
	 *  Dispose MTable.
	 *  Called by close-final
	 */
	private void dispose()
	{
		//  MFields
		for (int i = 0; i < m_fields.size(); i++)
			((MField)m_fields.get(i)).dispose();
		m_fields.clear();
		m_fields = null;
		//
		m_dataStatusListeners = null;
		m_tableModelListeners = null;
		m_vetoableChangeSupport = null;
		//
		m_parameterSELECT.clear();
		m_parameterSELECT = null;
		m_parameterWHERE.clear();
		m_parameterWHERE = null;
		//  clear data arrays
		m_buffer = null;
		m_sort = null;
		m_rowData = null;
		m_oldValue = null;
		m_loader = null;
	}   //  dispose

	/**
	 *	Get total database column count (displayed and not displayed)
	 *  @return column count
	 */
	public int getColumnCount()
	{
		return m_fields.size();
	}	//	getColumnCount

	/**
	 *	Get (displayed) field count
	 *  @return field count
	 */
	public int getFieldCount()
	{
		return m_fields.size();
	}	//	getFieldCount

	/**
	 *  Return number of rows
	 *  @return Number of rows or 0 if not opened
	 */
	public int getRowCount()
	{
		return m_rowCount;
	}	//	getRowCount

	/**
	 *	Set the Column to determine the color of the row
	 *  @param columnName column name
	 */
	public void setColorColumn (String columnName)
	{
		m_colorColumnIndex = findColumn(columnName);
	}	//  setColorColumn

	/**
	 *	Get ColorCode for Row.
	 *  <pre>
	 *	If numerical value in compare column is
	 *		negative = -1,
	 *      positive = 1,
	 *      otherwise = 0
	 *  </pre>
	 *  @see #setColorColumn
	 *  @param row row
	 *  @return color code
	 */
	public int getColorCode (int row)
	{
		if (m_colorColumnIndex  == -1)
			return 0;
		Object data = getValueAt(row, m_colorColumnIndex);
		//	We need to have a Number
		if (data == null || !(data instanceof BigDecimal))
			return 0;
		int cmp = Env.ZERO.compareTo(data);
		if (cmp > 0)
			return -1;
		if (cmp < 0)
			return 1;
		return 0;
	}	//	getColorCode


	/**
	 *	Sort Entries by Column.
	 *  <p>
	 *  actually the rows are not sorted, just the access pointer ArrayList
	 *  with the same size as m_buffer with MSort entities
	 *  @param col col
	 *  @param ascending ascending
	 */
	public void sort (int col, boolean ascending)
	{
		Log.trace(Log.l4_Data, "MTable.sort #" + col + " " + ascending);
		if (getRowCount() == 0)
			return;
		MField field = getField (col);
		//	RowIDs are not sorted
		if (field.getDisplayType() == DisplayType.RowID)
			return;
		boolean isLookup = DisplayType.isLookup(field.getDisplayType());

		//	fill MSort entities with data entity
		for (int i = 0; i < m_sort.size(); i++)
		{
			MSort sort = (MSort)m_sort.get(i);
			Object[] rowData = (Object[])m_buffer.get(sort.index);
			if (isLookup)
				sort.data = field.getLookup().getDisplay(rowData[col]);	//	lookup
			else
				sort.data = rowData[col];								//	data
		}

		//	sort it
		MSort sort = new MSort(0, null);
		sort.setSortAsc(ascending);
		Collections.sort(m_sort, sort);
		//	update UI
		fireTableDataChanged();
		//  Info detected by MTab.dataStatusChanged and current row set to 0
		fireDataStatusIEvent("Sorted");
	}	//	sort

	/**
	 *	Get Key ID or -1 of none
	 *  @param row row
	 *  @return ID or -1
	 */
	public int getKeyID (int row)
	{
	//	Log.info("MTable.getKeyID - row=" + row + ", keyColIdx=" + m_keyColumnIndex);
		if (m_keyColumnIndex != -1)
		{
			try
			{
				Integer ii = (Integer)getValueAt(row, m_keyColumnIndex);
				if (ii == null)
					return -1;
				return ii.intValue();
			}
			catch (Exception e)     //  Alpha Key
			{
				return -1;
			}
		}
		return -1;
	}	//	getKeyID

	/**
	 *	Get Key ColumnName
	 *  @return key column name
	 */
	public String getKeyColumnName()
	{
		if (m_keyColumnIndex != -1)
			return getColumnName(m_keyColumnIndex);
		return "";
	}	//	getKeyColumnName

	/**
	 *	Get Selected ROWID or null, if no RowID exists
	 *  @param row row
	 *  @return ROWID
	 */
	public Object getRowID (int row)
	{
		Object[] rid = getRID(row);
		if (rid == null)
			return null;
		return rid[0];
	}	//	getSelectedRowID

	/**
	 *	Get RowID Structure [0]=RowID, [1]=Selected, [2]=ID.
	 *  <p>
	 *  Either RowID or ID is populated (views don't have RowID)
	 *  @param row row
	 *  @return RowID
	 */
	public Object[] getRID (int row)
	{
		if (m_rowIdColumnIndex == -1 || row < 0 || row >= getRowCount())
			return null;
		return (Object[])getValueAt(row, m_rowIdColumnIndex);
	}	//	getRID

	/**
	 *	Find Row with RowID
	 *  @param RowID row id or oid
	 *	@return number of row or 0 if not found
	 */
	public int getRow (Object RowID)
	{
		if (RowID == null)
			return 0;

		//	the value to find
		String find = RowID.toString();

		//	Wait a bit to load rows
		if (m_loader != null && m_loader.isAlive())
		{
			try
			{
				Thread.currentThread().sleep(250);		//	1/4 second
			}
			catch (InterruptedException ie)
			{}
		}

		//	Build search vector
		int size = m_sort.size();		//	may still change
		ArrayList search = new ArrayList(size);
		for (int i = 0; i < size; i++)
		{
			Object[] r = (Object[])getValueAt(i, 0);
			String s = r[0].toString();
			MSort so = new MSort(i, s);
			search.add(so);
		}

		//	Sort it
		MSort sort = new MSort(0, null);
		Collections.sort(search, sort);

		//	Find it
		int index = Collections.binarySearch(search, find, sort);
		if (index < 0)	//	not found
		{
			search.clear();
			return 0;
		}
		//	Get Info
		MSort result = (MSort)search.get(index);
		//	Clean up
		search.clear();
		return result.index;
	}	//	getRow


	/*************************************************************************/

	/**
	 * 	Get Value in Resultset
	 *  @param row row
	 *  @param col col
	 *  @return Object of that row/column
	 */
	public Object getValueAt (int row, int col)
	{
	//	Log.trace(Log.l4_Data, "MTable.getValueAt r=" + row + " c=" + col);
		if (!m_open || row < 0 || col < 0 || row >= m_rowCount)
		{
		//	Log.trace(Log.l5_DData, "Out of bounds - Open=" + m_open + ", RowCount=" + m_rowCount);
			return null;
		}

		//	need to wait for data read into buffer
		int loops = 0;
		while (row >= m_buffer.size() && m_loader.isAlive() && loops < 15)
		{
			Log.trace(Log.l5_DData, "MTable.getValueAt - waiting for loader row=" + row + ", size=" + m_buffer.size());
			try
			{
				Thread.currentThread().sleep(500);		//	1/2 second
			}
			catch (InterruptedException ie)
			{}
			loops++;
		}

		//	empty buffer
		if (row >= m_buffer.size())
		{
		//	Log.trace(Log.l5_DData, "Empty buffer");
			return null;
		}

		//	return Data item
		MSort sort = (MSort)m_sort.get(row);
		Object[] rowData = (Object[])m_buffer.get(sort.index);
		//	out of bounds
		if (rowData == null || col > rowData.length)
		{
		//	Log.trace(Log.l5_DData, "No data or Column out of bounds");
			return null;
		}
		return rowData[col];
	}	//	getValueAt

	/**
	 *	Indicate that there will be a change
	 *  @param changed changed
	 */
	public void setChanged (boolean changed)
	{
		//	Can we edit?
		if (!m_open || m_readOnly)
			return;

		//	Indicate Change
		m_changed = changed;
		if (!changed)
			m_rowChanged = -1;
		fireDataStatusIEvent("");
	}	//	setChanged

	/**
	 * 	Set Value in data and update MField.
	 *  (called directly or from JTable.editingStopped())
	 *
	 *  @param  value value to assign to cell
	 *  @param  row row index of cell
	 *  @param  col column index of cell
	 */
	public final void setValueAt (Object value, int row, int col)
	{
		setValueAt (value, row, col, false);
	}	//	setValueAt

	/**
	 * 	Set Value in data and update MField.
	 *  (called directly or from JTable.editingStopped())
	 *
	 *  @param  value value to assign to cell
	 *  @param  row row index of cell
	 *  @param  col column index of cell
	 * 	@param	force force setting new value
	 */
	public final void setValueAt (Object value, int row, int col, boolean force)
	{
		//	Can we edit?
		if (!m_open || m_readOnly       //  not accessible
				|| row < 0 || col < 0   //  invalid index
				|| col == 0             //  cannot change ID
				|| m_rowCount == 0)     //  no rows
			return;

		dataSave(row, false);

		//	Has anything changed?
		Object oldValue = getValueAt(row, col);
		if (!force && (
			(oldValue == null && value == null)
			||	(oldValue != null && oldValue.equals(value))
			||	(oldValue != null && value != null && oldValue.toString().equals(value.toString()))
			))
			return;

		Log.trace(Log.l4_Data, "MTable.setValueAt r=" + row + " c=" + col
			+ " = " + oldValue + " => " + value);
		//  Save old value
		m_oldValue = new Object[3];
		m_oldValue[0] = new Integer(row);
		m_oldValue[1] = new Integer(col);
		m_oldValue[2] = oldValue;

		//	Set Data item
		MSort sort = (MSort)m_sort.get(row);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -