content: /*
   * Copyright (c) 2016, Alex Blewitt, Bandlem Ltd
   * Copyright (c) 2016, Packt Publishing Ltd
   *
   * 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
   */
  package com.packtpub.e4.clock.ui.views;
  
  import java.time.ZoneId;
  
  import javax.annotation.PostConstruct;
  import javax.inject.Inject;
  import javax.inject.Named;
  
  import org.eclipse.e4.core.di.annotations.Optional;
  import org.eclipse.e4.ui.di.Focus;
  import org.eclipse.e4.ui.services.EMenuService;
  import org.eclipse.e4.ui.services.IServiceConstants;
  import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
  import org.eclipse.jface.viewers.ArrayContentProvider;
  import org.eclipse.jface.viewers.IStructuredSelection;
  import org.eclipse.jface.viewers.StructuredSelection;
  import org.eclipse.jface.viewers.TableViewer;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.widgets.Composite;
  
  import com.packtpub.e4.clock.ui.internal.TimeZoneDisplayNameColumn;
  import com.packtpub.e4.clock.ui.internal.TimeZoneIDColumn;
  import com.packtpub.e4.clock.ui.internal.TimeZoneOffsetColumn;
  import com.packtpub.e4.clock.ui.internal.TimeZoneSummerTimeColumn;
  
  public class TimeZoneTableView {
  	private TableViewer tableViewer;
  
  	@PostConstruct
  	public void create(Composite parent, EMenuService menuService, ESelectionService selectionService) {
  		tableViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
  		tableViewer.getTable().setHeaderVisible(true);
  		tableViewer.setContentProvider(ArrayContentProvider.getInstance());
  		new TimeZoneIDColumn().addColumnTo(tableViewer);
  		new TimeZoneDisplayNameColumn().addColumnTo(tableViewer);
  		new TimeZoneOffsetColumn().addColumnTo(tableViewer);
  		new TimeZoneSummerTimeColumn().addColumnTo(tableViewer);
  		tableViewer.setInput(ZoneId.getAvailableZoneIds().stream().map(ZoneId::of).toArray());
  		menuService.registerContextMenu(tableViewer.getControl(), "com.packtpub.e4.clock.ui.popup");
  		tableViewer.addSelectionChangedListener(event -> {
  			// forward selection
  			Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
  			if (selection != null && selectionService != null) {
  				selectionService.setSelection(selection);
  			}
  		});
  	}
  
  	@Focus
  	public void focus() {
  		tableViewer.getControl().setFocus();
  	}
  
  	@Inject
  	@Optional
  	public void setTimeZone(@Named(IServiceConstants.ACTIVE_SELECTION) ZoneId timeZone) {
  		if (timeZone != null && tableViewer != null) {
  			tableViewer.setSelection(new StructuredSelection(timeZone));
  			tableViewer.reveal(timeZone);
  		}
  	}
  }