Skip to main content
summaryrefslogtreecommitdiffstats
blob: 82002fba145b05800d7a2b0e16a4264127d8eb65 (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
/*******************************************************************************
 * 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.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.FilteredCheckboxTreeDialog;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

/**
 * @author Roberto E. Escobar
 */
public abstract class XSelectFromDialog<T> extends XText {

   private final List<T> selected = new ArrayList<T>();
   private final List<T> input = new ArrayList<T>();
   private int minSelectionRequired, maxSelectionRequired = 1;
   private Button selectionButton;

   public XSelectFromDialog(String displayLabel) {
      super(displayLabel);
      setToolTip("Click the button on the left to change the current selection.");
   }

   public void setRequiredSelection(int minSelectionRequired, int maxSelectionRequired) throws OseeArgumentException {
      if (minSelectionRequired < 0) {
         throw new OseeArgumentException("Min Number of Selection must be greater than or equal to 0");
      }
      if (maxSelectionRequired < 1) {
         throw new OseeArgumentException("Max Number of Selection must be at least 1");
      }

      if (maxSelectionRequired < minSelectionRequired) {
         throw new OseeArgumentException("Invalid required number of selections [%s] < [%s]", maxSelectionRequired,
            minSelectionRequired);
      }
      this.minSelectionRequired = minSelectionRequired;
      this.maxSelectionRequired = maxSelectionRequired;
   }

   @Override
   protected int getTextStyle() {
      return SWT.READ_ONLY | SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL;
   }

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

   @Override
   public Object getData() {
      return getSelected();
   }

   @Override
   public void setEditable(boolean editable) {
      super.setEditable(editable);
      if (Widgets.isAccessible(selectionButton)) {
         selectionButton.setEnabled(editable);
      }
   }

   @Override
   public void createControls(final Composite parent, int horizontalSpan, boolean fillText) {
      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(ALayout.getZeroMarginLayout(3, false));
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));

      Label label = getLabelWidget();
      if (label != null) {
         label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
      }
      setVerticalLabel(true);
      super.createControls(composite, horizontalSpan, fillText);

      getStyledText().setEditable(false);
      getStyledText().setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL));

      selectionButton = new Button(composite, SWT.PUSH);
      selectionButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
      selectionButton.setText("Set...");
      selectionButton.setEnabled(isEditable());
      selectionButton.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            if (openSelectionDialog()) {
               notifyXModifiedListeners();
            }
         }
      });
      addToolTip(composite, getToolTip());
      getStyledText().setBackground(Displays.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
      refresh();
   }

   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);
            }
         }
      }
   }

   public void setSelectableItems(Collection<T> input) {
      this.input.clear();
      this.input.addAll(input);
   }

   public List<T> getSelectableItems() {
      return new ArrayList<T>(input);
   }

   public void setSelected(Collection<T> input) {
      this.selected.clear();
      this.selected.addAll(input);
      setText(Collections.toString("\n", selected));
   }

   public List<T> getSelected() {
      return new ArrayList<T>(selected);
   }

   public abstract FilteredCheckboxTreeDialog createDialog();

   @SuppressWarnings("unchecked")
   protected boolean openSelectionDialog() {
      boolean selectedChanged = false;
      if (getSelectableItems().isEmpty()) {
         MessageDialog.openInformation(Displays.getActiveShell(), getLabel(),
            "Could not find items available to select from.");
      } else {
         try {
            FilteredCheckboxTreeDialog dialog = createDialog();
            Set<T> choices = new HashSet<T>(selected);
            choices.addAll(input);
            dialog.setInput(choices);
            dialog.setInitialSelections(getSelected());

            int result = dialog.open();
            if (result == 0) {
               List<T> dialogSelections = new ArrayList<T>();
               for (Object obj : dialog.getResult()) {
                  dialogSelections.add((T) obj);
               }

               //               boolean wasDifference = !Collections.setComplement(selected, dialogSelections).isEmpty();
               //               wasDifference &= !Collections.setComplement(dialogSelections, selected).isEmpty();
               //               if (wasDifference) {
               setSelected(dialogSelections);
               selectedChanged = true;
               //               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
         }
      }
      return selectedChanged;
   }

   /**
    * @return the minSelectionRequired
    */
   public int getMinSelectionRequired() {
      return minSelectionRequired;
   }

   /**
    * @return the maxSelectionRequired
    */
   public int getMaxSelectionRequired() {
      return maxSelectionRequired;
   }

}

Back to the top