OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 150 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 150 Rev 153
Line 79... Line 79...
79
            spacer.setMinimumSize(new Dimension(3, 5));
79
            spacer.setMinimumSize(new Dimension(3, 5));
80
            spacer.setPreferredSize(new Dimension(3, 5));
80
            spacer.setPreferredSize(new Dimension(3, 5));
81
            this.add(spacer);
81
            this.add(spacer);
82
        }
82
        }
83
 
83
 
-
 
84
        String iconFileName = "calendar.png";
-
 
85
        if (getFont().getSize() > 16) {
-
 
86
            iconFileName = "calendar_2x.png";
-
 
87
        }
84
        final ImageIcon icon = new ImageIcon(this.getClass().getResource("calendar.png"));
88
        final ImageIcon icon = new ImageIcon(this.getClass().getResource(iconFileName));
85
        button = new JButton(new AbstractAction(null, icon) {
89
        button = new JButton(new AbstractAction(null, icon) {
86
 
90
 
87
            @Override
91
            @Override
88
            public void actionPerformed(ActionEvent e) {
92
            public void actionPerformed(ActionEvent e) {
89
                final JComponent source = (JComponent) e.getSource();
-
 
90
 
-
 
91
                if (text != null) {
-
 
92
                    // if the button isn't focusable, no FOCUS_LOST will occur and the text won't
-
 
93
                    // get
-
 
94
                    // committed (or reverted). So act as if the user requested a commit, so that
-
 
95
                    // invalidEdit() can be called (usually causing a beep).
-
 
96
                    if (!source.isFocusable()) {
-
 
97
                        for (final Action a : text.getActions()) {
-
 
98
                            final String name = (String) a.getValue(Action.NAME);
-
 
99
                            if (JTextField.notifyAction.equals(name))
-
 
100
                                a.actionPerformed(new ActionEvent(text, e.getID(), null));
-
 
101
                        }
-
 
102
                    }
-
 
103
 
-
 
104
                    // if after trying to commit, the value is invalid then don't show the popup
-
 
105
                    if (!text.isEditValid())
-
 
106
                        return;
-
 
107
                }
-
 
108
                if (dialog == null) {
-
 
109
                    final JDialog d = new JDialog(SwingUtilities.getWindowAncestor(DatePicker.this));
-
 
110
                    pickerPanel = new DatePickerPanel();
-
 
111
 
-
 
112
                    d.setContentPane(pickerPanel);
-
 
113
                    d.setUndecorated(true);
-
 
114
                    pickerPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
-
 
115
                    d.setSize(330, 270);
-
 
116
 
-
 
117
                    pickerPanel.addPropertyChangeListener(DatePickerPanel.TIME_IN_MILLIS, new PropertyChangeListener() {
-
 
118
 
-
 
119
                        @Override
-
 
120
                        public void propertyChange(PropertyChangeEvent evt) {
-
 
121
                            if (!DatePicker.this.isEditable())
-
 
122
                                return;
-
 
123
                            final Long millis = (Long) evt.getNewValue();
-
 
124
                            final Calendar c = Calendar.getInstance();
-
 
125
                            c.setTimeInMillis(millis);
-
 
126
                            setDate(c.getTime());
-
 
127
                            if (dialog != null) {
-
 
128
                                dialog.dispose();
-
 
129
                                dialog = null;
-
 
130
                            }
-
 
131
                            fireActionPerformed();
-
 
132
                        }
-
 
133
 
-
 
134
                    });
-
 
135
                    d.addWindowFocusListener(new WindowFocusListener() {
-
 
136
 
-
 
137
                        public void windowGainedFocus(WindowEvent e) {
-
 
138
                            // do nothing
-
 
139
                        }
-
 
140
 
-
 
141
                        public void windowLostFocus(WindowEvent e) {
-
 
142
                            if (dialog != null) {
-
 
143
                                final Window oppositeWindow = e.getOppositeWindow();
-
 
144
                                if (oppositeWindow != null && SwingUtilities.isDescendingFrom(oppositeWindow, dialog)) {
-
 
145
                                    return;
-
 
146
                                }
-
 
147
                                dialog.dispose();
-
 
148
                                dialog = null;
-
 
149
                            }
-
 
150
                            dialogLostFocusTime = System.currentTimeMillis();
-
 
151
                        }
-
 
152
 
-
 
153
                    });
-
 
154
                    dialog = d;
-
 
155
                }
-
 
156
                // Set picker panel date
-
 
157
                final Calendar calendar = Calendar.getInstance();
-
 
158
                if (date != null) {
-
 
159
                    calendar.setTime(date);
93
                calendarButtonPressed(e);
160
                    pickerPanel.setSelectedDate(calendar);
-
 
161
                } else {
-
 
162
                    pickerPanel.setSelectedDate(null);
-
 
163
                }
-
 
164
                // Show dialog
-
 
165
                final int x = source.getLocation().x;
-
 
166
                final int y = source.getLocation().y + source.getHeight();
-
 
167
                Point p = new Point(x, y);
-
 
168
                SwingUtilities.convertPointToScreen(p, DatePicker.this);
-
 
169
                p = adjustPopupLocationToFitScreen(p.x, p.y, dialog.getSize(), source.getSize());
-
 
170
                dialog.setLocation(p.x, p.y);
-
 
171
                final long time = System.currentTimeMillis() - dialogLostFocusTime;
-
 
172
                if (time > RELEASE_TIME_MS) {
-
 
173
                    dialog.setVisible(true);
-
 
174
                } else {
-
 
175
                    dialogLostFocusTime = System.currentTimeMillis() - RELEASE_TIME_MS;
-
 
176
                }
-
 
177
            }
94
            }
178
 
95
 
179
        });
96
        });
