我正在尝试按照MVC进行测试项目,因此我的模型应该完全独立于我的视图,但是我不确定如何更新在后台线程中更新的Observable List(它正在给出有关通过FTP上传文件的字符串),以便消息在ListView中显示在UI上。
我正在使用JavaFX并试图使我的程序尽可能松散耦合。在目前这个时刻,视图包中的GUI取决于我的模型使用Platform.runLater(...)更新我的列表的事实 - 据我所知,我的模型应该完全独立于视图,并且应该'必须符合View的需求。
现在,以下代码实际上“按预期工作”,它只是没有正确建模,我不确定如何正确建模。一些初步的研究提出我可能不得不使用Observer和observable - 并且在中间有另一个类作为我的可观察列表 - 但我不确定如何设置它。
所以我有一个Observable列表,它在后台线程上更新:
private ObservableList<String> transferMessages;
public FTPUtil(String host, int port, String user, String pass) {
this.host = host;
this.port = port;
this.username = user;
this.password = pass;
transferMessages = FXCollections.observableArrayList();
connect();
}
public void upload(File src) {
System.out.println("Uploading: " + src.getName());
try {
if (src.isDirectory()) {
ftpClient.makeDirectory(src.getName());
ftpClient.changeWorkingDirectory(src.getName());
for (File file : src.listFiles()) {
upload(file);
}
ftpClient.changeToParentDirectory();
} else {
InputStream srcStream = null;
try {
addMessage("Uploading: " + src.getName());
srcStream = src.toURI().toURL().openStream();
ftpClient.storeFile(src.getName(), srcStream);
addMessage("Uploaded: " + src.getName() + " - Successfully.");
} catch (Exception ex) {
System.out.println(ex);
addMessage("Error Uploading: " + src.getName() + " - Speak to Administrator.");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addMessage(String message){
Platform.runLater(() -> transferMessages.add(0, message));
}
FTPUtil类是我的模型。
我还有一个Model Manager类,它控制着这个FTPUtil类:
public class ModelManager {
private ObservableList<String> fileAndFolderLocations;
private FTPUtil ftpUtil;
public ModelManager(String host, int port, String user, String pass) {
ftpUtil = new FTPUtil(host, port, user, pass);
fileAndFolderLocations = FXCollections.observableArrayList();
}
public boolean startBackup() {
Task task = new Task() {
@Override
protected Object call() throws Exception {
System.out.println("I started");
ftpUtil.clearMessages();
for(String location : fileAndFolderLocations){
File localDirPath = new File(location);
ftpUtil.upload(localDirPath);
}
return null;
}
};
new Thread(task).start();
return true;
}
public void addFileOrFolder(String fileOrFolder){
if(!fileAndFolderLocations.contains(fileOrFolder)){
fileAndFolderLocations.add(fileOrFolder);
}
}
public boolean removeFileOrFolder(String fileOrFolder){
return fileAndFolderLocations.remove(fileOrFolder);
}
public ObservableList<String> getFilesAndFoldersList() {
return fileAndFolderLocations;
}
public ObservableList<String> getMessages() {
return ftpUtil.getMessages();
}
}
最后是我的GUI:
public class BackupController {
private Main main;
private ModelManager mm;
@FXML
private ListView<String> messagesList;
@FXML
void forceBackup(ActionEvent event) {
mm.startBackup();
}
public void initController(Main main, ModelManager mm) {
this.main = main;
this.mm = mm;
messagesList.setItems(mm.getMessages());
}
}
答案 0 :(得分:3)
基本设置:
一个非常原始的片段来说明设置:
private Parent getContent() {
ModelManager manager = new ModelManager();
ObservableList<String> uploading = FXCollections.observableArrayList("one", "two", "three");
ObservableList<String> items = FXCollections.observableArrayList();
manager.getMessages().addListener((ListChangeListener) c -> {
while (c.next()) {
if (c.wasAdded()) {
Platform.runLater(() ->
items.addAll(c.getFrom(), c.getAddedSubList()));
}
if (c.wasRemoved()) {
Platform.runLater(() ->
items.removeAll(c.getRemoved()));
}
}
});
ListView<String> list = new ListView<>(items);
Button button = new Button("start");
button.setOnAction(ev -> {
uploading.stream().forEach(e -> manager.addFile(e));
manager.startBackup();
});
BorderPane pane = new BorderPane(list);
pane.setBottom(button);
return pane;
}
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(getContent());
stage.setScene(scene);
stage.show();
}
答案 1 :(得分:1)
您需要在列表周围包装一个包装,以将更改发布到正确的线程上。
ObservableList<String> source = FXCollections.observableArrayList();
ObservableList<String> items = new UiThreadList(source);
ListView<String> list = new ListView<>(items);
与
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.TransformationList;
class UiThreadList<T> extends TransformationList<T, T> {
public UiThreadList(ObservableList<? extends T> source) {
super(source);
}
@Override
protected void sourceChanged(ListChangeListener.Change<? extends T> change) {
Platform.runLater(() -> fireChange(change));
}
@Override
public int getSourceIndex(int index) {
return index;
}
@Override
public T get(int index) {
return getSource().get(index);
}
@Override
public int size() {
return getSource().size();
}
}
此解决方案的想法类似于上面的@kleopatra。