// Extract all quotes from the quote bundle which cause the given // expression to equate to true. If there is no expression (string is null or // empty) then extract all the quotes. private List extractQuotesUsingRule(String filterExpression, EODQuoteBundle quoteBundle) { // If there is no rule, then just return all quotes if (filterExpression == null || filterExpression.length() == 0) return extractAllQuotes(quoteBundle); // First parse the expression Expression expression = null; try { expression = Parser.parse(filterExpressionString); } catch (ExpressionException e) { // We should have already checked the string for errors before here assert false; } // Add symbols to list when expression proves true ArrayList quotes = new ArrayList(); Iterator iterator = quoteBundle.iterator(); TradingDate lastDate = quoteBundle.getLastDate(); try { // Traverse all symbols on all dates while (iterator.hasNext()) { Quote quote = (Quote) iterator.next(); Symbol symbol = quote.getSymbol(); TradingDate date = quote.getDate(); int dateOffset = 0; try { dateOffset = quoteBundle.dateToOffset(date); } catch (WeekendDateException e) { assert false; } if (!singleDate || (lastDate.equals(quote.getDate()))) { if (expression.evaluate(new Variables(), quoteBundle, symbol, dateOffset) >= Expression.TRUE_LEVEL) quotes.add(quote); } } return quotes; } catch (EvaluationException e) { // Tell user expression didnt evaluate properly JOptionPane.showInternalMessageDialog( DesktopManager.getDesktop(), e.getReason() + ": " + expression.toString(), Locale.getString("ERROR_EVALUATION_EQUATION"), JOptionPane.ERROR_MESSAGE); // delete erroneous expression expression = null; // If the expression didn't evaluate then just return all the quotes return extractAllQuotes(quoteBundle); } }
/** * Return the window title. * * @return the window title */ public String getTitle() { // Title depends on the quote bundle we are listing String title = Locale.getString("TABLE_OF", quoteBundle.getQuoteRange().getDescription()); // If there is only one date it makes sense to tell the user it if (singleDate) title = title.concat(" (" + quoteBundle.getLastDate().toString("dd/mm/yyyy") + ")"); return title; }
// This function extracts all quotes from the quote bundle and returns // them as a list of Quotes. private List extractAllQuotes(EODQuoteBundle quoteBundle) { List quotes = new ArrayList(); Iterator iterator = quoteBundle.iterator(); TradingDate lastDate = quoteBundle.getLastDate(); // Traverse all symbols on all dates while (iterator.hasNext()) { Quote quote = (Quote) iterator.next(); if (!singleDate || (lastDate.equals(quote.getDate()))) quotes.add(quote); } return quotes; }
/** * Create a new module that only lists the quotes in the given bundle where the filter expression * returns true. Set the <code>singleDate</code> flag if you want to display a single day's * trading - and don't want to display the quotes from the bundle that may appear from executing * some expressions. (e.g. comparing today's prices to yesterdays). * * @param quoteBundle quotes to table * @param filterExpressionString expression string to filter by * @param singleDate if this is set to true then only display the quotes on the last date in the * quote bundle, otherwise display them all. */ public QuoteModule( EODQuoteBundle quoteBundle, String filterExpressionString, boolean singleDate) { this.filterExpressionString = filterExpressionString; this.quoteBundle = quoteBundle; this.singleDate = singleDate; propertySupport = new PropertyChangeSupport(this); // Get list of quotes to display List quotes = extractQuotesUsingRule(filterExpressionString, quoteBundle); // If we are listing stocks on a single day then don't bother showing // the date column. On the other hand if we are only listing a single // stock then don't bother showing the symbol column model = new EODQuoteModel( quoteBundle, quotes, singleDate ? Column.HIDDEN : Column.VISIBLE, quoteBundle.getAllSymbols().size() == 1 ? Column.HIDDEN : Column.VISIBLE); setModel( model, quoteBundle.getAllSymbols().size() == 1 ? EODQuoteModel.DATE_COLUMN : EODQuoteModel.ACTIVITY_COLUMN, SORT_UP); model.addTableModelListener(this); showColumns(model); resort(); addMenu(); // If the user clicks on the table trap it. addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent evt) { handleMouseClicked(evt); } }); // Set up DND // dragSource = DragSource.getDefaultDragSource(); // dragGestureListener = new DragGestureListener(); // dragSourceListener = new DragSourceListener(); // component, action, listener // dragSource.createDefaultDragGestureRecognizer(this, // DnDConstants.ACTION_COPY, // dgListener); }