Skip to main content
summaryrefslogtreecommitdiffstats
blob: 11ec45d1e97b6c14ad48deb07507c81777a6f6fe (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
/*******************************************************************************
 * 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.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.osee.framework.db.connection.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Branch;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeType;
import org.eclipse.osee.framework.skynet.core.attribute.TypeValidityManager;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.ArtifactTreeContentProvider;
import org.eclipse.swt.SWT;
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.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * @author Donald G. Dunne
 */
public class AttributeCheckListDialog extends SelectionDialog {
   private CheckboxTreeViewer treeViewer;
   private final ArrayList<AttributeType> selectedAttributes;
   private String preferenceKey;
   private Branch branch;

   public AttributeCheckListDialog(Shell parent, String preferenceKey, Branch branch) {
      this(parent, null, preferenceKey, branch);
   }

   public AttributeCheckListDialog(Shell parent, Collection<AttributeType> attrTypes, String preferenceKey, Branch branch) {
      super(parent);
      setTitle("Select Attributes");
      setMessage("Select Attributes");
      this.selectedAttributes = new ArrayList<AttributeType>();
      this.preferenceKey = preferenceKey;
      this.branch = branch;

      if (attrTypes != null && !attrTypes.isEmpty()) {
         selectedAttributes.addAll(attrTypes);
      }
   }

   public String getSelectedAttributeData(Artifact artifact) throws Exception {
      if (artifact == null) {
         throw new IllegalArgumentException(" - ERROR: Null Artifact");
      }

      StringBuilder result = new StringBuilder();
      for (AttributeType attributeType : artifact.getAttributeTypes()) {
         if (selectedAttributes.contains(attributeType)) {
            result.append(" - ");
            result.append(artifact.getAttributesToString(attributeType.getName()));
         }
      }
      return result.toString();
   }

   @Override
   protected Control createDialogArea(Composite container) {

      Composite comp = new Composite(container, SWT.NONE);
      comp.setLayout(new GridLayout(2, false));
      comp.setLayoutData(new GridData(GridData.FILL_BOTH));

      treeViewer =
            new CheckboxTreeViewer(comp,
                  SWT.MULTI | SWT.CHECK | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
      treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      treeViewer.setContentProvider(new ArtifactTreeContentProvider());
      treeViewer.setSorter(new AttributeViewerSorter());
      ArrayList<Object> objs = new ArrayList<Object>();
      for (Object obj : selectedAttributes)
         objs.add(obj);
      treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
         public void selectionChanged(SelectionChangedEvent event) {
            selectedAttributes.clear();
            for (Object obj : treeViewer.getCheckedElements())
               selectedAttributes.add((AttributeType) obj);
         };
      });
      treeViewer.setLabelProvider(new LabelProvider() {

         public String getText(Object obj) {
            return obj.toString();
         }
      });
      try {
         if (branch != null){
            treeViewer.setInput(TypeValidityManager.getValidAttributeTypes(branch));
         } else {
            treeViewer.setInput(selectedAttributes);
         }
         treeViewer.setCheckedElements(objs.toArray(new Object[objs.size()]));
      } catch (OseeCoreException ex) {
         OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
      }
      return container;
   }

   public class AttributeViewerSorter extends ViewerSorter {
      public AttributeViewerSorter() {
         super();
      }

      @SuppressWarnings("unchecked")
      public int compare(Viewer viewer, Object o1, Object o2) {
         return getComparator().compare(((AttributeType) o1).getName(), ((AttributeType) o2).getName());
      }
   }

   public boolean noneSelected() {
      return selectedAttributes.isEmpty();
   }

   /**
    * @return the selectedAttributes
    */
   public Collection<AttributeType> getSelectedAttributes() {
      return selectedAttributes;
   }

   /*
    * (non-Javadoc)
    * 
    * @see org.eclipse.jface.dialogs.Dialog#okPressed()
    */
   @Override
   protected void okPressed() {
      super.okPressed();

      IPreferenceStore prefStore = SkynetGuiPlugin.getInstance().getPreferenceStore();
      prefStore.setValue(preferenceKey, Collections.toString(getSelectedAttributes(), "", "|", ""));
   }
}

Back to the top