From 87943fac959b8dcfbbda459a2d9b4bf74abd4e00 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 27 Dec 2019 18:50:08 +0100 Subject: [PATCH] Fix bug in moveActiveRecordingsToFront There was a bug in moveActiveRecordingsToFront, which caused the models to rotate their positions Also some refactoring to reduce method complexity. --- .../main/java/ctbrec/ui/ThumbOverviewTab.java | 402 +++++++++--------- .../ctbrec/ui/ThumbOverviewTabSearchTask.java | 71 ++++ 2 files changed, 269 insertions(+), 204 deletions(-) create mode 100644 client/src/main/java/ctbrec/ui/ThumbOverviewTabSearchTask.java diff --git a/client/src/main/java/ctbrec/ui/ThumbOverviewTab.java b/client/src/main/java/ctbrec/ui/ThumbOverviewTab.java index f6875396..61b3bf1e 100644 --- a/client/src/main/java/ctbrec/ui/ThumbOverviewTab.java +++ b/client/src/main/java/ctbrec/ui/ThumbOverviewTab.java @@ -7,7 +7,6 @@ import java.net.SocketTimeoutException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -85,7 +84,7 @@ import javafx.scene.transform.Transform; import javafx.util.Duration; public class ThumbOverviewTab extends Tab implements TabSelectionListener { - private static final transient Logger LOG = LoggerFactory.getLogger(ThumbOverviewTab.class); + private static final Logger LOG = LoggerFactory.getLogger(ThumbOverviewTab.class); protected static BlockingQueue queue = new LinkedBlockingQueue<>(); static ExecutorService threadPool = new ThreadPoolExecutor(2, 2, 10, TimeUnit.MINUTES, queue, createThreadFactory()); @@ -173,7 +172,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { topBar.getChildren().add(filterInput); if (site.supportsTips() && site.credentialsAvailable()) { Button buyTokens = new Button("Buy Tokens"); - buyTokens.setOnAction((e) -> DesktopIntegration.open(site.getBuyTokensLink())); + buyTokens.setOnAction(e -> DesktopIntegration.open(site.getBuyTokensLink())); TokenLabel tokenBalance = new TokenLabel(site); tokenBalance.setAlignment(Pos.CENTER_RIGHT); tokenBalance.prefHeightProperty().bind(buyTokens.heightProperty()); @@ -196,15 +195,15 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { pagination.getChildren().add(pageInput); BorderPane.setMargin(pagination, new Insets(5)); pageInput.setPrefWidth(50); - pageInput.setOnAction((e) -> handlePageNumberInput()); - pagePrev.setOnAction((e) -> { + pageInput.setOnAction(e -> handlePageNumberInput()); + pagePrev.setOnAction(e -> { int page = updateService.getPage(); page = Math.max(1, --page); pageInput.setText(Integer.toString(page)); updateService.setPage(page); restartUpdateService(); }); - pageNext.setOnAction((e) -> { + pageNext.setOnAction(e -> { int page = updateService.getPage(); page++; pageInput.setText(Integer.toString(page)); @@ -224,7 +223,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { thumbWidths.add(360); thumbWidth = new ComboBox<>(FXCollections.observableList(thumbWidths)); thumbWidth.getSelectionModel().select(Integer.valueOf(Config.getInstance().getSettings().thumbWidth)); - thumbWidth.setOnAction((e) -> { + thumbWidth.setOnAction(e -> { int width = thumbWidth.getSelectionModel().getSelectedItem(); Config.getInstance().getSettings().thumbWidth = width; updateThumbSize(); @@ -253,51 +252,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { if(searchTask != null) { searchTask.cancel(true); } - if(newValue.length() < 2) { return; } - - - searchTask = new Task>() { - @Override - protected List call() throws Exception { - if(site.searchRequiresLogin()) { - boolean loggedin = false; - try { - loggedin = SiteUiFactory.getUi(site).login(); - } catch (IOException e) { - loggedin = false; - } - if(!loggedin) { - showError("Login failed", "Search won't work correctly without login", null); - } - } - return site.search(newValue); - } - - @Override - protected void failed() { - LOG.error("Search failed", getException()); - } - - @Override - protected void succeeded() { - Platform.runLater(() -> { - List models = getValue(); - LOG.debug("Search result {} {}", isCancelled(), models); - if(models.isEmpty()) { - popover.hide(); - } else { - popoverTreelist.getItems().clear(); - for (Model model : getValue()) { - popoverTreelist.getItems().add(model); - } - popover.show(); - } - }); - } - }; + searchTask = new ThumbOverviewTabSearchTask(site, popover, popoverTreelist, newValue); new Thread(searchTask).start(); }; } @@ -323,6 +281,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { updateService.setPage(page); restartUpdateService(); } catch(NumberFormatException e) { + // noop } finally { pageInput.setText(Integer.toString(updateService.getPage())); } @@ -343,8 +302,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { void initializeUpdateService() { int refreshRate = Config.getInstance().getSettings().overviewUpdateIntervalInSecs; updateService.setPeriod(new Duration(TimeUnit.SECONDS.toMillis(refreshRate))); - updateService.setOnSucceeded((event) -> onSuccess()); - updateService.setOnFailed((event) -> onFail(event)); + updateService.setOnSucceeded(event -> onSuccess()); + updateService.setOnFailed(this::onFail); } protected void onSuccess() { @@ -368,63 +327,79 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { ObservableList nodes = grid.getChildren(); // first remove models, which are not in the updated list - for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { - Node node = iterator.next(); - if (!(node instanceof ThumbCell)) continue; - ThumbCell cell = (ThumbCell) node; - if(!models.contains(cell.getModel())) { - iterator.remove(); - } - } + removeModelsMissingInUpdate(nodes, models); - List positionChangedOrNew = new ArrayList<>(); - int index = 0; - for (Model model : models) { - boolean found = false; - for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { - Node node = iterator.next(); - if (!(node instanceof ThumbCell)) continue; - ThumbCell cell = (ThumbCell) node; - if(cell.getModel().equals(model)) { - found = true; - cell.setModel(model); - if(index != cell.getIndex()) { - cell.setIndex(index); - positionChangedOrNew.add(cell); - } - } - } - if(!found) { - ThumbCell newCell = createThumbCell(this, model, recorder); - newCell.setIndex(index); - positionChangedOrNew.add(newCell); - } - index++; - } - for (ThumbCell thumbCell : positionChangedOrNew) { - nodes.remove(thumbCell); - if(thumbCell.getIndex() < nodes.size()) { - nodes.add(thumbCell.getIndex(), thumbCell); - } else { - nodes.add(thumbCell); - } - } + // now update existing cells and create new ones models, which are new in the update + createOrUpdateModelCells(nodes, models); + // reapply the filter filteredThumbCells.clear(); filter(); + + // move models, which are tracked by the recorder to the front moveActiveRecordingsToFront(); } finally { gridLock.unlock(); } } - ThumbCell createThumbCell(ThumbOverviewTab thumbOverviewTab, Model model, Recorder recorder) { + private void createOrUpdateModelCells(ObservableList nodes, List models) { + List positionChangedOrNew = new ArrayList<>(); + int index = 0; + for (Model model : models) { + boolean found = false; + for (Node node : nodes) { // NOSONAR + if (!(node instanceof ThumbCell)) continue; + ThumbCell cell = (ThumbCell) node; + if(cell.getModel().equals(model)) { + found = true; + cell.setModel(model); + if(index != cell.getIndex()) { + cell.setIndex(index); + positionChangedOrNew.add(cell); + } + break; + } + } + if(!found) { + ThumbCell newCell = createThumbCell(model, recorder); + newCell.setIndex(index); + positionChangedOrNew.add(newCell); + } + index++; + } + rearrangeCells(nodes, positionChangedOrNew); + } + + private void rearrangeCells(ObservableList nodes, List positionChangedOrNew) { + for (ThumbCell thumbCell : positionChangedOrNew) { + nodes.remove(thumbCell); + if(thumbCell.getIndex() < nodes.size()) { + nodes.add(thumbCell.getIndex(), thumbCell); + } else { + nodes.add(thumbCell); + } + } + } + + private void removeModelsMissingInUpdate(ObservableList nodes, List models) { + for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { + Node node = iterator.next(); + if (!(node instanceof ThumbCell)) continue; + ThumbCell cell = (ThumbCell) node; + if (!models.contains(cell.getModel())) { + iterator.remove(); + } + } + } + + ThumbCell createThumbCell(Model model, Recorder recorder) { ThumbCell newCell = new ThumbCell(this, model, recorder); newCell.addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, event -> { suspendUpdates(true); popup = createContextMenu(newCell); popup.show(newCell, event.getScreenX(), event.getScreenY()); - popup.setOnHidden((e) -> suspendUpdates(false)); + popup.setOnHidden(e -> suspendUpdates(false)); event.consume(); }); newCell.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> { @@ -435,7 +410,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } }); newCell.selectionProperty().addListener((obs, oldValue, newValue) -> { - if(newValue) { + if(newValue.booleanValue()) { selectedThumbCells.add(newCell); } else { selectedThumbCells.remove(newCell); @@ -447,75 +422,32 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { private ContextMenu createContextMenu(ThumbCell cell) { MenuItem openInPlayer = new MenuItem("Open in Player"); - openInPlayer.setOnAction((e) -> startPlayer(getSelectedThumbCells(cell))); + openInPlayer.setOnAction(e -> startPlayer(getSelectedThumbCells(cell))); MenuItem start = new MenuItem("Start Recording"); - start.setOnAction((e) -> startStopAction(getSelectedThumbCells(cell), true)); + start.setOnAction(e -> startStopAction(getSelectedThumbCells(cell), true)); MenuItem stop = new MenuItem("Stop Recording"); - stop.setOnAction((e) -> startStopAction(getSelectedThumbCells(cell), false)); + stop.setOnAction(e -> startStopAction(getSelectedThumbCells(cell), false)); MenuItem startStop = recorder.isTracked(cell.getModel()) ? stop : start; MenuItem pause = new MenuItem("Pause Recording"); - pause.setOnAction((e) -> pauseResumeAction(getSelectedThumbCells(cell), true)); + pause.setOnAction(e -> pauseResumeAction(getSelectedThumbCells(cell), true)); MenuItem resume = new MenuItem("Resume Recording"); - resume.setOnAction((e) -> pauseResumeAction(getSelectedThumbCells(cell), false)); + resume.setOnAction(e -> pauseResumeAction(getSelectedThumbCells(cell), false)); MenuItem pauseResume = recorder.isSuspended(cell.getModel()) ? resume : pause; MenuItem follow = new MenuItem("Follow"); - follow.setOnAction((e) -> follow(getSelectedThumbCells(cell), true)); + follow.setOnAction(e -> follow(getSelectedThumbCells(cell), true)); MenuItem unfollow = new MenuItem("Unfollow"); - unfollow.setOnAction((e) -> follow(getSelectedThumbCells(cell), false)); - - MenuItem copyUrl = new MenuItem("Copy URL"); - copyUrl.setOnAction((e) -> { - final Clipboard clipboard = Clipboard.getSystemClipboard(); - final ClipboardContent content = new ClipboardContent(); - content.putString(cell.getModel().getUrl()); - clipboard.setContent(content); - }); + unfollow.setOnAction(e -> follow(getSelectedThumbCells(cell), false)); MenuItem ignore = new MenuItem("Ignore"); - ignore.setOnAction((e) -> ignore(getSelectedThumbCells(cell))); + ignore.setOnAction(e -> ignore(getSelectedThumbCells(cell))); - MenuItem sendTip = new MenuItem("Send Tip"); - sendTip.setOnAction((e) -> { - TipDialog tipDialog = new TipDialog(getTabPane().getScene(), site, cell.getModel()); - tipDialog.showAndWait(); - String tipText = tipDialog.getResult(); - if(tipText != null) { - DecimalFormat df = new DecimalFormat("0.##"); - try { - Number tokens = df.parse(tipText); - SiteUiFactory.getUi(site).login(); - cell.getModel().receiveTip(tokens.doubleValue()); - Map event = new HashMap<>(); - event.put("event", "tokens.sent"); - event.put("amount", tokens.doubleValue()); - EventBusHolder.BUS.post(event); - } catch (IOException ex) { - LOG.error("An error occured while sending tip", ex); - showError("Couldn't send tip", "An error occured while sending tip:", ex); - } catch (Exception ex) { - showError("Couldn't send tip", "You entered an invalid amount of tokens", ex); - } - } - }); - sendTip.setDisable(!site.credentialsAvailable()); + MenuItem copyUrl = createCopyUrlMenuItem(cell); + MenuItem sendTip = createTipMenuItem(cell); - // check, if other cells are selected, too. in that case, we have to disable menu item, which make sense only for - // single selections. but only do that, if the popup has been triggered on a selected cell. otherwise remove the - // selection and show the normal menu - if (selectedThumbCells.size() > 1 || selectedThumbCells.size() == 1 && selectedThumbCells.get(0) != cell) { - if(cell.isSelected()) { - if(Config.getInstance().getSettings().singlePlayer) { - openInPlayer.setDisable(true); - } - copyUrl.setDisable(true); - sendTip.setDisable(true); - } else { - removeSelection(); - } - } + configureItemsForSelection(cell, openInPlayer, copyUrl, sendTip); ContextMenu contextMenu = new ContextMenu(); contextMenu.setAutoHide(true); @@ -542,6 +474,63 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { return contextMenu; } + /* check, if other cells are selected, too. in that case, we have to disable menu items, which make sense only for + * single selections. but only do that, if the popup has been triggered on a selected cell. otherwise remove the + * selection and show the normal menu + */ + private void configureItemsForSelection(ThumbCell cell, MenuItem openInPlayer, MenuItem copyUrl, MenuItem sendTip) { + if (selectedThumbCells.size() > 1 || selectedThumbCells.size() == 1 && selectedThumbCells.get(0) != cell) { + if(cell.isSelected()) { + if(Config.getInstance().getSettings().singlePlayer) { + openInPlayer.setDisable(true); + } + copyUrl.setDisable(true); + sendTip.setDisable(true); + } else { + removeSelection(); + } + } + } + + private MenuItem createCopyUrlMenuItem(ThumbCell cell) { + MenuItem copyUrl = new MenuItem("Copy URL"); + copyUrl.setOnAction(e -> { + final Clipboard clipboard = Clipboard.getSystemClipboard(); + final ClipboardContent content = new ClipboardContent(); + content.putString(cell.getModel().getUrl()); + clipboard.setContent(content); + }); + return copyUrl; + } + + private MenuItem createTipMenuItem(ThumbCell cell) { + MenuItem sendTip = new MenuItem("Send Tip"); + sendTip.setOnAction(e -> { + TipDialog tipDialog = new TipDialog(getTabPane().getScene(), site, cell.getModel()); + tipDialog.showAndWait(); + String tipText = tipDialog.getResult(); + if(tipText != null) { + DecimalFormat df = new DecimalFormat("0.##"); + try { + Number tokens = df.parse(tipText); + SiteUiFactory.getUi(site).login(); + cell.getModel().receiveTip(tokens.doubleValue()); + Map event = new HashMap<>(); + event.put("event", "tokens.sent"); + event.put("amount", tokens.doubleValue()); + EventBusHolder.BUS.post(event); + } catch (IOException ex) { + LOG.error("An error occured while sending tip", ex); + showError("Couldn't send tip", "An error occured while sending tip:", ex); + } catch (Exception ex) { + showError("Couldn't send tip", "You entered an invalid amount of tokens", ex); + } + } + }); + sendTip.setDisable(!site.credentialsAvailable()); + return sendTip; + } + private List getSelectedThumbCells(ThumbCell cell) { if(selectedThumbCells.isEmpty()) { return Collections.singletonList(cell); @@ -552,8 +541,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { protected void follow(List selection, boolean follow) { for (ThumbCell thumbCell : selection) { - thumbCell.follow(follow).thenAccept((success) -> { - if(follow && success) { + thumbCell.follow(follow).thenAccept(success -> { + if(follow && success.booleanValue()) { showAddToFollowedAnimation(thumbCell); } }); @@ -600,9 +589,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { scale.setToY(0.1); ParallelTransition pt = new ParallelTransition(translate, scale); pt.play(); - pt.setOnFinished((evt) -> { - root.getChildren().remove(iv); - }); + pt.setOnFinished(evt -> root.getChildren().remove(iv)); String normalStyle = followedTab.getStyle(); Color normal = Color.web("#f4f4f4"); @@ -631,7 +618,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { }; blink.setCycleCount(6); blink.setAutoReverse(true); - blink.setOnFinished((evt) -> followedTab.setStyle(normalStyle)); + blink.setOnFinished(evt -> followedTab.setStyle(normalStyle)); blink.play(); }); } @@ -670,20 +657,17 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } } - private EventHandler mouseClickListener = new EventHandler() { - @Override - public void handle(MouseEvent e) { - ThumbCell cell = (ThumbCell) e.getSource(); - if (e.getButton() == MouseButton.PRIMARY && e.getClickCount() == 2) { - cell.setSelected(false); - cell.startPlayer(); - } else if (e.getButton() == MouseButton.PRIMARY && e.isControlDown()) { - if(popup == null) { - cell.setSelected(!cell.isSelected()); - } - } else if (e.getButton() == MouseButton.PRIMARY) { - removeSelection(); + private EventHandler mouseClickListener = e -> { + ThumbCell cell = (ThumbCell) e.getSource(); + if (e.getButton() == MouseButton.PRIMARY && e.getClickCount() == 2) { + cell.setSelected(false); + cell.startPlayer(); + } else if (e.getButton() == MouseButton.PRIMARY && e.isControlDown()) { + if(popup == null) { + cell.setSelected(!cell.isSelected()); } + } else if (e.getButton() == MouseButton.PRIMARY) { + removeSelection(); } }; @@ -709,16 +693,13 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } private void filter() { - Collections.sort(filteredThumbCells, new Comparator() { - @Override - public int compare(Node o1, Node o2) { - ThumbCell c1 = (ThumbCell) o1; - ThumbCell c2 = (ThumbCell) o2; + Collections.sort(filteredThumbCells, (o1, o2) -> { + ThumbCell c1 = o1; + ThumbCell c2 = o2; - if(c1.getIndex() < c2.getIndex()) return -1; - if(c1.getIndex() > c2.getIndex()) return 1; - return c1.getModel().getName().compareTo(c2.getModel().getName()); - } + if(c1.getIndex() < c2.getIndex()) return -1; + if(c1.getIndex() > c2.getIndex()) return 1; + return c1.getModel().getName().compareTo(c2.getModel().getName()); }); if (filter == null || filter.isEmpty()) { @@ -753,9 +734,9 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } private void moveActiveRecordingsToFront() { - List thumbsToMove = new ArrayList<>(); + List thumbsToMove = new ArrayList<>(); ObservableList thumbs = grid.getChildren(); - for (int i = thumbs.size()-1; i > 0; i--) { + for (int i = thumbs.size()-1; i >= 0; i--) { ThumbCell thumb = (ThumbCell) thumbs.get(i); if(recorder.isTracked(thumb.getModel())) { thumbs.remove(i); @@ -780,33 +761,9 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { private boolean matches(Model m, String filter) { try { String[] tokens = filter.split(" "); - StringBuilder searchTextBuilder = new StringBuilder(m.getName()); - searchTextBuilder.append(' '); - searchTextBuilder.append(m.getDisplayName()); - searchTextBuilder.append(' '); - for (String tag : m.getTags()) { - searchTextBuilder.append(tag).append(' '); - } - int[] resolution = m.getStreamResolution(true); - searchTextBuilder.append(resolution[1]); - String searchText = searchTextBuilder.toString().trim(); boolean tokensMissing = false; for (String token : tokens) { - if(token.matches(">\\d+")) { - int res = Integer.parseInt(token.substring(1)); - if(resolution[1] < res) { - tokensMissing = true; - } - } else if(token.matches("<\\d+")) { - int res = Integer.parseInt(token.substring(1)); - if(resolution[1] > res) { - tokensMissing = true; - } - } else if(token.equals("public")) { - if(!m.getOnlineState(true).toString().equals(token)) { - tokensMissing = true; - } - } else if(!searchText.toLowerCase().contains(token.toLowerCase())) { + if(!modelPropertiesMatchToken(token, m)) { tokensMissing = true; } } @@ -817,6 +774,43 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } } + private boolean modelPropertiesMatchToken(String token, Model m) throws IOException, ExecutionException { + int[] resolution = m.getStreamResolution(true); + String searchText = createSearchText(m); + boolean tokensMissing = false; + if(token.matches(">\\d+")) { + int res = Integer.parseInt(token.substring(1)); + if(resolution[1] < res) { + tokensMissing = true; + } + } else if(token.matches("<\\d+")) { + int res = Integer.parseInt(token.substring(1)); + if(resolution[1] > res) { + tokensMissing = true; + } + } else if(token.equals("public")) { + if(!m.getOnlineState(true).toString().equals(token)) { + tokensMissing = true; + } + } else if(!searchText.toLowerCase().contains(token.toLowerCase())) { + tokensMissing = true; + } + return tokensMissing; + } + + private String createSearchText(Model m) throws ExecutionException { + StringBuilder searchTextBuilder = new StringBuilder(m.getName()); + searchTextBuilder.append(' '); + searchTextBuilder.append(m.getDisplayName()); + searchTextBuilder.append(' '); + for (String tag : m.getTags()) { + searchTextBuilder.append(tag).append(' '); + } + int[] resolution = m.getStreamResolution(true); + searchTextBuilder.append(resolution[1]); + return searchTextBuilder.toString().trim(); + } + public void setRecorder(Recorder recorder) { this.recorder = recorder; popoverTreelist.setRecorder(recorder); @@ -847,7 +841,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { } private void removeSelection() { - while(selectedThumbCells.size() > 0) { + while (!selectedThumbCells.isEmpty()) { selectedThumbCells.get(0).setSelected(false); } } diff --git a/client/src/main/java/ctbrec/ui/ThumbOverviewTabSearchTask.java b/client/src/main/java/ctbrec/ui/ThumbOverviewTabSearchTask.java new file mode 100644 index 00000000..1daa7ded --- /dev/null +++ b/client/src/main/java/ctbrec/ui/ThumbOverviewTabSearchTask.java @@ -0,0 +1,71 @@ +package ctbrec.ui; + +import static ctbrec.ui.controls.Dialogs.*; + +import java.io.IOException; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ctbrec.Model; +import ctbrec.sites.Site; +import ctbrec.ui.controls.SearchPopover; +import ctbrec.ui.controls.SearchPopoverTreeList; +import javafx.application.Platform; +import javafx.concurrent.Task; + +public class ThumbOverviewTabSearchTask extends Task> { + + private static final Logger LOG = LoggerFactory.getLogger(ThumbOverviewTabSearchTask.class); + + private Site site; + private SearchPopover popover; + private SearchPopoverTreeList popoverTreelist; + private String query; + + public ThumbOverviewTabSearchTask(Site site, SearchPopover popover, SearchPopoverTreeList popoverTreelist, String query) { + this.site = site; + this.popover = popover; + this.popoverTreelist = popoverTreelist; + this.query = query; + } + + @Override + protected List call() throws Exception { + if(site.searchRequiresLogin()) { + boolean loggedin = false; + try { + loggedin = SiteUiFactory.getUi(site).login(); + } catch (IOException e) { + loggedin = false; + } + if(!loggedin) { + showError("Login failed", "Search won't work correctly without login", null); + } + } + return site.search(query); + } + + @Override + protected void failed() { + LOG.error("Search failed", getException()); + } + + @Override + protected void succeeded() { + Platform.runLater(() -> { + List models = getValue(); + LOG.debug("Search result {} {}", isCancelled(), models); + if(models.isEmpty()) { + popover.hide(); + } else { + popoverTreelist.getItems().clear(); + for (Model model : getValue()) { + popoverTreelist.getItems().add(model); + } + popover.show(); + } + }); + } +}