📄 indicatorpanel.java
字号:
// Avoid from using old history monitor. Their duration are not the same.
final Duration oldDuration = stockHistoryMonitor.getDuration();
if (oldDuration.isContains(historyDuration) == false)
{
this.initStockHistoryMonitor(m.getStockServerFactory());
this.stockHistoryMonitor.setDuration(historyDuration);
}
// Action!
StockHistoryServer stockHistoryServer = this.stockHistoryMonitor.getStockHistoryServer(code);
if(stockHistoryServer == null) {
final java.util.concurrent.CountDownLatch countDownLatch = new java.util.concurrent.CountDownLatch(1);
org.yccheok.jstock.engine.Observer<StockHistoryMonitor, StockHistoryMonitor.StockHistoryRunnable> observer = new org.yccheok.jstock.engine.Observer<StockHistoryMonitor, StockHistoryMonitor.StockHistoryRunnable>() {
@Override
public void update(StockHistoryMonitor monitor, StockHistoryMonitor.StockHistoryRunnable runnable)
{
if(runnable.getCode().equals(code)) {
countDownLatch.countDown();
}
}
};
this.stockHistoryMonitor.attach(observer);
this.stockHistoryMonitor.addStockCode(code);
try {
countDownLatch.await();
}
catch(java.lang.InterruptedException exp) {
log.error("", exp);
return;
}
this.stockHistoryMonitor.dettach(observer);
stockHistoryServer = this.stockHistoryMonitor.getStockHistoryServer(code);
}
if(stockHistoryServer == null) {
this.jButton4.setEnabled(true);
this.jButton6.setEnabled(false);
m.setStatusBar(false, "Database not found");
return;
}
if(Thread.interrupted() || simulationThread != Thread.currentThread())
return;
m.setStatusBar(true, "Stock history information calculation in progress...");
operatorIndicator.setStockHistoryServer(stockHistoryServer);
} /* if(operatorIndicator.isStockHistoryServerNeeded()) { */
if(Thread.interrupted() || simulationThread != Thread.currentThread())
return;
m.setStatusBar(true, "Real time stock information calculation in progress...");
Object o = ((ObjectInspectorJPanel)this.objectInspectorJPanel).getBean();
MutableStock mutableStock = (MutableStock)o;
Stock stock = mutableStock.getStock();
operatorIndicator.preCalculate();
if(Thread.interrupted() || simulationThread != Thread.currentThread())
return;
m.setStatusBar(true, "Final calculation...");
long startTime = System.nanoTime();
operatorIndicator.setStock(stock);
operatorIndicator.isTriggered();
long estimatedTime = System.nanoTime() - startTime;
if(Thread.interrupted() || simulationThread != Thread.currentThread())
return;
m.setStatusBar(false, "Simulation done with time taken " + ((double)estimatedTime / (double)1000000.0) + "ms");
this.jButton4.setEnabled(true);
this.jButton6.setEnabled(false);
}
@Override
public void stateChanged(javax.swing.event.ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
if(pane.getSelectedComponent() == this) {
final MainFrame m = MainFrame.getMe();
final String password = Utils.decrypt(m.getJStockOptions().getIndicatorPassword());
boolean status = false;
if(password.length() > 0 && (passwordSuccessOnce == false)) {
PasswordInputJDialog passwordInputJDialog = new PasswordInputJDialog(m, true);
if(passwordInputJDialog.doModal())
{
if(passwordInputJDialog.isPasswordMatch(password) == false) {
JOptionPane.showMessageDialog(this, "Password not match. You are not allowed to use indicator editor.", "Wrong password", JOptionPane.WARNING_MESSAGE);
status = false;
}
else {
passwordSuccessOnce = true;
status = true;
}
}
else {
// Cancel
status = false;
}
}
else
{
status = true;
}
jList1.setEnabled(status);
jButton1.setEnabled(status);
jButton5.setEnabled(status);
jButton2.setEnabled(status);
jButton3.setEnabled(status);
}
else {
/* We will not call stop, to avoid GUI from being freezed. But, is it
* safe to do so?
*/
// Simulate and stop buttons.
this.jButton4.setEnabled(true);
this.jButton6.setEnabled(false);
}
}
public void initIndicatorProjectManager() {
try {
File f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + "indicator" + File.separator + "project.xml");
XStream xStream = new XStream();
InputStream inputStream = new java.io.FileInputStream(f);
indicatorProjectManager = (IndicatorProjectManager)xStream.fromXML(inputStream);
log.info("indicatorProjectManager loaded from " + f.toString() + " successfully.");
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
indicatorProjectManager = new IndicatorProjectManager(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + "indicator");
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
indicatorProjectManager = new IndicatorProjectManager(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + "indicator");
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DefaultListModel defaultListModel = (DefaultListModel)jList1.getModel();
for(int i = 0; i < indicatorProjectManager.getNumOfProject(); i++) {
OperatorIndicator operatorIndicator = indicatorProjectManager.getOperatorIndicator(indicatorProjectManager.getProject(i));
if(operatorIndicator == null) {
defaultListModel.addElement(indicatorProjectManager.getProject(i) + " *");
}
else {
if(operatorIndicator.isValid() == false) {
defaultListModel.addElement(indicatorProjectManager.getProject(i) + " *");
}
else {
defaultListModel.addElement(indicatorProjectManager.getProject(i));
}
}
}
}
});
}
public boolean saveIndicatorProjectManager() {
File f = new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + "indicator" + File.separator + "project.xml");
XStream xStream = new XStream();
try {
OutputStream outputStream = new FileOutputStream(f);
xStream.toXML(indicatorProjectManager, outputStream);
}
catch(java.io.FileNotFoundException exp) {
log.error("", exp);
return false;
}
catch(com.thoughtworks.xstream.core.BaseException exp) {
log.error("", exp);
return false;
}
return true;
}
public IndicatorProjectManager getIndicatorProjectManager() {
return indicatorProjectManager;
}
public void initStockHistoryMonitor(java.util.List<StockServerFactory> stockServerFactories) {
if(stockHistoryMonitor != null) {
final StockHistoryMonitor oldStockHistoryMonitor = stockHistoryMonitor;
Utils.getZoombiePool().execute(new Runnable() {
public void run() {
log.info("Prepare to shut down " + oldStockHistoryMonitor + "...");
oldStockHistoryMonitor.clearStockCodes();
oldStockHistoryMonitor.dettachAll();
oldStockHistoryMonitor.stop();
log.info("Shut down " + oldStockHistoryMonitor + " peacefully.");
}
});
}
this.stockHistoryMonitor = new StockHistoryMonitor(NUM_OF_THREADS_HISTORY_MONITOR);
for(StockServerFactory factory : stockServerFactories) {
stockHistoryMonitor.addStockServerFactory(factory);
}
// No StockHistorySerializer at this moment, either read or write. This is because
// (1) Read - If the duration of the history selected in Real-Time panel is shorter than
// indicators's, we will be in trouble.
// (2) Write - If the duration of the indicator's is shorter than Real-Time panel, we will
// be in trouble again.
//
// Currently, we have no way but disable it.
}
private StockHistoryMonitor stockHistoryMonitor = null;
private static final int NUM_OF_THREADS_HISTORY_MONITOR = 1;
private IndicatorProjectManager indicatorProjectManager;
private static final Log log = LogFactory.getLog(IndicatorPanel.class);
private StockTask stockTask;
// As workaround to overcome the bug, when new look n feel being applied during runtime, the original
// KeyListner for ComboBoxEditor will be removed.
private final KeyListener jComboBox1EditorComponentKeyAdapter = getjComboBox1EditorComponentKeyAdapter();
private Thread simulationThread;
private boolean passwordSuccessOnce = false;
private static final int NUM_OF_RETRY = 3;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JToolBar creationToolbar;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel jPanel8;
private javax.swing.JPanel jPanel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JSplitPane jSplitPane2;
private javax.swing.JPanel objectInspectorJPanel;
private javax.swing.JScrollPane scrollPane;
private javax.swing.ButtonGroup toolButtonGroup;
private org.jhotdraw.draw.DefaultDrawingView view;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -