Skip to content

Commit fe6dcb1

Browse files
author
Sergey
committed
Merge pull request #54 from MaximillianMor/dev
Editable notice statuses
2 parents 51014b0 + 21bbc67 commit fe6dcb1

6 files changed

Lines changed: 186 additions & 19 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import com.temporaryteam.noticeditor.io.ExportException;
2424
import com.temporaryteam.noticeditor.io.ExportStrategy;
2525
import com.temporaryteam.noticeditor.io.ExportStrategyHolder;
26-
import com.temporaryteam.noticeditor.model.NoticeItem;
27-
import com.temporaryteam.noticeditor.model.NoticeTree;
28-
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
29-
import com.temporaryteam.noticeditor.model.PreviewStyles;
26+
import com.temporaryteam.noticeditor.model.*;
3027
import com.temporaryteam.noticeditor.view.Chooser;
3128
import com.temporaryteam.noticeditor.view.EditNoticeTreeCell;
3229
import java.util.ResourceBundle;
@@ -224,6 +221,7 @@ private void handleContextMenu(ActionEvent event) {
224221
private void handleNew(ActionEvent event) {
225222
rebuildTree(resources.getString("help"));
226223
fileSaved = null;
224+
NoticeStatusList.restore();
227225
}
228226

229227
@FXML
@@ -242,6 +240,7 @@ private void handleOpen(ActionEvent event) {
242240
createSearchBinding(noticeTree.getRoot());
243241
currentTreeItem = null;
244242
open();
243+
noticeSettingsController.updateStatuses();
245244
} catch (IOException | JSONException e) {
246245
logger.log(Level.SEVERE, null, e);
247246
}

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

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

3+
import com.temporaryteam.noticeditor.model.NoticeStatus;
4+
import com.temporaryteam.noticeditor.model.NoticeStatusList;
35
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
46
import java.net.URL;
57
import java.util.ResourceBundle;
@@ -28,7 +30,7 @@ public class NoticeSettingsController implements Initializable {
2830
@FXML
2931
private Button btnSelectFile;
3032
@FXML
31-
private ChoiceBox<String> choiceBoxNoticeStatus;
33+
private ChoiceBox<NoticeStatus> choiceBoxNoticeStatus;
3234

3335
private NoticeController noticeController;
3436

@@ -41,28 +43,36 @@ public void setNoticeController(NoticeController noticeController) {
4143
*/
4244
@Override
4345
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-
49-
@Override
50-
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
46+
NoticeStatusList.add(rb.getString("normal"));
47+
NoticeStatusList.add(rb.getString("important"));
48+
NoticeStatusList.save();
49+
updateStatuses();
50+
51+
choiceBoxNoticeStatus.getSelectionModel().selectedItemProperty().addListener(
52+
(ObservableValue<? extends NoticeStatus> observable, NoticeStatus oldValue, NoticeStatus newValue) -> {
5153
NoticeTreeItem currentNotice = noticeController.getCurrentNotice();
52-
if (currentNotice != null && currentNotice.isLeaf()) {
53-
currentNotice.setStatus(newValue.intValue());
54+
if (currentNotice != null && newValue != null && currentNotice.isLeaf()) {
55+
currentNotice.setStatus(newValue.getCode());
5456
}
5557
}
56-
});
58+
);
59+
5760
open(null);
5861
}
5962

63+
/**
64+
* Updates items (statuses) in choice box
65+
*/
66+
public void updateStatuses() {
67+
choiceBoxNoticeStatus.setItems(NoticeStatusList.asObservable());
68+
}
69+
6070
public void open(NoticeTreeItem item) {
6171
if (item == null || item.isBranch()) {
6272
choiceBoxNoticeStatus.getSelectionModel().clearSelection();
6373
settingsPane.setDisable(true);
6474
} else {
65-
choiceBoxNoticeStatus.getSelectionModel().select(item.getStatus());
75+
choiceBoxNoticeStatus.getSelectionModel().select(NoticeStatusList.getStatus(item.getStatus()));
6676
settingsPane.setDisable(false);
6777
}
6878
}

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: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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.NoticeTree;
6-
import com.temporaryteam.noticeditor.model.NoticeTreeItem;
4+
import com.temporaryteam.noticeditor.model.*;
75
import java.io.File;
86
import java.io.IOException;
97
import javafx.scene.control.TreeItem;
@@ -29,6 +27,21 @@ private JsonFormat(File file) {
2927

3028
public NoticeTree importDocument() throws IOException, JSONException {
3129
JSONObject json = new JSONObject(IOUtil.readContent(file));
30+
31+
if (json.has(KEY_STATUSINFO)) {
32+
JSONArray statusList = json.getJSONArray(KEY_STATUSINFO);
33+
if (statusList.length() > 0) {
34+
NoticeStatusList.clear();
35+
}
36+
for (int i = 0; i < statusList.length(); i++) {
37+
JSONObject obj = (JSONObject) statusList.get(i);
38+
String name = obj.getString("name");
39+
int code = obj.getInt("code");
40+
NoticeStatusList.add(name, code);
41+
}
42+
} else {
43+
NoticeStatusList.restore();
44+
}
3245
return new NoticeTree(jsonToTree(json));
3346
}
3447

@@ -52,6 +65,9 @@ public void export(NoticeTree tree) throws JSONException, IOException {
5265
public JSONObject export(NoticeTreeItem root) throws JSONException {
5366
JSONObject json = new JSONObject();
5467
treeToJson(root, json);
68+
69+
json.put(KEY_STATUSINFO, NoticeStatusList.asObservable());
70+
5571
return json;
5672
}
5773

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.temporaryteam.noticeditor.model;
2+
3+
/**
4+
* Representation of notice status
5+
* @author Maximillian M.
6+
*/
7+
public class NoticeStatus {
8+
private String name;
9+
private int code;
10+
11+
/**
12+
* Instantiates notice status
13+
* @param name Status name
14+
* @param code Status code
15+
*/
16+
public NoticeStatus(String name, int code) {
17+
this.name = name;
18+
this.code = code;
19+
}
20+
21+
/**
22+
* @return Status code
23+
*/
24+
public int getCode() {
25+
return code;
26+
}
27+
28+
public void setCode(int code) {
29+
this.code = code;
30+
}
31+
32+
/**
33+
* @return Status name
34+
*/
35+
public String getName() {
36+
return name;
37+
}
38+
public void setName(String value) {
39+
name = value;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return name;
45+
}
46+
}
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.ArrayList;
4+
import javafx.collections.FXCollections;
5+
import javafx.collections.ObservableList;
6+
7+
/**
8+
* Collection of notice statuses
9+
* @author Maximillian M.
10+
*/
11+
public class NoticeStatusList {
12+
private static ArrayList<NoticeStatus> list;
13+
private static int lastIndex;
14+
15+
private static ArrayList<NoticeStatus> mementoList;
16+
private static int mementoLastIndex;
17+
18+
static {
19+
list = new ArrayList<>();
20+
lastIndex = -1;
21+
}
22+
23+
/**
24+
* Saves status list
25+
*/
26+
public static void save() {
27+
mementoList = new ArrayList<>(list);
28+
mementoLastIndex = lastIndex;
29+
}
30+
31+
/**
32+
* Restores statuses to saved state
33+
*/
34+
public static void restore() {
35+
list = mementoList;
36+
lastIndex = mementoLastIndex;
37+
}
38+
39+
/**
40+
* Adds new status with next status code
41+
* @param statusName Status name
42+
*/
43+
public static void add(String statusName) {
44+
++lastIndex;
45+
NoticeStatus newStatus = new NoticeStatus(statusName, lastIndex);
46+
list.add(newStatus);
47+
}
48+
49+
/**
50+
* Adds new status with specified status code
51+
* @param statusName Status name
52+
* @param statusCode Status code
53+
*/
54+
public static void add(String statusName, int statusCode) {
55+
lastIndex = (lastIndex < statusCode) ? statusCode : lastIndex + 1;
56+
NoticeStatus newStatus = new NoticeStatus(statusName, statusCode);
57+
list.add(newStatus);
58+
}
59+
60+
/**
61+
* Clears status list
62+
*/
63+
public static void clear() {
64+
list = new ArrayList<>();
65+
}
66+
67+
/**
68+
* Returns status by code
69+
* @param statusCode Status code
70+
* @return Status
71+
*/
72+
public static NoticeStatus getStatus(int statusCode) {
73+
NoticeStatus foo = list.stream().filter(status -> status.getCode() == statusCode).findFirst().orElse(list.get(0));
74+
return foo;
75+
}
76+
77+
/**
78+
* Returns status code by name
79+
* @param statusName Status name
80+
* @return Status code
81+
*/
82+
public static int getStatusCode(String statusName) {
83+
for (NoticeStatus status : list) {
84+
if (status.getName().equals(statusName)) {
85+
return status.getCode();
86+
}
87+
}
88+
return 0;
89+
}
90+
91+
public static ObservableList<NoticeStatus> asObservable() {
92+
return FXCollections.observableList(list);
93+
}
94+
}

0 commit comments

Comments
 (0)