Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e9fd08fc811ff0ac90608de40641df6878933b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
 * Copyright (c) 2007 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.properties.internal;

import java.util.Collections;
import java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jst.pagedesigner.properties.WPETabbedPropertySheetPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

/**
 * Using the tag entity's QuickEditTabSections meta-data, this section reacts to changes in selection,
 * and will dynamically create a QuickEditTabGroup.   The QuickEditTabGroup is cached and reused.
 * <br><br>
 * This section delegates construction to the sections discovered thru meta data.   
 * Because this section is not disposed of until the tab is disposed, this section will enforce the expected section lifecycle
 * on the sections loaded from meta data.   This occurs during setInput.  But will pass on all section lifecycle events as 
 * this section receives them.
 * <br><br>
 * The lifecycle that this section enforces on it's child sections in the setInput call on this section are (in order):
 * 	<li>createControls
 * 	<li>setInput
 *  <li>aboutToBeShown
 *  <li>refresh
 *  
 * When tab section lifecycle events occur to this section, they are passed on to all child sections also. 
 * 	
 */
public class QuickEditTabSection extends AbstractPropertySection {
	
	private QuickEditTabManager manager;
	private Composite _composite;
	private Composite _qeGroupComposite;
	private WPETabbedPropertySheetPage _tabbedPropertySheetPage;

	private QuickEditTabManager getTabManager() {
		if (manager == null) {
			manager = _tabbedPropertySheetPage.getTabManager();
		}
		return manager;
	}

	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		_composite = parent;
		_tabbedPropertySheetPage = (WPETabbedPropertySheetPage)tabbedPropertySheetPage;
	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		if (getTabManager() != null){
			aboutToBeHidden();
			createOrResetQuickEditGroupComposite();//disposes of old and recreates new topComp
			getTabManager().selectionChanged(part, selection);
			for (ISection section : getSections()){
				section.createControls(_qeGroupComposite, _tabbedPropertySheetPage);
				section.setInput(part, selection);
			}
			_composite.getParent().layout(true, true);
			
			aboutToBeShown();
			refresh();
		}			
	}

	@Override
	public void aboutToBeHidden() {
		super.aboutToBeHidden();
		for (ISection section : getSections()){
			section.aboutToBeHidden();
		}
	}

	@Override
	public void aboutToBeShown() {
		super.aboutToBeShown();
		for (ISection section : getSections()){
			section.aboutToBeShown();
		}
	}
	
	@Override
	public void refresh() {
		super.refresh();
		for (ISection section : getSections()){
			section.refresh();
		}
	}

	private void createOrResetQuickEditGroupComposite() {
		if (_qeGroupComposite != null && !_qeGroupComposite.isDisposed()){
			//dispose of current sections
			disposeCurrentQuickEditTabSections();
			_qeGroupComposite.dispose();
		}			
		
		_qeGroupComposite = _tabbedPropertySheetPage.getWidgetFactory().createComposite(_composite, SWT.NO_FOCUS);
		QuickEditTabLayout layout = new QuickEditTabLayout();
		_qeGroupComposite.setLayout(layout);
		
	}

	private void disposeCurrentQuickEditTabSections() {		
		for (ISection section : getSections()){
			section.dispose();
		}
	}

	private List<ISection> getSections(){
		if (getTabManager().getCurrentTabGroupDescriptor() != null)
			return getTabManager().getCurrentTabGroupDescriptor().getSections();
		
		return Collections.EMPTY_LIST;
	}
	
	public void dispose() {
		super.dispose();
		disposeCurrentQuickEditTabSections();
		manager.dispose();
		manager = null;
		_composite = null;
		_qeGroupComposite = null;
		_tabbedPropertySheetPage = null;
	}	

	public boolean shouldUseExtraSpace() {
		return false;
	}

	private class QuickEditTabLayout extends Layout {

		// allow for adjustments
		private static final int MARGIN = 0;
		private static final int SPACING = 0;

		// cache
		Point[] sizes;
		int maxWidth, totalHeight;

		protected Point computeSize(Composite composite, int wHint, int hHint,
				boolean flushCache) {

			Control children[] = composite.getChildren();
			if (flushCache || sizes == null || sizes.length != children.length) {
				initialize(children);
			}

			int width = wHint, height = hHint;
			if (wHint == SWT.DEFAULT)
				width = maxWidth;

			if (hHint == SWT.DEFAULT)
				height = totalHeight;

			return new Point(width + 2 * MARGIN, height + 2 * MARGIN);
		}

		protected void layout(Composite composite, boolean flushCache) {
			Control children[] = composite.getChildren();
			if (flushCache || sizes == null || sizes.length != children.length) {
				initialize(children);
			}
			Rectangle rect = composite.getClientArea();
			int x = MARGIN, y = MARGIN;
			int width = Math.max(rect.width - 2 * MARGIN, maxWidth);
//			System.out.println("--- Comp id: "+composite.toString()+ "[#Children: "+ composite.getChildren().length +"] -------");
			for (int i = 0; i < children.length; i++) {
				int height = sizes[i].y;
				children[i].setBounds(x, y, width, height);
				y += height + SPACING;
//				System.out.println("h="+height+", y="+y);
			}
		    composite.setRedraw(true);
		}

		void initialize(Control children[]) {
			maxWidth = 0;
			totalHeight = 0;
			sizes = new Point[children.length];
			for (int i = 0; i < children.length; i++) {
				sizes[i] = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
				maxWidth = Math.max(maxWidth, sizes[i].x);
				totalHeight += sizes[i].y;
			}
			totalHeight += (children.length - 1) * SPACING;
		}
	}
}

Back to the top