~pixeldrama/annis/merge_restr_vis

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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Copyright 2011 Corpuslinguistic working group Humboldt University Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.gui.resultview;

import annis.gui.Helper;
import annis.gui.PluginSystem;
import annis.gui.visualizers.ComponentVisualizerPlugin;
import annis.gui.visualizers.IFrameVisualizer;
import annis.gui.visualizers.VisualizerInput;
import annis.gui.visualizers.VisualizerPlugin;
import annis.gui.visualizers.component.KWICPanel;
import annis.gui.widgets.AutoHeightIFrame;
import annis.resolver.ResolverEntry;
import com.sun.jersey.api.client.WebResource;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.themes.ChameleonTheme;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.SaltProject;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sCorpusStructure.SDocument;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.STextualDS;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SNode;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author thomas, Benjamin Weißenfels
 *
 */
public class VisualizerPanel extends Panel implements Button.ClickListener
{

  private final Logger log = LoggerFactory.getLogger(VisualizerPanel.class);
  public static final ThemeResource ICON_COLLAPSE = new ThemeResource(
    "icon-collapse.gif");
  public static final ThemeResource ICON_EXPAND = new ThemeResource(
    "icon-expand.gif");
  private ApplicationResource resource = null;
  private AutoHeightIFrame iframe;
  private SDocument result;
  private PluginSystem ps;
  private ResolverEntry entry;
  private Random rand = new Random();
  private Map<SNode, Long> markedAndCovered;
  private List<SNode> token;
  private Map<String, String> markersExact;
  private Map<String, String> markersCovered;
  private Button btEntry;
  private KWICPanel kwicPanel;
  private List<String> mediaIDs;
  private String htmlID;
  private CustomLayout visContainer;
  private ComponentVisualizerPlugin compVis;
  private Set<String> visibleTokenAnnos;
  private STextualDS text;
  private SingleResultPanel parentPanel;
  private String segmentationName;
  private List<VisualizerPanel> mediaVisualizer;

  /**
   * This Constructor should be used for {@link ComponentVisualizerPlugin}
   * Visualizer.
   *
   */
  public VisualizerPanel(String visType, SDocument result, List<SNode> token,
    Set<String> visibleTokenAnnos, Map<SNode, Long> markedAndCovered,
    STextualDS text, List<String> mediaIDs,
    List<VisualizerPanel> mediaVisualizer, String htmlID,
    SingleResultPanel parent, String segmentationName, PluginSystem ps,
    CustomLayout visContainer)
  {
    // get the Visualizer instance
    VisualizerPlugin tmpVis = ps.getVisualizer(visType);
    VisualizerPlugin vis = null;
    Label label;

    /**
     * build a new instance, cause the Pluginsystem holds only one instance of
     * the plugin.
     */
    try
    {
      vis = tmpVis.getClass().newInstance();
    }
    catch (InstantiationException ex)
    {
      log.error("could not initiate {}", tmpVis.getShortName(), ex);
    }
    catch (IllegalAccessException ex)
    {
      log.error("could not initiate {}", tmpVis.getShortName(), ex);
    }

    this.result = result;
    this.token = token;
    this.visibleTokenAnnos = visibleTokenAnnos;
    this.markedAndCovered = markedAndCovered;
    this.text = text;
    this.parentPanel = parent;
    this.segmentationName = segmentationName;
    this.mediaVisualizer = mediaVisualizer;
    this.mediaIDs = mediaIDs;
    // TODO use this also for ComponentVisualizer, or lookup a native mediaplayer
    this.htmlID = htmlID;

    this.visContainer = visContainer;

    if (!(vis instanceof ComponentVisualizerPlugin))
    {
      log.warn("{} is not a ComponentVisualizer", vis.getShortName());
      return;
    }

    compVis = (ComponentVisualizerPlugin) vis;
    this.setWidth("100%");
    label = new Label("KWIC");
    this.setContent(this.visContainer);
    this.visContainer.addComponent(label, "btEntry");
  }

  public VisualizerPanel(final ResolverEntry entry, SDocument result,
    PluginSystem ps, Map<String, String> markersExact,
    Map<String, String> markersCovered, CustomLayout costumLayout,
    List<String> mediaIDs, String htmlID)
  {
    this.result = result;
    this.ps = ps;
    this.entry = entry;
    this.markersExact = markersExact;
    this.markersCovered = markersCovered;
    this.visContainer = costumLayout;
    this.mediaIDs = mediaIDs;
    this.htmlID = htmlID;


    setContent(this.visContainer);

    this.setWidth("100%");
    this.setHeight("-1px");

    addStyleName(ChameleonTheme.PANEL_BORDERLESS);

    btEntry = new Button(entry.getDisplayName());
    btEntry.setIcon(ICON_EXPAND);
    btEntry.setStyleName(ChameleonTheme.BUTTON_BORDERLESS + " "
      + ChameleonTheme.BUTTON_SMALL);
    btEntry.addListener((Button.ClickListener) this);
    visContainer.addComponent(btEntry, "btEntry");
  }

