Skip to main content
summaryrefslogtreecommitdiffstats
blob: 87cacc5e299c403d1932a3448f4f6ea45b347428 (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
/*******************************************************************************
 * 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.matrix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.jdk.core.util.AHTML;

/**
 * Creates HTML matrix
 * 
 * @author Donald G. Dunne
 */
public class Matrix {
   private final String title;
   private final ArrayList<MatrixItem> items;
   private final Map<String, MatrixItem> nameToItem = new HashMap<>();
   private final Set<String> values = new HashSet<>();
   private final Map<String, Set<String>> nameToValues = new HashMap<>();
   // Names with no values will be listed at the bottom of the report so they don't take up space
   private final Set<String> noValueNames = new HashSet<>();
   private boolean useNameAsMark = false;
   private IProgressMonitor monitor;

   public Matrix(String title, ArrayList<MatrixItem> items) {
      this.title = title;
      this.items = items;
   }

   public String getMatrix() {
      StringBuilder sb = new StringBuilder();
      sb.append(AHTML.heading(3, title));
      sb.append(getMatrixBody());
      return sb.toString();
   }

   private void processData() {
      for (MatrixItem item : items) {
         nameToItem.put(item.getName(), item);
         values.addAll(item.getValues());
         if (nameToValues.containsKey(item.getName())) {
            Set<String> vals = nameToValues.get(item.getName());
            vals.addAll(item.getValues());
            nameToValues.remove(item.getName());
            nameToValues.put(item.getName(), vals);
         } else {
            nameToValues.put(item.getName(), item.getValues());
         }
      }
   }

   private String getMatrixBody() {
      processData();
      StringBuilder sb = new StringBuilder();
      sb.append(AHTML.beginMultiColumnTable(100, 1));
      // Determine all the names to deal with
      Set<String> names = new HashSet<>();
      // Don't want to take up valuable table space with names that have no values; keep track
      // of them and print them at the end of the report
      for (String name : nameToItem.keySet()) {
         System.out.println("nameToValues.get(name) *" + nameToValues.get(name) + "*");
         if (nameToValues.get(name) == null || nameToValues.get(name).isEmpty()) {
            noValueNames.add(name);
         } else {
            names.add(name);
         }
      }
      // Create sortedNames for use in looping through
      String[] sortedNames = names.toArray(new String[names.size()]);
      Arrays.sort(sortedNames);
      // Create headerNames with one more field due to value name column
      names.add(" ");
      String[] headerNames = names.toArray(new String[names.size()]);
      Arrays.sort(headerNames);
      // Add header names to table
      sb.append(AHTML.addHeaderRowMultiColumnTable(headerNames));
      int x = 1;
      // Create sorted list of values
      String[] sortedValues = values.toArray(new String[values.size()]);
      Arrays.sort(sortedValues);
      for (String value : sortedValues) {
         String str = String.format("Processing %s/%s \"%s\"", x++ + "", values.size(), value);
         System.out.println(str);
         if (monitor != null) {
            monitor.subTask(str);
         }
         List<String> marks = new ArrayList<>();
         marks.add(value);
         for (String name : sortedNames) {
            if (nameToValues.get(name) != null && nameToValues.get(name).contains(value)) {
               marks.add(useNameAsMark ? name : "X");
            } else {
               marks.add(".");
            }
         }
         String[] colOptions = new String[marks.size()];
         int i = 0;
         colOptions[i] = "";
         for (i = 1; i < marks.size(); i++) {
            colOptions[i] = " align=center";
         }
         sb.append(AHTML.addRowMultiColumnTable(marks.toArray(new String[marks.size()]), colOptions));
      }
      sb.append(AHTML.endMultiColumnTable());
      if (noValueNames.size() > 0) {
         sb.append(AHTML.newline(2) + AHTML.bold("Items with no values: "));
         String[] sortedItems = noValueNames.toArray(new String[noValueNames.size()]);
         Arrays.sort(sortedItems);
         for (String str : sortedItems) {
            sb.append(AHTML.newline() + str);
         }
         sb.append(AHTML.newline());
      }
      return sb.toString();
   }

   /**
    * @return Returns the useNameAsMark.
    */
   public boolean isUseNameAsMark() {
      return useNameAsMark;
   }

   /**
    * @param useNameAsMark The useNameAsMark to set.
    */
   public void setUseNameAsMark(boolean useNameAsMark) {
      this.useNameAsMark = useNameAsMark;
   }

   public IProgressMonitor getMonitor() {
      return monitor;
   }

   public void setMonitor(IProgressMonitor monitor) {
      this.monitor = monitor;
   }

}

Back to the top