3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
;
13 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
14 import org
.eclipse
.jface
.viewers
.Viewer
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.custom
.SashForm
;
17 import org
.eclipse
.swt
.events
.ControlEvent
;
18 import org
.eclipse
.swt
.events
.ControlListener
;
19 import org
.eclipse
.swt
.graphics
.Point
;
20 import org
.eclipse
.swt
.widgets
.Composite
;
21 import org
.eclipse
.swt
.widgets
.Label
;
22 import org
.eclipse
.ui
.ISelectionListener
;
23 import org
.eclipse
.ui
.ISelectionService
;
24 import org
.eclipse
.ui
.part
.PageBook
;
25 import org
.eclipse
.ui
.part
.ViewPart
;
27 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
28 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
31 * <p>Abstract AbstractSplitableViewPart class.</p>
34 * @created Feb 12, 2010
37 public abstract class AbstractSplitableViewPart
extends ViewPart
implements ISelectionListener
{
39 static final int VIEW_ORIENTATION_VERTICAL
= 0;
40 static final int VIEW_ORIENTATION_HORIZONTAL
= 1;
41 static final int VIEW_ORIENTATION_SINGLE
= 2;
42 static final int VIEW_ORIENTATION_AUTOMATIC
= 3;
44 private static final int PAGE_EMPTY
= 0;
45 private static final int PAGE_VIEWER
= 1;
47 private final IDialogSettings dialogSettings
;
49 private SashForm detailSplitter
;
51 private Composite parent
;
53 private PageBook pagebook
;
55 private int orientation
;
57 private int currentOrientation
;
59 private boolean showViewer2
;
61 private Viewer viewer1
;
63 private Viewer viewer2
;
65 private Label emptySelectionLabel
;
67 private ISelectionService selectionService
;
71 * <p>Constructor for AbstractSplitableViewPart.</p>
73 public AbstractSplitableViewPart() {
76 dialogSettings
= TaxeditorEditorPlugin
.getDefault().getDialogSettings();
81 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
85 public void createPartControl(Composite parent
) {
86 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
87 selectionService
.addSelectionListener(this);
88 selectionService
.addPostSelectionListener(this);
92 addResizeListener(parent
);
93 pagebook
= new PageBook(parent
, SWT
.NULL
);
96 createDetailSplitter(pagebook
);
97 viewer1
= createViewer1(detailSplitter
);
98 viewer2
= createViewer2(detailSplitter
);
100 // Page 2: Nothing selected
101 emptySelectionLabel
= new Label(pagebook
, SWT
.TOP
+ SWT
.LEFT
+ SWT
.WRAP
);
102 emptySelectionLabel
.setText("Current selection does not support this view"); //
104 showPage(PAGE_EMPTY
);
108 // set the selection if there is an open editor
109 if(EditorUtil
.getActiveMultiPageTaxonEditor() != null){
110 selectionChanged(EditorUtil
.getActiveMultiPageTaxonEditor(), EditorUtil
.getCurrentSelection());
114 private void initOrientation() {
117 orientation
= dialogSettings
.getInt(getDialogstoreVieworientationKey());
119 if ((orientation
< 0) || (orientation
> 3)) {
120 orientation
= VIEW_ORIENTATION_AUTOMATIC
;
122 } catch (NumberFormatException e
) {
123 orientation
= VIEW_ORIENTATION_AUTOMATIC
;
127 currentOrientation
= -1;
128 setOrientation(orientation
);
132 * <p>getDialogstoreVieworientationKey</p>
134 * @return a {@link java.lang.String} object.
136 protected abstract String
getDialogstoreVieworientationKey();
139 * <p>showEmptyPage</p>
141 public void showEmptyPage(){
142 showPage(PAGE_EMPTY
);
148 public void showViewer(){
149 showPage(PAGE_VIEWER
);
152 private void showPage(int page
) {
153 if (page
== PAGE_EMPTY
) {
154 pagebook
.showPage(emptySelectionLabel
);
156 pagebook
.showPage(detailSplitter
);
160 private void addResizeListener(Composite parent
) {
161 parent
.addControlListener(new ControlListener() {
162 public void controlMoved(ControlEvent e
) {
164 public void controlResized(ControlEvent e
) {
165 computeOrientation();
170 void computeOrientation() {
172 dialogSettings
.put(getDialogstoreVieworientationKey(), orientation
);
173 if (orientation
!= VIEW_ORIENTATION_AUTOMATIC
) {
174 setOrientation(orientation
);
177 if (orientation
== VIEW_ORIENTATION_SINGLE
)
179 Point size
= parent
.getSize();
180 if (size
.x
!= 0 && size
.y
!= 0) {
182 setOrientation(VIEW_ORIENTATION_HORIZONTAL
);
184 setOrientation(VIEW_ORIENTATION_VERTICAL
);
189 private void saveSplitterRatio() {
190 if (detailSplitter
!= null && ! detailSplitter
.isDisposed()) {
191 int[] weigths
= detailSplitter
.getWeights();
192 int ratio
= (weigths
[0] * 1000) / (weigths
[0] + weigths
[1]);
193 String key
= getDialogStoreRatioKey() + currentOrientation
;
194 dialogSettings
.put(key
, ratio
);
199 * <p>getDialogStoreRatioKey</p>
201 * @return a {@link java.lang.String} object.
203 protected abstract String
getDialogStoreRatioKey();
205 void setOrientation(int orientation
) {
206 if (currentOrientation
!= orientation
) {
207 if ((getViewer1() != null)
208 && !getViewer1().getControl().isDisposed()
209 && (detailSplitter
!= null)
210 && !detailSplitter
.isDisposed()) {
212 if (orientation
== VIEW_ORIENTATION_SINGLE
) {
213 setShowViewer2(false);
215 if (currentOrientation
== VIEW_ORIENTATION_SINGLE
) {
216 setShowViewer2(true);
218 boolean horizontal
= orientation
== VIEW_ORIENTATION_HORIZONTAL
;
219 detailSplitter
.setOrientation(horizontal ? SWT
.HORIZONTAL
223 detailSplitter
.layout();
226 updateCheckedState();
228 currentOrientation
= orientation
;
230 restoreSplitterRatio();
237 private void setShowViewer2(boolean show
) {
245 private void showOrHideViewer2() {
247 detailSplitter
.setMaximizedControl(null);
249 detailSplitter
.setMaximizedControl(getViewer1().getControl());
253 private void restoreSplitterRatio() {
254 String ratio
= dialogSettings
.get(getDialogStoreRatioKey() + currentOrientation
);
257 int intRatio
= Integer
.parseInt(ratio
);
258 detailSplitter
.setWeights(new int[] {intRatio
, 1000 - intRatio
});
261 private void updateCheckedState() {
262 // for (int i= 0; i < fToggleOrientationActions.length; i++) {
263 // fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
268 * <p>createViewer1</p>
270 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
271 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
273 protected abstract Viewer
createViewer1(Composite parent
);
277 * <p>createViewer2</p>
279 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
280 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
282 protected abstract Viewer
createViewer2(Composite parent
);
288 private void createDetailSplitter(Composite parent
) {
289 detailSplitter
= new SashForm(parent
, SWT
.HORIZONTAL
);
293 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
297 public void dispose() {
298 selectionService
.removeSelectionListener(this);
299 selectionService
.removePostSelectionListener(this);
304 * <p>Getter for the field <code>viewer1</code>.</p>
306 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
308 protected Viewer
getViewer1() {
313 * <p>Getter for the field <code>viewer2</code>.</p>
315 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
317 protected Viewer
getViewer2() {