📄 vdate.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-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.grid.ed;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.text.*;
import java.sql.*;
import java.beans.*;
import org.compiere.util.*;
import org.compiere.apps.*;
import org.compiere.plaf.*;
import org.compiere.swing.*;
import java.util.*;
/**
* Date Edit.
* Maintains data as Timestamp
*
* @author Jorg Janke
* @version $Id: VDate.java,v 1.28 2003/04/29 09:20:59 jpedersen Exp $
*/
public class VDate extends JComponent
implements VEditor, ActionListener, KeyListener, FocusListener
{
/**
* IDE Bean Constructor
*/
public VDate()
{
this (DisplayType.Date);
} // VDate
/**
* Simple Constructor
* @param displayType display Type
*/
public VDate (int displayType)
{
this("Date", false, false, true, displayType, "Date");
} // VDate
/**
* Create right aligned Date field
* @param columnName column name
* @param mandatory mandatory
* @param isReadOnly read only
* @param isUpdateable updateable
* @param displayType display type
* @param title title
*/
public VDate(String columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
int displayType, String title)
{
super();
m_columnName = columnName;
m_title = title;
//
LookAndFeel.installBorder(this, "TextField.border");
this.setLayout(new BorderLayout());
// Size
this.setPreferredSize(m_text.getPreferredSize());
int height = m_text.getPreferredSize().height;
// *** Text ***
m_text.setBorder(null);
m_text.setHorizontalAlignment(JTextField.TRAILING);
if (m_displayType == DisplayType.Date)
{
this.addFocusListener(this);
m_text.addFocusListener(this);
m_text.addKeyListener(this);
m_text.setCaret(new VOvrCaret());
m_text.setInputVerifier(new CInputVerifier()); //Must be added after FocusListener in order to work
}
else if (m_displayType == DisplayType.DateTime)
m_text.setColumns(20);
// Background
setMandatory(mandatory);
this.add(m_text, BorderLayout.CENTER);
//
if (displayType == DisplayType.DateTime || displayType == DisplayType.Time)
m_displayType = displayType; // default = Date
setFormat();
// *** Button ***
m_button.setIcon(Env.getImageIcon("Calendar10.gif"));
m_button.setMargin(new Insets(0, 0, 0, 0));
m_button.setPreferredSize(new Dimension(height, height));
m_button.addActionListener(this);
m_button.setFocusable(false);
this.add(m_button, BorderLayout.EAST);
// Prefereed Size
this.setPreferredSize(this.getPreferredSize()); // causes r/o to be the same length
// ReadWrite
if (isReadOnly || !isUpdateable)
setReadWrite(false);
else
setReadWrite(true);
} // VDate
/**
* Dispose
*/
public void dispose()
{
m_text = null;
m_button = null;
} // dispose
/**
* Set Document
* @param doc doc
*/
protected void setDocument(Document doc)
{
m_text.setDocument(doc);
} // getDocument
private String m_columnName;
protected int m_displayType = DisplayType.Date;
private String m_title;
private String m_oldText;
private boolean m_firstChange;
//
private SimpleDateFormat m_format;
//
private boolean m_readWrite;
private boolean m_mandatory;
/** The Text Field */
private CTextField m_text = new CTextField (12);
/** The Button */
private CButton m_button = new CButton();
/**
* Set ReadWrite - field is always r/o for Time or DateTime
* @param value value
*/
public void setReadWrite (boolean value)
{
m_readWrite = value;
this.setFocusable(value);
// editor
if (m_displayType == DisplayType.Date)
m_text.setReadWrite(value); // sets Background
else
{
m_text.setEditable(false);
m_text.setFocusable(false);
setBackground(false);
}
// Don't show button if not ReadWrite
if (m_button.isVisible() != value)
m_button.setVisible(value);
if (m_button.isEnabled() != value)
m_button.setEnabled(value);
} // setReadWrite
/**
* IsReadWrite
* @return true if rw
*/
public boolean isReadWrite()
{
return m_readWrite;
} // isReadWrite
/**
* Set Mandatory (and back bolor)
* @param mandatory mandatory
*/
public void setMandatory (boolean mandatory)
{
m_mandatory = mandatory;
m_text.setMandatory(mandatory);
setBackground(false);
} // setMandatory
/**
* Is it mandatory
* @return true if mandatory
*/
public boolean isMandatory()
{
return m_mandatory;
} // isMandatory
/**
* Set Background based on ReadWrite / mandatory / error
* @param error if true, set background to error color, otherwise mandatory/ReadWrite
*/
public void setBackground (boolean error)
{
if (error)
m_text.setBackground(CompierePLAF.getFieldBackground_Error());
else if (!m_readWrite)
m_text.setBackground(CompierePLAF.getFieldBackground_Inactive());
else if (m_mandatory)
m_text.setBackground(CompierePLAF.getFieldBackground_Mandatory());
else
m_text.setBackground(CompierePLAF.getFieldBackground_Normal());
} // setBackground
/**
* Set Foreground
* @param fg color
*/
public void setForeground(Color fg)
{
m_text.setForeground(fg);
} // setForeground
/**
* Set Format
* Required when Format/Locale changed
*/
public void setFormat()
{
m_format = DisplayType.getDateFormat(m_displayType);
if (m_displayType == DisplayType.Date)
m_text.setDocument(new MDocDate(m_displayType, m_format, m_text, m_title));
} // setFormat
/**
* Set Editor to value
* @param value timestamp/date or String to be parsed
*/
public void setValue (Object value)
{
// Log.trace(Log.l5_DData, "VDate.setValue", value);
m_oldText = "";
if (value == null)
;
else if (value instanceof java.util.Date)
m_oldText = m_format.format(value);
else
{
String strValue = value.toString();
// String values - most likely in YYYY-MM-DD (JDBC format)
try
{
java.util.Date date = DisplayType.getDateFormat_JDBC().parse (strValue);
m_oldText = m_format.format(date); // convert to display value
}
catch (ParseException pe0)
{
// Try local string format
try
{
java.util.Date date = m_format.parse(strValue);
m_oldText = m_format.format(date);
}
catch (ParseException pe1)
{
Log.error("VDate.setValue - " + pe1.getMessage());
m_oldText = "";
}
}
}
m_text.setText(m_oldText);
m_firstChange = true;
} // setValue
/**
* Property Change Listener
* @param evt event
*/
public void propertyChange (PropertyChangeEvent evt)
{
if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
setValue(evt.getNewValue());
} // propertyChange
/**
* Return Editor value
* @return value
*/
public Timestamp getTimestamp()
{
if (m_text == null)
return null;
String value = m_text.getText();
if (value == null || value.length() == 0)
return null;
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -