diff --git a/src/mahomaps/overlays/SearchOverlay.java b/src/mahomaps/overlays/SearchOverlay.java index 6579af8..7398467 100644 --- a/src/mahomaps/overlays/SearchOverlay.java +++ b/src/mahomaps/overlays/SearchOverlay.java @@ -7,6 +7,7 @@ import mahomaps.MahoMapsApp; import mahomaps.map.Geopoint; import mahomaps.map.MapState; +import mahomaps.screens.BookmarksScreen; import mahomaps.screens.SearchResultScreen; import mahomaps.screens.SearchScreen; import mahomaps.ui.Button; @@ -71,11 +72,12 @@ private void SetSelection(Geopoint p) { content = new FillFlowContainer(new UIElement[] { new SimpleText(data.getNullableString("name")), new SimpleText(data.getString("description", "")), - new ColumnsContainer( - new UIElement[] { new Button(MahoMapsApp.text[109], 2, this), new Button(MahoMapsApp.text[110], 3, this) }), + new ColumnsContainer(new UIElement[] { new Button(MahoMapsApp.text[109], 2, this), + new Button(MahoMapsApp.text[110], 3, this) }), new ColumnsContainer(new UIElement[] { new Button(MahoMapsApp.text[104], 4, this), new Button(MahoMapsApp.text[105], 5, this) }), - new Button(MahoMapsApp.text[1], 6, this) }); + new ColumnsContainer(new UIElement[] { new Button("В закладки", 7, this), + new Button(MahoMapsApp.text[1], 6, this) }), }); InvalidateSize(); } @@ -132,6 +134,10 @@ public void OnButtonTap(UIElement sender, int uid) { case 6: SetNullSelection(); break; + case 7: + BookmarksScreen.BeginAdd(selected, + ((JSONObject) selected.object).getObject("properties").getNullableString("name")); + break; } } diff --git a/src/mahomaps/overlays/SelectOverlay.java b/src/mahomaps/overlays/SelectOverlay.java index cd65124..f808623 100644 --- a/src/mahomaps/overlays/SelectOverlay.java +++ b/src/mahomaps/overlays/SelectOverlay.java @@ -4,6 +4,7 @@ import mahomaps.MahoMapsApp; import mahomaps.map.Geopoint; +import mahomaps.screens.BookmarksScreen; import mahomaps.screens.SearchLoader; import mahomaps.ui.Button; import mahomaps.ui.ColumnsContainer; @@ -26,8 +27,9 @@ public SelectOverlay(final Geopoint p) { selection.color = Geopoint.COLOR_RED; v.addElement(selection); - content = new FillFlowContainer(new UIElement[] { new SimpleText(p.toString()), - new Button(MahoMapsApp.text[103], 1, this), new ColumnsContainer(new UIElement[] { + content = new FillFlowContainer(new UIElement[] { + new SimpleText(p.toString()), new Button(MahoMapsApp.text[103], 1, this), + new Button("В закладки", 4, this), new ColumnsContainer(new UIElement[] { new Button(MahoMapsApp.text[104], 2, this), new Button(MahoMapsApp.text[105], 3, this) }), new Button(MahoMapsApp.text[38], 0, this) }); } @@ -63,6 +65,9 @@ public void OnButtonTap(UIElement sender, int uid) { Close(); RouteBuildOverlay.Get().SetB(selection); break; + case 4: + BookmarksScreen.BeginAdd(selection, null); + break; } } } diff --git a/src/mahomaps/screens/BookmarksScreen.java b/src/mahomaps/screens/BookmarksScreen.java new file mode 100644 index 0000000..c62e325 --- /dev/null +++ b/src/mahomaps/screens/BookmarksScreen.java @@ -0,0 +1,142 @@ +package mahomaps.screens; + +import javax.microedition.lcdui.Choice; +import javax.microedition.lcdui.Command; +import javax.microedition.lcdui.CommandListener; +import javax.microedition.lcdui.Displayable; +import javax.microedition.lcdui.List; +import javax.microedition.lcdui.TextBox; +import javax.microedition.rms.RecordStore; + +import cc.nnproject.json.JSON; +import cc.nnproject.json.JSONArray; +import cc.nnproject.json.JSONObject; +import mahomaps.MahoMapsApp; +import mahomaps.map.Geopoint; +import mahomaps.map.MapState; +import mahomaps.overlays.RouteBuildOverlay; + +public class BookmarksScreen extends List implements CommandListener { + + public final static String RMS_NAME = "mm_v1_bookmarks"; + + private JSONArray list; + + private Command from = new Command("Отсюда", Command.ITEM, 0); + private Command to = new Command("Сюда", Command.ITEM, 1); + private Command del = new Command("Удалить", Command.ITEM, 1); + + public BookmarksScreen() { + super("Закладки", Choice.IMPLICIT); + list = read(); + list.build(); + fillList(); + addCommand(MahoMapsApp.back); + if (list.size() > 0) { + addCommand(from); + addCommand(to); + addCommand(del); + } + setCommandListener(this); + } + + private void fillList() { + for (int i = 0; i < list.size(); i++) { + append(list.getObject(i).getString("name", "Not named"), null); + } + } + + private static JSONArray read() { + try { + RecordStore r = RecordStore.openRecordStore(RMS_NAME, true); + byte[] d = null; + if (r.getNumRecords() > 0) { + d = r.getRecord(1); + } + r.closeRecordStore(); + + if (d == null) + return JSON.getArray("[]"); + + return JSON.getArray(new String(d, "UTF-8")); + } catch (Throwable e) { + return JSON.getArray("[]"); + } + } + + public static void BeginAdd(final Geopoint p, String defaultName) { + final TextBox tb = new TextBox("Название закладки?", defaultName == null ? "" : defaultName, 100, 0); + tb.addCommand(MahoMapsApp.back); + tb.addCommand(MahoMapsApp.ok); + tb.setCommandListener(new CommandListener() { + public void commandAction(Command c, Displayable d) { + if (c == MahoMapsApp.back) + MahoMapsApp.BringMap(); + else if (c == MahoMapsApp.ok) { + String text = tb.getString(); + if (text != null && text.length() > 0) { + Add(p, text); + } + MahoMapsApp.BringMap(); + } + } + }); + MahoMapsApp.BringSubScreen(tb); + } + + public static void Add(Geopoint p, String name) { + JSONArray arr = read(); + JSONObject obj = new JSONObject(); + obj.put("name", name); + obj.put("lat", p.lat); + obj.put("lon", p.lon); + arr.add(obj); + Save(arr); + } + + private static void Save(JSONArray arr) { + try { + byte[] d = arr.toString().getBytes("UTF-8"); + RecordStore r = RecordStore.openRecordStore(RMS_NAME, true); + if (r.getNumRecords() == 0) + r.addRecord(new byte[1], 0, 1); + r.setRecord(1, d, 0, d.length); + r.closeRecordStore(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void commandAction(Command c, Displayable d) { + if (c == MahoMapsApp.back) { + MahoMapsApp.BringMap(); + return; + } + + if (list.size() == 0) + return; + int n = getSelectedIndex(); + if (n == -1) + return; + + if (c == del) { + list.remove(n); + delete(n); + Save(list); + return; + } + JSONObject obj = list.getObject(n); + Geopoint p = new Geopoint(obj.getDouble("lat"), obj.getDouble("lon")); + if (c == SELECT_COMMAND) { + MahoMapsApp.GetCanvas().state = MapState.FocusAt(p); + MahoMapsApp.BringMap(); + } else if (c == from) { + RouteBuildOverlay.Get().SetA(p); + MahoMapsApp.BringMap(); + } else if (c == to) { + RouteBuildOverlay.Get().SetB(p); + MahoMapsApp.BringMap(); + } + } + +} diff --git a/src/mahomaps/screens/MapCanvas.java b/src/mahomaps/screens/MapCanvas.java index 30cbfe0..de8e910 100644 --- a/src/mahomaps/screens/MapCanvas.java +++ b/src/mahomaps/screens/MapCanvas.java @@ -450,7 +450,7 @@ protected void keyPressed(int k) { BeginTextSearch(); break handling; case KEY_NUM9: - // bookmarks + MahoMapsApp.BringSubScreen(new BookmarksScreen()); break handling; case KEY_STAR: state = state.ZoomIn(); diff --git a/src/mahomaps/screens/MenuScreen.java b/src/mahomaps/screens/MenuScreen.java index b6fd95c..87357be 100644 --- a/src/mahomaps/screens/MenuScreen.java +++ b/src/mahomaps/screens/MenuScreen.java @@ -15,8 +15,8 @@ public class MenuScreen extends List implements CommandListener { public MenuScreen(TilesProvider tiles) { super("MahoMaps v1", Choice.IMPLICIT, - new String[] { MahoMapsApp.text[9], MahoMapsApp.text[10], MahoMapsApp.text[11], MahoMapsApp.text[69], - MahoMapsApp.text[12], MahoMapsApp.text[13], MahoMapsApp.text[0] }, + new String[] { "Закладки", MahoMapsApp.text[9], MahoMapsApp.text[10], MahoMapsApp.text[11], + MahoMapsApp.text[69], MahoMapsApp.text[12], MahoMapsApp.text[13], MahoMapsApp.text[0] }, null); this.tiles = tiles; addCommand(MahoMapsApp.back); @@ -30,18 +30,20 @@ public void commandAction(Command c, Displayable d) { } else if (c == SELECT_COMMAND) { int sel = getSelectedIndex(); if (sel == 0) { - MahoMapsApp.BringSubScreen(new APIReconnectForm()); + MahoMapsApp.BringSubScreen(new BookmarksScreen()); } else if (sel == 1) { - MahoMapsApp.BringSubScreen(new KeyboardHelpScreen()); + MahoMapsApp.BringSubScreen(new APIReconnectForm()); } else if (sel == 2) { - MahoMapsApp.BringSubScreen(new SettingsScreen()); + MahoMapsApp.BringSubScreen(new KeyboardHelpScreen()); } else if (sel == 3) { - MahoMapsApp.BringSubScreen(new CacheManager(tiles)); + MahoMapsApp.BringSubScreen(new SettingsScreen()); } else if (sel == 4) { - MahoMapsApp.BringSubScreen(new AboutScreen()); + MahoMapsApp.BringSubScreen(new CacheManager(tiles)); } else if (sel == 5) { - MahoMapsApp.BringSubScreen(new OtherAppsScreen()); + MahoMapsApp.BringSubScreen(new AboutScreen()); } else if (sel == 6) { + MahoMapsApp.BringSubScreen(new OtherAppsScreen()); + } else if (sel == 7) { MahoMapsApp.Exit(); } }