  @Override
  public void attach()
  {
    VisualizerInput visInput;

    /**
     * check if this is a ComponentVisualizer.
     */
    if (compVis == null)
    {
      return;
    }

    visInput = createInput();
    visInput.setResult(result);
    visInput.setToken(token);
    visInput.setVisibleTokenAnnos(visibleTokenAnnos);
    visInput.setText(text);
    visInput.setSingleResultPanelRef(parentPanel);
    visInput.setSegmentationName(segmentationName);
    visInput.setMediaIDs(mediaIDs);
    visInput.setMediaVisualizer(mediaVisualizer);

    compVis.setVisualizerInput(visInput);
    visContainer.addComponent(compVis, "compVis");
  }

  private VisualizerInput createInput()
  {
    VisualizerInput input = new VisualizerInput();
    input.setAnnisRemoteServiceURL(getApplication().getProperty(
      "AnnisRemoteService.URL"));
    input.setContextPath(Helper.getContext(getApplication()));
    input.setDotPath(getApplication().getProperty("DotPath"));
    input.setId("" + rand.nextLong());

    input.setMarkableExactMap(markersExact);
    input.setMarkableMap(markersCovered);
    input.setMediaIDs(mediaIDs);
    input.setMarkedAndCovered(markedAndCovered);

    if (entry != null)
    {
      input.setMappings(entry.getMappings());
      input.setNamespace(entry.getNamespace());
      String template = Helper.getContext(getApplication())
        + "/Resource/" + entry.getVisType() + "/%s";
      input.setResourcePathTemplate(template);
    }

    return input;
  }

  private ApplicationResource createResource(
    final ByteArrayOutputStream byteStream,
    String mimeType)
  {

    StreamResource r;

    r = new StreamResource(new StreamResource.StreamSource()
    {
      @Override
      public InputStream getStream()
      {
        return new ByteArrayInputStream(byteStream.toByteArray());
      }
    }, entry.getVisType() + "_" + Math.abs(rand.nextLong()), getApplication());
    r.setMIMEType(mimeType);

    return r;
  }

  private SaltProject getText(String toplevelCorpusName, String documentName)
  {
    SaltProject text = null;
    try
    {
      toplevelCorpusName = URLEncoder.encode(toplevelCorpusName, "UTF-8");
      documentName = URLEncoder.encode(documentName, "UTF-8");
      WebResource annisResource = Helper.getAnnisWebResource(getApplication());
      text = annisResource.path("graphs").path(toplevelCorpusName).path(
        documentName).get(SaltProject.class);
    }
    catch (Exception e)
    {
      log.error("General remote service exception", e);
    }
    return text;
  }

  @Override
  public void detach()
  {
    super.detach();

    if (resource != null)
    {
      getApplication().removeResource(resource);
    }
  }

  @Override
  public void buttonClick(ClickEvent event)
  {
    toggleVisualizer(true);
  }

  /**
   * Opens and closes visualizer.
   *
   * @param collapse when collapse is false, the Visualizer would never be
   * closed
   */
  public void toggleVisualizer(boolean collapse)
  {
    if (resource != null && collapse)
    {
      getApplication().removeResource(resource);
    }

    if (btEntry.getIcon() == ICON_EXPAND)
    {
      // expand
      if (iframe == null)
      {

        VisualizerPlugin vis = ps.getVisualizer(entry.getVisType());

        if (vis == null)
        {
          entry.setVisType(PluginSystem.DEFAULT_VISUALIZER);
          vis = ps.getVisualizer(entry.getVisType());
        }

        VisualizerInput input = createInput();
        IFrameVisualizer iframeVis;

        //TODO print error message
        if (vis instanceof IFrameVisualizer)
        {
          iframeVis = (IFrameVisualizer) vis;

          if (iframeVis.isUsingText()
            && result.getSDocumentGraph().getSNodes().size() > 0)
          {
            SaltProject p = getText(result.getSCorpusGraph().getSRootCorpus().
              get(0).getSName(), result.getSName());

            input.setDocument(p.getSCorpusGraphs().get(0).getSDocuments().get(0));

          }
          else
          {

            input.setDocument(result);

          }

          ByteArrayOutputStream outStream = new ByteArrayOutputStream();
          iframeVis.writeOutput(input, outStream);

          resource = createResource(outStream, iframeVis.getContentType());
          String url = getApplication().getRelativeLocation(resource);
          iframe = new AutoHeightIFrame(url == null ? "/error.html" : url, this);

          visContainer.addComponent(iframe, "iframe");
        }

      }

      btEntry.setIcon(ICON_COLLAPSE);
      iframe.setVisible(true);
    }
    else if (btEntry.getIcon() == ICON_COLLAPSE && collapse)
    {
      // collapse
      if (iframe != null)
      {
        iframe.setVisible(false);
        stopMediaVisualizers();
      }
      btEntry.setIcon(ICON_EXPAND);
    }
  }

  public void setKwicPanel(KWICPanel kwicPanel)
  {
    this.kwicPanel = kwicPanel;
  }

  public void startMediaVisFromKWIC()
  {
    if (kwicPanel != null)
    {
      kwicPanel.startMediaVisualizers();
      // set back to null, otherwise the movie will stop
      kwicPanel = null;
    }
  }

  private void stopMediaVisualizers()
  {
    String stopCommand = ""
      + "document.getElementById(\"" + this.htmlID + "\")"
      + ".getElementsByTagName(\"iframe\")[0].contentWindow.stop()";
    getWindow().executeJavaScript(stopCommand);

  }
}