Skip to main content
summaryrefslogtreecommitdiffstats
blob: e0aa06e26ec5e7cdfbf918e235f7bc30c734725b (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
/*******************************************************************************
 * 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.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.jdk.core.util.AHTML;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.widgets.XRadioButton.ButtonType;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

/**
 * Generic label and radiobutton field object for use by single entry artifact attributes
 * 
 * @author Donald G. Dunne
 */
public class XRadioButtons extends XWidget {

   private Composite comp;
   private ArrayList<XRadioButton> xButtons = new ArrayList<XRadioButton>();
   private boolean multiSelect;
   private boolean vertical;
   private int verticalColumns;
   private boolean sortNames;

   public XRadioButtons(String displayLabel, String xmlRoot) {
      super(displayLabel, xmlRoot);
   }

   public void setFocus() {
   }

   /*
    * (non-Javadoc)
    * 
    * @see org.eclipse.osee.framework.ui.skynet.widgets.XWidget#getControl()
    */
   @Override
   public Control getControl() {
      return null;
   }

   public void addButtons(String items[]) {
      xButtons.clear();
      for (String item : items) {
         xButtons.add(new XRadioButton(item, item));
      }
   }

   public void addButton(String item, String toolTip) {
      XRadioButton rb = new XRadioButton(item, item);
      rb.setToolTip(toolTip);
      xButtons.add(rb);
      // System.out.println("added " + item);
   }

   public void addButton(String item) {
      XRadioButton rb = new XRadioButton(item, item);
      xButtons.add(rb);
      // System.out.println("added " + item);
   }

   public void selectAll(boolean selected) {
      for (XRadioButton rb : xButtons)
         rb.setSelected(selected);
      refresh();
   }

   public XRadioButton getButton(String name) {
      for (XRadioButton button : xButtons)
         if (button.getLabel().equals(name)) return button;
      return null;
   }

   public void setSelected(String selected[]) {
      // First, clear out all previous selections
      selectAll(false);
      // Set, selected items sent in
      for (String name : selected) {
         XRadioButton rb = getButton(name);
         if (rb != null) rb.setSelected(true);
      }
      refresh();
   }

   public void setSelected(Collection<String> selected) {
      if (selected != null) setSelected((String[]) selected.toArray(new String[selected.size()]));
   }

   public void setFromXml(String xml) throws IllegalStateException {
      selectAll(false);
      if (!multiSelect)
         super.setFromXml(xml);
      else {
         Matcher m;
         m =
               Pattern.compile("<" + getXmlRoot() + ">(.*?)</" + getXmlRoot() + ">", Pattern.MULTILINE | Pattern.DOTALL).matcher(
                     xml);
         if (m.find()) {
            String str = m.group(1);
            String strs[] = str.split(",");
            setSelected(strs);
         }
      }
      refresh();
   }

   public String getXmlData() {
      String sel = "";
      for (String str : getSelectedNames())
         sel += str + ",";
      sel = sel.replaceFirst(",$", "");
      return sel;
   }

   public String getDisplayStr() {
      return getXmlData();
   }

   public String toString() {
      return getLabel() + ": " + Collections.toString(",", getSelectedNames());
   }

   public void setXmlData(String str) {
      setSelected(str);
   }

   /**
    * Create radio Widgets. Widgets Created: Label: "text entry" horizonatalSpan takes up 2 columns; horizontalSpan must
    * be >=2
    */
   protected void createControls(Composite parent, int horizontalSpan) {

      Map<String, XRadioButton> nameToButton = new HashMap<String, XRadioButton>();
      String names[] = new String[xButtons.size()];
      int x = 0;
      for (XRadioButton rb : xButtons) {
         nameToButton.put(rb.getLabel().toLowerCase(), rb);
         names[x++] = rb.getLabel().toLowerCase();
      }

      int numColumns = (names.length * 2) + (isDisplayLabel() ? 1 : 0);
      if (vertical && horizontalSpan == 1)
         numColumns = 1;
      else if (vertical) numColumns = (isDisplayLabel() ? 1 : 0) + 1; // only need label an composite
      // System.out.println("numColumns *" + numColumns + "*");
      comp = new Composite(parent, SWT.NONE);
      comp.setLayout(new GridLayout(numColumns, false));
      GridData gd = new GridData();
      gd.horizontalSpan = horizontalSpan;
      comp.setLayoutData(gd);
      // comp.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));

      // Create Text Widgets
      if (isDisplayLabel()) {
         labelWidget = new Label(comp, SWT.NONE);
         labelWidget.setText(getLabel() + ":");
      }
      if (getToolTip() != null && isDisplayLabel()) {
         labelWidget.setToolTipText(getToolTip());
      }

      if (sortNames) Arrays.sort(names);

      int numRows = 1;
      if (vertical && xButtons.size() > verticalColumns) {
         numRows = (int) (xButtons.size() / verticalColumns);
         if ((xButtons.size() / verticalColumns) > 0) numRows++;
         // System.out.println("numRows *" + numRows + "*");
      }

      Composite c = comp;
      if (vertical) {
         // System.out.println("verticalColumns *" + verticalColumns + "*");
         c = new Composite(comp, SWT.NONE);
         c.setLayout(ALayout.getZeroMarginLayout(verticalColumns, false));
         // c.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
      }

      Composite inComp = null;
      if (!vertical) inComp = c;
      for (int i = 0; i < names.length; i++) {
         if (vertical && i % numRows == 0) {
            inComp = new Composite(c, SWT.NONE);
            inComp.setLayout(new GridLayout(2, false));
            inComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
            // inComp.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN+i));
         }
         XRadioButton button = nameToButton.get(names[i]);
         if (multiSelect) button.setButtonType(ButtonType.Check);
         button.setLabelAfter(true);
         button.createWidgets(inComp, 2);
         // Since each button has it's own listeners, pass the notification on to anyone listening
         // to XRadioButtons:w
         button.addXModifiedListener(new XModifiedListener() {
            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.osee.framework.ui.skynet.widgets.XModifiedListener#widgetModified(org.eclipse.osee.framework.ui.skynet.widgets.XWidget)
             */
            public void widgetModified(XWidget widget) {
               notifyXModifiedListeners();
            }
         });
      }
      refresh();
   }

   @Override
   public void dispose() {
      for (XRadioButton rb : xButtons)
         rb.dispose();
      if (labelWidget != null) labelWidget.dispose();
      if (comp != null && !comp.isDisposed()) comp.dispose();
   }

   public void addSelectionListener(SelectionListener selectionListener) {
      for (XRadioButton rb : xButtons) {
         rb.addSelectionListener(selectionListener);
      }
   }

   public void removeSelectionListener(SelectionListener selectionListener) {
      for (XRadioButton rb : xButtons) {
         rb.removeSelectionListener(selectionListener);
      }
   }

   public Set<String> getSelectedNames() {
      Set<String> names = new HashSet<String>();
      for (XRadioButton rb : xButtons) {
         if (rb.isSelected()) names.add(rb.getLabel());
      }
      return names;
   }

   public boolean isSelected(String name) {
      XRadioButton rb = getButton(name);
      if (rb != null) return rb.isSelected();
      return false;
   }

   public boolean isSelected() {
      for (XRadioButton rb : xButtons) {
         if (rb.isSelected()) return true;
      }
      return false;
   }

   public void setSelected(String name) {
      setSelected(new String[] {name});
   }

   public void refresh() {
      validate();
   }

   public IStatus isValid() {
      if (isRequiredEntry() && getSelectedNames().size() == 0) {
         return new Status(IStatus.ERROR, SkynetGuiPlugin.PLUGIN_ID, getLabel() + " must have at least one selection.");
      }
      return Status.OK_STATUS;
   }

   public String getReportData() {
      return getXmlData();
   }

   public String toHTML(String labelFont) {
      return AHTML.getLabelStr(labelFont, getLabel() + ": ") + getDisplayStr();
   }

   public boolean isMultiSelect() {
      return multiSelect;
   }

   public void setMultiSelect(boolean multiSelect) {
      this.multiSelect = multiSelect;
   }

   public boolean isVertical() {
      return vertical;
   }

   public void setVertical(boolean vertical, int columns) {
      this.vertical = vertical;
      this.verticalColumns = columns;
   }

   public boolean isSortNames() {
      return sortNames;
   }

   public void setSortNames(boolean sortNames) {
      this.sortNames = sortNames;
   }

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

Back to the top