Skip to content

Commit 73c0f7f

Browse files
author
SeTSeR
committed
Added hierarchy read-write support
1 parent 0efdf43 commit 73c0f7f

File tree

5 files changed

+125
-25
lines changed

5 files changed

+125
-25
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
JC=javac
2-
JCOPTS=-classpath $(LIBS)/pegdown-1.5.0.jar -sourcepath $(SRC) -d $(BUILD) -g $(SRC)/$(MAIN_CLASS)
2+
JCOPTS=-classpath $(LIBS)/pegdown-1.5.0.jar:$(LIBS)/org.json-chargebee-1.0.jar -sourcepath $(SRC) -d $(BUILD) -g $(SRC)/$(MAIN_CLASS)
33
PACKCMD=jar cfm $(DIST)/$(OUTPUT) $(MANIFEST) -C $(BUILD) .
44
RM=rm -rf
55

libs/org.json-chargebee-1.0.jar

39.7 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
package com.temporaryteam.noticeditor.model;
22

3-
import javafx.beans.property.StringProperty;
4-
import javafx.beans.property.SimpleStringProperty;
3+
import org.json.JSONObject;
4+
import org.json.JSONException;
55

6-
/**
7-
* Model class for a Notice.
8-
*/
96
public class Notice {
107

11-
private final StringProperty notice;
8+
private String notice;
129

13-
/**
14-
* Default constructor.
15-
*/
1610
public Notice() {
17-
this(null);
11+
notice = null;
1812
}
1913

20-
/*
21-
* Constructor with notice.
22-
*/
2314
public Notice(String notice) {
24-
this.notice = new SimpleStringProperty(notice);
15+
this.notice = notice;
2516
}
2617

2718
public String getNotice() {
28-
return notice.get();
19+
return notice;
2920
}
3021

31-
public void setNotice(String newNotice) {
32-
notice.set(newNotice);
22+
public void setNotice(String notice) {
23+
this.notice = notice;
3324
}
3425

35-
public StringProperty getNoticeProperty() {
36-
return notice;
26+
public JSONObject toJson() throws JSONException {
27+
JSONObject obj = new JSONObject();
28+
obj.put("content", notice);
29+
return obj;
30+
}
31+
32+
public void fromJson(JSONObject jsobj) throws JSONException {
33+
notice = jsobj.getString("content");
3734
}
3835

3936
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.temporaryteam.noticeditor.model;
2+
3+
import java.util.Vector;
4+
5+
import org.json.JSONObject;
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
9+
public class NoticeCategory {
10+
11+
private String name;
12+
private NoticeCategory[] subcategories;
13+
private Notice content;
14+
15+
public NoticeCategory() {
16+
name = null;
17+
content = null;
18+
subcategories = null;
19+
}
20+
21+
public NoticeCategory(String name, Notice content) {
22+
this.name = name;
23+
this.content = content;
24+
subcategories = null;
25+
}
26+
27+
public NoticeCategory(String name, NoticeCategory[] subcategories) {
28+
this.name = name;
29+
content = null;
30+
this.subcategories = subcategories;
31+
}
32+
33+
public NoticeCategory(String name, NoticeCategory[] subcategories, Notice content) {
34+
this.name = name;
35+
this.content = content;
36+
this.subcategories = subcategories;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public NoticeCategory[] getSubCategories() {
44+
return subcategories;
45+
}
46+
47+
public Notice getContent() {
48+
return content;
49+
}
50+
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
public void setSubCategories(NoticeCategory[] subcategories) {
56+
this.subcategories = subcategories;
57+
}
58+
59+
public void setContent(Notice content) {
60+
this.content = content;
61+
}
62+
63+
public JSONObject toJson() throws JSONException {
64+
JSONObject obj = new JSONObject();
65+
obj.put("name", name);
66+
obj.put("content", content.getNotice());
67+
Vector<JSONObject> vect = new Vector<JSONObject>();
68+
if(subcategories!=null) for(NoticeCategory subcategory : subcategories) vect.addElement(subcategory.toJson());
69+
obj.put("subcategories", new JSONArray(vect));
70+
return obj;
71+
}
72+
73+
public void fromJson(JSONObject jsobj) throws JSONException {
74+
name = jsobj.getString("name");
75+
content = new Notice();
76+
content.fromJson(jsobj);
77+
JSONArray arr = jsobj.getJSONArray("subcategories");
78+
subcategories = new NoticeCategory[arr.length()];
79+
NoticeCategory category = new NoticeCategory();
80+
for(int i = 0; i<arr.length(); i++) {
81+
subcategories[i].fromJson(arr.getJSONObject(i));
82+
}
83+
}
84+
85+
}

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.temporaryteam.noticeditor.view;
22

3+
import org.json.JSONObject;
4+
import org.json.JSONException;
5+
36
import org.pegdown.PegDownProcessor;
47

58
import java.io.File;
@@ -22,6 +25,7 @@
2225

2326
import com.temporaryteam.noticeditor.Main;
2427
import com.temporaryteam.noticeditor.model.Notice;
28+
import com.temporaryteam.noticeditor.model.NoticeCategory;
2529

2630
public class NoticeController {
2731

@@ -58,6 +62,7 @@ public class NoticeController {
5862
private WebEngine engine;
5963
private String input;
6064
private PegDownProcessor processor;
65+
private NoticeCategory currentNotice;
6166

6267
/**
6368
* The constructor. Must be called before initialization method
@@ -74,6 +79,12 @@ public NoticeController() {
7479
processor = new PegDownProcessor();
7580
}
7681

82+
/**
83+
* Method for generating TreeView items
84+
*/
85+
private void generateTreeItems() {
86+
}
87+
7788
/**
7889
* Method for operate with markdown
7990
*/
@@ -88,6 +99,7 @@ private String operate(String source) {
8899
private void initialize() {
89100
noticeArea.setText("Enter your notice here");
90101
engine = viewer.getEngine();
102+
currentNotice = new NoticeCategory("", new Notice("Enter your notice here"));
91103
engine.loadContent(noticeArea.getText());
92104
noticeArea.textProperty().addListener((observable, oldValue, newValue) -> engine.loadContent(operate(newValue)));
93105
}
@@ -100,6 +112,7 @@ private void handleMenu(ActionEvent event) {
100112
MenuItem source = (MenuItem)event.getSource();
101113
if(source.equals(newItem)) {
102114
noticeArea.setText("");
115+
currentNotice = new NoticeCategory("", new Notice(""));
103116
openedFile = null;
104117
}
105118
else if(source.equals(saveItem)) {
@@ -112,13 +125,14 @@ else if(source.equals(saveItem)) {
112125
}
113126
else toSave = openedFile;
114127
if(toSave!=null) {
115-
String notice = noticeArea.getText();
116128
if(!toSave.exists()) toSave.createNewFile();
117129
FileWriter writeFile = new FileWriter(toSave);
118-
writeFile.write(notice);
130+
JSONObject obj = currentNotice.toJson();
131+
obj.write(writeFile);
119132
writeFile.close();
120133
}
121134
} catch(IOException ioe) {
135+
} catch(JSONException e) {
122136
}
123137
}
124138
else if((source.equals(openItem))||(source.equals(saveAsItem))) {
@@ -137,20 +151,24 @@ else if(source.equals(saveAsItem)) {
137151
while(in.hasNext()) {
138152
notice+=in.nextLine()+"\n";
139153
}
140-
noticeArea.setText(notice);
154+
JSONObject obj = new JSONObject(notice);
155+
currentNotice.fromJson(obj);
156+
noticeArea.setText("");
141157
openedFile = selected;
142158
in.close();
159+
generateTreeItems();
143160
}
144161
else if(source.equals(saveAsItem)) {
145-
String notice = noticeArea.getText();
146162
if(!selected.exists()) selected.createNewFile();
147163
FileWriter writeFile = new FileWriter(selected);
148-
writeFile.write(notice);
164+
JSONObject obj = currentNotice.toJson();
165+
obj.write(writeFile);
149166
writeFile.close();
150167
openedFile = selected;
151168
}
152169
}
153170
} catch (IOException ioe) {
171+
} catch (JSONException e) {
154172
}
155173
}
156174
else if(source.equals(exportHTMLItem)) {

0 commit comments

Comments
 (0)