Statistics
| Revision:

root / trunk / libraries / libTopology / src / com / vividsolutions / jcs / plugin / issuelog / IssueLogPanel.java @ 22873

History | View | Annotate | Download (14.6 KB)

1
package com.vividsolutions.jcs.plugin.issuelog;
2

    
3
import java.awt.*;
4

    
5
import javax.swing.*;
6

    
7
import java.text.SimpleDateFormat;
8
import java.util.ArrayList;
9
import java.util.Collection;
10
import java.util.Date;
11
import java.util.List;
12

    
13
import com.vividsolutions.jcs.jump.FUTURE_CollectionUtil;
14
import com.vividsolutions.jcs.jump.FUTURE_GUIUtil;
15
import com.vividsolutions.jcs.plugin.conflate.roads.NewSessionPlugIn;
16
import com.vividsolutions.jcs.plugin.conflate.roads.ToolboxModel;
17
import com.vividsolutions.jump.util.CollectionUtil;
18
import com.vividsolutions.jump.util.LangUtil;
19
import com.vividsolutions.jump.util.StringUtil;
20
import com.vividsolutions.jump.workbench.WorkbenchContext;
21
import com.vividsolutions.jump.workbench.model.LayerManager;
22
import com.vividsolutions.jump.workbench.ui.ButtonPanel;
23
import com.vividsolutions.jump.workbench.ui.GUIUtil;
24
import com.vividsolutions.jump.workbench.ui.images.IconLoader;
25
import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
26

    
27
import java.awt.event.*;
28

    
29
public class IssueLogPanel extends JPanel {
30

    
31
        private static final int MAX_RECORDED_DESCRIPTIONS_COUNT = 50;
32

    
33
        private WorkbenchContext context;
34

    
35
        private Date createdDate;
36

    
37
        private Date updatedDate;
38

    
39
        private static final String USER_DEFINED_LABEL = "< User-defined >";
40

    
41
        public IssueLogPanel(WorkbenchContext context) {
42
                this.context = context;
43
                fixLabelMinimumSizes();
44
                statusComboBox.setModel(new DefaultComboBoxModel(IssueLog.instance(
45
                                context.getLayerManager()).getStatusCodes().toArray()));
46
                typeComboBox.setModel(new DefaultComboBoxModel(IssueLog.instance(
47
                                context.getLayerManager()).getTypeCodes().toArray()));
48
                updateRecordedDescriptionsComboBox();
49
                descriptionTextArea.setFont(new JLabel().getFont());
50
                commentTextArea.setFont(new JLabel().getFont());
51
                GUIUtil.makeTabMoveFocus(descriptionTextArea);
52
                GUIUtil.makeTabMoveFocus(commentTextArea);
53
                try {
54
                        jbInit();
55
                } catch (Exception ex) {
56
                        ex.printStackTrace();
57
                }
58
                descriptionTextArea.getDocument().addDocumentListener(
59
                                GUIUtil.toDocumentListener(new ActionListener() {
60

    
61
                                        public void actionPerformed(ActionEvent e) {
62
                                                if (transferringRecordedDescription) {
63
                                                        return;
64
                                                }
65
                                                recordedDescriptionsComboBox
66
                                                                .setSelectedItem(USER_DEFINED_LABEL);
67
                                        }
68
                                }));
69
        }
70

    
71
        /**
72
         * Prevent GridBagLayout from collapsing labels when their minimum width
73
         * requirement is not satisfied.
74
         */
75
        private void fixLabelMinimumSizes() {
76
                userNameLabel.setMinimumSize(new Dimension(0, 0));
77
                createdLabel.setMinimumSize(new Dimension(0, 0));
78
                updatedLabel.setMinimumSize(new Dimension(0, 0));
79
        }
80

    
81
        private boolean transferringRecordedDescription = false;
82

    
83
        private void updateRecordedDescriptionsComboBox() {
84
                recordedDescriptionsComboBox
85
                                .setModel(new DefaultComboBoxModel(
86
                                                ToolboxModel.instance(context).getSession() == null ? new Object[] {}
87
                                                                : insertUserDefinedLabel(
88
                                                                                (List) FUTURE_CollectionUtil
89
                                                                                                .concatenate(
90
                                                                                                                (Collection) ToolboxModel
91
                                                                                                                                .instance(
92
                                                                                                                                                context)
93
                                                                                                                                .getSession()
94
                                                                                                                                .getBlackboard()
95
                                                                                                                                .get(
96
                                                                                                                                                NewSessionPlugIn.ISSUE_LOG_DESCRIPTIONS),
97
                                                                                                                recordedDescriptions()))
98
                                                                                .toArray()));
99
        }
100

    
101
        private List insertUserDefinedLabel(List recordedDescriptions) {
102
                List newRecordedDescriptions = new ArrayList();
103
                newRecordedDescriptions.add(USER_DEFINED_LABEL);
104
                newRecordedDescriptions.addAll(recordedDescriptions);
105
                return newRecordedDescriptions;
106
        }
107

    
108
        public void setType(String type) {
109
                typeComboBox.setSelectedItem(type);
110
        }
111

    
112
        public String getType() {
113
                return (String) typeComboBox.getSelectedItem();
114
        }
115

    
116
        public void setStatus(String status) {
117
                statusComboBox.setSelectedItem(status);
118
        }
119

    
120
        public String getStatus() {
121
                return (String) statusComboBox.getSelectedItem();
122
        }
123

    
124
        private List recordedDescriptions() {
125
                return (java.util.List) PersistentBlackboardPlugIn.get(context).get(
126
                                getClass().getName() + " - RECORDED DESCRIPTIONS",
127
                                new ArrayList());
128
        }
129

    
130
        public void setCreatedDate(Date createdDate) {
131
                createdLabel.setText("<html><b>Created:</b> " + string(createdDate)
132
                                + "</html>");
133
        }
134

    
135
        public void setUpdatedDate(Date updatedDate) {
136
                updatedLabel.setText("<html><b>Updated:</b> " + string(updatedDate)
137
                                + "</html>");
138
        }
139

    
140
        private String string(Date date) {
141
                return new SimpleDateFormat("d-MMM-yyyy").format(date);
142
        }
143

    
144
        public String getDescription() {
145
                return descriptionTextArea.getText().trim();
146
        }
147

    
148
        public String getComment() {
149
                return commentTextArea.getText().trim();
150
        }
151

    
152
        void jbInit() throws Exception {
153
                this.setLayout(gridBagLayout1);
154
                metadataPanel.setLayout(gridBagLayout2);
155
                typeAndStatusPanel.setLayout(gridBagLayout3);
156
                typeLabel.setText("Type: ");
157
                statusLabel.setText("Status: ");
158
                userNameLabel.setText("User: abcdefg");
159
                createdLabel.setText("Created: abcdefg");
160
                updatedLabel.setText("Updated: abcdefg");
161
                descriptionPanel.setLayout(gridBagLayout4);
162
                recordDescriptionButton.setMargin(new Insets(2, 2, 2, 2));
163
                recordDescriptionButton.setText("Record");
164
                recordDescriptionButton
165
                                .addActionListener(new java.awt.event.ActionListener() {
166

    
167
                                        public void actionPerformed(ActionEvent e) {
168
                                                recordDescriptionButton_actionPerformed(e);
169
                                        }
170
                                });
171
                recordDescriptionPanel.setLayout(gridBagLayout6);
172
                descriptionTextArea.setLineWrap(true);
173
                descriptionTextArea.setWrapStyleWord(true);
174
                descriptionLabel.setToolTipText("");
175
                descriptionLabel.setText("Description: ");
176
                recordedDescriptionsComboBox
177
                                .addActionListener(new java.awt.event.ActionListener() {
178

    
179
                                        public void actionPerformed(ActionEvent e) {
180
                                                recordedDescriptionsComboBox_actionPerformed(e);
181
                                        }
182
                                });
183
                descriptionScrollPane.setPreferredSize(new Dimension(377, 50));
184
                commentScrollPane.setPreferredSize(new Dimension(377, 100));
185
                commentTextArea.setLineWrap(true);
186
                commentTextArea.setWrapStyleWord(true);
187
                commentLabel.setText("Comment:");
188
                commentPanel.setLayout(gridBagLayout5);
189
                this.add(metadataPanel, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0,
190
                                GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,
191
                                                0, 0, 0), 0, 0));
192
                this.add(typeAndStatusPanel, new GridBagConstraints(0, 0, 1, 1, 0.0,
193
                                0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
194
                                new Insets(0, 0, 0, 0), 0, 0));
195
                typeAndStatusPanel.add(typeLabel, new GridBagConstraints(0, 0, 1, 1,
196
                                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
197
                                new Insets(0, 0, 0, 0), 0, 0));
198
                typeAndStatusPanel.add(typeComboBox, new GridBagConstraints(1, 0, 1, 1,
199
                                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
200
                                new Insets(0, 0, 0, 0), 0, 0));
201
                typeAndStatusPanel.add(statusLabel, new GridBagConstraints(2, 0, 1, 1,
202
                                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
203
                                new Insets(0, 10, 0, 0), 0, 0));
204
                typeAndStatusPanel.add(statusComboBox, new GridBagConstraints(3, 0, 1,
205
                                1, 0.0, 0.0, GridBagConstraints.CENTER,
206
                                GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
207
                this.add(descriptionPanel, new GridBagConstraints(0, 8, 1, 1, 1.0, 0.0,
208
                                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
209
                                new Insets(5, 0, 0, 0), 0, 0));
210
                descriptionPanel.add(descriptionScrollPane, new GridBagConstraints(1,
211
                                1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
212
                                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
213
                descriptionPanel.add(recordDescriptionPanel, new GridBagConstraints(1,
214
                                0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
215
                                GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
216
                descriptionScrollPane.getViewport().add(descriptionTextArea, null);
217
                recordDescriptionPanel.add(descriptionLabel, new GridBagConstraints(0,
218
                                0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
219
                                GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
220
                recordDescriptionPanel.add(recordedDescriptionsComboBox,
221
                                new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
222
                                                GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
223
                                                new Insets(0, 0, 0, 0), 0, 0));
224
                recordDescriptionButton
225
                                .setToolTipText("Saves the description below to the dropdown on the left");
226
                recordDescriptionPanel.add(recordDescriptionButton,
227
                                new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
228
                                                GridBagConstraints.CENTER, GridBagConstraints.NONE,
229
                                                new Insets(0, 5, 0, 0), 0, 0));
230
                this.add(commentPanel, new GridBagConstraints(0, 10, 1, 1, 1.0, 1.0,
231
                                GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
232
                                                5, 0, 0, 0), 0, 0));
233
                commentPanel.add(commentLabel, new GridBagConstraints(0, 0, 1, 1, 0.0,
234
                                0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
235
                                new Insets(0, 0, 0, 0), 0, 0));
236
                commentPanel.add(commentScrollPane, new GridBagConstraints(0, 1, 1, 1,
237
                                1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
238
                                new Insets(0, 0, 0, 0), 0, 0));
239
                commentScrollPane.getViewport().add(commentTextArea, null);
240
                metadataPanel.add(userNameLabel, new GridBagConstraints(0, 0, 1, 1,
241
                                0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
242
                                new Insets(0, 0, 0, 10), 0, 0));
243
                metadataPanel.add(createdLabel, new GridBagConstraints(1, 0, 1, 1, 0.0,
244
                                0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
245
                                new Insets(0, 0, 0, 10), 0, 0));
246
                metadataPanel.add(updatedLabel, new GridBagConstraints(2, 0, 1, 1, 0.0,
247
                                0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
248
                                new Insets(0, 0, 0, 0), 0, 0));
249
        }
250

    
251
        public void setDescription(String description) {
252
                descriptionTextArea.setText(description);
253
                recordedDescriptionsComboBox.setSelectedItem(getDescription());
254
        }
255

    
256
        public void setComment(String comment) {
257
                commentTextArea.setText(comment);
258
        }
259

    
260
        private GridBagLayout gridBagLayout1 = new GridBagLayout();
261

    
262
        private JPanel metadataPanel = new JPanel();
263

    
264
        private GridBagLayout gridBagLayout2 = new GridBagLayout();
265

    
266
        private JPanel typeAndStatusPanel = new JPanel();
267

    
268
        private GridBagLayout gridBagLayout3 = new GridBagLayout();
269

    
270
        private JLabel typeLabel = new JLabel();
271

    
272
        private JComboBox typeComboBox = new JComboBox() {
273

    
274
                {
275
                        setRenderer(new DefaultListCellRenderer() {
276

    
277
                                private Icon warningIcon = GUIUtil.toSmallIcon(IconLoader
278
                                                .icon("Caution.gif"));
279

    
280
                                private Icon errorIcon = GUIUtil.toSmallIcon(IconLoader
281
                                                .icon("Delete.gif"));
282

    
283
                                private Icon commentIcon = GUIUtil.toSmallIcon(IconLoader
284
                                                .icon("Draw.gif"));
285

    
286
                                public Component getListCellRendererComponent(JList list,
287
                                                Object value, int index, boolean isSelected,
288
                                                boolean cellHasFocus) {
289
                                        try {
290
                                                return super.getListCellRendererComponent(list, value,
291
                                                                index, isSelected, cellHasFocus);
292
                                        } finally {
293
                                                setIcon(value
294
                                                                .equals(IssueLog.AttributeValues.COMMENT_TYPE) ? commentIcon
295
                                                                : value
296
                                                                                .equals(IssueLog.AttributeValues.WARNING_TYPE) ? warningIcon
297
                                                                                : value
298
                                                                                                .equals(IssueLog.AttributeValues.ERROR_TYPE) ? errorIcon
299
                                                                                                : null);
300
                                        }
301
                                }
302
                        });
303
                }
304
        };
305

    
306
        private JLabel statusLabel = new JLabel();
307

    
308
        private JComboBox statusComboBox = new JComboBox();
309

    
310
        private JLabel userNameLabel = new JLabel();
311

    
312
        private JLabel createdLabel = new JLabel();
313

    
314
        private JLabel updatedLabel = new JLabel();
315

    
316
        private JPanel descriptionPanel = new JPanel();
317

    
318
        private GridBagLayout gridBagLayout6 = new GridBagLayout();
319

    
320
        private JButton recordDescriptionButton = new JButton();
321

    
322
        private GridBagLayout gridBagLayout4 = new GridBagLayout();
323

    
324
        private JPanel recordDescriptionPanel = new JPanel();
325

    
326
        private JTextArea descriptionTextArea = new JTextArea();
327

    
328
        private JLabel descriptionLabel = new JLabel();
329

    
330
        private JComboBox recordedDescriptionsComboBox = new JComboBox() {
331

    
332
                {
333
                        setRenderer(new DefaultListCellRenderer() {
334

    
335
                                public Component getListCellRendererComponent(JList list,
336
                                                Object value, int index, boolean isSelected,
337
                                                boolean cellHasFocus) {
338
                                        return super.getListCellRendererComponent(list,
339
                                                        value != null ? StringUtil.replaceAll(StringUtil
340
                                                                        .replaceAll((String) value, "\n", " "),
341
                                                                        "\r", " ") : value, index, isSelected,
342
                                                        cellHasFocus);
343
                                }
344
                        });
345
                }
346
        };
347

    
348
        private JScrollPane descriptionScrollPane = new JScrollPane();
349

    
350
        private JTextArea commentTextArea = new JTextArea();
351

    
352
        private JLabel commentLabel = new JLabel();
353

    
354
        private GridBagLayout gridBagLayout5 = new GridBagLayout();
355

    
356
        private JPanel commentPanel = new JPanel();
357

    
358
        private JScrollPane commentScrollPane = new JScrollPane();
359

    
360
        void recordedDescriptionsComboBox_actionPerformed(ActionEvent e) {
361
                if (recordedDescriptionsComboBox.getSelectedItem().equals(
362
                                USER_DEFINED_LABEL)) {
363
                        return;
364
                }
365
                transferringRecordedDescription = true;
366
                try {
367
                        descriptionTextArea.setText((String) recordedDescriptionsComboBox
368
                                        .getSelectedItem());
369
                } finally {
370
                        transferringRecordedDescription = false;
371
                }
372
        }
373

    
374
        public JTextArea getDescriptionTextArea() {
375
                return descriptionTextArea;
376
        }
377

    
378
        public void setUserName(String userName) {
379
                userNameLabel.setText("<html><b>User:</b> "
380
                                + LangUtil.ifNull(userName, "") + "</html>");
381
        }
382

    
383
        public static IssueLogPanel prompt(String title, String description,
384
                        String comment, String status, String type, Date createdDate,
385
                        Date modifiedDate, String userName, ButtonPanel buttonPanel,
386
                        WorkbenchContext workbenchContext) {
387
                final IssueLogPanel issueLogPanel = new IssueLogPanel(workbenchContext);
388
                issueLogPanel.setDescription(description);
389
                issueLogPanel.setComment(comment);
390
                issueLogPanel.setStatus(status);
391
                issueLogPanel.setUserName(userName);
392
                issueLogPanel.setType(type);
393
                issueLogPanel.setCreatedDate(createdDate);
394
                issueLogPanel.setUpdatedDate(modifiedDate);
395
                final JDialog dialog = new JDialog(workbenchContext.getWorkbench()
396
                                .getFrame(), title, true);
397
                dialog.getContentPane().setLayout(new BorderLayout());
398
                dialog.getContentPane().add(issueLogPanel, BorderLayout.CENTER);
399
                dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
400
                buttonPanel.addActionListener(new ActionListener() {
401

    
402
                        public void actionPerformed(ActionEvent e) {
403
                                dialog.setVisible(false);
404
                        }
405
                });
406
                dialog.pack();
407
                dialog.addWindowListener(new WindowAdapter() {
408

    
409
                        public void windowOpened(WindowEvent e) {
410
                                issueLogPanel.getDescriptionTextArea().requestFocus();
411
                        }
412
                });
413
                GUIUtil.centreOnWindow(dialog);
414
                dialog.setVisible(true);
415
                return issueLogPanel;
416
        }
417

    
418
        void recordDescriptionButton_actionPerformed(ActionEvent e) {
419
                if (getDescription().length() == 0) {
420
                        return;
421
                }
422
                recordedDescriptions().remove(getDescription());
423
                recordedDescriptions().add(0, getDescription());
424
                if (recordedDescriptions().size() > MAX_RECORDED_DESCRIPTIONS_COUNT) {
425
                        recordedDescriptions().subList(MAX_RECORDED_DESCRIPTIONS_COUNT,
426
                                        recordedDescriptions().size()).clear();
427
                }
428
                updateRecordedDescriptionsComboBox();
429
                recordedDescriptionsComboBox.setSelectedItem(getDescription());
430
        }
431
}