@Override
	public void fillModelFromDialog(Object model) {

		if (compareOnRadio.isSelected()) {

			TagDocument[] tags = new TagDocument[2];
			int cnt = 0;

			for (int i=0; i<checkBoxes.length; i++) {
				if (checkBoxes[i].isSelected()) {
					if (cnt > 1) {
						throw new SanityCheckException("More than 2 tags selected");
					}
					tags[cnt] = tagDocuments[i];
					cnt++;
				}
			}

			if (cnt == 2) {
				signalView.setComparedTags(tags[0], tags[1]);
			} else {
				signalView.setComparedTags(null, null);
			}

		} else {
			signalView.setComparedTags(null, null);
		}

	}
	@Override
	public void fillDialogFromModel(Object model) throws SignalMLException {

		TagDocument[] comparedDocuments = signalView.getComparedTags();

		if (comparedDocuments == null) {

			compareOffRadio.setSelected(true);

		} else {

			for (int i=0; i<tagDocuments.length; i++) {
				if (tagDocuments[i] == comparedDocuments[0] || tagDocuments[i] == comparedDocuments[1]) {
					checkBoxes[i].setSelected(true); // any deselection is handled by coordinator
				}
			}

			compareOnRadio.setSelected(true);

		}

	}
  public void calculate() throws SignalMLException {

    this.setStyledTagSet(tagDocument != null ? tagDocument.getTagSet() : null);

    SignalSpace signalSpace = getParameters().getSignalSpace();

    MultichannelSampleSource sampleSource;
    if (signalDocument.getDocumentView() != null) {
      SignalView signalView = (SignalView) signalDocument.getDocumentView();

      SignalProcessingChain signalChain = signalView.getMasterPlot().getSignalChain();

      sampleSource = signalChain.createLevelCopyChain(signalSpace.getSignalSourceLevel());
    } else {
      sampleSource = signalDocument.getSampleSource();
    }

    List<String> artifactTagStyleNames = new ArrayList<String>();
    for (TagStyleGroup styleGroup : getParameters().getArtifactTagStyles()) {
      artifactTagStyleNames.addAll(styleGroup.getTagStyleNames());
    }

    List<MarkerSegmentedSampleSource> averagedSampleSources =
        new ArrayList<MarkerSegmentedSampleSource>();
    List<MarkerSegmentedSampleSource> baselineSampleSources =
        new ArrayList<MarkerSegmentedSampleSource>();
    for (TagStyleGroup tagStyleGroup : getParameters().getAveragedTagStyles()) {

      List<String> styleNames = tagStyleGroup.getTagStyleNames();

      Double startAveragingTime = null, endAveragingTime = null;
      if (signalSpace.getTimeSpaceType() == TimeSpaceType.SELECTION_BASED) {
        startAveragingTime = signalSpace.getSelectionTimeSpace().getPosition();
        endAveragingTime = startAveragingTime + signalSpace.getSelectionTimeSpace().getLength();
      }

      MarkerSegmentedSampleSource segmentedSampleSource =
          new MarkerSegmentedSampleSource(
              sampleSource,
              startAveragingTime,
              endAveragingTime,
              getStyledTagSet(),
              styleNames,
              artifactTagStyleNames,
              getParameters().getAveragingStartTime(),
              getParameters().getAveragingTimeLength(),
              signalSpace.getChannelSpace());

      MarkerSegmentedSampleSource baselineSampleSource =
          new MarkerSegmentedSampleSource(
              sampleSource,
              startAveragingTime,
              endAveragingTime,
              getStyledTagSet(),
              styleNames,
              artifactTagStyleNames,
              getParameters().getBaselineTimeStart(),
              getParameters().getBaselineTimeLength(),
              signalSpace.getChannelSpace());

      averagedSampleSources.add(segmentedSampleSource);
      baselineSampleSources.add(baselineSampleSource);
    }

    setSampleSource(averagedSampleSources);
    setBaselineSampleSources(baselineSampleSources);
  }
	@Override
	public JComponent createInterface() {

		List<TagDocument> tags = signalView.getDocument().getTagDocuments();
		int cnt = tags.size();
		if (cnt < 2) {
			throw new SanityCheckException("Too few tag documents");
		}
		tagDocuments = new TagDocument[cnt];
		tags.toArray(tagDocuments);

		analyzeAction = new AnalyzeAction();

		JPanel graphicalComparePanel = new JPanel();
		graphicalComparePanel.setLayout(new BoxLayout(graphicalComparePanel, BoxLayout.Y_AXIS));
		graphicalComparePanel.setBorder(new CompoundBorder(
											new TitledCrossBorder(_("Graphical comparison"), true),
											new EmptyBorder(3,3,3,3)
										));

		compareOnRadio = new JRadioButton(_("Comparison mode enabled"));
		compareOffRadio = new JRadioButton(_("Comparison mode disabled"));

		ButtonGroup compareGroup = new ButtonGroup();
		compareGroup.add(compareOffRadio);
		compareGroup.add(compareOnRadio);

		compareOnRadio.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				fillModelFromDialog(getCurrentModel());
			}

		});

		graphicalComparePanel.add(compareOffRadio);
		graphicalComparePanel.add(compareOnRadio);

		JPanel chooseTagsPanel = new JPanel();

		chooseTagsPanel.setLayout(new GridLayout(cnt, 1, 3, 3));

		chooseTagsPanel.setBorder(new CompoundBorder(
									  new TitledBorder(_("Choose tag documents (2 required)")),
									  new EmptyBorder(3,3,3,3)
								  ));

		checkBoxes = new JCheckBox[cnt];
		CheckBoxCoordinator checkBoxCoordinator = new CheckBoxCoordinator();

		for (int i=0; i<cnt; i++) {
			final String message;
			if (tagDocuments[i].getBackingFile() == null) {
				message = _R("New tag {0}", tagDocuments[i].getName());
			} else {
				message = tagDocuments[i].getName();
			}
			checkBoxes[i] = new JCheckBox(message);
			chooseTagsPanel.add(checkBoxes[i]);
			checkBoxes[i].addItemListener(checkBoxCoordinator);
		}

		checkBoxes[0].setSelected(true);
		checkBoxes[1].setSelected(true);

		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.setBorder(new EmptyBorder(3,0,0,0));

		analyzeButton = new JButton(analyzeAction);
		buttonPanel.add(analyzeButton, BorderLayout.CENTER);

		JPanel interfacePanel = new JPanel(new BorderLayout());

		interfacePanel.add(graphicalComparePanel, BorderLayout.NORTH);
		interfacePanel.add(chooseTagsPanel, BorderLayout.CENTER);
		interfacePanel.add(buttonPanel, BorderLayout.SOUTH);

		Dimension size = chooseTagsPanel.getPreferredSize();
		if (size.width < 270) {
			size.width = 270;
		}
		chooseTagsPanel.setPreferredSize(size);

		return interfacePanel;

	}