package ru.testing.client.message;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import ru.testing.client.gui.ContextMenuItems;

/**
 * Output message cell factory
 */
public class OutputMessageCell extends ListCell<OutputMessage> {

    private static final String SEND_MESSAGE_CSS = "message_send";
    private ImageView imageView = new ImageView();
    private ListView listView;

    public OutputMessageCell(ListView listView) {
        this.listView = listView;
    }

    @Override
    protected void updateItem(OutputMessage message, boolean empty) {
        super.updateItem(message, empty);
        if (message != null) {
            setText(String.format("%-12s %s", message.getFormattedTime(), message.getMessage()));
            Image image;
            if (message.getMessageType() == MessageType.SEND) {
                getStyleClass().add(SEND_MESSAGE_CSS);
                image = new Image(getClass().getResource("/images/arrow-up.png").toExternalForm(), true);
            } else {
                getStyleClass().removeAll(SEND_MESSAGE_CSS);
                image = new Image(getClass().getResource("/images/arrow-down.png").toExternalForm(), true);
            }
            imageView.setImage(image);
            setGraphic(imageView);
            setContextMenu(getOutputContextMenu(this));
        } else {
            setText(null);
            setGraphic(null);
            getStyleClass().removeAll(SEND_MESSAGE_CSS);
        }
    }



    /**
     * Context menu for history pop over
     * @param cell ListCell string
     * @return ContextMenu
     */
    private ContextMenu getOutputContextMenu(ListCell<OutputMessage> cell) {
        ContextMenu contextMenu = new ContextMenu();
        ContextMenuItems m = new ContextMenuItems();
        contextMenu.getItems().addAll(
                m.getCopyCellAll(cell),
                m.getCopyCellMessage(cell),
                m.getCopyCellTime(cell),
                m.clearListView(listView)
        );
        return contextMenu;
    }
}

@@ -881,19 +881,28 @@
     
-Image image
+String imageUrl = %22%22
 ;%0A  
@@ -1043,91 +1043,36 @@
 mage
- = new Image(getClass().getResource(%22/images/arrow-up.png%22).toExternalForm(), true)
+Url = %22/images/arrow-up.png%22
 ;%0A  
@@ -1176,145 +1176,152 @@
 mage
- = new Image(getClass().getResource(%22/images/arrow-down.png%22).toExternalForm(), true);%0A            %7D%0A            imageView.setImage(image
+Url = %22/images/arrow-down.png%22;%0A            %7D%0A            imageView.setImage(new Image(getClass().getResource(imageUrl).toExternalForm(), false)
 );%0A 

fix display image in output list view