Merge branch 'hotfix/3.12.4'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / AbstractSplitableViewPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.view;
12
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;
26
27 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
28
29 /**
30 * <p>Abstract AbstractSplitableViewPart class.</p>
31 *
32 * @author n.hoffmann
33 * @created Feb 12, 2010
34 * @version 1.0
35 */
36 public abstract class AbstractSplitableViewPart extends ViewPart implements ISelectionListener{
37
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;
42
43 private static final int PAGE_EMPTY = 0;
44 private static final int PAGE_VIEWER = 1;
45
46 private final IDialogSettings dialogSettings;
47
48 private SashForm detailSplitter;
49
50 private Composite parent;
51
52 private PageBook pagebook;
53
54 private int orientation;
55
56 private int currentOrientation;
57 //
58 private boolean showViewer2;
59
60 private Viewer viewer1;
61
62 private Viewer viewer2;
63
64 private Label emptySelectionLabel;
65
66 private ISelectionService selectionService;
67
68
69 /**
70 * <p>Constructor for AbstractSplitableViewPart.</p>
71 */
72 public AbstractSplitableViewPart() {
73 super();
74
75 dialogSettings = TaxeditorStorePlugin.getDefault().getDialogSettings();
76
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
81 */
82 /** {@inheritDoc} */
83 @Override
84 public void createPartControl(Composite parent) {
85 selectionService = getSite().getWorkbenchWindow().getSelectionService();
86 selectionService.addSelectionListener(this);
87 selectionService.addPostSelectionListener(this);
88
89
90 this.parent = parent;
91 addResizeListener(parent);
92 pagebook = new PageBook(parent, SWT.NULL);
93
94 // Page 1: Viewers
95 createDetailSplitter(pagebook);
96 viewer1 = createViewer1(detailSplitter);
97 viewer2 = createViewer2(detailSplitter);
98
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"); //
102
103 showPage(PAGE_EMPTY);
104
105 initOrientation();
106
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());
112 // }
113 }
114
115 private void initOrientation() {
116
117 try {
118 orientation = dialogSettings.getInt(getDialogstoreVieworientationKey());
119
120 if ((orientation < 0) || (orientation > 3)) {
121 orientation = VIEW_ORIENTATION_AUTOMATIC;
122 }
123 } catch (NumberFormatException e) {
124 orientation = VIEW_ORIENTATION_AUTOMATIC;
125 }
126
127 // force the update
128 currentOrientation = -1;
129 setOrientation(orientation);
130 }
131
132 /**
133 * <p>getDialogstoreVieworientationKey</p>
134 *
135 * @return a {@link java.lang.String} object.
136 */
137 protected abstract String getDialogstoreVieworientationKey();
138
139 /**
140 * <p>showEmptyPage</p>
141 */
142 public void showEmptyPage(){
143 showPage(PAGE_EMPTY);
144 }
145
146 /**
147 * <p>showViewer</p>
148 */
149 public void showViewer(){
150 showPage(PAGE_VIEWER);
151 }
152
153 private void showPage(int page) {
154 if (page == PAGE_EMPTY) {
155 pagebook.showPage(emptySelectionLabel);
156 } else {
157 pagebook.showPage(detailSplitter);
158 }
159 }
160
161 private void addResizeListener(Composite parent) {
162 parent.addControlListener(new ControlListener() {
163 public void controlMoved(ControlEvent e) {
164 }
165 public void controlResized(ControlEvent e) {
166 computeOrientation();
167 }
168 });
169 }
170
171 void computeOrientation() {
172 saveSplitterRatio();
173 dialogSettings.put(getDialogstoreVieworientationKey(), orientation);
174 if (orientation != VIEW_ORIENTATION_AUTOMATIC) {
175 setOrientation(orientation);
176 }
177 else {
178 if (orientation == VIEW_ORIENTATION_SINGLE)
179 return;
180 Point size= parent.getSize();
181 if (size.x != 0 && size.y != 0) {
182 if (size.x > size.y)
183 setOrientation(VIEW_ORIENTATION_HORIZONTAL);
184 else
185 setOrientation(VIEW_ORIENTATION_VERTICAL);
186 }
187 }
188 }
189
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);
196 }
197 }
198
199 /**
200 * <p>getDialogStoreRatioKey</p>
201 *
202 * @return a {@link java.lang.String} object.
203 */
204 protected abstract String getDialogStoreRatioKey();
205
206 void setOrientation(int orientation) {
207 if (currentOrientation != orientation) {
208 if ((getViewer1() != null)
209 && !getViewer1().getControl().isDisposed()
210 && (detailSplitter != null)
211 && !detailSplitter.isDisposed()) {
212
213 if (orientation == VIEW_ORIENTATION_SINGLE) {
214 setShowViewer2(false);
215 } else {
216 if (currentOrientation == VIEW_ORIENTATION_SINGLE) {
217 setShowViewer2(true);
218 }
219 boolean horizontal = orientation == VIEW_ORIENTATION_HORIZONTAL;
220 detailSplitter.setOrientation(horizontal ? SWT.HORIZONTAL
221 : SWT.VERTICAL);
222 }
223
224 detailSplitter.layout();
225 }
226
227 updateCheckedState();
228
229 currentOrientation = orientation;
230
231 restoreSplitterRatio();
232 }
233 }
234
235 /**
236 * @param show
237 */
238 private void setShowViewer2(boolean show) {
239 showViewer2 = show;
240 showOrHideViewer2();
241 }
242
243 /**
244 *
245 */
246 private void showOrHideViewer2() {
247 if (showViewer2) {
248 detailSplitter.setMaximizedControl(null);
249 } else {
250 detailSplitter.setMaximizedControl(getViewer1().getControl());
251 }
252 }
253
254 private void restoreSplitterRatio() {
255 String ratio= dialogSettings.get(getDialogStoreRatioKey() + currentOrientation);
256 if (ratio == null)
257 return;
258 int intRatio= Integer.parseInt(ratio);
259 detailSplitter.setWeights(new int[] {intRatio, 1000 - intRatio});
260 }
261
262 private void updateCheckedState() {
263 // for (int i= 0; i < fToggleOrientationActions.length; i++) {
264 // fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
265 // }
266 }
267
268 /**
269 * <p>createViewer1</p>
270 *
271 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
272 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
273 */
274 protected abstract Viewer createViewer1(Composite parent);
275
276
277 /**
278 * <p>createViewer2</p>
279 *
280 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
281 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
282 */
283 protected abstract Viewer createViewer2(Composite parent);
284
285
286 /**
287 * @param pagebook2
288 */
289 private void createDetailSplitter(Composite parent) {
290 detailSplitter = new SashForm(parent, SWT.HORIZONTAL);
291 }
292
293 /* (non-Javadoc)
294 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
295 */
296 /** {@inheritDoc} */
297 @Override
298 public void dispose() {
299 selectionService.removeSelectionListener(this);
300 selectionService.removePostSelectionListener(this);
301 super.dispose();
302 }
303
304 /**
305 * <p>Getter for the field <code>viewer1</code>.</p>
306 *
307 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
308 */
309 protected Viewer getViewer1() {
310 return viewer1;
311 }
312
313 /**
314 * <p>Getter for the field <code>viewer2</code>.</p>
315 *
316 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
317 */
318 protected Viewer getViewer2() {
319 return viewer2;
320 }
321 }