Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4594034c1522431277e7517b29f473ee8000a5b (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2006, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.ui.internal.views;

import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.jpa.ui.internal.JptUiMessages;
import org.eclipse.jpt.jpa.ui.selection.JpaEditorManager;
import org.eclipse.jpt.jpa.ui.selection.JpaViewManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.Page;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.PageBookView;

/**
 * The JPA structure view is a page book view that maintains a
 * {@link JpaStructurePage JPA structure page}
 * per editor and a default page when the editor does not contain JPA
 * content.
 * <p>
 * See <code>org.eclipse.jpt.jpa.ui/plugin.xml</code>.
 */
public class JpaStructureView
	extends PageBookView
{
	/**
	 * The manager is created when the view's control is
	 * {@link #createPartControl(Composite) created}
	 * and disposed, if necessary, when the view is
	 * {@link #dispose() disposed}.
	 */
	private volatile Manager manager;


	public JpaStructureView() {
		super();
	}

	@Override
	public void createPartControl(Composite parent) {
		this.manager = this.buildManager();
		super.createPartControl(parent);
	}

	private Manager buildManager() {
		return new Manager(this.getPageManager());
	}

	/**
	 * Go to the singleton in the sky.
	 * <p>
	 * <strong>NB:</strong> This will trigger the creation of the appropriate
	 * page manager if it does not already exist.
	 */
	private JpaViewManager.PageManager getPageManager() {
		return (JpaViewManager.PageManager) this.getAdapter(JpaViewManager.PageManager.class);
	}

	@Override
	protected IPage createDefaultPage(PageBook book) {
		DefaultPage page = new DefaultPage();
        this.initPage(page);
        page.createControl(book);
        return page;
	}

	/**
	 * Start with the currently active editor.
	 */
	@Override
	protected IWorkbenchPart getBootstrapPart() {
		IWorkbenchPage page = this.getSite().getPage();
        return (page == null) ? null : page.getActiveEditor();
	}

	/**
	 * Behave the same way as {@link #partActivated(IWorkbenchPart)};
	 * i.e. {@link #isImportant(IWorkbenchPart) check whether the specified
	 * part is an editor} and display the appropriate page if it is.
	 */
	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
		super.partBroughtToTop(part);
		this.partActivated(part);
	}

	/**
	 * Display the appropriate page if an editor is
	 * {@link #partActivated(IWorkbenchPart) activated}
	 * or {@link #partBroughtToTop(IWorkbenchPart) brought to the top}.
	 */
	@Override
	protected boolean isImportant(IWorkbenchPart part) {
		return part instanceof IEditorPart;
	}

	/**
	 * The specified part is an editor (see {@link #isImportant(IWorkbenchPart)}.
	 */
	@Override
	protected PageRec doCreatePage(IWorkbenchPart part) {
		IEditorPart editor = (IEditorPart) part;
		JpaEditorManager editorManager = this.manager.getEditorManager(editor);
		if (editorManager == null) {
			// if there is no editor manager corresponding to the current
			// editor return null so the default page is displayed
			return null;
		}
		JpaStructurePage page = new JpaStructurePage(this, editorManager);
		this.initPage(page);
		page.createControl(this.getPageBook());
		return new PageRec(editor, page);
	}

	@Override
	protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
		JpaStructurePage page = (JpaStructurePage) pageRecord.page;
        page.dispose();
        pageRecord.dispose();
	}

	@Override
	public void dispose() {
		super.dispose();
		if (this.manager != null) {
			this.manager.dispose();
		}
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this);
	}


	// ********** JPA view manager **********

	/**
	 * Adapter to the view's page manager.
	 */
	/* CU private */ class Manager
		implements JpaViewManager
	{
		/**
		 * The manager for the structure view's workbench page.
		 */
		private final JpaViewManager.PageManager pageManager;


		Manager(JpaViewManager.PageManager pageManager) {
			super();
			if (pageManager == null) {
				throw new NullPointerException();  // shouldn't happen...
			}
			this.pageManager = pageManager;
			this.pageManager.addViewManager(this);
		}

		public IViewPart getView() {
			return JpaStructureView.this;
		}

		JpaEditorManager getEditorManager(IEditorPart editor) {
			return this.pageManager.getEditorManager(editor);
		}

		void dispose() {
			this.pageManager.removeViewManager(this);
		}

		@Override
		public String toString() {
			return StringTools.buildToStringFor(this);
		}
	}


	// ********** default page **********

	/**
	 * Simply display a message stating the JPA structure is not available.
	 * @see org.eclipse.ui.part.MessagePage
	 */
	/* CU private */ static class DefaultPage
		extends Page
	{
		private /* virtually final */ Composite composite;

		DefaultPage() {
			super();
		}

		@Override
		public void createControl(Composite parent) {
			// a composite will put some margins around the label
			this.composite = new Composite(parent, SWT.NULL);
			this.composite.setLayout(new FillLayout());
			Label label = new Label(this.composite, SWT.LEFT | SWT.TOP | SWT.WRAP);
			label.setText(JptUiMessages.JpaStructureView_structureNotAvailable);
		}

		@Override
		public Control getControl() {
			return this.composite;
		}

		@Override
		public void setFocus() {
			this.composite.setFocus();
		}
	}
}

Back to the top