Skip to main content
summaryrefslogtreecommitdiffstats
blob: d19ac62d074096f01e0fb5e9bb262c141b6ccf1e (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
/*******************************************************************************
 * 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.ote.ui.test.manager.preferences.environment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.osee.framework.jdk.core.type.TreeObject;
import org.eclipse.osee.framework.jdk.core.type.TreeParent;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.ote.ui.test.manager.OteTestManagerImage;
import org.eclipse.osee.ote.ui.test.manager.internal.TestManagerPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Group;

/**
 * @author Roberto E. Escobar
 */
public class EnvironmentPreferencePage {
   public static final String CHECKED = "selected";
   public static final String NAME = "name";
   public static final String NUMBER_OF_VALUES = "numberOfValues";
   public static final String PAGE_KEY = "org.eclipse.osee.ote.ui.test.manager.EnvironmentPreferencePage";
   public static final String VALUE = "value";

   public static Map<String, String> getSelectedItems() {
      Map<String, String> environmentVariables = new HashMap<String, String>();

      ArrayList<EnvironmentPreferenceNode> envList = loadVariables();

      for (EnvironmentPreferenceNode node : envList) {
         if (node.isChecked()) {
            String name = node.getEnvName();
            String value = node.getValue();
            if (name != null && !name.equals("")) {
               environmentVariables.put(name, value != null ? value : "");
            }
         }
      }
      return environmentVariables;
   }

   private static ArrayList<EnvironmentPreferenceNode> loadVariables() {
      ArrayList<EnvironmentPreferenceNode> list = new ArrayList<EnvironmentPreferenceNode>();
      IPreferenceStore prefStore = TestManagerPlugin.getInstance().getPreferenceStore();

      int numberOfValues = prefStore.getInt(PAGE_KEY + "." + NUMBER_OF_VALUES);
      for (int index = 0; index < numberOfValues; index++) {
         String name = prefStore.getString(PAGE_KEY + "." + NAME + "_" + index);
         String value = prefStore.getString(PAGE_KEY + "." + VALUE + "_" + index);
         String selectedString = prefStore.getString(PAGE_KEY + "." + CHECKED + "_" + index);
         boolean selected = Boolean.parseBoolean(selectedString);

         if (name != null && name != "") {
            EnvironmentPreferenceNode node = new EnvironmentPreferenceNode(name);
            node.setValue((value != null ? value : ""));
            node.setChecked(selected);
            list.add(node);
         }
      }
      return list;
   }

   private Button addButton;

   private Composite buttonComposite;
   private EnvironmentPageEventHandler environmentPageEventHandler;
   private Button removeButton;

   private final ArrayList<EnvironmentPreferenceNode> treeInputList;

   private CheckboxTreeViewer treeViewer;

   public EnvironmentPreferencePage(Composite parent) {
      this.treeInputList = loadVariables();
      this.createTreeArea(parent);
   }

   public ISelection getSelection() {
      return treeViewer.getSelection();
   }

   public void refresh() {
      treeViewer.refresh();
   }

   public void storeVariables() {
      IPreferenceStore prefStore = TestManagerPlugin.getInstance().getPreferenceStore();
      prefStore.setValue(PAGE_KEY + "." + NUMBER_OF_VALUES, treeInputList.size());
      int index = 0;
      for (EnvironmentPreferenceNode node : treeInputList) {
         index = treeInputList.indexOf(node);
         String name = node.getEnvName();
         if (name != null && name != "") {
            prefStore.putValue(PAGE_KEY + "." + NAME + "_" + index, name);
            String value = node.getValue();
            prefStore.putValue(PAGE_KEY + "." + VALUE + "_" + index, (value != null ? value : ""));
            prefStore.putValue(PAGE_KEY + "." + CHECKED + "_" + index, Boolean.toString(node.isChecked()));
         }
      }
   }

   private void attachListeners() {
      treeViewer.getTree().addKeyListener(new KeyListener() {
         public void keyPressed(KeyEvent e) {
         }

         public void keyReleased(KeyEvent e) {
            if (e.character == SWT.DEL) {
               environmentPageEventHandler.handleRemoveSelectedViewEvent();
            }
         }
      });

      treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
         public void selectionChanged(SelectionChangedEvent event) {
            environmentPageEventHandler.handleTreeSelectionEvent(event);
         }
      });

      treeViewer.getTree().addMouseListener(new MouseListener() {
         public void mouseDoubleClick(MouseEvent e) {
            environmentPageEventHandler.handleEditVariableEvent();
         }

         public void mouseDown(MouseEvent e) {
         }

         public void mouseUp(MouseEvent e) {
         }
      });

      treeViewer.addCheckStateListener(new ICheckStateListener() {

         public void checkStateChanged(CheckStateChangedEvent event) {
            environmentPageEventHandler.handleCheckStateChangeEvent(event);
         }

      });
   }

   private Control createButtonArea(Composite parent) {

      buttonComposite = new Composite(parent, SWT.NONE);
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 1;
      GridData gd = new GridData(SWT.FILL);
      buttonComposite.setLayout(gridLayout);
      buttonComposite.setLayoutData(gd);

      addButton = new Button(buttonComposite, SWT.PUSH);
      addButton.setText("Add");
      addButton.addSelectionListener(new SelectionListener() {

         public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
         }

         public void widgetSelected(SelectionEvent e) {
            environmentPageEventHandler.handleAddEnvironmentVariableEvent();
         }
      });

      removeButton = new Button(buttonComposite, SWT.PUSH);
      removeButton.setText("Remove");
      removeButton.addSelectionListener(new SelectionListener() {

         public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
         }

         public void widgetSelected(SelectionEvent e) {
            environmentPageEventHandler.handleRemoveSelectedViewEvent();
         }

      });
      return parent;
   }

   private Control createTreeArea(Composite parent) {

      Group defaultEnvironmentVariablesGroup = new Group(parent, SWT.NONE);
      defaultEnvironmentVariablesGroup.setText("Select Default Environment Variables to Use");
      GridLayout gL = new GridLayout();
      gL.numColumns = 2;
      GridData gd = new GridData(GridData.FILL_BOTH);
      gd.grabExcessHorizontalSpace = true;
      gd.grabExcessVerticalSpace = true;
      defaultEnvironmentVariablesGroup.setLayout(gL);
      defaultEnvironmentVariablesGroup.setLayoutData(gd);

      Composite areaComposite = new Composite(defaultEnvironmentVariablesGroup, SWT.NONE);
      GridLayout treeLayout = new GridLayout();
      treeLayout.numColumns = 1;
      GridData gd1 = new GridData();
      gd1.horizontalAlignment = GridData.FILL;
      gd1.verticalAlignment = GridData.FILL;
      gd1.grabExcessHorizontalSpace = true;
      gd1.grabExcessVerticalSpace = true;
      areaComposite.setLayout(treeLayout);
      areaComposite.setLayoutData(gd1);

      GridData treeGridData = new GridData();
      treeGridData.grabExcessHorizontalSpace = true;
      treeGridData.grabExcessVerticalSpace = true;
      treeGridData.horizontalAlignment = GridData.FILL;
      treeGridData.verticalAlignment = GridData.FILL;

      treeViewer =
            new CheckboxTreeViewer(areaComposite, SWT.MULTI | SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
      treeViewer.getTree().setLayoutData(treeGridData);
      treeViewer.setContentProvider(new ITreeContentProvider() {

         public void dispose() {

         }

         public Object[] getChildren(Object parentElement) {
            if (parentElement != null && parentElement instanceof TreeParent) {
               TreeParent parent = (TreeParent) parentElement;
               if (parent.hasChildren()) {
                  return parent.getChildren();
               }
            }
            return new Object[0];
         }

         public Object[] getElements(Object inputElement) {
            if (inputElement != null && inputElement instanceof ArrayList<?>) {
               ArrayList<?> elementArray = (ArrayList<?>) inputElement;
               return elementArray.toArray();
            }
            return new Object[0];
         }

         public Object getParent(Object element) {
            if (element != null && element instanceof TreeObject) {
               TreeObject child = (TreeObject) element;
               return child.getParent();
            }
            return new Object();
         }

         public boolean hasChildren(Object element) {
            if (element instanceof TreeParent) {
               TreeParent parent = (TreeParent) element;
               return parent.hasChildren();
            }
            return false;
         }

         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

         }
      });
      treeViewer.setLabelProvider(new LabelProvider() {

         @Override
         public Image getImage(Object obj) {
            return ImageManager.getImage(OteTestManagerImage.ENVIRONMENT);
         }

         @Override
         public String getText(Object obj) {
            return obj.toString();
         }
      });
      treeViewer.setInput(treeInputList);
      treeViewer.getTree().setToolTipText("Double click on an item to edit.\nClick once to preview content.");

      environmentPageEventHandler = new EnvironmentPageEventHandler(parent, treeViewer, treeInputList);

      createButtonArea(defaultEnvironmentVariablesGroup);

      attachListeners();

      for (EnvironmentPreferenceNode parentNode : treeInputList) {
         treeViewer.setChecked(parentNode, parentNode.isChecked());
      }

      return parent;
   }
}

Back to the top