Skip to main content
summaryrefslogtreecommitdiffstats
blob: 74cfeed22a5bfac23ec2273905d45bb5c6fd11fd (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
/*******************************************************************************
 * Copyright (c) 2011 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.display.view.web.components;

import org.eclipse.osee.display.api.components.SearchResultComponent;
import org.eclipse.osee.display.api.data.DisplayOptions;
import org.eclipse.osee.display.api.data.SearchResultMatch;
import org.eclipse.osee.display.api.data.StyledText;
import org.eclipse.osee.display.api.data.ViewArtifact;
import org.eclipse.osee.display.view.web.CssConstants;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

/**
 * @author Shawn F. Cook
 */
@SuppressWarnings("serial")
public class OseeSearchResultComponent extends VerticalLayout implements SearchResultComponent {

   private ViewArtifact artifact;
   private final OseeArtifactNameLinkComponent artifactName = new OseeArtifactNameLinkComponent();
   private final OseeBreadcrumbComponent breadcrumbComp = new OseeBreadcrumbComponent();
   private final VerticalLayout vLayout_Matches = new VerticalLayout();
   private final Label artifactType = new Label("", Label.CONTENT_XHTML);
   private final int TOPBOTTOM_VERT_SPACE = 8;
   private boolean isLayoutComplete = false;

   @Override
   public void attach() {
      if (!isLayoutComplete) {
         createLayout();
         isLayoutComplete = true;
      } else {
         breadcrumbComp.updateLayout();
      }
   }

   private void createLayout() {
      setSizeUndefined();

      HorizontalLayout row0 = new HorizontalLayout();

      Label spacer1 = new Label("");
      spacer1.setHeight(null);
      spacer1.setWidth(15, UNITS_PIXELS);
      artifactType.setStyleName(CssConstants.OSEE_SEARCHRESULT_ARTTYPE);

      Label bottomSpacer = new Label("");
      bottomSpacer.setHeight(TOPBOTTOM_VERT_SPACE, UNITS_PIXELS);

      Label topSpacer = new Label("");
      topSpacer.setHeight(TOPBOTTOM_VERT_SPACE, UNITS_PIXELS);

      row0.addComponent(artifactName);
      row0.addComponent(spacer1);
      row0.addComponent(artifactType);

      addComponent(topSpacer);
      addComponent(row0);

      addComponent(breadcrumbComp);

      addComponent(vLayout_Matches);
      addComponent(bottomSpacer);

      row0.setComponentAlignment(artifactName, Alignment.BOTTOM_LEFT);
      row0.setComponentAlignment(artifactType, Alignment.MIDDLE_LEFT);
   }

   @Override
   public void setArtifact(ViewArtifact artifact) {
      this.artifact = artifact;
      artifactName.setArtifact(this.artifact);
      breadcrumbComp.setArtifact(this.artifact);
      artifactType.setCaption(String.format("[%s]", artifact.getArtifactType()));

   }

   @Override
   public void addSearchResultMatch(SearchResultMatch match) {
      OseeSearchResultMatchComponent matchComp = new OseeSearchResultMatchComponent(match);
      vLayout_Matches.addComponent(matchComp);
   }

   @Override
   public void setErrorMessage(String shortMsg, String longMsg, MsgType msgType) {
      // do nothing
   }

   @Override
   public void setDisplayOptions(DisplayOptions options) {
      if (options != null) {
         boolean showVerbose = options.getVerboseResults();
         vLayout_Matches.setVisible(showVerbose);
         breadcrumbComp.setVisible(showVerbose);
      } else {
         ComponentUtility.logWarn("OseeSearchResultComponent.setDisplayOptions - WARNING: null value detected.", this);
      }
   }

   private class OseeSearchResultMatchComponent extends HorizontalLayout {
      public OseeSearchResultMatchComponent(SearchResultMatch match) {
         Label matchLabel = new Label(String.format("%s: ", match.getAttributeType()), Label.CONTENT_XHTML);
         matchLabel.setStyleName(CssConstants.OSEE_ATTRNAME);
         Label spacer4 = new Label();
         spacer4.setWidth(15, UNITS_PIXELS);

         int firstMatch = -1;
         int charsSinceFirst = 0;
         int displaySize = 50;
         String styleOpen = "<SPAN style=\"BACKGROUND-COLOR: #ffff00\">";
         String styleClose = "</SPAN>";

         StringBuilder builder = new StringBuilder();
         for (StyledText text : match.getData()) {
            if (text.isHighLighted()) {
               if (firstMatch == -1) {
                  firstMatch = builder.length();
               }
               builder.append(styleOpen);
               if (charsSinceFirst <= 50) {
                  displaySize += styleOpen.length() + styleClose.length();
               }
            }

            if (firstMatch != -1) {
               charsSinceFirst += text.getData().length();
            }
            builder.append(text.getData());

            if (text.isHighLighted()) {
               builder.append(styleClose);
            }
         }

         if (builder.length() > displaySize) {
            int end = Math.min(firstMatch + displaySize, builder.length());
            builder.delete(end, builder.length());
            builder.delete(0, firstMatch);
            if (end != builder.length()) {
               builder.append("...");
            }
            if (firstMatch != 0) {
               builder.insert(0, "...");
            }
         }

         Label matchLabelHint = new Label(builder.toString(), Label.CONTENT_XHTML);
         Label spacer3 = new Label();
         spacer3.setWidth(15, UNITS_PIXELS);

         Label matchManyLabel = new Label(String.format("(%d matches)", match.getManyMatches()), Label.CONTENT_XHTML);
         matchManyLabel.setStyleName(CssConstants.OSEE_SEARCHRESULT_MATCH_MANY);

         addComponent(matchLabel);
         addComponent(spacer4);
         addComponent(matchLabelHint);
         addComponent(spacer3);
         addComponent(matchManyLabel);
      }
   }

}

Back to the top