001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.Component; 008import java.awt.Image; 009import java.awt.event.ActionEvent; 010import java.text.SimpleDateFormat; 011import java.util.ArrayList; 012import java.util.Arrays; 013import java.util.List; 014 015import javax.swing.AbstractAction; 016import javax.swing.AbstractListModel; 017import javax.swing.DefaultListCellRenderer; 018import javax.swing.ImageIcon; 019import javax.swing.JLabel; 020import javax.swing.JList; 021import javax.swing.JOptionPane; 022import javax.swing.JPanel; 023import javax.swing.JScrollPane; 024import javax.swing.ListCellRenderer; 025import javax.swing.ListSelectionModel; 026import javax.swing.event.ListSelectionEvent; 027import javax.swing.event.ListSelectionListener; 028 029import org.openstreetmap.josm.Main; 030import org.openstreetmap.josm.actions.UploadNotesAction; 031import org.openstreetmap.josm.actions.mapmode.AddNoteAction; 032import org.openstreetmap.josm.data.notes.Note; 033import org.openstreetmap.josm.data.notes.Note.State; 034import org.openstreetmap.josm.data.osm.NoteData; 035import org.openstreetmap.josm.gui.MapView; 036import org.openstreetmap.josm.gui.MapView.LayerChangeListener; 037import org.openstreetmap.josm.gui.NoteInputDialog; 038import org.openstreetmap.josm.gui.NoteSortDialog; 039import org.openstreetmap.josm.gui.SideButton; 040import org.openstreetmap.josm.gui.layer.Layer; 041import org.openstreetmap.josm.gui.layer.NoteLayer; 042import org.openstreetmap.josm.tools.ImageProvider; 043 044/** 045 * Dialog to display and manipulate notes. 046 * @since 7852 (renaming) 047 * @since 7608 (creation) 048 */ 049public class NotesDialog extends ToggleDialog implements LayerChangeListener { 050 051 /** Small icon size for use in graphics calculations */ 052 public static final int ICON_SMALL_SIZE = 16; 053 /** Large icon size for use in graphics calculations */ 054 public static final int ICON_LARGE_SIZE = 24; 055 /** 24x24 icon for unresolved notes */ 056 public static final ImageIcon ICON_OPEN = ImageProvider.get("dialogs/notes", "note_open.png"); 057 /** 16x16 icon for unresolved notes */ 058 public static final ImageIcon ICON_OPEN_SMALL = 059 new ImageIcon(ICON_OPEN.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH)); 060 /** 24x24 icon for resolved notes */ 061 public static final ImageIcon ICON_CLOSED = ImageProvider.get("dialogs/notes", "note_closed.png"); 062 /** 16x16 icon for resolved notes */ 063 public static final ImageIcon ICON_CLOSED_SMALL = 064 new ImageIcon(ICON_CLOSED.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH)); 065 /** 24x24 icon for new notes */ 066 public static final ImageIcon ICON_NEW = ImageProvider.get("dialogs/notes", "note_new.png"); 067 /** 16x16 icon for new notes */ 068 public static final ImageIcon ICON_NEW_SMALL = 069 new ImageIcon(ICON_NEW.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH)); 070 /** Icon for note comments */ 071 public static final ImageIcon ICON_COMMENT = ImageProvider.get("dialogs/notes", "note_comment.png"); 072 073 private NoteTableModel model; 074 private JList<Note> displayList; 075 private final AddCommentAction addCommentAction; 076 private final CloseAction closeAction; 077 private final NewAction newAction; 078 private final ReopenAction reopenAction; 079 private final SortAction sortAction; 080 private final UploadNotesAction uploadAction; 081 082 private NoteData noteData; 083 084 /** Creates a new toggle dialog for notes */ 085 public NotesDialog() { 086 super("Notes", "notes/note_open.png", "List of notes", null, 150); 087 addCommentAction = new AddCommentAction(); 088 closeAction = new CloseAction(); 089 newAction = new NewAction(); 090 reopenAction = new ReopenAction(); 091 sortAction = new SortAction(); 092 uploadAction = new UploadNotesAction(); 093 buildDialog(); 094 MapView.addLayerChangeListener(this); 095 } 096 097 @Override 098 public void showDialog() { 099 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}