source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java@ 10395

Last change on this file since 10395 was 10395, checked in by Don-vip, 8 years ago

gsoc-core - remove more deprecation warnings

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