Skip to main content
summaryrefslogtreecommitdiffstats
blob: 39877a5dbc58c9e5e38aae16fbfd10e9c6564f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (c) 2012 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.widgets.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * @author Roberto E. Escobar
 */
public class CalendarWidget extends Composite {

   public static interface CalendarListener {
      void dateChanged(Calendar calendar);
   }

   private final List<CalendarListener> listeners = new CopyOnWriteArrayList<CalendarListener>();
   private Text text;
   private Calendar calendar;

   public CalendarWidget(Composite parent, int style) {
      super(parent, SWT.NONE);
      GridLayout gridLayout = ALayout.getZeroMarginLayout();
      gridLayout.horizontalSpacing = 0;
      this.setLayout(gridLayout);
      this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      calendar = Calendar.getInstance();
      calendar.clear();
      createControl(this);
   }

   private void createControl(final Composite parent) {
      GridLayout layout = ALayout.getZeroMarginLayout(3, false);
      layout.horizontalSpacing = 0;

      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(layout);
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      text = new Text(composite, SWT.SINGLE | SWT.BORDER);
      text.setEditable(false);
      createButtons(composite);
      setCalendar(getCalendar());
   }

   private void createButtons(final Composite parent) {
      final ToolBar toolBar = new ToolBar(parent, SWT.FLAT);

      ToolItem dropDown = new ToolItem(toolBar, SWT.DROP_DOWN);
      dropDown.setImage(ImageManager.getImage(FrameworkImage.CALENDAR));
      dropDown.setToolTipText("click to select date");
      dropDown.addSelectionListener(new SelectionAdapter() {
         DateTimePanel panel = null;

         @Override
         public void widgetSelected(SelectionEvent event) {
            if (panel == null) {
               try {
                  panel = new DateTimePanel(parent);
                  panel.setVisible(false);
                  panel.pack();
               } catch (Exception ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);
               }
            }
            final ToolItem toolItem = (ToolItem) event.widget;
            Rectangle rect = toolItem.getBounds();
            Point pt = new Point(rect.x - 1, rect.y + rect.height - 1);
            pt = text.toDisplay(pt);

            panel.setLocation(pt.x, pt.y);
            panel.setVisible(true);
            panel.forceFocus();
         }
      });
   }

   public Calendar getCalendar() {
      return calendar;
   }

   public Date getDate() {
      return getCalendar().getTime();
   }

   public boolean isValid() {
      return calendar.isSet(Calendar.DATE);
   }

   public void setCalendar(Calendar calendar) {
      if (calendar != null) {
         this.calendar = calendar;
         String current = text.getText();
         if (current == null) {
            current = "";
         }
         String newText = getDateAsString();
         if (!current.equals(newText)) {
            setText(newText);
            notifyListeners();
         }
      } else {
         clearText();
      }
   }

   public void setDate(Date date) {
      if (date != null) {
         Calendar calendar = getCalendar();
         calendar.setTime(date);
         setCalendar(calendar);
      } else {
         clearText();
      }
   }

   protected void setText(String string) {
      if (Widgets.isAccessible(text)) {
         text.setText(string);
         text.selectAll();
      }
   }

   protected void clearText() {
      Calendar calendar = getCalendar();
      calendar.clear();
      setText("");
      notifyListeners();
   }

   public String getDateAsString() {
      String toReturn = "";
      if (isValid()) {
         Calendar calendar = getCalendar();
         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
         try {
            toReturn = sdf.format(calendar.getTime());
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      return toReturn;
   }

   private void notifyListeners() {
      final Calendar calendar = isValid() ? (Calendar) getCalendar().clone() : null;
      for (CalendarListener listener : listeners) {
         try {
            listener.dateChanged(calendar);
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

   }

   public void addCalendarListener(CalendarListener listener) {
      if (listener != null) {
         if (!listeners.contains(listener)) {
            listeners.add(listener);
         }
      }
   }

   public void removeCalendarListener(CalendarListener listener) {
      if (listener != null) {
         listeners.remove(listener);
      }
   }

   public void addModifyListener(ModifyListener listener) {
      if (listener != null && text != null) {
         text.addModifyListener(listener);
      }
   }

   public void removeModifyListener(ModifyListener listener) {
      if (listener != null && text != null) {
         text.removeModifyListener(listener);
      }
   }

   private final class DateTimePanel {

      private final Shell shell;
      private DateTime dateTime;

      public DateTimePanel(Composite parent) {
         shell = new Shell(parent.getShell(), SWT.MENU | SWT.BORDER | SWT.NO_TRIM | SWT.SHEET);
         GridLayout layout = ALayout.getZeroMarginLayout();

         shell.setLayout(layout);
         shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
         shell.setBackground(Displays.getSystemColor(SWT.COLOR_WHITE));
         createControl(shell);

         parent.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
               if (isShellValid()) {
                  shell.dispose();
               }
            }
         });
      }

      private boolean isShellValid() {
         return Widgets.isAccessible(shell);
      }

      public void forceFocus() {
         if (isShellValid()) {
            shell.forceFocus();
         }
      }

      public void setLocation(int x, int y) {
         if (isShellValid()) {
            shell.setLocation(x, y);
         }
      }

      public void pack() {
         if (isShellValid()) {
            shell.pack();
         }
      }

      public void setVisible(boolean visible) {
         if (visible && Widgets.isAccessible(dateTime)) {
            if (isValid()) {
               Calendar calendar = getCalendar();
               dateTime.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                  calendar.get(Calendar.DAY_OF_MONTH));
            }
            dateTime.setFocus();
         }
         if (isShellValid()) {
            shell.setVisible(visible);
         }
      }

      private void createControl(Composite parent) {
         dateTime = new DateTime(parent, SWT.CALENDAR);

         Composite buttons = new Composite(parent, SWT.NONE);
         GridLayout layout = ALayout.getZeroMarginLayout(3, false);
         layout.horizontalSpacing = 0;
         buttons.setLayout(layout);
         buttons.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
         buttons.setBackground(Displays.getSystemColor(SWT.COLOR_WHITE));

         Button clear = new Button(buttons, SWT.PUSH);
         clear.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
         clear.setImage(ImageManager.getImage(FrameworkImage.ERASE));
         clear.setToolTipText("clear date field");
         clear.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
               clearText();
               if (Widgets.isAccessible(text)) {
                  text.setFocus();
               }
               setVisible(false);
            }
         });

         Button ok = new Button(buttons, SWT.PUSH);
         ok.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
         ok.setText("Ok");
         ok.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
               Calendar calendar = Calendar.getInstance();
               calendar.set(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay());
               setCalendar(calendar);
               if (Widgets.isAccessible(text)) {
                  text.setFocus();
               }
               setVisible(false);
            }
         });
         Button cancel = new Button(buttons, SWT.PUSH);
         cancel.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
         cancel.setText("Cancel");
         cancel.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
               if (Widgets.isAccessible(text)) {
                  text.setFocus();
               }
               setVisible(false);
            }
         });
      }
   }

}

Back to the top