180
        button.setContentAreaFilled(false);
97
        button.setContentAreaFilled(false);
181
        button.setOpaque(false);
98
        button.setOpaque(false);
Line 343... Line 260...
343
        if (text != null) {
260
        if (text != null) {
344
            this.text.commitEdit();
261
            this.text.commitEdit();
345
        }
262
        }
346
    }
263
    }
347
 
264
 
-
 
265
    private void calendarButtonPressed(ActionEvent e) {
-
 
266
        final JComponent source = (JComponent) e.getSource();
-
 
267
 
-
 
268
        if (text != null) {
-
 
269
            // if the button isn't focusable, no FOCUS_LOST will occur and the text won't
-
 
270
            // get
-
 
271
            // committed (or reverted). So act as if the user requested a commit, so that
-
 
272
            // invalidEdit() can be called (usually causing a beep).
-
 
273
            if (!source.isFocusable()) {
-
 
274
                for (final Action a : text.getActions()) {
-
 
275
                    final String name = (String) a.getValue(Action.NAME);
-
 
276
                    if (JTextField.notifyAction.equals(name))
-
 
277
                        a.actionPerformed(new ActionEvent(text, e.getID(), null));
-
 
278
                }
-
 
279
            }
-
 
280
 
-
 
281
            // if after trying to commit, the value is invalid then don't show the popup
-
 
282
            if (!text.isEditValid())
-
 
283
                return;
-
 
284
        }
-
 
285
        if (dialog == null) {
-
 
286
            final JDialog d = new JDialog(SwingUtilities.getWindowAncestor(DatePicker.this));
-
 
287
            pickerPanel = new DatePickerPanel();
-
 
288
 
-
 
289
            d.setContentPane(pickerPanel);
-
 
290
            d.setUndecorated(true);
-
 
291
            pickerPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
-
 
292
            d.pack();
-
 
293
 
-
 
294
            pickerPanel.addPropertyChangeListener(DatePickerPanel.TIME_IN_MILLIS, new PropertyChangeListener() {
-
 
295
 
-
 
296
                @Override
-
 
297
                public void propertyChange(PropertyChangeEvent evt) {
-
 
298
                    if (!DatePicker.this.isEditable())
-
 
299
                        return;
-
 
300
                    final Long millis = (Long) evt.getNewValue();
-
 
301
                    final Calendar c = Calendar.getInstance();
-
 
302
                    c.setTimeInMillis(millis);
-
 
303
                    setDate(c.getTime());
-
 
304
                    if (dialog != null) {
-
 
305
                        dialog.dispose();
-
 
306
                        dialog = null;
-
 
307
                    }
-
 
308
                    fireActionPerformed();
-
 
309
                }
-
 
310
 
-
 
311
            });
-
 
312
            d.addWindowFocusListener(new WindowFocusListener() {
-
 
313
 
-
 
314
                public void windowGainedFocus(WindowEvent e) {
-
 
315
                    // do nothing
-
 
316
                }
-
 
317
 
-
 
318
                public void windowLostFocus(WindowEvent e) {
-
 
319
                    if (dialog != null) {
-
 
320
                        final Window oppositeWindow = e.getOppositeWindow();
-
 
321
                        if (oppositeWindow != null && SwingUtilities.isDescendingFrom(oppositeWindow, dialog)) {
-
 
322
                            return;
-
 
323
                        }
-
 
324
                        dialog.dispose();
-
 
325
                        dialog = null;
-
 
326
                    }
-
 
327
                    dialogLostFocusTime = System.currentTimeMillis();
-
 
328
                }
-
 
329
 
-
 
330
            });
-
 
331
            dialog = d;
-
 
332
        }
-
 
333
        // Set picker panel date
-
 
334
        final Calendar calendar = Calendar.getInstance();
-
 
335
        if (date != null) {
-
 
336
            calendar.setTime(date);
-
 
337
            pickerPanel.setSelectedDate(calendar);
-
 
338
        } else {
-
 
339
            pickerPanel.setSelectedDate(null);
-
 
340
        }
-
 
341
        // Show dialog
-
 
342
        final int x = source.getLocation().x;
-
 
343
        final int y = source.getLocation().y + source.getHeight();
-
 
344
        Point p = new Point(x, y);
-
 
345
        SwingUtilities.convertPointToScreen(p, DatePicker.this);
-
 
346
        p = adjustPopupLocationToFitScreen(p.x, p.y, dialog.getSize(), source.getSize());
-
 
347
        dialog.setLocation(p.x, p.y);
-
 
348
        final long time = System.currentTimeMillis() - dialogLostFocusTime;
-
 
349
        if (time > RELEASE_TIME_MS) {
-
 
350
            dialog.setVisible(true);
-
 
351
        } else {
-
 
352
            dialogLostFocusTime = System.currentTimeMillis() - RELEASE_TIME_MS;
-
 
353
        }
-
 
354
    }
-
 
355
 
348
    static public final boolean equals(Object o1, Object o2) {
356
    public static final boolean equals(Object o1, Object o2) {
349
        if (o1 == null && o2 == null)
357
        if (o1 == null && o2 == null)
350
            return true;
358
            return true;
351
        if (o1 == null || o2 == null)
359
        if (o1 == null || o2 == null)
352
            return false;
360
            return false;
353
        return o1.equals(o2);
361
        return o1.equals(o2);