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
.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
.store
.internal
.TaxeditorStorePlugin
;
30 * <p>Abstract AbstractSplitableViewPart class.</p>
33 * @created Feb 12, 2010
36 public abstract class AbstractSplitableViewPart
extends ViewPart
implements ISelectionListener
{
38 static final int VIEW_ORIENTATION_VERTICAL
= 0;
39 static final int VIEW_ORIENTATION_HORIZONTAL
= 1;
40 static final int VIEW_ORIENTATION_SINGLE
= 2;
41 static final int VIEW_ORIENTATION_AUTOMATIC
= 3;
43 private static final int PAGE_EMPTY
= 0;
44 private static final int PAGE_VIEWER
= 1;
46 private final IDialogSettings dialogSettings
;
48 private SashForm detailSplitter
;
50 private Composite parent
;
52 private PageBook pagebook
;
54 private int orientation
;
56 private int currentOrientation
;
58 private boolean showViewer2
;
60 private Viewer viewer1
;
62 private Viewer viewer2
;
64 private Label emptySelectionLabel
;
66 private ISelectionService selectionService
;
70 * <p>Constructor for AbstractSplitableViewPart.</p>
72 public AbstractSplitableViewPart() {
75 dialogSettings
= TaxeditorStorePlugin
.getDefault().getDialogSettings();
80 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
84 public void createPartControl(Composite parent
) {
85 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
86 selectionService
.addSelectionListener(this);
87 selectionService
.addPostSelectionListener(this);
91 addResizeListener(parent
);
92 pagebook
= new PageBook(parent
, SWT
.NULL
);
95 createDetailSplitter(pagebook
);
96 viewer1
= createViewer1(detailSplitter
);
97 viewer2
= createViewer2(detailSplitter
);
99 // Page 2: Nothing selected
100 emptySelectionLabel
= new Label(pagebook
, SWT
.TOP
+ SWT
.LEFT
+ SWT
.WRAP
);
101 emptySelectionLabel
.setText("Current selection does not support this view"); //
103 showPage(PAGE_EMPTY
);
107 // FIXME since this class is not used at the moment we will not have to fix this soon
108 // it might also become irrelevant in future uses
109 // set the selection if there is an open editor
110 // if(EditorUtil.getActiveMultiPageTaxonEditor() != null){
111 // selectionChanged(EditorUtil.getActiveMultiPageTaxonEditor(), EditorUtil.getCurrentSelection());
115 private void initOrientation() {
118 orientation
= dialogSettings
.getInt(getDialogstoreVieworientationKey());
120 if ((orientation
< 0) || (orientation
> 3)) {
121 orientation
= VIEW_ORIENTATION_AUTOMATIC
;
123 } catch (NumberFormatException e
) {
124 orientation
= VIEW_ORIENTATION_AUTOMATIC
;
128 currentOrientation
= -1;
129 setOrientation(orientation
);
133 * <p>getDialogstoreVieworientationKey</p>
135 * @return a {@link java.lang.String} object.
137 protected abstract String
getDialogstoreVieworientationKey();
140 * <p>showEmptyPage</p>
142 public void showEmptyPage(){
143 showPage(PAGE_EMPTY
);
149 public void showViewer(){
150 showPage(PAGE_VIEWER
);
153 private void showPage(int page
) {
154 if (page
== PAGE_EMPTY
) {
155 pagebook
.showPage(emptySelectionLabel
);
157 pagebook
.showPage(detailSplitter
);
161 private void addResizeListener(Composite parent
) {
162 parent
.addControlListener(new ControlListener() {
163 public void controlMoved(ControlEvent e
) {
165 public void controlResized(ControlEvent e
) {
166 computeOrientation();
171 void computeOrientation() {
173 dialogSettings
.put(getDialogstoreVieworientationKey(), orientation
);
174 if (orientation
!= VIEW_ORIENTATION_AUTOMATIC
) {
175 setOrientation(orientation
);
178 if (orientation
== VIEW_ORIENTATION_SINGLE
)
180 Point size
= parent
.getSize();
181 if (size
.x
!= 0 && size
.y
!= 0) {
183 setOrientation(VIEW_ORIENTATION_HORIZONTAL
);
185 setOrientation(VIEW_ORIENTATION_VERTICAL
);
190 private void saveSplitterRatio() {
191 if (detailSplitter
!= null && ! detailSplitter
.isDisposed()) {
192 int[] weigths
= detailSplitter
.getWeights();
193 int ratio
= (weigths
[0] * 1000) / (weigths
[0] + weigths
[1]);
194 String key
= getDialogStoreRatioKey() + currentOrientation
;
195 dialogSettings
.put(key
, ratio
);
200 * <p>getDialogStoreRatioKey</p>
202 * @return a {@link java.lang.String} object.
204 protected abstract String
getDialogStoreRatioKey();
206 void setOrientation(int orientation
) {
207 if (currentOrientation
!= orientation
) {
208 if ((getViewer1() != null)
209 && !getViewer1().getControl().isDisposed()
210 && (detailSplitter
!= null)
211 && !detailSplitter
.isDisposed()) {
213 if (orientation
== VIEW_ORIENTATION_SINGLE
) {
214 setShowViewer2(false);
216 if (currentOrientation
== VIEW_ORIENTATION_SINGLE
) {
217 setShowViewer2(true);
219 boolean horizontal
= orientation
== VIEW_ORIENTATION_HORIZONTAL
;
220 detailSplitter
.setOrientation(horizontal ? SWT
.HORIZONTAL
224 detailSplitter
.layout();
227 updateCheckedState();
229 currentOrientation
= orientation
;
231 restoreSplitterRatio();
238 private void setShowViewer2(boolean show
) {
246 private void showOrHideViewer2() {
248 detailSplitter
.setMaximizedControl(null);
250 detailSplitter
.setMaximizedControl(getViewer1().getControl());
254 private void restoreSplitterRatio() {
255 String ratio
= dialogSettings
.get(getDialogStoreRatioKey() + currentOrientation
);
258 int intRatio
= Integer
.parseInt(ratio
);
259 detailSplitter
.setWeights(new int[] {intRatio
, 1000 - intRatio
});
262 private void updateCheckedState() {
263 // for (int i= 0; i < fToggleOrientationActions.length; i++) {
264 // fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
269 * <p>createViewer1</p>
271 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
272 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
274 protected abstract Viewer
createViewer1(Composite parent
);
278 * <p>createViewer2</p>
280 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
281 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
283 protected abstract Viewer
createViewer2(Composite parent
);
289 private void createDetailSplitter(Composite parent
) {
290 detailSplitter
= new SashForm(parent
, SWT
.HORIZONTAL
);
294 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
298 public void dispose() {
299 selectionService
.removeSelectionListener(this);
300 selectionService
.removePostSelectionListener(this);
305 * <p>Getter for the field <code>viewer1</code>.</p>
307 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
309 protected Viewer
getViewer1() {
314 * <p>Getter for the field <code>viewer2</code>.</p>
316 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
318 protected Viewer
getViewer2() {