📄 indicatorpanel.java
字号:
if(indicatorProjectManager.contains(projectName)) {
JOptionPane.showMessageDialog(this, "Database already contains " + projectName, "Duplicated project name", JOptionPane.WARNING_MESSAGE);
continue;
}
IndicatorDefaultDrawing newDrawing = (IndicatorDefaultDrawing)createDrawing();
if(this.indicatorProjectManager.addProject(newDrawing, projectName))
{
// New project will always invalid.
((javax.swing.DefaultListModel)this.jList1.getModel()).addElement(projectName + " *");
this.jList1.setSelectedIndex(this.jList1.getModel().getSize() - 1);
return;
}
else
{
JOptionPane.showMessageDialog(this, "You have provided an invalid project name.", "Invalid project name", JOptionPane.WARNING_MESSAGE);
continue;
}
}
}//GEN-LAST:event_jButton1ActionPerformed
private void addCreationButtonsTo(JToolBar tb, final DrawingEditor editor) {
// AttributeKeys for the entitie sets
HashMap<AttributeKey,Object> attributes;
ResourceBundleUtil labels = ResourceBundleUtil.getLAFBundle("org.yccheok.jstock.data.Labels");
ResourceBundleUtil drawLabels = ResourceBundleUtil.getLAFBundle("org.jhotdraw.draw.Labels");
//ToolBarButtonFactory.addSelectionToolTo(tb, editor);
ButtonFactory.addSelectionToolTo(tb, editor, createDrawingActions(editor), createSelectionActions(editor));
tb.addSeparator();
attributes = new HashMap<AttributeKey,Object>();
attributes.put(AttributeKeys.FILL_COLOR, Color.white);
attributes.put(AttributeKeys.STROKE_COLOR, Color.black);
attributes.put(AttributeKeys.TEXT_COLOR, Color.black);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.LogicalOperatorFigure", attributes), "createLogical", labels);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.EqualityOperatorFigure", attributes), "createEquality", labels);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.ArithmeticOperatorFigure", attributes), "createArithmetic", labels);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.DoubleConstantOperatorFigure", attributes), "createDoubleConstant", labels);
tb.addSeparator();
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.StockRelativeHistoryOperatorFigure", attributes), "createStockRelativeHistory", labels);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.StockHistoryOperatorFigure", attributes), "createStockHistory", labels);
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.StockOperatorFigure", attributes), "createStock", labels);
tb.addSeparator();
ButtonFactory.addToolTo(tb, editor, new OperatorFigureCreationTool("org.yccheok.jstock.gui.SinkOperatorFigure", attributes), "createSink", labels);
tb.addSeparator();
attributes = new HashMap<AttributeKey,Object>();
attributes.put(AttributeKeys.STROKE_COLOR, Color.black);
attributes.put(AttributeKeys.TEXT_COLOR, Color.black);
attributes.put(AttributeKeys.FONT_BOLD, true);
attributes.put(AttributeKeys.FILL_COLOR, new Color(255, 204, 0));
ButtonFactory.addToolTo(tb, editor, new TextAreaTool(new TextAreaFigure(), attributes), "createText", drawLabels);
}
private static Collection<Action> createDrawingActions(DrawingEditor editor) {
LinkedList<Action> a = new LinkedList<Action>();
return a;
}
private static Collection<Action> createSelectionActions(DrawingEditor editor) {
LinkedList<Action> a = new LinkedList<Action>();
a.add(new PropertiesAction(editor));
a.add(null);
a.add(new DeleteAction(editor));
return a;
}
/**
* Writes the project to the specified file.
*/
public void write(String projectName) throws IOException {
IndicatorDefaultDrawing drawing = (IndicatorDefaultDrawing)view.getDrawing();
// drawing.write(projectName);
}
/**
* Reads the project from the specified file.
*/
public void read(String projectName) throws IOException {
final IndicatorDefaultDrawing drawing = (IndicatorDefaultDrawing)createDrawing();
// drawing.read(projectName);
SwingUtilities.invokeLater(new Runnable() { public void run() {
view.setDrawing(drawing);
}});
}
public void setStockCodeAndSymbolDatabase(StockCodeAndSymbolDatabase stockCodeAndSymbolDatabase) {
((AutoCompleteJComboBox)jComboBox1).setStockCodeAndSymbolDatabase(stockCodeAndSymbolDatabase);
}
public void initjComboBox1EditorComponentKeyListerner() {
KeyListener[] listeners = this.jComboBox1.getEditor().getEditorComponent().getKeyListeners();
for(KeyListener listener : listeners) {
if(listener == jComboBox1EditorComponentKeyAdapter) {
return;
}
}
// Bug in Java 6. Most probably this listener had been removed during look n feel updating, reassign!
this.jComboBox1.getEditor().getEditorComponent().addKeyListener(jComboBox1EditorComponentKeyAdapter);
log.info("Reassign key adapter to combo box");
}
private KeyAdapter getjComboBox1EditorComponentKeyAdapter() {
return new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(KeyEvent.VK_ENTER == e.getKeyCode()) {
String stock = IndicatorPanel.this.jComboBox1.getEditor().getItem().toString();
if(stock.length() > 0) {
MainFrame m = MainFrame.getMe();
if(m == null) return;
final StockCodeAndSymbolDatabase stockCodeAndSymbolDatabase = m.getStockCodeAndSymbolDatabase();
Code code = stockCodeAndSymbolDatabase.searchStockCode(stock);
Symbol symbol = null;
if(code != null) {
symbol = stockCodeAndSymbolDatabase.codeToSymbol(code);
}
else {
symbol = stockCodeAndSymbolDatabase.searchStockSymbol(stock);
if(symbol != null) {
code = stockCodeAndSymbolDatabase.symbolToCode(symbol);
}
}
if(code != null && symbol != null) {
if(stockTask != null) {
stockTask._stop();
try {
stockTask.get();
} catch (InterruptedException exp) {
log.error("", exp);
} catch (ExecutionException exp) {
log.error("", exp);
}
}
stockTask = new StockTask(code, symbol);
stockTask.execute();
}
} /* if(stock.length() > 0) */
} /* if(KeyEvent.VK_ENTER == e.getKeyCode()) */
} /* public void keyReleased(KeyEvent e) */
};
}
private class StockTask extends SwingWorker<Boolean, Stock> {
private volatile boolean runnable = true;
final Code code;
final Symbol symbol;
public StockTask(Code code, Symbol symbol) {
this.code = code;
this.symbol = symbol;
}
public void _stop() {
runnable = false;
}
public Boolean doInBackground() {
Boolean success = false;
Stock s = null;
int tries = 0;
MainFrame m = MainFrame.getMe();
if(m == null) {
publish(s);
return success;
}
m.setStatusBar(true, "Stock sample data retrieving in progress...");
java.util.List<StockServerFactory> stockServerFactories = m.getStockServerFactory();
while(!isCancelled() && !success && runnable) {
for (StockServerFactory factory : stockServerFactories) {
StockServer server = factory.getStockServer();
try {
s = server.getStock(code);
success = true;
break;
}
catch(StockNotFoundException exp) {
log.error("", exp);
}
if(isCancelled() || !runnable) {
break;
}
}
tries++;
// We had tried NUM_OF_RETRY times, but still failed. Abort.
if(tries >= NUM_OF_RETRY) break;
}
publish(s);
return success;
}
@Override
protected void process(java.util.List<Stock> stocks) {
for (Stock stock : stocks) {
MainFrame m = MainFrame.getMe();
if(stock != null) {
((ObjectInspectorJPanel)objectInspectorJPanel).setBean(new MutableStock(stock));
if(m != null)
m.setStatusBar(false, "Stock sample data retrieved success");
}
else {
if(m != null)
m.setStatusBar(false, "Stock sample data retrieved failed");
}
}
}
}
public void stop()
{
if(stockTask != null) {
stockTask._stop();
try {
stockTask.get();
} catch (InterruptedException exp) {
log.error("", exp);
} catch (ExecutionException exp) {
log.error("", exp);
}
stockTask = null;
log.info("Terminated stock task");
}
if(this.simulationThread != null) {
this.simulationThread.interrupt();
try {
this.simulationThread.join();
}
catch(InterruptedException exp) {
log.error("", exp);
}
this.simulationThread = null;
log.info("Terminated simulation thread");
}
}
// Run by worker thread only.
private void simulate(final Code code) {
MainFrame m = MainFrame.getMe();
// First, check whether there is a need to get history.
final IndicatorDefaultDrawing indicatorDefaultDrawing = (IndicatorDefaultDrawing)this.view.getDrawing();
final OperatorIndicator operatorIndicator = indicatorDefaultDrawing.getOperatorIndicator();
final Duration historyDuration = operatorIndicator.getNeededStockHistoryDuration();
if(operatorIndicator.isStockHistoryServerNeeded()) {
m.setStatusBar(true, "Stock history retrieving in progress...");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -