-
Notifications
You must be signed in to change notification settings - Fork 2
/
DlgOpenProject.java
163 lines (149 loc) · 5.96 KB
/
DlgOpenProject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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 tappas;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Window;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
/**
*
* @author Hector del Risco - [email protected] & Pedro Salguero - [email protected]
*/
public class DlgOpenProject extends DlgBase {
ListView lvProjects;
ArrayList<Project.ProjectDef> lstProjects = new ArrayList<>();
public DlgOpenProject(Project project, Window window) {
super(project, window);
}
public Params showAndWait(Params dlftParams) {
if(createDialog("OpenProject.fxml", "Open Project", true, null)) {
// get control objects
lvProjects = (ListView) scene.lookup("#lstProjects");
// populate projects list
lstProjects = app.getProjectsList();
for(Project.ProjectDef def : lstProjects)
lvProjects.getItems().add(def.name);
if(!lvProjects.getItems().isEmpty())
lvProjects.getSelectionModel().select(0);
lvProjects.setOnMouseClicked((MouseEvent event) -> {
if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() > 1){
ObservableList<ButtonType> lst = dialog.getDialogPane().getButtonTypes();
for(ButtonType bt : lst) {
if(bt.getText().equals("OK")) {
if(dialog.getDialogPane().lookupButton(bt) != null)
((Button) dialog.getDialogPane().lookupButton(bt)).fire();
}
}
}
});
// setup dialog event handlers
dialog.setOnCloseRequest((DialogEvent event) -> {
if(dialog.getResult() != null && dialog.getResult().containsKey("ERRMSG")) {
showDlgMsg((String)dialog.getResult().get("ERRMSG"));
dialog.setResult(null);
event.consume();
}
});
dialog.setResultConverter((ButtonType b) -> {
HashMap<String, String> params = null;
System.out.println(b.getButtonData().toString());
if (b.getButtonData() == ButtonBar.ButtonData.OK_DONE)
params = validate(dialog);
return params;
});
// process dialog
Optional<HashMap> result = dialog.showAndWait();
if(result.isPresent())
return(new Params(result.get()));
}
return null;
}
//
// Dialog Validation
//
private HashMap<String, String> validate(Dialog dialog) {
HashMap<String, String> results = new HashMap<>();
// check project selection
String errmsg = "";
int pidx = lvProjects.getSelectionModel().getSelectedIndex();
if(pidx != -1) {
String id = "";
String name = (String) lvProjects.getSelectionModel().getSelectedItem();
results.put(Params.NAME_PARAM, name);
for(Project.ProjectDef def : lstProjects) {
if(name.equals(def.name)) {
id = def.id;
results.put(Params.ID_PARAM, id);
break;
}
}
if(id.isEmpty()) {
errmsg = "Internal error: Unable to get project's folder name.";
lvProjects.requestFocus();
}
}
else {
errmsg = "You must select a project from list.";
lvProjects.requestFocus();
}
if(!errmsg.isEmpty())
results.put("ERRMSG", errmsg);
return results;
}
//
// Data Classes
//
public static class Params extends DlgParams {
// minimum data version with which this code will work and current data version
// code will never work with a data version greater than the current data version
// the only reason to change the data version is because it is no longer compatible
public static final int MIN_DATA_VER = 1;
public static final int CUR_DATA_VER = 1;
public static final String NAME_PARAM = "name";
public static final String ID_PARAM = "id";
public static final String DATAVER_PARAM = "dataver";
String name = "";
String id = "";
int dataVersion;
public Params() {
this.name = "";
this.id = "";
this.dataVersion = CUR_DATA_VER;
}
public Params(HashMap<String, String> hmParams) {
this.name = hmParams.containsKey(NAME_PARAM)? hmParams.get(NAME_PARAM) : "";
this.id = hmParams.containsKey(ID_PARAM)? hmParams.get(ID_PARAM) : "";
this.dataVersion = hmParams.containsKey(DATAVER_PARAM)? Integer.parseInt(hmParams.get(DATAVER_PARAM)) : 0;
}
@Override
public HashMap<String, String> getParams() {
HashMap<String, String> hm = new HashMap<>();
hm.put(NAME_PARAM, name);
hm.put(ID_PARAM, id);
hm.put(DATAVER_PARAM, "" + dataVersion);
return hm;
}
// base class implements boolean save(String filepath)
//
// Static functions
//
public static Params load(String filepath) {
HashMap<String, String> params = new HashMap<>();
Utils.loadParams(params, filepath);
return (new Params(params));
}
public static boolean isValidData(int dv) {
boolean result = false;
if(dv >= MIN_DATA_VER && dv <= CUR_DATA_VER)
result = true;
return result;
}
}
}