📄 newselltransactionjdialog.java
字号:
return false;
}
if(this.jFormattedTextField5.getText().length() <= 0) {
this.jFormattedTextField5.requestFocus();
return false;
}
if(this.jFormattedTextField7.getText().length() <= 0) {
this.jFormattedTextField7.requestFocus();
return false;
}
if(this.jFormattedTextField6.getText().length() <= 0) {
this.jFormattedTextField6.requestFocus();
return false;
}
return true;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
if(isValidInput() == false) {
return;
}
commitEdit();
this.transaction = generateTransaction();
this.setVisible(false);
this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed
private void update() {
// Commit the value first before updating. This is to prevent
// double rounding issue. We force the current value to
// follow the formatter text field's.
commitEdit();
if(shouldAutoCalculateBrokerFee())
{
final BrokingFirm brokingFirm = MainFrame.getJStockOptions().getSelectedBrokingFirm();
SwingUtilities.invokeLater(new Runnable() { public void run() {
final String name = jTextField1.getText();
final int unit = (Integer)jSpinner1.getValue();
final double price = (Double)jFormattedTextField1.getValue();
final DateField dateField = (DateField)jPanel3;
final Date date = (Date)dateField.getValue();
// Stock and date information is not important at this moment.
Contract.ContractBuilder builder = new Contract.ContractBuilder(Utils.getEmptyStock(Code.newInstance(name), Symbol.newInstance(name)), new SimpleDate(date));
Contract contract = builder.type(Contract.Type.Sell).quantity(unit).price(price).build();
final double brokerFee = brokingFirm.brokerCalculate(contract);
final double clearingFee = brokingFirm.clearingFeeCalculate(contract);
final double stampDuty = brokingFirm.stampDutyCalculate(contract);
jFormattedTextField4.setValue(brokerFee);
jFormattedTextField5.setValue(clearingFee);
jFormattedTextField7.setValue(stampDuty);
final double sellValue = price * (double)unit;
final double buyValue = buyCost * (double)unit;
final double totalCost = buyValue + brokerFee + clearingFee + stampDuty;
final double netProfit = sellValue - totalCost;
final double netProfitPercentage = (totalCost == 0.0) ? 0.0 : netProfit / totalCost * 100.0;
jFormattedTextField2.setValue(sellValue);
jFormattedTextField3.setValue(buyValue);
jFormattedTextField6.setValue(netProfitPercentage);
jFormattedTextField6.setForeground(Utils.getColor(netProfitPercentage, 0.0));
jFormattedTextField8.setValue(netProfit);
jFormattedTextField8.setForeground(Utils.getColor(netProfit, 0.0));
}});
}
else {
SwingUtilities.invokeLater(new Runnable() { public void run() {
final int unit = (Integer)jSpinner1.getValue();
final double price = (Double)jFormattedTextField1.getValue();
final double brokerFee = (Double)jFormattedTextField4.getValue();
final double clearingFee = (Double)jFormattedTextField5.getValue();
final double stampDuty = (Double)jFormattedTextField7.getValue();
final double sellValue = price * (double)unit;
final double buyValue = buyCost * (double)unit;
final double totalCost = buyValue + brokerFee + clearingFee + stampDuty;
final double netProfit = sellValue - totalCost;
final double netProfitPercentage = (totalCost == 0.0) ? 0.0 : netProfit / totalCost * 100.0;
jFormattedTextField2.setValue(sellValue);
jFormattedTextField3.setValue(buyValue);
jFormattedTextField6.setValue(netProfitPercentage);
jFormattedTextField6.setForeground(Utils.getColor(netProfitPercentage, 0.0));
jFormattedTextField8.setValue(netProfit);
jFormattedTextField8.setForeground(Utils.getColor(netProfit, 0.0));
}});
}
}
private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
// TODO add your handling code here:
update();
}//GEN-LAST:event_jSpinner1StateChanged
private void jFormattedTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusLost
// TODO add your handling code here:
update();
}//GEN-LAST:event_jFormattedTextField1FocusLost
private void jFormattedTextField4FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField4FocusLost
// TODO add your handling code here:
update();
}//GEN-LAST:event_jFormattedTextField4FocusLost
private void jFormattedTextField5FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField5FocusLost
// TODO add your handling code here:
update();
}//GEN-LAST:event_jFormattedTextField5FocusLost
private void jFormattedTextField7FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField7FocusLost
// TODO add your handling code here:
update();
}//GEN-LAST:event_jFormattedTextField7FocusLost
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
this.setPrice(this.suggestBestSellingPrice());
}//GEN-LAST:event_jButton3ActionPerformed
private void commitEdit() {
try {
jFormattedTextField1.commitEdit();
jFormattedTextField2.commitEdit();
jFormattedTextField3.commitEdit();
jFormattedTextField4.commitEdit();
jFormattedTextField5.commitEdit();
jFormattedTextField6.commitEdit();
jFormattedTextField7.commitEdit();
jFormattedTextField8.commitEdit();
} catch (ParseException ex) {
log.error("", ex);
}
}
private JFormattedTextField getCurrencyJFormattedTextField(boolean isNegativeAllowed) {
NumberFormat format= NumberFormat.getNumberInstance();
NumberFormatter formatter= new NumberFormatter(format);
if(isNegativeAllowed == false)
formatter.setMinimum(0.0);
else
formatter.setMinimum(null);
formatter.setValueClass(Double.class);
JFormattedTextField field= new JFormattedTextField(formatter);
return field;
}
public void setPrice(double price) {
this.jFormattedTextField1.setValue(price);
update();
}
public void setStockSymbol(Symbol symbol) {
this.jTextField1.setText(symbol.toString());
}
public Transaction getTransaction() {
return this.transaction;
}
public double suggestBestSellingPrice() {
final double expectedProfitPercentage = MainFrame.getJStockOptions().getExpectedProfitPercentage();
final int unit = (Integer)jSpinner1.getValue();
if(unit <= 0) return 0.0;
final double brokerFee = (Double)jFormattedTextField4.getValue();
final double clearingFee = (Double)jFormattedTextField5.getValue();
final double stampDuty = (Double)jFormattedTextField7.getValue();
final double buyValue = buyCost * (double)unit;
final double totalCost = buyValue + brokerFee + clearingFee + stampDuty;
final double bestSellingValue = (1.0 + expectedProfitPercentage / 100.0) * totalCost;
final double bestPrice = bestSellingValue / (double)unit;
return bestPrice;
}
private static final Log log = LogFactory.getLog(NewSellTransactionJDialog.class);
private Transaction transaction = null;
/* Cost per unit. */
private double buyCost = 0.0;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JFormattedTextField jFormattedTextField2;
private javax.swing.JFormattedTextField jFormattedTextField3;
private javax.swing.JFormattedTextField jFormattedTextField4;
private javax.swing.JFormattedTextField jFormattedTextField5;
private javax.swing.JFormattedTextField jFormattedTextField6;
private javax.swing.JFormattedTextField jFormattedTextField7;
private javax.swing.JFormattedTextField jFormattedTextField8;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -