📄 vnumber.java
字号:
{
if (m_text == null || m_text.getText() == null || m_text.getText().length() == 0)
return null;
String value = m_text.getText();
// return 0 if text deleted
if (value == null || value.length() == 0)
{
if (!m_modified)
return null;
if (m_displayType == DisplayType.Integer)
return new Integer(0);
return Env.ZERO;
}
if (value.equals(".") || value.equals(",") || value.equals("-"))
value = "0";
try
{
Number number = m_format.parse(value);
value = number.toString(); // converts it to US w/o thousands
BigDecimal bd = new BigDecimal(value);
if (m_displayType == DisplayType.Integer)
return new Integer(bd.intValue());
if (bd.signum() == 0)
return bd;
return bd.setScale(m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
}
catch (Exception e)
{
log.log(Level.SEVERE, "Value=" + value, e);
}
if (m_displayType == DisplayType.Integer)
return new Integer(0);
return Env.ZERO;
} // getValue
/**
* Return Display Value
* @return value
*/
public String getDisplay()
{
return m_text.getText();
} // getDisplay
/**
* Get Title
* @return title
*/
public String getTitle()
{
return m_title;
} // getTitle
/**
* Plus - add one.
* Also sets Value
* @return new value
*/
public Object plus()
{
Object value = getValue();
if (value == null)
{
if (m_displayType == DisplayType.Integer)
value = new Integer(0);
else
value = Env.ZERO;
}
// Add
if (value instanceof BigDecimal)
value = ((BigDecimal)value).add(Env.ONE);
else
value = new Integer(((Integer)value).intValue() + 1);
//
setValue(value);
return value;
} // plus
/**
* Minus - subtract one, but not below minimum.
* Also sets Value
* @param minimum minimum
* @return new value
*/
public Object minus (int minimum)
{
Object value = getValue();
if (value == null)
{
if (m_displayType == DisplayType.Integer)
value = new Integer(minimum);
else
value = new BigDecimal(minimum);
setValue(value);
return value;
}
// Subtract
if (value instanceof BigDecimal)
{
BigDecimal bd = ((BigDecimal)value).subtract(Env.ONE);
BigDecimal min = new BigDecimal(minimum);
if (bd.compareTo(min) < 0)
value = min;
else
value = bd;
}
else
{
int i = ((Integer)value).intValue();
i--;
if (i < minimum)
i = minimum;
value = new Integer(i);
}
//
setValue(value);
return value;
} // minus
/**************************************************************************
* Action Listener
* @param e event
*/
public void actionPerformed (ActionEvent e)
{
log.config(e.getActionCommand());
if (ValuePreference.NAME.equals(e.getActionCommand()))
{
if (MRole.getDefault().isShowPreference())
ValuePreference.start (m_mField, getValue());
return;
}
if (e.getSource() == m_button)
{
m_button.setEnabled(false);
String str = startCalculator(this, m_text.getText(), m_format, m_displayType, m_title);
m_text.setText(str);
m_button.setEnabled(true);
try
{
fireVetoableChange (m_columnName, m_oldText, getValue());
}
catch (PropertyVetoException pve) {}
m_text.requestFocus();
}
} // actionPerformed
/**************************************************************************
* Key Listener Interface
* @param e event
*/
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
/**
* Key Listener.
* - Escape - Restore old Text
* - firstChange - signal change
* @param e event
*/
public void keyReleased(KeyEvent e)
{
log.finest("Key=" + e.getKeyCode() + " - " + e.getKeyChar()
+ " -> " + m_text.getText());
// ESC
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
m_text.setText(m_initialText);
m_modified = true;
m_setting = true;
try
{
if (e.getKeyCode() == KeyEvent.VK_ENTER) // 10
{
fireVetoableChange (m_columnName, m_oldText, getValue());
fireActionPerformed();
}
else // indicate change
fireVetoableChange (m_columnName, m_oldText, null);
}
catch (PropertyVetoException pve) {}
m_setting = false;
} // keyReleased
/**
* Focus Gained
* @param e event
*/
public void focusGained (FocusEvent e)
{
if (m_text != null)
m_text.selectAll();
} // focusGained
/**
* Data Binding to MTable (via GridController.vetoableChange).
* @param e event
*/
public void focusLost (FocusEvent e)
{
// log.finest(e.toString());
// APanel - Escape
if (e.getOppositeComponent() instanceof AGlassPane)
{
m_text.setText(m_initialText);
return;
}
try
{
fireVetoableChange (m_columnName, m_initialText, getValue());
fireActionPerformed();
}
catch (PropertyVetoException pve) {}
} // focusLost
/**
* Invalid Entry - Start Calculator
* @param jc parent
* @param value value
* @param format format
* @param displayType display type
* @param title title
* @return value
*/
public static String startCalculator(Container jc, String value,
DecimalFormat format, int displayType, String title)
{
log.config("Value=" + value);
BigDecimal startValue = new BigDecimal(0.0);
try
{
if (value != null && value.length() > 0)
{
Number number = format.parse(value);
startValue = new BigDecimal (number.toString());
}
}
catch (ParseException pe)
{
log.info("InvalidEntry - " + pe.getMessage());
}
// Find frame
Frame frame = Env.getFrame(jc);
// Actual Call
Calculator calc = new Calculator(frame, title,
displayType, format, startValue);
AEnv.showCenterWindow(frame, calc);
BigDecimal result = calc.getNumber();
log.config( "Result=" + result);
//
calc = null;
if (result != null)
return format.format(result);
else
return value; // original value
} // startCalculator
/**
* Set Field/WindowNo for ValuePreference
* @param mField field
*/
public void setField (MField mField)
{
m_mField = mField;
/**
if (m_mField != null
&& MRole.getDefault().isShowPreference())
ValuePreference.addMenu (this, popupMenu);
**/
} // setField
/**************************************************************************
* Remove Action Listner
* @param l Action Listener
*/
public void removeActionListener(ActionListener l)
{
listenerList.remove(ActionListener.class, l);
} // removeActionListener
/**
* Add Action Listner
* @param l Action Listener
*/
public void addActionListener(ActionListener l)
{
listenerList.add(ActionListener.class, l);
} // addActionListener
/**
* Fire Action Event to listeners
*/
protected void fireActionPerformed()
{
int modifiers = 0;
AWTEvent currentEvent = EventQueue.getCurrentEvent();
if (currentEvent instanceof InputEvent)
modifiers = ((InputEvent)currentEvent).getModifiers();
else if (currentEvent instanceof ActionEvent)
modifiers = ((ActionEvent)currentEvent).getModifiers();
ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED,
"VNumber", EventQueue.getMostRecentEventTime(), modifiers);
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2)
{
if (listeners[i]==ActionListener.class)
{
((ActionListener)listeners[i+1]).actionPerformed(ae);
}
}
} // fireActionPerformed
/**/
} // VNumber
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -