Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9c4a2e4be407b5251a8f8d963aa5f5142052bd5e (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
/********************************************************************************
 * Copyright (c) 2006, 2009 Versant. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Versant and Others. - initial API and implementation
 ********************************************************************************/
package org.eclipse.jpt.jpa.ui.internal.views;

import org.eclipse.jpt.common.ui.WidgetFactory;
import org.eclipse.jpt.common.ui.internal.widgets.FormWidgetFactory;
import org.eclipse.jpt.common.ui.internal.widgets.PropertySheetWidgetFactory;
import org.eclipse.jpt.jpa.ui.JptJpaUiPlugin;
import org.eclipse.jpt.jpa.ui.internal.selection.JpaSelection;
import org.eclipse.jpt.jpa.ui.internal.selection.JpaSelectionManager;
import org.eclipse.jpt.jpa.ui.internal.selection.SelectionManagerFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * This is the abstract implementation of the JPA view. The selection is changed
 * by receiving a <code>IJpaSelection</code>.
 *
 * @see JpaSelection
 *
 * @version 2.0
 * @since 1.0
 */
public abstract class AbstractJpaView extends ViewPart
{
	/**
	 * The default page used when nothing can be shown.
	 */
	private Composite defaultComposite;

	/**
	 * The string to display when there is no view content
	 */
	private String defaultLabel;

	/**
	 * The container of the current page.
	 */
	private PageBook pageBook;

	private ScrolledForm scrolledForm;
	
	
	/**
	 * The factory used to create the various widgets.
	 */
	private WidgetFactory widgetFactory;

	/**
	 * Creates a new <code>AbstractJpaView</code>.
	 *
	 * @param defaultLabel
	 */
	public AbstractJpaView(String defaultLabel) {
		super();
		this.defaultLabel = defaultLabel;
		this.initialize();
	}

	private Composite buildDefaultComposite() {
		Composite composite = widgetFactory.createComposite(pageBook);
		composite.setLayout(new FillLayout(SWT.VERTICAL));
		getWidgetFactory().createLabel(composite, defaultLabel);
		return composite;
	}

	@Override
	public final void createPartControl(Composite parent) {
		this.scrolledForm = getFormWidgetFactory().createScrolledForm(parent);
		JptJpaUiPlugin.instance().controlAffectsJavaSource(this.scrolledForm);
		this.scrolledForm.getBody().setLayout(new GridLayout());

		this.pageBook = new PageBook(this.scrolledForm.getBody(), SWT.NONE);
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		this.pageBook.setLayoutData(gridData);
		this.scrolledForm.setContent(this.pageBook);

		this.defaultComposite = buildDefaultComposite();
		this.pageBook.showPage(this.defaultComposite);

		subcreatePartControl(parent);

		JpaSelectionManager selectionManager =
			SelectionManagerFactory.getSelectionManager(getViewSite().getWorkbenchWindow());

		selectionManager.register(this);
		select(selectionManager.getCurrentSelection());
	}

	protected final PageBook getPageBook() {
		return this.pageBook;
	}

	public final WidgetFactory getWidgetFactory() {
		return this.widgetFactory;
	}

	/**
	 * Initializes this JPA view.
	 */
	protected void initialize() {
		this.widgetFactory = new PropertySheetWidgetFactory(
			new TabbedPropertySheetWidgetFactory()
		);
	}

	private FormToolkit getFormWidgetFactory() {
		return ((FormWidgetFactory) widgetFactory).getWidgetFactory();
	}

	/**
	 * The selection has changed, update the current page by using the given
	 * selection state.
	 *
	 * @param jpaSelection The new selection used to update this JPA view
	 */
	public abstract void select(JpaSelection jpaSelection);

	/*
	 * (non-Javadoc)
	 */
	@Override
	public void setFocus() {
		pageBook.setFocus();
	}

	/**
	 * Changes the current page and show the default one.
	 */
	protected void showDefaultPage() {
		showPage(defaultComposite);
	}

	/**
	 * Changes the current page and show the given one.
	 *
	 * @param page The new page to show, <code>null</code> can't be passed
	 */
	protected final void showPage(Control page) {
		page.setParent(this.pageBook);
		this.pageBook.showPage(page);
		this.scrolledForm.reflow(true);
	}

	protected void subcreatePartControl(@SuppressWarnings("unused") Composite parent) {
		// no op - for subclasses to override if wished
	}

}

Back to the top