public class Main extends Application {
@Override
public void start(Stage stage) {
VBox root = new VBox ();
root.setSpacing(5);
root.setPadding(new Insets(10, 10, 10, 10));
Button btn = new Button();
btn.setText("add object");
btn.setOnAction((ActionEvent event) -> {
btnAction (root);
});
root.getChildren().add(btn);
FileReader.reader (); // чтение файла
if (ObjMap.get ("ident") == null) {
ObjMap.set ("ident", "0"); // создаем счетчик объектов в карте
}
creatContent (root); // создаем контент, при наличии
Scene scene = new Scene (root, 300, 250);
stage.setScene (scene);
stage.show();
// сохранение при закрытии окна
stage.setOnCloseRequest((WindowEvent we) -> {
FileWriter.writer (); // запись в файл
});
}
private void btnAction (VBox root) {
String id = ObjMap.newId(); // создаем новый идентификатор
for (int i = 0; ; i ++) {
String order = ObjMap.get ("c" + i);
if (order == null) {
ObjMap.set (("c" + i), id); // пишем в карту порядок идентификаторa
break;
}
}
root.getChildren().add(objBox(id)); // прикручиваем объект
}
private void creatContent (VBox root) {
for (int i = 0; ; i ++) {
String order = ObjMap.get ("c" + i);
if (order != null) {
root.getChildren().add(objBox(order));
} else {
break;
}
}
}
private HBox objBox (String id) {
HBox box = new HBox (new Label ("object " + id));
box.setId(id);
return box;
}
public static void main(String[] args) {
launch(args);
}
}
public class ObjMap {
private static final HashMap<String, String> map = new HashMap<>();
public static String get (String key){
return map.get(key);
}
public static void set (String key, String value){
map.put(key, value);
}
public static void rem (String key){
map.remove(key);
}
public static HashMap<String, String> map(){
return map;
}
// создание нового идентификатора
public static String newId (){
String count = map.get ("ident");
int id = (Integer.valueOf(count) + 1); // текущий счетчик увеличенный на один
String newId = String.valueOf(id);
map.put("ident", newId); // сохраняем новое значение
return newId;
}
}
public class FileReader {
public static void reader () {
final String path = (System.getProperty("user.dir") + "\\change.txt");
final File file = new File(path);
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
FileInputStream fileInStrim = null;
InputStreamReader inStrRead = null;
BufferedReader reader = null;
try {
fileInStrim = new FileInputStream(path);
inStrRead = new InputStreamReader(fileInStrim, "UTF-8");
reader = new BufferedReader(inStrRead);
String line;
String[] parts;
while ((line = reader.readLine()) != null){
parts = line.split("=", 2);
if (parts.length >= 2){
ObjMap.set(parts[0], parts[1]); // пишем пару в карту
}
}
} catch (IOException e) {
System.out.println("Unable to read file: " + e.toString());
} finally {
try {
fileInStrim.close();
inStrRead.close();
reader.close();
} catch (Exception e1){ }
}
}
}
public class FileWriter {
public static void writer (){
FileOutputStream fileOutStream = null;
OutputStreamWriter outStreamWriter = null;
BufferedWriter writer = null;
try {
fileOutStream = new FileOutputStream(System.getProperty("user.dir") + "\\change.txt");
outStreamWriter = new OutputStreamWriter(fileOutStream, "UTF-8");
writer = new BufferedWriter(outStreamWriter);
//writer.write("");
for (Map.Entry<String, String> entry : ObjMap.map().entrySet()){
writer.write(entry.getKey() + "=" + entry.getValue());
writer.newLine();
}
} catch (IOException ex) {
Logger.getLogger(FileWriter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
writer.close();
outStreamWriter.close ();
fileOutStream.close ();
} catch (IOException e) { }
}
}
}