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 | } catch (IOException e) {
|
---|
42 | e.printStackTrace();
|
---|
43 | JOptionPane.showMessageDialog(Main.parent, tr("Could not read bookmarks.")+"\n"+e.getMessage());
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Save all bookmarks to the preferences file
|
---|
49 | */
|
---|
50 | public void save() {
|
---|
51 | try {
|
---|
52 | Collection<Preferences.Bookmark> bookmarks = new LinkedList<Preferences.Bookmark>();
|
---|
53 | for (Object o : ((DefaultListModel)getModel()).toArray())
|
---|
54 | bookmarks.add((Preferences.Bookmark)o);
|
---|
55 | Main.pref.saveBookmarks(bookmarks);
|
---|
56 | } catch (IOException e) {
|
---|
57 | JOptionPane.showMessageDialog(Main.parent,tr("Could not write bookmark.")+"\n"+e.getMessage());
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|