-// $Id$
-/**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
-
-package eu.etaxonomy.taxeditor.view;
-
-import org.eclipse.jface.dialogs.IDialogSettings;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.SashForm;
-import org.eclipse.swt.events.ControlEvent;
-import org.eclipse.swt.events.ControlListener;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.ui.ISelectionListener;
-import org.eclipse.ui.ISelectionService;
-import org.eclipse.ui.part.PageBook;
-import org.eclipse.ui.part.ViewPart;
-
-import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
-
-/**
- * <p>Abstract AbstractSplitableViewPart class.</p>
- *
- * @author n.hoffmann
- * @created Feb 12, 2010
- * @version 1.0
- */
-public abstract class AbstractSplitableViewPart extends ViewPart implements ISelectionListener{
-
- static final int VIEW_ORIENTATION_VERTICAL = 0;
- static final int VIEW_ORIENTATION_HORIZONTAL = 1;
- static final int VIEW_ORIENTATION_SINGLE = 2;
- static final int VIEW_ORIENTATION_AUTOMATIC = 3;
-
- private static final int PAGE_EMPTY = 0;
- private static final int PAGE_VIEWER = 1;
-
- private final IDialogSettings dialogSettings;
-
- private SashForm detailSplitter;
-
- private Composite parent;
-
- private PageBook pagebook;
-
- private int orientation;
-
- private int currentOrientation;
-//
- private boolean showViewer2;
-
- private Viewer viewer1;
-
- private Viewer viewer2;
-
- private Label emptySelectionLabel;
-
- private ISelectionService selectionService;
-
-
- /**
- * <p>Constructor for AbstractSplitableViewPart.</p>
- */
- public AbstractSplitableViewPart() {
- super();
-
- dialogSettings = TaxeditorStorePlugin.getDefault().getDialogSettings();
-
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
- */
- /** {@inheritDoc} */
- @Override
- public void createPartControl(Composite parent) {
- selectionService = getSite().getWorkbenchWindow().getSelectionService();
- selectionService.addSelectionListener(this);
- selectionService.addPostSelectionListener(this);
-
-
- this.parent = parent;
- addResizeListener(parent);
- pagebook = new PageBook(parent, SWT.NULL);
-
- // Page 1: Viewers
- createDetailSplitter(pagebook);
- viewer1 = createViewer1(detailSplitter);
- viewer2 = createViewer2(detailSplitter);
-
- // Page 2: Nothing selected
- emptySelectionLabel = new Label(pagebook, SWT.TOP + SWT.LEFT + SWT.WRAP);
- emptySelectionLabel.setText("Current selection does not support this view"); //
-
- showPage(PAGE_EMPTY);
-
- initOrientation();
-
- // FIXME since this class is not used at the moment we will not have to fix this soon
- // it might also become irrelevant in future uses
- // set the selection if there is an open editor
-// if(EditorUtil.getActiveMultiPageTaxonEditor() != null){
-// selectionChanged(EditorUtil.getActiveMultiPageTaxonEditor(), EditorUtil.getCurrentSelection());
-// }
- }
-
- private void initOrientation() {
-
- try {
- orientation = dialogSettings.getInt(getDialogstoreVieworientationKey());
-
- if ((orientation < 0) || (orientation > 3)) {
- orientation = VIEW_ORIENTATION_AUTOMATIC;
- }
- } catch (NumberFormatException e) {
- orientation = VIEW_ORIENTATION_AUTOMATIC;
- }
-
- // force the update
- currentOrientation = -1;
- setOrientation(orientation);
- }
-
- /**
- * <p>getDialogstoreVieworientationKey</p>
- *
- * @return a {@link java.lang.String} object.
- */
- protected abstract String getDialogstoreVieworientationKey();
-
- /**
- * <p>showEmptyPage</p>
- */
- public void showEmptyPage(){
- showPage(PAGE_EMPTY);
- }
-
- /**
- * <p>showViewer</p>
- */
- public void showViewer(){
- showPage(PAGE_VIEWER);
- }
-
- private void showPage(int page) {
- if (page == PAGE_EMPTY) {
- pagebook.showPage(emptySelectionLabel);
- } else {
- pagebook.showPage(detailSplitter);
- }
- }
-
- private void addResizeListener(Composite parent) {
- parent.addControlListener(new ControlListener() {
- public void controlMoved(ControlEvent e) {
- }
- public void controlResized(ControlEvent e) {
- computeOrientation();
- }
- });
- }
-
- void computeOrientation() {
- saveSplitterRatio();
- dialogSettings.put(getDialogstoreVieworientationKey(), orientation);
- if (orientation != VIEW_ORIENTATION_AUTOMATIC) {
- setOrientation(orientation);
- }
- else {
- if (orientation == VIEW_ORIENTATION_SINGLE)
- return;
- Point size= parent.getSize();
- if (size.x != 0 && size.y != 0) {
- if (size.x > size.y)
- setOrientation(VIEW_ORIENTATION_HORIZONTAL);
- else
- setOrientation(VIEW_ORIENTATION_VERTICAL);
- }
- }
- }
-
- private void saveSplitterRatio() {
- if (detailSplitter != null && ! detailSplitter.isDisposed()) {
- int[] weigths = detailSplitter.getWeights();
- int ratio = (weigths[0] * 1000) / (weigths[0] + weigths[1]);
- String key= getDialogStoreRatioKey() + currentOrientation;
- dialogSettings.put(key, ratio);
- }
- }
-
- /**
- * <p>getDialogStoreRatioKey</p>
- *
- * @return a {@link java.lang.String} object.
- */
- protected abstract String getDialogStoreRatioKey();
-
- void setOrientation(int orientation) {
- if (currentOrientation != orientation) {
- if ((getViewer1() != null)
- && !getViewer1().getControl().isDisposed()
- && (detailSplitter != null)
- && !detailSplitter.isDisposed()) {
-
- if (orientation == VIEW_ORIENTATION_SINGLE) {
- setShowViewer2(false);
- } else {
- if (currentOrientation == VIEW_ORIENTATION_SINGLE) {
- setShowViewer2(true);
- }
- boolean horizontal = orientation == VIEW_ORIENTATION_HORIZONTAL;
- detailSplitter.setOrientation(horizontal ? SWT.HORIZONTAL
- : SWT.VERTICAL);
- }
-
- detailSplitter.layout();
- }
-
- updateCheckedState();
-
- currentOrientation = orientation;
-
- restoreSplitterRatio();
- }
- }
-
- /**
- * @param show
- */
- private void setShowViewer2(boolean show) {
- showViewer2 = show;
- showOrHideViewer2();
- }
-
- /**
- *
- */
- private void showOrHideViewer2() {
- if (showViewer2) {
- detailSplitter.setMaximizedControl(null);
- } else {
- detailSplitter.setMaximizedControl(getViewer1().getControl());
- }
- }
-
- private void restoreSplitterRatio() {
- String ratio= dialogSettings.get(getDialogStoreRatioKey() + currentOrientation);
- if (ratio == null)
- return;
- int intRatio= Integer.parseInt(ratio);
- detailSplitter.setWeights(new int[] {intRatio, 1000 - intRatio});
- }
-
- private void updateCheckedState() {
-// for (int i= 0; i < fToggleOrientationActions.length; i++) {
-// fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
-// }
- }
-
- /**
- * <p>createViewer1</p>
- *
- * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
- * @return a {@link org.eclipse.jface.viewers.Viewer} object.
- */
- protected abstract Viewer createViewer1(Composite parent);
-
-
- /**
- * <p>createViewer2</p>
- *
- * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
- * @return a {@link org.eclipse.jface.viewers.Viewer} object.
- */
- protected abstract Viewer createViewer2(Composite parent);
-
-
- /**
- * @param pagebook2
- */
- private void createDetailSplitter(Composite parent) {
- detailSplitter = new SashForm(parent, SWT.HORIZONTAL);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#dispose()
- */
- /** {@inheritDoc} */
- @Override
- public void dispose() {
- selectionService.removeSelectionListener(this);
- selectionService.removePostSelectionListener(this);
- super.dispose();
- }
-
- /**
- * <p>Getter for the field <code>viewer1</code>.</p>
- *
- * @return a {@link org.eclipse.jface.viewers.Viewer} object.
- */
- protected Viewer getViewer1() {
- return viewer1;
- }
-
- /**
- * <p>Getter for the field <code>viewer2</code>.</p>
- *
- * @return a {@link org.eclipse.jface.viewers.Viewer} object.
- */
- protected Viewer getViewer2() {
- return viewer2;
- }
-}