1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Image;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.text.SimpleDateFormat;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.AbstractListModel;
|
---|
17 | import javax.swing.DefaultListCellRenderer;
|
---|
18 | import javax.swing.ImageIcon;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.ListCellRenderer;
|
---|
25 | import javax.swing.ListSelectionModel;
|
---|
26 | import javax.swing.event.ListSelectionEvent;
|
---|
27 | import javax.swing.event.ListSelectionListener;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.actions.UploadNotesAction;
|
---|
31 | import org.openstreetmap.josm.actions.mapmode.AddNoteAction;
|
---|
32 | import org.openstreetmap.josm.data.notes.Note;
|
---|
33 | import org.openstreetmap.josm.data.notes.Note.State;
|
---|
34 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
35 | import org.openstreetmap.josm.gui.MapView;
|
---|
36 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
37 | import org.openstreetmap.josm.gui.NoteInputDialog;
|
---|
38 | import org.openstreetmap.josm.gui.NoteSortDialog;
|
---|
39 | import org.openstreetmap.josm.gui.SideButton;
|
---|
40 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
41 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
42 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Dialog to display and manipulate notes.
|
---|
46 | * @since 7852 (renaming)
|
---|
47 | * @since 7608 (creation)
|
---|
48 | */
|
---|
49 | public class NotesDialog extends ToggleDialog implements LayerChangeListener {
|
---|
50 |
|
---|
51 | /** Small icon size for use in graphics calculations */
|
---|
52 | public static final int ICON_SMALL_SIZE = 16;
|
---|
53 | /** Large icon size for use in graphics calculations */
|
---|
54 | public static final int ICON_LARGE_SIZE = 24;
|
---|
55 | /** 24x24 icon for unresolved notes */
|
---|
56 | public static final ImageIcon ICON_OPEN = ImageProvider.get("dialogs/notes", "note_open.png");
|
---|
57 | /** 16x16 icon for unresolved notes */
|
---|
58 | public static final ImageIcon ICON_OPEN_SMALL =
|
---|
59 | new ImageIcon(ICON_OPEN.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
|
---|
60 | /** 24x24 icon for resolved notes */
|
---|
61 | public static final ImageIcon ICON_CLOSED = ImageProvider.get("dialogs/notes", "note_closed.png");
|
---|
62 | /** 16x16 icon for resolved notes */
|
---|
63 | public static final ImageIcon ICON_CLOSED_SMALL =
|
---|
64 | new ImageIcon(ICON_CLOSED.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
|
---|
65 | /** 24x24 icon for new notes */
|
---|
66 | public static final ImageIcon ICON_NEW = ImageProvider.get("dialogs/notes", "note_new.png");
|
---|
67 | /** 16x16 icon for new notes */
|
---|
68 | public static final ImageIcon ICON_NEW_SMALL =
|
---|
69 | new ImageIcon(ICON_NEW.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
|
---|
70 | /** Icon for note comments */
|
---|
71 | public static final ImageIcon ICON_COMMENT = ImageProvider.get("dialogs/notes", "note_comment.png");
|
---|
72 |
|
---|
73 | private NoteTableModel model;
|
---|
74 | private JList<Note> displayList;
|
---|
75 | private final AddCommentAction addCommentAction;
|
---|
76 | private final CloseAction closeAction;
|
---|
77 | private final NewAction newAction;
|
---|
78 | private final ReopenAction reopenAction;
|
---|
79 | private final SortAction sortAction;
|
---|
80 | private final UploadNotesAction uploadAction;
|
---|
81 |
|
---|
82 | private NoteData noteData;
|
---|
83 |
|
---|
84 | /** Creates a new toggle dialog for notes */
|
---|
85 | public NotesDialog() {
|
---|
86 | super("Notes", "notes/note_open.png", "List of notes", null, 150);
|
---|
87 | addCommentAction = new AddCommentAction();
|
---|
88 | closeAction = new CloseAction();
|
---|
89 | newAction = new NewAction();
|
---|
90 | reopenAction = new ReopenAction();
|
---|
91 | sortAction = new SortAction();
|
---|
92 | uploadAction = new UploadNotesAction();
|
---|
93 | buildDialog();
|
---|
94 | MapView.addLayerChangeListener(this);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void showDialog() {
|
---|
99 | super.showDialog();
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void buildDialog() {
|
---|
103 | model = new NoteTableModel();
|
---|
104 | displayList = new JList<Note>(model);
|
---|
105 | displayList.setCellRenderer(new NoteRenderer());
|
---|
106 | displayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
107 | displayList.addListSelectionListener(new ListSelectionListener() {
|
---|
108 | @Override
|
---|
109 | public void valueChanged(ListSelectionEvent e) {
|
---|
110 | if (noteData != null) { //happens when layer is deleted while note selected
|
---|
111 | noteData.setSelectedNote(displayList.getSelectedValue());
|
---|
112 | }
|
---|
113 | updateButtonStates();
|
---|
114 | }});
|
---|
115 |
|
---|
116 | JPanel pane = new JPanel(new BorderLayout());
|
---|
117 | pane.add(new JScrollPane(displayList), BorderLayout.CENTER);
|
---|
118 |
|
---|
119 | createLayout(pane, false, Arrays.asList(new SideButton[]{
|
---|
120 | new SideButton(newAction, false),
|
---|
121 | new SideButton(addCommentAction, false),
|
---|
122 | new SideButton(closeAction, false),
|
---|
123 | new SideButton(reopenAction, false),
|
---|
124 | new SideButton(sortAction, false),
|
---|
125 | new SideButton(uploadAction, false)}));
|
---|
126 | updateButtonStates();
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void updateButtonStates() {
|
---|
130 | if (noteData == null || noteData.getSelectedNote() == null) {
|
---|
131 | closeAction.setEnabled(false);
|
---|
132 | addCommentAction.setEnabled(false);
|
---|
133 | reopenAction.setEnabled(false);
|
---|
134 | } else if (noteData.getSelectedNote().getState() == State.open){
|
---|
135 | closeAction.setEnabled(true);
|
---|
136 | addCommentAction.setEnabled(true);
|
---|
137 | reopenAction.setEnabled(false);
|
---|
138 | } else { //note is closed
|
---|
139 | closeAction.setEnabled(false);
|
---|
140 | addCommentAction.setEnabled(false);
|
---|
141 | reopenAction.setEnabled(true);
|
---|
142 | }
|
---|
143 | if(noteData == null || !noteData.isModified()) {
|
---|
144 | uploadAction.setEnabled(false);
|
---|
145 | } else {
|
---|
146 | uploadAction.setEnabled(true);
|
---|
147 | }
|
---|
148 | //enable sort button if any notes are loaded
|
---|
149 | if (noteData == null || noteData.getNotes().isEmpty()) {
|
---|
150 | sortAction.setEnabled(false);
|
---|
151 | } else {
|
---|
152 | sortAction.setEnabled(true);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public void showNotify() { }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public void hideNotify() { }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public void activeLayerChange(Layer oldLayer, Layer newLayer) { }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void layerAdded(Layer newLayer) {
|
---|
167 | if (newLayer instanceof NoteLayer) {
|
---|
168 | noteData = ((NoteLayer)newLayer).getNoteData();
|
---|
169 | model.setData(noteData.getNotes());
|
---|
170 | setNoteList(noteData.getNotes());
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public void layerRemoved(Layer oldLayer) {
|
---|
176 | if (oldLayer instanceof NoteLayer) {
|
---|
177 | if (Main.isDebugEnabled()) {
|
---|
178 | Main.debug("note layer removed. Clearing everything");
|
---|
179 | }
|
---|
180 | noteData = null;
|
---|
181 | model.clearData();
|
---|
182 | if (Main.map.mapMode instanceof AddNoteAction) {
|
---|
183 | Main.map.selectMapMode(Main.map.mapModeSelect);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Sets the list of notes to be displayed in the dialog.
|
---|
190 | * The dialog should match the notes displayed in the note layer.
|
---|
191 | * @param noteList List of notes to display
|
---|
192 | */
|
---|
193 | public void setNoteList(List<Note> noteList) {
|
---|
194 | model.setData(noteList);
|
---|
195 | updateButtonStates();
|
---|
196 | this.repaint();
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Notify the dialog that the note selection has changed.
|
---|
201 | * Causes it to update or clear its selection in the UI.
|
---|
202 | */
|
---|
203 | public void selectionChanged() {
|
---|
204 | if (noteData == null || noteData.getSelectedNote() == null) {
|
---|
205 | displayList.clearSelection();
|
---|
206 | } else {
|
---|
207 | displayList.setSelectedValue(noteData.getSelectedNote(), true);
|
---|
208 | }
|
---|
209 | updateButtonStates();
|
---|
210 | }
|
---|
211 |
|
---|
212 | private class NoteRenderer implements ListCellRenderer<Note> {
|
---|
213 |
|
---|
214 | private DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
|
---|
215 | private final SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy kk:mm");
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public Component getListCellRendererComponent(JList<? extends Note> list, Note note, int index,
|
---|
219 | boolean isSelected, boolean cellHasFocus) {
|
---|
220 | Component comp = defaultListCellRenderer.getListCellRendererComponent(list, note, index, isSelected, cellHasFocus);
|
---|
221 | if (note != null && comp instanceof JLabel) {
|
---|
222 | String text = note.getFirstComment().getText();
|
---|
223 | String userName = note.getFirstComment().getUser().getName();
|
---|
224 | if (userName == null || userName.isEmpty()) {
|
---|
225 | userName = "<Anonymous>";
|
---|
226 | }
|
---|
227 | String toolTipText = userName + " @ " + sdf.format(note.getCreatedAt());
|
---|
228 | JLabel jlabel = (JLabel)comp;
|
---|
229 | jlabel.setText(note.getId() + ": " +text);
|
---|
230 | ImageIcon icon;
|
---|
231 | if (note.getId() < 0) {
|
---|
232 | icon = ICON_NEW_SMALL;
|
---|
233 | } else if (note.getState() == State.closed) {
|
---|
234 | icon = ICON_CLOSED_SMALL;
|
---|
235 | } else {
|
---|
236 | icon = ICON_OPEN_SMALL;
|
---|
237 | }
|
---|
238 | jlabel.setIcon(icon);
|
---|
239 | jlabel.setToolTipText(toolTipText);
|
---|
240 | }
|
---|
241 | return comp;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | class NoteTableModel extends AbstractListModel<Note> {
|
---|
246 | private List<Note> data;
|
---|
247 |
|
---|
248 | public NoteTableModel() {
|
---|
249 | data = new ArrayList<Note>();
|
---|
250 | }
|
---|
251 |
|
---|
252 | @Override
|
---|
253 | public int getSize() {
|
---|
254 | if (data == null) {
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 | return data.size();
|
---|
258 | }
|
---|
259 |
|
---|
260 | @Override
|
---|
261 | public Note getElementAt(int index) {
|
---|
262 | return data.get(index);
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void setData(List<Note> noteList) {
|
---|
266 | data.clear();
|
---|
267 | data.addAll(noteList);
|
---|
268 | fireContentsChanged(this, 0, noteList.size());
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void clearData() {
|
---|
272 | displayList.clearSelection();
|
---|
273 | data.clear();
|
---|
274 | fireIntervalRemoved(this, 0, getSize());
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | class AddCommentAction extends AbstractAction {
|
---|
279 |
|
---|
280 | public AddCommentAction() {
|
---|
281 | putValue(SHORT_DESCRIPTION,tr("Add comment"));
|
---|
282 | putValue(NAME, tr("Comment"));
|
---|
283 | putValue(SMALL_ICON, ICON_COMMENT);
|
---|
284 | }
|
---|
285 |
|
---|
286 | @Override
|
---|
287 | public void actionPerformed(ActionEvent e) {
|
---|
288 | Note note = displayList.getSelectedValue();
|
---|
289 | if (note == null) {
|
---|
290 | JOptionPane.showMessageDialog(Main.map,
|
---|
291 | "You must select a note first",
|
---|
292 | "No note selected",
|
---|
293 | JOptionPane.ERROR_MESSAGE);
|
---|
294 | return;
|
---|
295 | }
|
---|
296 | NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Comment on note"), tr("Add comment"));
|
---|
297 | dialog.showNoteDialog(tr("Add comment to note:"), NotesDialog.ICON_COMMENT);
|
---|
298 | if (dialog.getValue() != 1) {
|
---|
299 | Main.debug("User aborted note reopening");
|
---|
300 | return;
|
---|
301 | }
|
---|
302 | noteData.addCommentToNote(note, dialog.getInputText());
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | class CloseAction extends AbstractAction {
|
---|
307 |
|
---|
308 | public CloseAction() {
|
---|
309 | putValue(SHORT_DESCRIPTION,tr("Close note"));
|
---|
310 | putValue(NAME, tr("Close"));
|
---|
311 | putValue(SMALL_ICON, ICON_CLOSED);
|
---|
312 | }
|
---|
313 |
|
---|
314 | @Override
|
---|
315 | public void actionPerformed(ActionEvent e) {
|
---|
316 | NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Close note"), tr("Close note"));
|
---|
317 | dialog.showNoteDialog(tr("Close note with message:"), NotesDialog.ICON_CLOSED);
|
---|
318 | if (dialog.getValue() != 1) {
|
---|
319 | Main.debug("User aborted note closing");
|
---|
320 | return;
|
---|
321 | }
|
---|
322 | Note note = displayList.getSelectedValue();
|
---|
323 | noteData.closeNote(note, dialog.getInputText());
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | class NewAction extends AbstractAction {
|
---|
328 |
|
---|
329 | public NewAction() {
|
---|
330 | putValue(SHORT_DESCRIPTION,tr("Create a new note"));
|
---|
331 | putValue(NAME, tr("Create"));
|
---|
332 | putValue(SMALL_ICON, ICON_NEW);
|
---|
333 | }
|
---|
334 |
|
---|
335 | @Override
|
---|
336 | public void actionPerformed(ActionEvent e) {
|
---|
337 | if (noteData == null) { //there is no notes layer. Create one first
|
---|
338 | Main.map.mapView.addLayer(new NoteLayer());
|
---|
339 | }
|
---|
340 | Main.map.selectMapMode(new AddNoteAction(Main.map, noteData));
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | class ReopenAction extends AbstractAction {
|
---|
345 |
|
---|
346 | public ReopenAction() {
|
---|
347 | putValue(SHORT_DESCRIPTION,tr("Reopen note"));
|
---|
348 | putValue(NAME, tr("Reopen"));
|
---|
349 | putValue(SMALL_ICON, ICON_OPEN);
|
---|
350 | }
|
---|
351 |
|
---|
352 | @Override
|
---|
353 | public void actionPerformed(ActionEvent e) {
|
---|
354 | NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Reopen note"), tr("Reopen note"));
|
---|
355 | dialog.showNoteDialog(tr("Reopen note with message:"), NotesDialog.ICON_OPEN);
|
---|
356 | if (dialog.getValue() != 1) {
|
---|
357 | Main.debug("User aborted note reopening");
|
---|
358 | return;
|
---|
359 | }
|
---|
360 |
|
---|
361 | Note note = displayList.getSelectedValue();
|
---|
362 | noteData.reOpenNote(note, dialog.getInputText());
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | class SortAction extends AbstractAction {
|
---|
367 |
|
---|
368 | public SortAction() {
|
---|
369 | putValue(SHORT_DESCRIPTION, tr("Sort notes"));
|
---|
370 | putValue(NAME, tr("Sort"));
|
---|
371 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "sort"));
|
---|
372 | }
|
---|
373 |
|
---|
374 | @Override
|
---|
375 | public void actionPerformed(ActionEvent e) {
|
---|
376 | NoteSortDialog sortDialog = new NoteSortDialog(Main.parent, tr("Sort notes"), tr("Apply"));
|
---|
377 | sortDialog.showSortDialog(noteData.getCurrentSortMethod());
|
---|
378 | if (sortDialog.getValue() == 1) {
|
---|
379 | noteData.setSortMethod(sortDialog.getSelectedComparator());
|
---|
380 | }
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|