public void clearSelected() { if (pageLines != null) { for (LineText lineText : pageLines) { lineText.clearSelected(); } } if (sortedPageLines != null) { for (LineText lineText : sortedPageLines) { lineText.clearSelected(); } } // check optional content. if (optionalPageLines != null) { // iterate over optional content keys and extract text from visible groups Set<OptionalContents> keys = optionalPageLines.keySet(); ArrayList<LineText> optionalLines; for (OptionalContents key : keys) { if (key != null) { optionalLines = optionalPageLines.get(key).getAllPageLines(); if (optionalLines != null) { for (LineText lineText : optionalLines) { lineText.clearSelected(); } } } } } }
/** * Finds the key (time) of the currently hovered entry, or -1 if none is hovered. * * @param p The current position of the mouse. * @return */ private long findHoverEntry(Point p) { double smallestDistance = HOVER_RADIUS; long foundHoverEntry = -1; for (Point point : locations.keySet()) { double distance = p.distance(point); if (distance < HOVER_RADIUS) { if (distance < smallestDistance) { foundHoverEntry = locations.get(point); smallestDistance = distance; } } } return foundHoverEntry; }
private void doAssert(Map<String, String> expected, PlaybackRunner.StatusCallback cb) throws AssertionError { final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (owner == null) { throw new AssertionError("No component focused"); } Component eachParent = owner; final LinkedHashMap<String, String> actual = new LinkedHashMap<String, String>(); while (eachParent != null) { if (eachParent instanceof TestableUi) { ((TestableUi) eachParent).putInfo(actual); } eachParent = eachParent.getParent(); } Set testedKeys = new LinkedHashSet<String>(); for (String eachKey : expected.keySet()) { testedKeys.add(eachKey); final String actualValue = actual.get(eachKey); final String expectedValue = expected.get(eachKey); if (!expectedValue.equals(actualValue)) { throw new AssertionError( eachKey + " expected: " + expectedValue + " but was: " + actualValue); } } Map<String, String> untested = new HashMap<String, String>(); for (String eachKey : actual.keySet()) { if (testedKeys.contains(eachKey)) continue; untested.put(eachKey, actual.get(eachKey)); } StringBuffer untestedText = new StringBuffer(); for (String each : untested.keySet()) { if (untestedText.length() > 0) { untestedText.append(","); } untestedText.append(each).append("=").append(untested.get(each)); } cb.message("Untested info: " + untestedText.toString(), getLine()); }
/** * Gets the starting time for the displayed range. If there is a fixed starting time, use that, * otherwise check if there is a range and calculate the starting time from that. Otherwise return * 0, meaning the data is displayed from the start. * * @param range The time in milliseconds that the data should be displayed, going backwards, * starting from the very end. * @return The start of the data displaying range in milliseconds. */ private long getStartAt(long range) { if (fixedStartTime > 0) { return fixedStartTime; } if (range <= 0) { return 0; } long end = -1; for (long time : history.keySet()) { end = time; } long startAt = end - range; if (startAt < 0) { startAt = 0; } return startAt; }
/** * Update the start/end/duration/min/max variables which can be changed when the data changes as * well when the displayed range changes. */ private void updateVars() { long startAt = getStartAt(currentRange); long endAt = getEndAt(); int max = 0; int min = -1; long start = -1; long end = -1; for (Long time : history.keySet()) { if (time < startAt) { continue; } if (endAt > startAt && time > endAt) { continue; } // Start/End time if (start == -1) { start = time; } end = time; // Max/min value StreamInfoHistoryItem historyObj = history.get(time); int viewerCount = historyObj.getViewers(); if (viewerCount < min || min == -1) { min = viewerCount; } if (viewerCount == -1) { min = 0; } if (viewerCount > max) { max = viewerCount; } } maxValue = max; minValue = min; startTime = start; endTime = end; duration = end - start; }
private ArrayList<LineText> getAllPageLines() { ArrayList<LineText> visiblePageLines = new ArrayList<LineText>(pageLines); // add optional content text that is visible. // check optional content. if (optionalPageLines != null) { // iterate over optional content keys and extract text from visible groups Set<OptionalContents> keys = optionalPageLines.keySet(); LineText currentLine = new LineText(); visiblePageLines.add(currentLine); for (OptionalContents key : keys) { if (key != null) { ArrayList<LineText> pageLines = optionalPageLines.get(key).getVisiblePageLines(true); for (LineText lineText : pageLines) { currentLine.addAll(lineText.getWords()); } } } // recalculate the bounds. currentLine.getBounds(); } return visiblePageLines; }
/** * Draw the text and graph. * * @param g */ @Override public void paintComponent(Graphics g) { locations.clear(); // Background g.setColor(background_color); g.fillRect(0, 0, getWidth(), getHeight()); // This color is used for everything until drawing the points g.setColor(foreground_color); // Anti-Aliasing Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Font FontMetrics fontMetrics = g.getFontMetrics(FONT); int fontHeight = fontMetrics.getHeight(); g.setFont(FONT); int topTextY = fontMetrics.getAscent(); // Margins int vMargin = fontHeight + MARGIN; int hMargin = MARGIN; // Calculate actual usable size double width = getWidth() - hMargin * 2; double height = getHeight() - vMargin * 2; boolean drawLowerLine = height > -vMargin; // If there is any data and no hovered entry is shown, draw current // viewercount int nowTextX = 0; if (history != null && hoverEntry == -1) { Integer viewers = history.get(endTime).getViewers(); long ago = System.currentTimeMillis() - endTime; String text; if (ago > CONSIDERED_AS_NOW) { text = "latest: " + Helper.formatViewerCount(viewers); } else { text = "now: " + Helper.formatViewerCount(viewers); } if (viewers == -1) { text = "Stream offline"; } nowTextX = getWidth() - fontMetrics.stringWidth(text); g.drawString(text, nowTextX, topTextY); } // Default text when no data is present if (history == null || history.size() < 2) { String text = "No viewer history yet"; int textWidth = fontMetrics.stringWidth(text); int y = getHeight() / 2 + fontMetrics.getDescent(); int x = (getWidth() - textWidth) / 2; boolean drawInfoText = false; if (history != null && y < topTextY + fontHeight + 4 && x + textWidth + 7 > nowTextX) { if (drawLowerLine || nowTextX > textWidth + 5) { if (drawLowerLine) { y = getHeight() - 2; } else { y = topTextY; } x = 0; drawInfoText = true; } } else { drawInfoText = true; } if (drawInfoText) { g.drawString(text, x, y); } return; } // ---------- // From here only when actual data is to be rendered // Show info on hovered entry String maxValueText = "max: " + Helper.formatViewerCount(maxValue); int maxValueEnd = fontMetrics.stringWidth(maxValueText); boolean displayMaxValue = true; if (hoverEntry != -1) { Integer viewers = history.get(hoverEntry).getViewers(); Date d = new Date(hoverEntry); String text = "Viewers: " + Helper.formatViewerCount(viewers) + " (" + sdf.format(d) + ")"; if (viewers == -1) { text = "Stream offline (" + sdf.format(d) + ")"; } int x = getWidth() - fontMetrics.stringWidth(text); if (maxValueEnd > x) { displayMaxValue = false; } g.drawString(text, x, topTextY); } String minText = "min: " + Helper.formatViewerCount(minValue); int minTextWidth = fontMetrics.stringWidth(minText); // Draw Times if (drawLowerLine) { String timeText = makeTimesText(startTime, endTime); int timeTextWidth = fontMetrics.stringWidth(timeText); int textX = getWidth() - timeTextWidth; g.drawString(timeText, textX, getHeight() - 1); if (minValue >= 1000 && timeTextWidth + minTextWidth > width) { minText = "min: " + minValue / 1000 + "k"; } } // Draw min/max if necessary if (isShowingInfo()) { if (displayMaxValue) { g.drawString(maxValueText, 0, topTextY); } if (drawLowerLine) { g.drawString(minText, 0, getHeight() - 1); } else if (maxValueEnd + minTextWidth + 29 < nowTextX) { g.drawString(minText, maxValueEnd + 10, topTextY); } } // If height available for the graph is too small, don't draw graph if (height < 5) { return; } // Calculation factors for calculating the points location int range = maxValue - minValue; if (showFullRange) { range = maxValue; } if (range == 0) { // Prevent division by zero range = 1; } double pixelPerViewer = height / range; double pixelPerTime = width / duration; // Go through all entries and calculate positions int prevX = -1; int prevY = -1; Iterator<Entry<Long, StreamInfoHistoryItem>> it = history.entrySet().iterator(); while (it.hasNext()) { Entry<Long, StreamInfoHistoryItem> entry = it.next(); // Get time and value to draw next long time = entry.getKey(); if (time < startTime || time > endTime) { continue; } long offsetTime = time - startTime; int viewers = entry.getValue().getViewers(); if (viewers == -1) { viewers = 0; } // Calculate point location int x = (int) (hMargin + offsetTime * pixelPerTime); int y; if (showFullRange) { y = (int) (-vMargin + getHeight() - (viewers) * pixelPerViewer); } else { y = (int) (-vMargin + getHeight() - (viewers - minValue) * pixelPerViewer); } // Draw connecting line if (prevX != -1) { g.drawLine(x, y, prevX, prevY); } // Save point coordinates to be able to draw the line next loop prevX = x; prevY = y; // Save point locations to draw points and to find entries on hover locations.put(new Point(x, y), time); } // Draw points (after lines, so they are in front) for (Point point : locations.keySet()) { int x = point.x; int y = point.y; long seconds = locations.get(point); StreamInfoHistoryItem historyObject = history.get(seconds); // Highlight hovered entry if (seconds == hoverEntry) { g.setColor(HOVER_COLOR); } else { // Draw offline points differently if (!historyObject.isOnline()) { g.setColor(OFFLINE_COLOR); } else { g.setColor(colors.get(seconds)); } } g.fillOval(x - POINT_SIZE / 2, y - POINT_SIZE / 2, POINT_SIZE, POINT_SIZE); } }