Skip to main content
summaryrefslogtreecommitdiffstats
blob: b4a790af0fca9ffde8ac89a3e4a06add562a096b (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*******************************************************************************
 * Copyright (c) 2004, 2007 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;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
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.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

public abstract class XFlatWidget<T> extends XLabel {

   private FlatControl flatControl;
   private StyledText currentPageLabel;
   private Composite container;
   private Label messageLabel;
   private Label messageIcon;
   private int minPage;
   private int maxPage;

   public XFlatWidget(String displayLabel) {
      super(displayLabel);
      setToolTip("Navigate pages by clicking forward and backward buttons.");
      minPage = 0;
      maxPage = 0;
   }

   @Override
   public void dispose() {
      flatControl.dispose();
      super.dispose();
   }

   public Collection<String> getPageIds() {
      return flatControl.pageIds;
   }

   protected void setPageRange(int minPage, int maxPage) throws OseeArgumentException {
      if (minPage < 0) {
         throw new OseeArgumentException("Min Number of Pages must be greater than 0");
      }
      if (maxPage < 1) {
         throw new OseeArgumentException("Max Number of Pages must be at least 1");
      }

      if (maxPage < minPage) {
         throw new OseeArgumentException(
            String.format("Invalid required number of pages [%s] < [%s]", maxPage, minPage));
      }
      this.minPage = minPage;
      this.maxPage = maxPage;
   }

   @Override
   public Control getControl() {
      return flatControl.flatComposite;
   }

   @Override
   public void setToolTip(String toolTip) {
      if (Strings.isValid(toolTip)) {
         super.setToolTip(toolTip);
      }
   }

   @Override
   public void refresh() {
      updateCurrentPageLabel();
      flatControl.refresh();
   }

   @Override
   protected void createControls(final Composite parent, int horizontalSpan) {
      container = new Composite(parent, SWT.NONE);
      GridLayout layout = new GridLayout(isDisplayLabel() ? 2 : 1, false);
      layout.marginHeight = 0;
      layout.marginWidth = 0;
      layout.marginRight = 0;
      container.setLayout(layout);
      container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      if (isDisplayLabel() && Strings.isValid(getLabel())) {
         labelWidget = new Label(container, SWT.NONE);
         labelWidget.setText(String.format("%s:", getLabel()));
         labelWidget.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
         if (getToolTip() != null) {
            labelWidget.setToolTipText(getToolTip());
         }
      }

      Composite composite = new Composite(container, SWT.NONE);
      GridLayout layout1 = new GridLayout(1, false);
      layout1.marginHeight = 0;
      layout1.marginWidth = 0;
      layout1.verticalSpacing = 0;
      layout1.horizontalSpacing = 0;
      composite.setLayout(layout1);
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      createToolBar(composite);
      flatControl = new FlatControl();
      flatControl.createControl(composite);
      createMessageArea(composite);

      addToolTip(container, getToolTip());
      refresh();
   }

   private void createMessageArea(Composite parent) {
      Composite messageArea = new Composite(parent, SWT.BORDER);
      GridLayout layout = new GridLayout(2, false);
      layout.marginHeight = 0;
      layout.marginWidth = 0;
      layout.marginLeft = 5;
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      layout.marginBottom = 5;

      messageArea.setLayout(layout);
      messageArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      messageArea.setBackground(Displays.getSystemColor(SWT.COLOR_INFO_BACKGROUND));

      messageIcon = new Label(messageArea, SWT.NONE);
      messageIcon.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));

      messageLabel = new Label(messageArea, SWT.NONE);
      messageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
   }

   private void createToolBar(Composite parent) {
      Composite composite = new Composite(parent, SWT.BORDER);
      GridLayout layout = new GridLayout(3, false);
      layout.marginHeight = 0;
      layout.marginLeft = 5;
      layout.marginWidth = 2;
      composite.setLayout(layout);
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      currentPageLabel = new StyledText(composite, SWT.READ_ONLY | SWT.SINGLE | SWT.WRAP);
      currentPageLabel.setAlignment(SWT.RIGHT);
      currentPageLabel.setFont(JFaceResources.getBannerFont());
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
      gd.minimumWidth = 10;
      currentPageLabel.setLayoutData(gd);
      currentPageLabel.setText("0 Items");

      Composite filler = new Composite(composite, SWT.NONE);
      GridLayout layout1 = new GridLayout(1, false);
      filler.setLayout(layout1);
      filler.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

      ToolBar toolbar = new ToolBar(composite, SWT.FLAT | SWT.HORIZONTAL);
      toolbar.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));
      ToolBarManager manager = new ToolBarManager(toolbar);
      manager.add(new Separator());
      manager.add(new AddPage());
      manager.update(true);
   }

   private void addToolTip(Control control, String toolTipText) {
      if (Strings.isValid(toolTipText)) {
         control.setToolTipText(toolTipText);
         if (control instanceof Composite) {
            for (Control child : ((Composite) control).getChildren()) {
               child.setToolTipText(toolTipText);
            }
         }
      }
   }

   protected int getTotalItems() {
      return flatControl.getTotalPages();
   }

   private void updateCurrentPageLabel() {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (Widgets.isAccessible(currentPageLabel)) {
               int totalPages = getTotalItems();
               currentPageLabel.setText(String.format("%s Items", totalPages));
            }
         }
      });
   }

   public void addPage(T value) {
      flatControl.addPage(value);
   }

   private void setMessage(final int severity, final String message) {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (Widgets.isAccessible(messageLabel)) {
               Composite parent = messageLabel.getParent();

               String text = message;
               boolean isVisible = Strings.isValid(text);

               String imageName = null;
               switch (severity) {
                  case IStatus.INFO:
                     imageName = ISharedImages.IMG_OBJS_INFO_TSK;
                     break;
                  case IStatus.ERROR:
                     imageName = ISharedImages.IMG_OBJS_ERROR_TSK;
                     break;
                  case IStatus.WARNING:
                     imageName = ISharedImages.IMG_OBJS_WARN_TSK;
                     break;
                  default:
                     break;
               }
               Image image =
                  Strings.isValid(imageName) ? PlatformUI.getWorkbench().getSharedImages().getImage(imageName) : null;
               messageIcon.setImage(image);
               messageLabel.setText(isVisible ? " " + text : text);

               messageIcon.setVisible(isVisible);
               messageLabel.setVisible(isVisible);
               parent.setVisible(isVisible);
               parent.layout();
            }
         }
      });
   }

   protected abstract void createPage(String id, Composite parent, T value);

   protected abstract void onRemovePage(String id);

   private final class AddPage extends Action {
      public AddPage() {
         super();
         setImageDescriptor(ImageManager.getImageDescriptor(FrameworkImage.ADD_GREEN));
         setToolTipText("Adds a page");
      }

      @Override
      public void run() {
         flatControl.addPage((T) null);
      }
   }

   private final class FlatControl {
      private final List<String> pageIds;
      private Composite flatComposite;
      private final Map<String, Composite> pages = new HashMap<String, Composite>();

      public FlatControl() {
         this.pageIds = new ArrayList<String>();
      }

      private void createControl(Composite parent) {
         pageIds.clear();
         flatComposite = new Composite(parent, SWT.BORDER);
         flatComposite.setLayout(ALayout.getZeroMarginLayout());
         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
         gd.minimumWidth = 60;
         flatComposite.setLayoutData(gd);
         flatComposite.layout();
      }

      public void dispose() {
         pageIds.clear();
         Widgets.disposeWidget(flatComposite);
      }

      public void refresh() {
         flatComposite.layout();
         flatComposite.getParent().layout();
      }

      private int getTotalPages() {
         return Widgets.isAccessible(flatComposite) ? pages.size() : 0;
      }

      private void addPage(T value) {
         int numberOfPages = getTotalPages();
         IStatus status = validate(numberOfPages + 1);
         if (status.isOK()) {
            setMessage(IStatus.OK, "");
            final String pageId = GUID.create();
            if (pageIds.add(pageId)) {
               Composite composite = new Composite(flatComposite, SWT.WRAP | SWT.BORDER);
               composite.setLayout(new GridLayout(3, false));
               composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

               createPage(pageId, composite, value);

               Button button = new Button(composite, SWT.FLAT);
               button.setImage(ImageManager.getImage(FrameworkImage.DELETE));
               button.addSelectionListener(new SelectionAdapter() {
                  @Override
                  public void widgetSelected(SelectionEvent e) {
                     removePage(pageId);
                  }
               });

               pages.put(pageId, composite);
               notifyXModifiedListeners();
            } else {
               setMessage(IStatus.WARNING, "Add page error");
            }
         } else {
            setMessage(IStatus.ERROR, status.getMessage());
         }
      }

      private void removePage(String pageId) {
         int numberOfPages = getTotalPages();
         IStatus status = validate(numberOfPages - 1);
         if (status.isOK()) {
            setMessage(IStatus.OK, "");

            pageIds.remove(pageId);
            if (pageId != null) {
               onRemovePage(pageId);
               Control control = pages.remove(pageId);
               Widgets.disposeWidget(control);
               notifyXModifiedListeners();
            } else {
               setMessage(IStatus.WARNING, "Remove page error");
            }
         } else {
            setMessage(IStatus.ERROR, status.getMessage());
         }
      }

      private IStatus validate(int numberOfPages) {
         IStatus status = null;
         if (minPage <= numberOfPages && maxPage >= numberOfPages) {
            status = Status.OK_STATUS;
         } else {
            List<String> message = new ArrayList<String>();
            if (numberOfPages < minPage) {
               message.add(String.format("Must have at least [%s] page%s", minPage, minPage == 1 ? "" : "s"));
            }
            if (numberOfPages > maxPage) {
               message.add(String.format("Can't add more than [%s] page%s", maxPage, maxPage == 1 ? "" : "s"));
            }
            status = new Status(IStatus.ERROR, SkynetGuiPlugin.PLUGIN_ID, Collections.toString(" &", message));
         }
         return status;
      }
   }

}

Back to the top