-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix do drag-and-drop para ordenar lista
- Loading branch information
1 parent
e67853d
commit a038bed
Showing
5 changed files
with
124 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/us/guihouse/projector/other/DragSortListCell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/us/guihouse/projector/other/Identifiable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
213
src/main/java/us/guihouse/projector/other/ListCellX.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters