Skip to content

Commit

Permalink
Fix do drag-and-drop para ordenar lista
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-otran committed Jul 22, 2017
1 parent e67853d commit a038bed
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
import us.guihouse.projector.other.ListCellX;
import us.guihouse.projector.other.DragSortListCell;
import us.guihouse.projector.other.YouTubeVideoResolve;
import us.guihouse.projector.projection.TextWrapperFactoryChangeListener;
import us.guihouse.projector.projection.text.TextWrapper;
Expand Down Expand Up @@ -184,19 +184,17 @@ private void initializeProjectionList() {
@Override
public ListCell<ProjectionItemSubScene> call(ListView<ProjectionItemSubScene> arg0) {
//My cell is on my way to call
ListCellX<ProjectionItemSubScene> cell = new ListCellX<ProjectionItemSubScene>() {
DragSortListCell<ProjectionItemSubScene> cell = new DragSortListCell<ProjectionItemSubScene>() {
@Override
public void updateItem(ProjectionItemSubScene item, boolean empty) {
super.updateItem(item, empty);

if (item != null) {
//finally every thing is just setup
setText(item.getTitle());
}
}
};
//this is my chemical solution
//Need to call on every cell for making things work
cell.init(projectionListView.getItems());

//Take my cell
return cell;
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/us/guihouse/projector/other/DragSortListCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Taked from https://gist.github.com/jewelsea/7821196
*/
package us.guihouse.projector.other;

import java.util.ArrayList;
import java.util.List;
import javafx.collections.ObservableList;
import javafx.scene.control.ListCell;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;


public class DragSortListCell<X extends Identifiable> extends ListCell<X> {

public DragSortListCell() {
ListCell thisCell = this;

setOnDragDetected(event -> {
if (getItem() == null) {
return;
}

Dragboard dragboard = startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(getItem().getIdentity());

dragboard.setDragView(DragSortListCell.this.snapshot(null, null));
dragboard.setContent(content);

event.consume();
});

setOnDragOver(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.MOVE);
}

event.consume();
});

setOnDragEntered(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
setOpacity(0.3);
}
});

setOnDragExited(event -> {
if (event.getGestureSource() != thisCell &&
event.getDragboard().hasString()) {
setOpacity(1);
}
});

setOnDragDropped(event -> {
if (getItem() == null) {
return;
}

Dragboard db = event.getDragboard();
boolean success = false;

if (db.hasString()) {
ObservableList<X> items = getListView().getItems();
X item = findItemByIdentity(db.getString());
int draggedIdx = items.indexOf(item);
int thisIdx = items.indexOf(getItem());

items.set(draggedIdx, getItem());
items.set(thisIdx, item);

List<X> itemscopy = new ArrayList<>(getListView().getItems());
getListView().getItems().setAll(itemscopy);

success = true;
}
event.setDropCompleted(success);

event.consume();
});

setOnDragDone(DragEvent::consume);
}

private X findItemByIdentity(String id) {
return getListView()
.getItems()
.stream()
.filter(i -> i.getIdentity().equals(id))
.findAny()
.get();
}
}
14 changes: 14 additions & 0 deletions src/main/java/us/guihouse/projector/other/Identifiable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package us.guihouse.projector.other;

/**
*
* @author guilherme
*/
public interface Identifiable {
public String getIdentity();
}
213 changes: 0 additions & 213 deletions src/main/java/us/guihouse/projector/other/ListCellX.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
*/
package us.guihouse.projector.scenes;

import java.util.UUID;
import javafx.scene.Parent;
import javafx.scene.SubScene;
import us.guihouse.projector.forms.controllers.ControllerObserver;
import us.guihouse.projector.forms.controllers.ProjectionController;
import us.guihouse.projector.other.Identifiable;
import us.guihouse.projector.projection.ProjectionManager;

/**
*
* @author guilherme
*/
public abstract class ProjectionItemSubScene extends SubScene implements ControllerObserver {
public abstract class ProjectionItemSubScene extends SubScene implements ControllerObserver, Identifiable {

private final String identity;
private ProjectionController controller;
private SceneObserver observer;
private String title;

protected ProjectionItemSubScene(Parent root, String title, double width, double height) {
super(root, width, height);
this.title = title;
this.identity = UUID.randomUUID().toString();
}

public SceneObserver getObserver() {
Expand Down Expand Up @@ -69,4 +73,9 @@ public void onEscapeKeyPressed() {
controller.onEscapeKeyPressed();
}
}

@Override
public String getIdentity() {
return identity;
}
}

0 comments on commit a038bed

Please sign in to comment.