package me.coley.recaf.ui.controls;

import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import me.coley.recaf.control.gui.GuiController;
import me.coley.recaf.ui.controls.tree.*;
import me.coley.recaf.util.UiUtil;
import me.coley.recaf.workspace.*;

import java.util.*;

// TODO: Account for user adds/removes a library to workspace

/**
 * Navigator for a workspace.
 *
 * @author Matt
 */
public class WorkspaceNavigator extends BorderPane {
	private final GuiController controller;
	private final Map<JavaResource, ResourceTree> resourceToTree = new HashMap<>();

	/**
	 * @param controller
	 * 		Controller.
	 */
	public WorkspaceNavigator(GuiController controller) {
		this.controller = controller;
		// Resource switcher
		ComboBox<JavaResource> comboResources = new ComboBox<>();
		comboResources.getItems().addAll(resources());
		comboResources.getSelectionModel().selectedItemProperty().addListener((v, o, n) -> setCurrent(n));
		comboResources.getSelectionModel().select(0);
		comboResources.setMaxWidth(Double.MAX_VALUE);
		comboResources.setCellFactory(e -> new ResourceSelectionCell());
		BorderPane.setAlignment(comboResources, Pos.CENTER);
 		setTop(comboResources);
	}

	private void setCurrent(JavaResource resource) {
		setCenter(resourceToTree.computeIfAbsent(resource, (k) -> new ResourceTree(controller, k)));
	}

	private List<JavaResource> resources() {
		Workspace workspace= controller.getWorkspace();
		if (workspace == null) return Collections.emptyList();
		List<JavaResource> list = new ArrayList<>(workspace.getLibraries());
		list.add(workspace.getPrimary());
		return list;
	}

	private class ResourceSelectionCell extends ComboBoxListCell<JavaResource> {
		@Override
		public void updateItem(JavaResource item, boolean empty) {
			super.updateItem(item, empty);
			if(!empty) {
				HBox g = new HBox();
				if(item != null) {
					String t = item.toString();
					// Add icon for resource types
					g.getChildren().add(new IconView(UiUtil.getResourceIcon(item)));
					// Indicate which resource is the primary resource
					if(item == controller.getWorkspace().getPrimary()) {
						Label lbl = new Label(" [Primary]");
						lbl.getStyleClass().add("bold");
						g.getChildren().add(lbl);
					}
					setText(t);
				}
				setGraphic(g);
			} else {
				setGraphic(null);
				setText(null);
			}
		}
	}
}

@@ -1575,17 +1575,20 @@
 == null)
- 
+%0A%09%09%09
 return C
@@ -1655,25 +1655,53 @@
 yList%3C%3E(
-w
+);%0A%09%09list.add(controller.getW
 orkspace
 .getLibr
@@ -1696,21 +1696,21 @@
 pace
+()
 .get
-Libraries
+Primary
 ());
@@ -1720,26 +1720,43 @@
 list.add
-(w
+All(controller.getW
 orkspace
 .getPrim
@@ -1747,27 +1747,31 @@
 orkspace
+()
 .get
-Primary
+Libraries
 ());%0A%09%09r

Fix order regression from pr