source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/NoteDialog.java@ 7699

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

fix #10700 - Add note upload functionality (patch by ToeBee)

File size: 13.1 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.text.SimpleDateFormat;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.List;
14
15import javax.swing.AbstractAction;
16import javax.swing.AbstractListModel;
17import javax.swing.DefaultListCellRenderer;
18import javax.swing.ImageIcon;
19import javax.swing.JLabel;
20import javax.swing.JList;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JScrollPane;
24import javax.swing.ListCellRenderer;
25import javax.swing.ListSelectionModel;
26import javax.swing.event.ListSelectionEvent;
27import javax.swing.event.ListSelectionListener;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.actions.UploadNotesAction;
31import org.openstreetmap.josm.actions.mapmode.AddNoteAction;
32import org.openstreetmap.josm.data.notes.Note;
33import org.openstreetmap.josm.data.notes.Note.State;
34import org.openstreetmap.josm.data.osm.NoteData;
35import org.openstreetmap.josm.gui.MapView;
36import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
37import org.openstreetmap.josm.gui.SideButton;
38import org.openstreetmap.josm.gui.layer.Layer;
39import org.openstreetmap.josm.gui.layer.NoteLayer;
40import org.openstreetmap.josm.tools.ImageProvider;
41
42/**
43 * Dialog to display and manipulate notes
44 */
45public class NoteDialog extends ToggleDialog implements LayerChangeListener {
46
47
48 /** Small icon size for use in graphics calculations */
49 public static final int ICON_SMALL_SIZE = 16;
50 /** Large icon size for use in graphics calculations */
51 public static final int ICON_LARGE_SIZE = 24;
52 /** 24x24 icon for unresolved notes */
53 public static final ImageIcon ICON_OPEN = ImageProvider.get("dialogs/notes", "note_open.png");
54 /** 16x16 icon for unresolved notes */
55 public static final ImageIcon ICON_OPEN_SMALL =
56 new ImageIcon(ICON_OPEN.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
57 /** 24x24 icon for resolved notes */
58 public static final ImageIcon ICON_CLOSED = ImageProvider.get("dialogs/notes", "note_closed.png");
59 /** 16x16 icon for resolved notes */
60 public static final ImageIcon ICON_CLOSED_SMALL =
61 new ImageIcon(ICON_CLOSED.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
62 /** 24x24 icon for new notes */
63 public static final ImageIcon ICON_NEW = ImageProvider.get("dialogs/notes", "note_new.png");
64 /** 16x16 icon for new notes */
65 public static final ImageIcon ICON_NEW_SMALL =
66 new ImageIcon(ICON_NEW.getImage().getScaledInstance(ICON_SMALL_SIZE, ICON_SMALL_SIZE, Image.SCALE_SMOOTH));
67 /** Icon for note comments */
68 public static final ImageIcon ICON_COMMENT = ImageProvider.get("dialogs/notes", "note_comment.png");
69
70 private NoteTableModel model;
71 private JList<Note> displayList;
72 private final AddCommentAction addCommentAction;
73 private final CloseAction closeAction;
74 private final NewAction newAction;
75 private final ReopenAction reopenAction;
76 private final UploadNotesAction uploadAction;
77
78 private NoteData noteData;
79
80 /** Creates a new toggle dialog for notes */
81 public NoteDialog() {
82 super("Notes", "notes/note_open.png", "List of notes", null, 150);
83 if (Main.isDebugEnabled()) {
84 Main.debug("constructed note dialog");
85 }
86
87 addCommentAction = new AddCommentAction();
88 closeAction = new CloseAction();
89 newAction = new NewAction();
90 reopenAction = new ReopenAction();
91 uploadAction = new UploadNotesAction();
92 buildDialog();
93 }
94
95 @Override
96 public void showDialog() {
97 super.showDialog();
98 }
99
100 private void buildDialog() {
101 model = new NoteTableModel();
102 displayList = new JList<Note>(model);
103 displayList.setCellRenderer(new NoteRenderer());
104 displayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105 displayList.addListSelectionListener(new ListSelectionListener() {
106 @Override
107 public void valueChanged(ListSelectionEvent e) {
108 if (noteData != null) { //happens when layer is deleted while note selected
109 noteData.setSelectedNote(displayList.getSelectedValue());
110 }
111 updateButtonStates();
112 }});
113
114 JPanel pane = new JPanel(new BorderLayout());
115 pane.add(new JScrollPane(displayList), BorderLayout.CENTER);
116
117 createLayout(pane, false, Arrays.asList(new SideButton[]{
118 new SideButton(newAction, false),
119 new SideButton(addCommentAction, false),
120 new SideButton(closeAction, false),
121 new SideButton(reopenAction, false),
122 new SideButton(uploadAction, false)}));
123 updateButtonStates();
124 }
125
126 private void updateButtonStates() {
127 if (noteData == null || noteData.getSelectedNote() == null) {
128 closeAction.setEnabled(false);
129 addCommentAction.setEnabled(false);
130 reopenAction.setEnabled(false);
131 } else if (noteData.getSelectedNote().getState() == State.open){
132 closeAction.setEnabled(true);
133 addCommentAction.setEnabled(true);
134 reopenAction.setEnabled(false);
135 } else { //note is closed
136 closeAction.setEnabled(false);
137 addCommentAction.setEnabled(false);
138 reopenAction.setEnabled(true);
139 }
140 if(noteData == null || !noteData.isModified()) {
141 uploadAction.setEnabled(false);
142 } else {
143 uploadAction.setEnabled(true);
144 }
145 }
146
147 @Override
148 public void showNotify() {
149 MapView.addLayerChangeListener(this);
150 }
151
152 @Override
153 public void hideNotify() {
154 MapView.removeLayerChangeListener(this);
155 }
156
157 @Override
158 public void activeLayerChange(Layer oldLayer, Layer newLayer) { }
159
160 @Override
161 public void layerAdded(Layer newLayer) {
162 if (Main.isDebugEnabled()) {
163 Main.debug("layer added: " + newLayer);
164 }
165 if (newLayer instanceof NoteLayer) {
166 if (Main.isDebugEnabled()) {
167 Main.debug("note layer added");
168 }
169 noteData = ((NoteLayer)newLayer).getNoteData();
170 model.setData(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(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 Object userInput = JOptionPane.showInputDialog(Main.map,
297 tr("Add comment to note:"),
298 tr("Add comment"),
299 JOptionPane.QUESTION_MESSAGE,
300 ICON_COMMENT,
301 null,null);
302 if (userInput == null) { //user pressed cancel
303 return;
304 }
305 noteData.addCommentToNote(note, userInput.toString());
306 }
307 }
308
309 class CloseAction extends AbstractAction {
310
311 public CloseAction() {
312 putValue(SHORT_DESCRIPTION,tr("Close note"));
313 putValue(NAME, tr("Close"));
314 putValue(SMALL_ICON, ICON_CLOSED);
315 }
316
317 @Override
318 public void actionPerformed(ActionEvent e) {
319 Object userInput = JOptionPane.showInputDialog(Main.map,
320 tr("Close note with message:"),
321 tr("Close Note"),
322 JOptionPane.QUESTION_MESSAGE,
323 ICON_CLOSED,
324 null,null);
325 if (userInput == null) { //user pressed cancel
326 return;
327 }
328 Note note = displayList.getSelectedValue();
329 noteData.closeNote(note, userInput.toString());
330 }
331 }
332
333 class NewAction extends AbstractAction {
334
335 public NewAction() {
336 putValue(SHORT_DESCRIPTION,tr("Create a new note"));
337 putValue(NAME, tr("Create"));
338 putValue(SMALL_ICON, ICON_NEW);
339 }
340
341 @Override
342 public void actionPerformed(ActionEvent e) {
343 if (noteData == null) { //there is no notes layer. Create one first
344 Main.map.mapView.addLayer(new NoteLayer());
345 }
346 Main.map.selectMapMode(new AddNoteAction(Main.map, noteData));
347 }
348 }
349
350 class ReopenAction extends AbstractAction {
351
352 public ReopenAction() {
353 putValue(SHORT_DESCRIPTION,tr("Reopen note"));
354 putValue(NAME, tr("Reopen"));
355 putValue(SMALL_ICON, ICON_OPEN);
356 }
357
358 @Override
359 public void actionPerformed(ActionEvent e) {
360 Object userInput = JOptionPane.showInputDialog(Main.map,
361 tr("Reopen note with message:"),
362 tr("Reopen note"),
363 JOptionPane.QUESTION_MESSAGE,
364 ICON_OPEN,
365 null,null);
366 if (userInput == null) { //user pressed cancel
367 return;
368 }
369 Note note = displayList.getSelectedValue();
370 noteData.reOpenNote(note, userInput.toString());
371 }
372 }
373}
Note: See TracBrowser for help on using the repository browser.