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