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

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

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 15.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.osm.NoteData;
40import org.openstreetmap.josm.gui.MapView;
41import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
42import org.openstreetmap.josm.gui.NoteInputDialog;
43import org.openstreetmap.josm.gui.NoteSortDialog;
44import org.openstreetmap.josm.gui.SideButton;
45import org.openstreetmap.josm.gui.layer.Layer;
46import org.openstreetmap.josm.gui.layer.NoteLayer;
47import org.openstreetmap.josm.tools.ImageProvider;
48import org.openstreetmap.josm.tools.date.DateUtils;
49
50/**
51 * Dialog to display and manipulate notes.
52 * @since 7852 (renaming)
53 * @since 7608 (creation)
54 */
55public class NotesDialog extends ToggleDialog implements LayerChangeListener {
56
57 /** Small icon size for use in graphics calculations */
58 public static final int ICON_SMALL_SIZE = 16;
59 /** Large icon size for use in graphics calculations */
60 public static final int ICON_LARGE_SIZE = 24;
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 MapView.addLayerChangeListener(this);
103 }
104
105 @Override
106 public void showDialog() {
107 super.showDialog();
108 }
109
110 private void buildDialog() {
111 model = new NoteTableModel();
112 displayList = new JList<Note>(model);
113 displayList.setCellRenderer(new NoteRenderer());
114 displayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115 displayList.addListSelectionListener(new ListSelectionListener() {
116 @Override
117 public void valueChanged(ListSelectionEvent e) {
118 if (noteData != null) { //happens when layer is deleted while note selected
119 noteData.setSelectedNote(displayList.getSelectedValue());
120 }
121 updateButtonStates();
122 }});
123 displayList.addMouseListener(new MouseAdapter() {
124 //center view on selected note on double click
125 @Override
126 public void mouseClicked(MouseEvent e) {
127 if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
128 if (noteData != null && noteData.getSelectedNote() != null) {
129 Main.map.mapView.zoomTo(noteData.getSelectedNote().getLatLon());
130 }
131 }
132 }
133 });
134
135 JPanel pane = new JPanel(new BorderLayout());
136 pane.add(new JScrollPane(displayList), BorderLayout.CENTER);
137
138 createLayout(pane, false, Arrays.asList(new SideButton[]{
139 new SideButton(downloadNotesInViewAction, false),
140 new SideButton(newAction, false),
141 new SideButton(addCommentAction, false),
142 new SideButton(closeAction, false),
143 new SideButton(reopenAction, false),
144 new SideButton(sortAction, false),
145 new SideButton(uploadAction, false)}));
146 updateButtonStates();
147 }
148
149 private void updateButtonStates() {
150 if (noteData == null || noteData.getSelectedNote() == null) {
151 closeAction.setEnabled(false);
152 addCommentAction.setEnabled(false);
153 reopenAction.setEnabled(false);
154 } else if (noteData.getSelectedNote().getState() == State.open){
155 closeAction.setEnabled(true);
156 addCommentAction.setEnabled(true);
157 reopenAction.setEnabled(false);
158 } else { //note is closed
159 closeAction.setEnabled(false);
160 addCommentAction.setEnabled(false);
161 reopenAction.setEnabled(true);
162 }
163 if(noteData == null || !noteData.isModified()) {
164 uploadAction.setEnabled(false);
165 } else {
166 uploadAction.setEnabled(true);
167 }
168 //enable sort button if any notes are loaded
169 if (noteData == null || noteData.getNotes().isEmpty()) {
170 sortAction.setEnabled(false);
171 } else {
172 sortAction.setEnabled(true);
173 }
174 }
175
176 @Override
177 public void showNotify() { }
178
179 @Override
180 public void hideNotify() { }
181
182 @Override
183 public void activeLayerChange(Layer oldLayer, Layer newLayer) { }
184
185 @Override
186 public void layerAdded(Layer newLayer) {
187 if (newLayer instanceof NoteLayer) {
188 noteData = ((NoteLayer)newLayer).getNoteData();
189 model.setData(noteData.getNotes());
190 setNotes(noteData.getSortedNotes());
191 }
192 }
193
194 @Override
195 public void layerRemoved(Layer oldLayer) {
196 if (oldLayer instanceof NoteLayer) {
197 noteData = null;
198 model.clearData();
199 if (Main.map.mapMode instanceof AddNoteAction) {
200 Main.map.selectMapMode(Main.map.mapModeSelect);
201 }
202 }
203 }
204
205 /**
206 * Sets the list of notes to be displayed in the dialog.
207 * The dialog should match the notes displayed in the note layer.
208 * @param noteList List of notes to display
209 */
210 public void setNotes(Collection<Note> noteList) {
211 model.setData(noteList);
212 updateButtonStates();
213 this.repaint();
214 }
215
216 /**
217 * Notify the dialog that the note selection has changed.
218 * Causes it to update or clear its selection in the UI.
219 */
220 public void selectionChanged() {
221 if (noteData == null || noteData.getSelectedNote() == null) {
222 displayList.clearSelection();
223 } else {
224 displayList.setSelectedValue(noteData.getSelectedNote(), true);
225 }
226 updateButtonStates();
227 }
228
229 private class NoteRenderer implements ListCellRenderer<Note> {
230
231 private DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
232 private final DateFormat dateFormat = DateUtils.getDateTimeFormat(DateFormat.MEDIUM, DateFormat.SHORT);
233
234 @Override
235 public Component getListCellRendererComponent(JList<? extends Note> list, Note note, int index,
236 boolean isSelected, boolean cellHasFocus) {
237 Component comp = defaultListCellRenderer.getListCellRendererComponent(list, note, index, isSelected, cellHasFocus);
238 if (note != null && comp instanceof JLabel) {
239 String text = note.getFirstComment().getText();
240 String userName = note.getFirstComment().getUser().getName();
241 if (userName == null || userName.isEmpty()) {
242 userName = "<Anonymous>";
243 }
244 String toolTipText = userName + " @ " + dateFormat.format(note.getCreatedAt());
245 JLabel jlabel = (JLabel)comp;
246 jlabel.setText(note.getId() + ": " +text);
247 ImageIcon icon;
248 if (note.getId() < 0) {
249 icon = ICON_NEW_SMALL;
250 } else if (note.getState() == State.closed) {
251 icon = ICON_CLOSED_SMALL;
252 } else {
253 icon = ICON_OPEN_SMALL;
254 }
255 jlabel.setIcon(icon);
256 jlabel.setToolTipText(toolTipText);
257 }
258 return comp;
259 }
260 }
261
262 class NoteTableModel extends AbstractListModel<Note> {
263 private transient List<Note> data;
264
265 public NoteTableModel() {
266 data = new ArrayList<>();
267 }
268
269 @Override
270 public int getSize() {
271 if (data == null) {
272 return 0;
273 }
274 return data.size();
275 }
276
277 @Override
278 public Note getElementAt(int index) {
279 return data.get(index);
280 }
281
282 public void setData(Collection<Note> noteList) {
283 data.clear();
284 data.addAll(noteList);
285 fireContentsChanged(this, 0, noteList.size());
286 }
287
288 public void clearData() {
289 displayList.clearSelection();
290 data.clear();
291 fireIntervalRemoved(this, 0, getSize());
292 }
293 }
294
295 class AddCommentAction extends AbstractAction {
296
297 public AddCommentAction() {
298 putValue(SHORT_DESCRIPTION,tr("Add comment"));
299 putValue(NAME, tr("Comment"));
300 putValue(SMALL_ICON, ICON_COMMENT);
301 }
302
303 @Override
304 public void actionPerformed(ActionEvent e) {
305 Note note = displayList.getSelectedValue();
306 if (note == null) {
307 JOptionPane.showMessageDialog(Main.map,
308 "You must select a note first",
309 "No note selected",
310 JOptionPane.ERROR_MESSAGE);
311 return;
312 }
313 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Comment on note"), tr("Add comment"));
314 dialog.showNoteDialog(tr("Add comment to note:"), NotesDialog.ICON_COMMENT);
315 if (dialog.getValue() != 1) {
316 return;
317 }
318 int selectedIndex = displayList.getSelectedIndex();
319 noteData.addCommentToNote(note, dialog.getInputText());
320 noteData.setSelectedNote(model.getElementAt(selectedIndex));
321 }
322 }
323
324 class CloseAction extends AbstractAction {
325
326 public CloseAction() {
327 putValue(SHORT_DESCRIPTION,tr("Close note"));
328 putValue(NAME, tr("Close"));
329 putValue(SMALL_ICON, ICON_CLOSED);
330 }
331
332 @Override
333 public void actionPerformed(ActionEvent e) {
334 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Close note"), tr("Close note"));
335 dialog.showNoteDialog(tr("Close note with message:"), NotesDialog.ICON_CLOSED);
336 if (dialog.getValue() != 1) {
337 return;
338 }
339 Note note = displayList.getSelectedValue();
340 int selectedIndex = displayList.getSelectedIndex();
341 noteData.closeNote(note, dialog.getInputText());
342 noteData.setSelectedNote(model.getElementAt(selectedIndex));
343 }
344 }
345
346 class NewAction extends AbstractAction {
347
348 public NewAction() {
349 putValue(SHORT_DESCRIPTION,tr("Create a new note"));
350 putValue(NAME, tr("Create"));
351 putValue(SMALL_ICON, ICON_NEW);
352 }
353
354 @Override
355 public void actionPerformed(ActionEvent e) {
356 if (noteData == null) { //there is no notes layer. Create one first
357 Main.map.mapView.addLayer(new NoteLayer());
358 }
359 Main.map.selectMapMode(new AddNoteAction(Main.map, noteData));
360 }
361 }
362
363 class ReopenAction extends AbstractAction {
364
365 public ReopenAction() {
366 putValue(SHORT_DESCRIPTION,tr("Reopen note"));
367 putValue(NAME, tr("Reopen"));
368 putValue(SMALL_ICON, ICON_OPEN);
369 }
370
371 @Override
372 public void actionPerformed(ActionEvent e) {
373 NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Reopen note"), tr("Reopen note"));
374 dialog.showNoteDialog(tr("Reopen note with message:"), NotesDialog.ICON_OPEN);
375 if (dialog.getValue() != 1) {
376 return;
377 }
378
379 Note note = displayList.getSelectedValue();
380 int selectedIndex = displayList.getSelectedIndex();
381 noteData.reOpenNote(note, dialog.getInputText());
382 noteData.setSelectedNote(model.getElementAt(selectedIndex));
383 }
384 }
385
386 class SortAction extends AbstractAction {
387
388 public SortAction() {
389 putValue(SHORT_DESCRIPTION, tr("Sort notes"));
390 putValue(NAME, tr("Sort"));
391 putValue(SMALL_ICON, ImageProvider.get("dialogs", "sort"));
392 }
393
394 @Override
395 public void actionPerformed(ActionEvent e) {
396 NoteSortDialog sortDialog = new NoteSortDialog(Main.parent, tr("Sort notes"), tr("Apply"));
397 sortDialog.showSortDialog(noteData.getCurrentSortMethod());
398 if (sortDialog.getValue() == 1) {
399 noteData.setSortMethod(sortDialog.getSelectedComparator());
400 }
401 }
402 }
403}
Note: See TracBrowser for help on using the repository browser.