Project

General

Profile

Download (8.51 KB) Statistics
| Branch: | Tag: | Revision:
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.editor.view;
12

    
13
import org.apache.log4j.Logger;
14
import org.eclipse.jface.dialogs.IDialogSettings;
15
import org.eclipse.jface.viewers.Viewer;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.custom.SashForm;
18
import org.eclipse.swt.events.ControlEvent;
19
import org.eclipse.swt.events.ControlListener;
20
import org.eclipse.swt.graphics.Point;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Label;
23
import org.eclipse.ui.ISelectionListener;
24
import org.eclipse.ui.ISelectionService;
25
import org.eclipse.ui.part.PageBook;
26
import org.eclipse.ui.part.ViewPart;
27

    
28
import eu.etaxonomy.taxeditor.editor.EditorUtil;
29
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
30

    
31
/**
32
 * <p>Abstract AbstractSplitableViewPart class.</p>
33
 *
34
 * @author n.hoffmann
35
 * @created Feb 12, 2010
36
 * @version 1.0
37
 */
38
public abstract class AbstractSplitableViewPart extends ViewPart implements ISelectionListener{
39
	private static final Logger logger = Logger
40
			.getLogger(AbstractSplitableViewPart.class);
41
	
42
	static final int VIEW_ORIENTATION_VERTICAL = 0;
43
	static final int VIEW_ORIENTATION_HORIZONTAL = 1;
44
	static final int VIEW_ORIENTATION_SINGLE = 2;
45
	static final int VIEW_ORIENTATION_AUTOMATIC = 3;
46

    
47
	private static final int PAGE_EMPTY = 0;
48
    private static final int PAGE_VIEWER = 1;
49
	
50
	private final IDialogSettings dialogSettings;
51
	
52
	private SashForm detailSplitter;
53

    
54
	private Composite parent;
55

    
56
	private PageBook pagebook;
57

    
58
	private int orientation;
59

    
60
	private int currentOrientation;
61
//
62
	private boolean showViewer2;
63

    
64
	private Viewer viewer1;
65
	
66
	private Viewer viewer2;
67
	
68
	private Label emptySelectionLabel;
69

    
70
	private ISelectionService selectionService;
71

    
72
	
73
	/**
74
	 * <p>Constructor for AbstractSplitableViewPart.</p>
75
	 */
76
	public AbstractSplitableViewPart() {
77
		super();
78
		
79
		dialogSettings = TaxeditorEditorPlugin.getDefault().getDialogSettings();
80
		
81
	}
82
	
83
	/* (non-Javadoc)
84
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
85
	 */
86
	/** {@inheritDoc} */
87
	@Override
88
	public void createPartControl(Composite parent) {	
89
		selectionService = getSite().getWorkbenchWindow().getSelectionService();
90
		selectionService.addSelectionListener(this);
91
		selectionService.addPostSelectionListener(this);
92
		
93
		
94
		this.parent = parent;
95
		addResizeListener(parent);
96
		pagebook = new PageBook(parent, SWT.NULL);
97
		
98
		// Page 1: Viewers
99
		createDetailSplitter(pagebook);
100
		viewer1 = createViewer1(detailSplitter);
101
		viewer2 = createViewer2(detailSplitter);
102
		
103
		// Page 2: Nothing selected
104
        emptySelectionLabel = new Label(pagebook, SWT.TOP + SWT.LEFT + SWT.WRAP);
105
        emptySelectionLabel.setText("Current selection does not support this view"); //
106
		
107
		showPage(PAGE_EMPTY);
108
		
109
		initOrientation();
110
		
111
		// set the selection if there is an open editor
112
		if(EditorUtil.getActiveMultiPageTaxonEditor() != null){
113
			selectionChanged(EditorUtil.getActiveMultiPageTaxonEditor(), EditorUtil.getCurrentSelection());
114
		}
115
	}
116
	
117
    private void initOrientation() {
118

    
119
        try {
120
            orientation = dialogSettings.getInt(getDialogstoreVieworientationKey());
121

    
122
            if ((orientation < 0) || (orientation > 3)) {
123
            	orientation = VIEW_ORIENTATION_AUTOMATIC;
124
            }
125
        } catch (NumberFormatException e) {
126
        	orientation = VIEW_ORIENTATION_AUTOMATIC;
127
        }
128

    
129
        // force the update
130
        currentOrientation = -1;
131
        setOrientation(orientation);
132
    }
133
	
134
    /**
135
     * <p>getDialogstoreVieworientationKey</p>
136
     *
137
     * @return a {@link java.lang.String} object.
138
     */
139
    protected abstract String getDialogstoreVieworientationKey();
140

    
141
    /**
142
     * <p>showEmptyPage</p>
143
     */
144
    public void showEmptyPage(){
145
    	showPage(PAGE_EMPTY);
146
    }
147
    
148
    /**
149
     * <p>showViewer</p>
150
     */
151
    public void showViewer(){
152
    	showPage(PAGE_VIEWER);
153
    }
154
    
155
	private void showPage(int page) {
156
        if (page == PAGE_EMPTY) {
157
            pagebook.showPage(emptySelectionLabel);
158
        } else {
159
            pagebook.showPage(detailSplitter);
160
        }
161
    }
162

    
163
	private void addResizeListener(Composite parent) {
164
		parent.addControlListener(new ControlListener() {
165
			public void controlMoved(ControlEvent e) {
166
			}
167
			public void controlResized(ControlEvent e) {
168
				computeOrientation();
169
			}
170
		});
171
	}
172
	
173
	void computeOrientation() {
174
		saveSplitterRatio();
175
		dialogSettings.put(getDialogstoreVieworientationKey(), orientation);
176
		if (orientation != VIEW_ORIENTATION_AUTOMATIC) {
177
			setOrientation(orientation);
178
		}
179
		else {
180
			if (orientation == VIEW_ORIENTATION_SINGLE)
181
				return;
182
			Point size= parent.getSize();
183
			if (size.x != 0 && size.y != 0) {
184
				if (size.x > size.y)
185
					setOrientation(VIEW_ORIENTATION_HORIZONTAL);
186
				else
187
					setOrientation(VIEW_ORIENTATION_VERTICAL);
188
			}
189
		}
190
	}
191
	
192
	private void saveSplitterRatio() {
193
		if (detailSplitter != null && ! detailSplitter.isDisposed()) {
194
	        int[] weigths = detailSplitter.getWeights();
195
	        int ratio = (weigths[0] * 1000) / (weigths[0] + weigths[1]);
196
			String key= getDialogStoreRatioKey() + currentOrientation;
197
	        dialogSettings.put(key, ratio);
198
		}
199
	}
200
	
201
    /**
202
     * <p>getDialogStoreRatioKey</p>
203
     *
204
     * @return a {@link java.lang.String} object.
205
     */
206
    protected abstract String getDialogStoreRatioKey();
207

    
208
	void setOrientation(int orientation) {
209
        if (currentOrientation != orientation) {
210
            if ((getViewer1() != null) 
211
            	&& !getViewer1().getControl().isDisposed() 
212
            	&& (detailSplitter != null) 
213
            	&& !detailSplitter.isDisposed()) {
214
                
215
            	if (orientation == VIEW_ORIENTATION_SINGLE) {
216
                    setShowViewer2(false);
217
                } else {
218
                    if (currentOrientation == VIEW_ORIENTATION_SINGLE) {
219
                    	setShowViewer2(true);
220
                    }
221
                    boolean horizontal = orientation == VIEW_ORIENTATION_HORIZONTAL;
222
                    detailSplitter.setOrientation(horizontal ? SWT.HORIZONTAL
223
                                                                     : SWT.VERTICAL);
224
                }
225

    
226
                detailSplitter.layout();
227
            }
228

    
229
            updateCheckedState();
230

    
231
            currentOrientation = orientation;
232

    
233
			restoreSplitterRatio();
234
        }
235
    }
236
    
237
	/**
238
	 * @param show
239
	 */
240
	private void setShowViewer2(boolean show) {
241
		showViewer2 = show;
242
		showOrHideViewer2();		
243
	}
244

    
245
	/**
246
	 * 
247
	 */
248
	private void showOrHideViewer2() {
249
        if (showViewer2) {
250
        	detailSplitter.setMaximizedControl(null);
251
        } else {
252
        	detailSplitter.setMaximizedControl(getViewer1().getControl());
253
        }
254
	}
255

    
256
	private void restoreSplitterRatio() {
257
		String ratio= dialogSettings.get(getDialogStoreRatioKey() + currentOrientation);
258
		if (ratio == null)
259
			return;
260
		int intRatio= Integer.parseInt(ratio);
261
		detailSplitter.setWeights(new int[] {intRatio, 1000 - intRatio});
262
	}
263
    
264
	private void updateCheckedState() {
265
//		for (int i= 0; i < fToggleOrientationActions.length; i++) {
266
//			fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
267
//		}
268
	}
269
	
270
	/**
271
	 * <p>createViewer1</p>
272
	 *
273
	 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
274
	 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
275
	 */
276
	protected abstract Viewer createViewer1(Composite parent);
277
	
278

    
279
	/**
280
	 * <p>createViewer2</p>
281
	 *
282
	 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
283
	 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
284
	 */
285
	protected abstract Viewer createViewer2(Composite parent);
286
	
287

    
288
	/**
289
	 * @param pagebook2
290
	 */
291
	private void createDetailSplitter(Composite parent) {
292
		detailSplitter = new SashForm(parent, SWT.HORIZONTAL);
293
	}
294
	
295
	/* (non-Javadoc)
296
	 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
297
	 */
298
	/** {@inheritDoc} */
299
	@Override
300
	public void dispose() {
301
		selectionService.removeSelectionListener(this);
302
		selectionService.removePostSelectionListener(this);
303
		super.dispose();
304
	}
305

    
306
	/**
307
	 * <p>Getter for the field <code>viewer1</code>.</p>
308
	 *
309
	 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
310
	 */
311
	protected Viewer getViewer1() {
312
		return viewer1;
313
	}
314

    
315
	/**
316
	 * <p>Getter for the field <code>viewer2</code>.</p>
317
	 *
318
	 * @return a {@link org.eclipse.jface.viewers.Viewer} object.
319
	 */
320
	protected Viewer getViewer2() {
321
		return viewer2;
322
	}
323
}
(3-3/3)