Skip to content

Commit 1f561b9

Browse files
committed
Some epic refactoring
1 parent b5dbee2 commit 1f561b9

5 files changed

Lines changed: 129 additions & 126 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private void handleContextMenu(ActionEvent event) {
221221
private void handleNew(ActionEvent event) {
222222
rebuildTree(resources.getString("help"));
223223
fileSaved = null;
224-
NoticeStatus.load();
224+
NoticeStatusList.restore();
225225
}
226226

227227
@FXML

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

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

33
import com.temporaryteam.noticeditor.model.NoticeStatus;
4+
import com.temporaryteam.noticeditor.model.NoticeStatusList;
45
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
56
import java.net.URL;
67
import java.util.ResourceBundle;
@@ -29,7 +30,7 @@ public class NoticeSettingsController implements Initializable {
2930
@FXML
3031
private Button btnSelectFile;
3132
@FXML
32-
private ChoiceBox<String> choiceBoxNoticeStatus;
33+
private ChoiceBox<NoticeStatus> choiceBoxNoticeStatus;
3334

3435
private NoticeController noticeController;
3536

@@ -42,33 +43,33 @@ public void setNoticeController(NoticeController noticeController) {
4243
*/
4344
@Override
4445
public void initialize(URL url, ResourceBundle rb) {
45-
NoticeStatus.add(rb.getString("normal"));
46-
NoticeStatus.add(rb.getString("important"));
47-
NoticeStatus.save();
46+
NoticeStatusList.add(rb.getString("normal"));
47+
NoticeStatusList.add(rb.getString("important"));
48+
NoticeStatusList.save();
49+
updateStatuses();
4850

49-
choiceBoxNoticeStatus.setItems(NoticeStatus.asObservable());
50-
choiceBoxNoticeStatus.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
51-
@Override
52-
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
51+
choiceBoxNoticeStatus.getSelectionModel().selectedItemProperty().addListener(
52+
(ObservableValue<? extends NoticeStatus> observable, NoticeStatus oldValue, NoticeStatus newValue) -> {
5353
NoticeTreeItem currentNotice = noticeController.getCurrentNotice();
54-
if (null != currentNotice && newValue != null && currentNotice.isLeaf()) {
55-
currentNotice.setStatus(NoticeStatus.getStatusCode(newValue));
54+
if (currentNotice != null && newValue != null && currentNotice.isLeaf()) {
55+
currentNotice.setStatus(newValue.getCode());
5656
}
5757
}
58-
});
58+
);
59+
5960
open(null);
6061
}
6162

6263
public void updateStatuses() {
63-
choiceBoxNoticeStatus.setItems(NoticeStatus.asObservable());
64+
choiceBoxNoticeStatus.setItems(NoticeStatusList.asObservable());
6465
}
6566

6667
public void open(NoticeTreeItem item) {
6768
if (item == null || item.isBranch()) {
6869
choiceBoxNoticeStatus.getSelectionModel().clearSelection();
6970
settingsPane.setDisable(true);
7071
} else {
71-
choiceBoxNoticeStatus.getSelectionModel().select(NoticeStatus.getStatusName(item.getStatus()));
72+
choiceBoxNoticeStatus.getSelectionModel().select(NoticeStatusList.getStatus(item.getStatus()));
7273
settingsPane.setDisable(false);
7374
}
7475
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.temporaryteam.noticeditor.io;
22

33
import static com.temporaryteam.noticeditor.io.JsonFields.*;
4-
import com.temporaryteam.noticeditor.model.NoticeItem;
5-
import com.temporaryteam.noticeditor.model.NoticeStatus;
6-
import com.temporaryteam.noticeditor.model.NoticeTree;
7-
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
4+
import com.temporaryteam.noticeditor.model.*;
85
import java.io.File;
96
import java.io.IOException;
107
import java.util.Iterator;
@@ -33,19 +30,19 @@ public NoticeTree importDocument() throws IOException, JSONException {
3330
JSONObject json = new JSONObject(IOUtil.readContent(file));
3431

3532
if (json.has(KEY_STATUSINFO)) {
36-
JSONObject status = json.getJSONObject(KEY_STATUSINFO);
37-
Iterator it = status.keys();
38-
if (it.hasNext()) {
39-
NoticeStatus.clear();
33+
JSONArray statusList = json.getJSONArray(KEY_STATUSINFO);
34+
if (statusList.length() > 0) {
35+
NoticeStatusList.clear();
4036
}
41-
while (it.hasNext()) {
42-
String key = it.next().toString();
43-
NoticeStatus.add(key, status.getInt(key));
37+
for (int i = 0; i < statusList.length(); i++) {
38+
JSONObject obj = (JSONObject) statusList.get(i);
39+
String name = obj.getString("name");
40+
int code = obj.getInt("code");
41+
NoticeStatusList.add(name, code);
4442
}
4543
} else {
46-
NoticeStatus.load();
44+
NoticeStatusList.restore();
4745
}
48-
4946
return new NoticeTree(jsonToTree(json));
5047
}
5148

@@ -69,7 +66,9 @@ public void export(NoticeTree tree) throws JSONException, IOException {
6966
public JSONObject export(NoticeTreeItem root) throws JSONException {
7067
JSONObject json = new JSONObject();
7168
treeToJson(root, json);
72-
json.put("statusInfo", NoticeStatus.asObservableMap());
69+
70+
json.put(KEY_STATUSINFO, NoticeStatusList.asObservable());
71+
7372
return json;
7473
}
7574

Lines changed: 23 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,38 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
16
package com.temporaryteam.noticeditor.model;
27

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-
118
/**
12-
* Collection of notice statuses
9+
*
1310
* @author Maximillian M.
1411
*/
1512
public class NoticeStatus {
16-
private static HashMap<String, Integer> map;
17-
private static int lastIndex;
18-
19-
private static HashMap<String, Integer> mementoMap;
20-
private static int mementoIndex;
13+
private String name;
14+
private int code;
2115

22-
static {
23-
map = new HashMap<>();
24-
lastIndex = -1;
16+
public NoticeStatus(String name, int code) {
17+
this.name = name;
18+
this.code = code;
2519
}
26-
27-
/**
28-
* Saves statuses
29-
*/
30-
public static void save() {
31-
mementoIndex = lastIndex;
32-
mementoMap = (HashMap) map.clone();
20+
21+
public int getCode() {
22+
return code;
3323
}
34-
35-
/**
36-
* Restores statuses to the saved state
37-
*/
38-
public static void load() {
39-
lastIndex = mementoIndex;
40-
map = mementoMap;
24+
public void setCode(int code) {
25+
this.code = code;
4126
}
42-
43-
/**
44-
* Associates the specified status with next status code
45-
* @param status Status name
46-
*/
47-
public static void add(String status) {
48-
lastIndex++;
49-
map.put(status, lastIndex);
50-
}
51-
52-
/**
53-
* Associates the specified status with the specified status code
54-
* @param status Status name
55-
* @param code Status code
56-
*/
57-
public static void add(String status, int code) {
58-
if (lastIndex < code) {
59-
lastIndex = code;
60-
}
61-
map.put(status, code);
27+
public String getName() {
28+
return name;
6229
}
63-
64-
/**
65-
* Removes the specified status
66-
* @param status
67-
*/
68-
public static void remove(String status) {
69-
map.remove(status);
70-
}
71-
72-
/**
73-
* Return status code by status name
74-
* @param status Status name
75-
* @return Status code if status exists, otherwise returns 0
76-
*/
77-
public static int getStatusCode(String status) {
78-
return map.containsKey(status) ? map.get(status) : 0;
79-
}
80-
81-
/**
82-
* Returns status name by status code
83-
* @param code Status code
84-
* @return Status name if status exists, otherwise returns empty string
85-
*/
86-
public static String getStatusName(int code) {
87-
List<String> foo = map.entrySet().stream()
88-
.filter(entry -> entry.getValue() == code)
89-
.map(Map.Entry::getKey)
90-
.collect(Collectors.toList());
91-
if (foo.isEmpty()) {
92-
// TODO: May be Status[0]?
93-
return "";
94-
} else {
95-
return foo.get(0);
96-
}
97-
}
98-
99-
/**
100-
* Clears collection of notice statuses
101-
*/
102-
public static void clear() {
103-
map.clear();
104-
}
105-
106-
public static ObservableList<String> asObservable() {
107-
return FXCollections.observableArrayList(map.keySet());
30+
public void setName(String value) {
31+
name = value;
10832
}
10933

110-
public static ObservableMap<String, Integer> asObservableMap() {
111-
return FXCollections.observableMap(map);
34+
@Override
35+
public String toString() {
36+
return name;
11237
}
11338
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.temporaryteam.noticeditor.model;
7+
8+
import java.util.ArrayList;
9+
import java.util.stream.Collectors;
10+
import javafx.collections.FXCollections;
11+
import javafx.collections.ObservableList;
12+
13+
/**
14+
* Collection of notice statuses
15+
* @author Maximillian M.
16+
*/
17+
public class NoticeStatusList {
18+
private static ArrayList<NoticeStatus> list;
19+
private static int lastIndex;
20+
21+
private static ArrayList<NoticeStatus> mementoList;
22+
private static int mementoLastIndex;
23+
24+
static {
25+
list = new ArrayList<>();
26+
lastIndex = -1;
27+
}
28+
29+
/**
30+
* Saves status list
31+
*/
32+
public static void save() {
33+
mementoList = new ArrayList<>(list);
34+
mementoLastIndex = lastIndex;
35+
}
36+
37+
/**
38+
* Restores statuses to saved state
39+
*/
40+
public static void restore() {
41+
list = mementoList;
42+
lastIndex = mementoLastIndex;
43+
}
44+
45+
public static void add(String statusName) {
46+
++lastIndex;
47+
NoticeStatus newStatus = new NoticeStatus(statusName, lastIndex);
48+
list.add(newStatus);
49+
}
50+
51+
public static void add(String statusName, int statusCode) {
52+
lastIndex = (lastIndex < statusCode) ? statusCode : lastIndex + 1;
53+
NoticeStatus newStatus = new NoticeStatus(statusName, statusCode);
54+
list.add(newStatus);
55+
}
56+
57+
public static void clear() {
58+
list = new ArrayList<>();
59+
}
60+
61+
public static NoticeStatus getStatus(int statusCode) {
62+
NoticeStatus foo = list.stream().filter(status -> status.getCode() == statusCode).findFirst().orElse(list.get(0));
63+
return foo;
64+
}
65+
66+
public static int getStatusCode(String statusName) {
67+
for (NoticeStatus status : list) {
68+
if (status.getName().equals(statusName)) {
69+
return status.getCode();
70+
}
71+
}
72+
return 0;
73+
}
74+
75+
public static ObservableList<NoticeStatus> asObservable() {
76+
return FXCollections.observableList(list);
77+
}
78+
}

0 commit comments

Comments
 (0)