Skip to content

Commit f58b7da

Browse files
committed
Editable notice statuses was implemented
1 parent 51014b0 commit f58b7da

4 files changed

Lines changed: 123 additions & 9 deletions

File tree

src/com/temporaryteam/noticeditor/controller/NoticeSettingsController.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.temporaryteam.noticeditor.controller;
22

3+
import com.temporaryteam.noticeditor.model.NoticeStatus;
34
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
45
import java.net.URL;
56
import java.util.ResourceBundle;
@@ -41,16 +42,16 @@ public void setNoticeController(NoticeController noticeController) {
4142
*/
4243
@Override
4344
public void initialize(URL url, ResourceBundle rb) {
44-
choiceBoxNoticeStatus.setItems(FXCollections.observableArrayList(
45-
rb.getString("normal"), rb.getString("important")
46-
));
47-
choiceBoxNoticeStatus.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
48-
45+
NoticeStatus.add(rb.getString("normal"));
46+
NoticeStatus.add(rb.getString("important"));
47+
48+
choiceBoxNoticeStatus.setItems(NoticeStatus.asObservable());
49+
choiceBoxNoticeStatus.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
4950
@Override
50-
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
51+
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
5152
NoticeTreeItem currentNotice = noticeController.getCurrentNotice();
52-
if (currentNotice != null && currentNotice.isLeaf()) {
53-
currentNotice.setStatus(newValue.intValue());
53+
if (null != currentNotice && newValue != null && currentNotice.isLeaf()) {
54+
currentNotice.setStatus(NoticeStatus.getStatusCode(newValue));
5455
}
5556
}
5657
});
@@ -62,7 +63,8 @@ public void open(NoticeTreeItem item) {
6263
choiceBoxNoticeStatus.getSelectionModel().clearSelection();
6364
settingsPane.setDisable(true);
6465
} else {
65-
choiceBoxNoticeStatus.getSelectionModel().select(item.getStatus());
66+
choiceBoxNoticeStatus.setItems(NoticeStatus.asObservable());
67+
choiceBoxNoticeStatus.getSelectionModel().select(NoticeStatus.getStatusName(item.getStatus()));
6668
settingsPane.setDisable(false);
6769
}
6870
}

src/com/temporaryteam/noticeditor/io/JsonFields.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public class JsonFields {
1010
static final String KEY_STATUS = "status";
1111
static final String KEY_CHILDREN = "children";
1212
static final String KEY_CONTENT = "content";
13+
14+
static final String KEY_STATUSINFO = "statusInfo";
1315
}

src/com/temporaryteam/noticeditor/io/JsonFormat.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import static com.temporaryteam.noticeditor.io.JsonFields.*;
44
import com.temporaryteam.noticeditor.model.NoticeItem;
5+
import com.temporaryteam.noticeditor.model.NoticeStatus;
56
import com.temporaryteam.noticeditor.model.NoticeTree;
67
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
78
import java.io.File;
89
import java.io.IOException;
10+
import java.util.Iterator;
911
import javafx.scene.control.TreeItem;
1012
import org.json.JSONArray;
1113
import org.json.JSONException;
@@ -29,6 +31,19 @@ private JsonFormat(File file) {
2931

3032
public NoticeTree importDocument() throws IOException, JSONException {
3133
JSONObject json = new JSONObject(IOUtil.readContent(file));
34+
35+
if (json.has(KEY_STATUSINFO)) {
36+
JSONObject status = json.getJSONObject(KEY_STATUSINFO);
37+
Iterator it = status.keys();
38+
if (it.hasNext()) {
39+
NoticeStatus.clear();
40+
}
41+
while (it.hasNext()) {
42+
String key = it.next().toString();
43+
NoticeStatus.add(key, status.getInt(key));
44+
}
45+
}
46+
3247
return new NoticeTree(jsonToTree(json));
3348
}
3449

@@ -52,6 +67,7 @@ public void export(NoticeTree tree) throws JSONException, IOException {
5267
public JSONObject export(NoticeTreeItem root) throws JSONException {
5368
JSONObject json = new JSONObject();
5469
treeToJson(root, json);
70+
json.put("statusInfo", NoticeStatus.asObservableMap());
5571
return json;
5672
}
5773

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.temporaryteam.noticeditor.model;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
import javafx.collections.FXCollections;
8+
import javafx.collections.ObservableList;
9+
import javafx.collections.ObservableMap;
10+
11+
/**
12+
* Collection of notice statuses
13+
* @author Maximillian M.
14+
*/
15+
public class NoticeStatus {
16+
private static final HashMap<String, Integer> map;
17+
private static int lastIndex;
18+
19+
static {
20+
map = new HashMap<>();
21+
lastIndex = -1;
22+
}
23+
24+
/**
25+
* Associates the specified status with next status code
26+
* @param status Status name
27+
*/
28+
public static void add(String status) {
29+
lastIndex++;
30+
map.put(status, lastIndex);
31+
}
32+
33+
/**
34+
* Associates the specified status with the specified status code
35+
* @param status Status name
36+
* @param code Status code
37+
*/
38+
public static void add(String status, int code) {
39+
if (lastIndex < code) {
40+
lastIndex = code;
41+
}
42+
map.put(status, code);
43+
}
44+
45+
/**
46+
* Removes the specified status
47+
* @param status
48+
*/
49+
public static void remove(String status) {
50+
map.remove(status);
51+
}
52+
53+
/**
54+
* Return status code by status name
55+
* @param status Status name
56+
* @return Status code if status exists, otherwise returns 0
57+
*/
58+
public static int getStatusCode(String status) {
59+
return map.containsKey(status) ? map.get(status) : 0;
60+
}
61+
62+
/**
63+
* Returns status name by status code
64+
* @param code Status code
65+
* @return Status name if status exists, otherwise returns empty string
66+
*/
67+
public static String getStatusName(int code) {
68+
List<String> foo = map.entrySet().stream()
69+
.filter(entry -> entry.getValue() == code)
70+
.map(Map.Entry::getKey)
71+
.collect(Collectors.toList());
72+
if (foo.isEmpty()) {
73+
// TODO: May be Status[0]?
74+
return "";
75+
} else {
76+
return foo.get(0);
77+
}
78+
}
79+
80+
/**
81+
* Clears collection of notice statuses
82+
*/
83+
public static void clear() {
84+
map.clear();
85+
}
86+
87+
public static ObservableList<String> asObservable() {
88+
return FXCollections.observableArrayList(map.keySet());
89+
}
90+
91+
public static ObservableMap<String, Integer> asObservableMap() {
92+
return FXCollections.observableMap(map);
93+
}
94+
}

0 commit comments

Comments
 (0)