/* 
 * The MIT License
 *
 * Copyright 2015 Simon Hardijanto.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package io.github.metaluna.ck2edit.gui.mod;

import io.github.metaluna.ck2edit.business.mod.ModFile;
import java.nio.file.Path;
import java.util.function.Consumer;
import javafx.event.ActionEvent;
import javafx.scene.control.TreeCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class ModTreeCell extends TreeCell<Object> {

  public ModTreeCell(Consumer<ModFile> openHandler) {
    this.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
      if (event.getClickCount() == 2) {
        open(openHandler);
      }
    });

  }

  @Override
  protected void updateItem(Object item, boolean empty) {
    super.updateItem(item, empty);
    LOG.entry(item, empty);
    setText(item == null ? "" : item.toString());
    LOG.exit();
  }

  // ---vvv--- PRIVATE ---vvv---
  private static final Logger LOG = LogManager.getFormatterLogger();

  private void open(Consumer<ModFile> openHandler) {
    LOG.entry(openHandler);
    if (this.getTreeItem() != null && this.getTreeItem().getValue() instanceof ModFile) {
      openHandler.accept((ModFile) this.getTreeItem().getValue());
    }
    LOG.exit();
  }
}

@@ -1251,21 +1251,20 @@
 ava.
-nio.file.Path
+util.Objects
 ;%0Aim
@@ -1315,25 +1315,33 @@
 afx.
-event.ActionEvent
+scene.control.ContextMenu
 ;%0Aim
@@ -1370,51 +1370,16 @@
 rol.
-TreeCell;%0Aimport javafx.scene.input.KeyCode
+MenuItem
 ;%0Aim
@@ -1400,22 +1400,24 @@
 ene.
-input.KeyEvent
+control.TreeCell
 ;%0Aim
@@ -1633,24 +1633,112 @@
 nHandler) %7B%0A
+    LOG.entry(openHandler);%0A    this.openHandler = Objects.requireNonNull(openHandler);%0A
     this.add
@@ -1840,27 +1840,16 @@
    open(
-openHandler
 );%0A     
@@ -1859,16 +1859,84 @@
     %7D);%0A
+    %0A    this.fileContextMenu = createContextMenu();%0A    LOG.exit();
 %0A  %7D%0A%0A  
@@ -2074,53 +2074,212 @@
     
-setText(item == null ? %22%22 : item.toString());
+if (empty %7C%7C item == null) %7B%0A      setText(null);%0A      setGraphic(null);%0A    %7D else %7B%0A      setText(item.toString());%0A      if (isFile(item)) %7B%0A        setContextMenu(this.fileContextMenu);%0A      %7D%0A    %7D
 %0A   
@@ -2398,17 +2398,16 @@
 gger();%0A
-%0A
   privat
@@ -2412,18 +2412,14 @@
 ate 
-void open(
+final 
 Cons
@@ -2435,32 +2435,100 @@
 ile%3E openHandler
+;%0A  private final ContextMenu fileContextMenu;%0A%0A  private void open(
 ) %7B%0A    LOG.entr
@@ -2525,35 +2525,24 @@
   LOG.entry(
-openHandler
 );%0A    if (t
@@ -2540,21 +2540,16 @@
     if (
-this.
 getTreeI
@@ -2565,21 +2565,23 @@
 null && 
-this.
+isFile(
 getTreeI
@@ -2600,27 +2600,9 @@
 ue()
- instanceof ModFile
+)
 ) %7B%0A
@@ -2603,24 +2603,29 @@
 ))) %7B%0A      
+this.
 openHandler.
@@ -2699,10 +2699,383 @@
 ();%0A  %7D%0A
+%0A  private boolean isFile(Object item) %7B%0A    return item instanceof ModFile;%0A  %7D%0A%0A  private ContextMenu createContextMenu() %7B%0A    LOG.entry();%0A    final ContextMenu result = new ContextMenu();%0A%0A    MenuItem openMenuItem = new MenuItem(%22Open%22);%0A    openMenuItem.setOnAction(e -%3E open());%0A    %0A    result.getItems().add(openMenuItem);%0A    %0A    return LOG.exit(result);%0A  %7D;%0A%0A
 %7D%0A

add context menu to mod file entries of the mod file tree. fixes #30