1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.LinkedList;
|
---|
9 |
|
---|
10 | import javax.swing.DefaultListModel;
|
---|
11 | import javax.swing.JList;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.Preferences;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * List class that read and save its content from the bookmark file.
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public class BookmarkList extends JList {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Create a bookmark list as well as the Buttons add and remove.
|
---|
25 | */
|
---|
26 | public BookmarkList() {
|
---|
27 | setModel(new DefaultListModel());
|
---|
28 | load();
|
---|
29 | setVisibleRowCount(7);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Loads the bookmarks from file.
|
---|
34 | */
|
---|
35 | public void load() {
|
---|
36 | DefaultListModel model = (DefaultListModel)getModel();
|
---|
37 | model.removeAllElements();
|
---|
38 | try {
|
---|
39 | for (Preferences.Bookmark b : Main.pref.loadBookmarks()) {
|
---|
40 | model.addElement(b);
|
---|
41 | }
|
---|
42 | } catch (IOException e) {
|
---|
43 | e.printStackTrace();
|
---|
44 | JOptionPane.showMessageDialog(
|
---|
45 | Main.parent,
|
---|
46 | tr("<html>Could not read bookmarks from<br>''{0}''<br>Error was: {1}</html>",
|
---|
47 | Main.pref.getBookmarksFile(),
|
---|
48 | e.getMessage()
|
---|
49 | ),
|
---|
50 | tr("Error"),
|
---|
51 | JOptionPane.ERROR_MESSAGE
|
---|
52 | );
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Save all bookmarks to the preferences file
|
---|
58 | */
|
---|
59 | public void save() {
|
---|
60 | try {
|
---|
61 | Collection<Preferences.Bookmark> bookmarks = new LinkedList<Preferences.Bookmark>();
|
---|
62 | for (Object o : ((DefaultListModel)getModel()).toArray()) {
|
---|
63 | bookmarks.add((Preferences.Bookmark)o);
|
---|
64 | }
|
---|
65 | Main.pref.saveBookmarks(bookmarks);
|
---|
66 | } catch (IOException e) {
|
---|
67 | JOptionPane.showMessageDialog(
|
---|
68 | Main.parent,
|
---|
69 | tr("<html>Could not write bookmark.<br>{0}</html>", e.getMessage()),
|
---|
70 | tr("Error"),
|
---|
71 | JOptionPane.ERROR_MESSAGE
|
---|
72 | );
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|