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

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

fix Checkstyle issues

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