Skip to main content
summaryrefslogtreecommitdiffstats
blob: 35036f2825a4e42193426a7df5be0097b040e526 (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
/*******************************************************************************
 * 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;

import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.help.ui.OseeHelpContext;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.ui.plugin.util.HelpUtil;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.cellEditor.UniversalCellEditor;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.FilteredCheckboxAttributeTypeDialog;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.IDirtiableEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;

public class AttributesComposite extends Composite {
   private TableViewer tableViewer;
   private Table table;
   private Text helpText;
   private static final String[] columnNames = new String[] {"name", "value"};
   private static final Integer[] columnWidths = new Integer[] {200, 600};
   private Artifact artifact;
   private final IDirtiableEditor editor;
   private Label warningLabel;
   private final ArrayList<ModifyAttributesListener> modifyAttrListeners = new ArrayList<>();
   private MenuItem deleteItem;
   private final ToolBar toolBar;

   public static final int NAME_COLUMN_INDEX = 0;
   public static final int VALUE_COLUMN_INDEX = 1;

   public AttributesComposite(IDirtiableEditor editor, Composite parent, int style, Artifact artifact) {
      this(editor, parent, style, artifact, null);
   }

   public AttributesComposite(IDirtiableEditor editor, Composite parent, int style, Artifact artifact, ToolBar toolBar) {
      super(parent, style);
      this.artifact = artifact;
      this.editor = editor;

      create(this);
      Menu popupMenu = new Menu(parent);
      createAddMenuItem(popupMenu);
      createDeleteMenuItem(popupMenu);
      popupMenu.addMenuListener(new AttributeMenuListener());
      tableViewer.getTable().setMenu(popupMenu);

      this.toolBar = toolBar;
   }

   public void updateLabel(String msg) {
      warningLabel.setText(msg);
      layout();
   }

   private void create(Composite parent) {
      this.setLayout(new GridLayout());
      this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

      SashForm mainSash = new SashForm(this, SWT.NONE);
      mainSash.setLayout(new GridLayout());
      mainSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      mainSash.setOrientation(SWT.VERTICAL);

      createTableArea(mainSash);

      SashForm sashForm = new SashForm(mainSash, SWT.NONE);
      sashForm.setLayout(new GridLayout());
      sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      sashForm.setOrientation(SWT.HORIZONTAL);

      createWarningArea(sashForm);
      createHelpArea(sashForm);

      mainSash.setWeights(new int[] {8, 2});
      sashForm.setWeights(new int[] {5, 5});

      HelpUtil.setHelp(tableViewer.getControl(), OseeHelpContext.ARTIFACT_EDITOR__ATTRIBUTES);
   }

   private void createTableArea(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      composite.setLayout(new GridLayout());
      composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
      composite.setText("Attributes");

      createTable(composite);
      createColumns();
      createTableViewer(composite);

      tableViewer.refresh();
      attachTableListeners();
   }

   private void createTable(Composite parent) {
      table =
         new Table(parent,
            SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
      table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      table.setLinesVisible(true);
      table.setHeaderVisible(true);
   }

   private void attachTableListeners() {
      tableViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {

         @Override
         public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object selected = selection.getFirstElement();

            if (selected instanceof Attribute<?>) {
               Attribute<?> attribute = (Attribute<?>) selected;
               String tipText = attribute.getAttributeType().getDescription();
               if (tipText != null && !tipText.equals("null")) {
                  helpText.setText(tipText);
               } else {
                  helpText.setText("");
               }
            }
         }
      });
   }

   public TableViewer getTableViewer() {
      return tableViewer;
   }

   private void createTableViewer(Composite parent) {
      tableViewer = new TableViewer(table);

      TableViewerEditor.create(
         tableViewer,
         new ColumnViewerEditorActivationStrategy(tableViewer),
         ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);
      tableViewer.setUseHashlookup(true);
      tableViewer.setColumnProperties(columnNames);

      if (!artifact.isReadOnly()) {
         CellEditor[] editors = new CellEditor[columnNames.length];
         editors[VALUE_COLUMN_INDEX] = new UniversalCellEditor(table, SWT.NONE);

         tableViewer.setCellEditors(editors);
         tableViewer.setCellModifier(new AttributeCellModifier(editor, tableViewer, this));
      }
      tableViewer.setContentProvider(new AttributeContentProvider());
      tableViewer.setLabelProvider(new AttributeLabelProvider());
      tableViewer.setSorter(new AttributeNameSorter());
      tableViewer.setInput(artifact);
   }
   public class AttributeNameSorter extends ViewerSorter {

      public AttributeNameSorter() {
         super();
      }

      @Override
      @SuppressWarnings("unchecked")
      public int compare(Viewer viewer, Object o1, Object o2) {
         if (o1 instanceof Attribute && o2 instanceof Attribute) {
            return getComparator().compare(((Attribute<?>) o1).getAttributeType().getName(),
               ((Attribute<?>) o2).getAttributeType().getName());
         } else if (o1 instanceof String && o2 instanceof String) {
            return getComparator().compare(o1, o2);
         }
         return super.compare(viewer, o1, o2);
      }

   }

   private void createColumns() {
      for (int index = 0; index < columnNames.length; index++) {
         TableColumn column = new TableColumn(table, SWT.LEFT, index);
         column.setText(columnNames[index]);
         column.setWidth(columnWidths[index]);
      }
   }

   private void createHelpArea(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      composite.setLayout(new GridLayout());
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      composite.setText("Tips");

      helpText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
      helpText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
   }

   private void createWarningArea(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      composite.setLayout(new GridLayout());
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      composite.setText("Warnings");

      warningLabel = new Label(composite, SWT.NONE);
      warningLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      warningLabel.setForeground(Displays.getSystemColor(SWT.COLOR_RED));
      updateLabel("");
   }

   private void createAddMenuItem(Menu parentMenu) {
      MenuItem addItem = new MenuItem(parentMenu, SWT.PUSH);
      addItem.setText("Add");
      addItem.setEnabled(true && !artifact.isReadOnly());
      addItem.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            Collection<IAttributeType> selectableTypes = new ArrayList<>();
            try {
               for (IAttributeType attrType : artifact.getAttributeTypes()) {
                  if (artifact.getRemainingAttributeCount(attrType) > 0) {
                     selectableTypes.add(attrType);
                  }
               }
               FilteredCheckboxAttributeTypeDialog dialog =
                  new FilteredCheckboxAttributeTypeDialog("Select Attribute Types",
                     "Select attribute types to display.");
               dialog.setSelectable(selectableTypes);
               if (dialog.open() == 0) {
                  for (Object obj : dialog.getResult()) {
                     getArtifact().addAttribute((IAttributeType) obj);
                  }
                  tableViewer.refresh();
                  layout();
                  getParent().layout();
                  editor.onDirtied();
                  notifyModifyAttribuesListeners();
               }

            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
            }
         }
      });

   }

   private void createDeleteMenuItem(Menu parentMenu) {
      deleteItem = new MenuItem(parentMenu, SWT.PUSH);
      deleteItem.setImage(null);
      deleteItem.setText("Delete");
      deleteItem.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            try {
               Attribute<?> attribute = getSelectedAttribute();
               attribute.delete();
               editor.onDirtied();
               notifyModifyAttribuesListeners();
               tableViewer.refresh();
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
            }
         }
      });
   }

   private Attribute<?> getSelectedAttribute() {
      TableItem[] items = tableViewer.getTable().getSelection();
      if (items.length > 0) {
         return (Attribute<?>) tableViewer.getTable().getSelection()[0].getData();
      } else {
         return null;
      }
   }

   public class AttributeMenuListener implements MenuListener {
      @Override
      public void menuHidden(MenuEvent e) {
         // do nothing
      }

      @Override
      public void menuShown(MenuEvent e) {
         Attribute<?> attribute = getSelectedAttribute();

         if (attribute == null) {
            deleteItem.setText("Delete - No Attribute Selected");
            deleteItem.setEnabled(false);
         } else if (!attribute.canDelete()) {
            deleteItem.setText("Delete - Lower Limit Met");
            deleteItem.setEnabled(false);
         } else {
            deleteItem.setText("Delete");
            deleteItem.setEnabled(!artifact.isReadOnly());
         }
      }
   }

   public Artifact getArtifact() {
      return artifact;
   }

   public void refreshArtifact(Artifact artifact) {
      this.artifact = artifact;

      if (tableViewer.getContentProvider() != null) {
         tableViewer.setInput(artifact);
         tableViewer.refresh();
      }
   }

   public void addModifyAttributesListener(ModifyAttributesListener listener) {
      if (!modifyAttrListeners.contains(listener)) {
         modifyAttrListeners.add(listener);
      }
   }

   public void removeModifyAttributesListener(ModifyAttributesListener listener) {
      modifyAttrListeners.remove(listener);
   }

   public void notifyModifyAttribuesListeners() {
      for (ModifyAttributesListener listener : modifyAttrListeners) {
         listener.handleEvent();
      }
   }

   /**
    * @return the toolBar
    */
   public ToolBar getToolBar() {
      return toolBar;
   }
}

Back to the top