Skip to main content
summaryrefslogtreecommitdiffstats
blob: 154ada0ab31fb2e797d0693a0339dda83cfeac8c (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
/*******************************************************************************
 *  Copyright (c) 2006, 2007 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.ui.internal.views;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jpt.core.internal.IJpaContentNode;
import org.eclipse.jpt.core.internal.IJpaFile;
import org.eclipse.jpt.ui.internal.IJpaPlatformUi;
import org.eclipse.jpt.ui.internal.JptUiMessages;
import org.eclipse.jpt.ui.internal.PlatformRegistry;
import org.eclipse.jpt.ui.internal.jface.NullLabelProvider;
import org.eclipse.jpt.ui.internal.jface.NullTreeContentProvider;
import org.eclipse.jpt.ui.internal.selection.Selection;
import org.eclipse.jpt.ui.internal.structure.IJpaStructureProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchActionConstants;

public class JpaStructureView extends AbstractJpaView 
{
	private StructureComposite structureComposite;
	
	
	public JpaStructureView() {
		super(JptUiMessages.JpaStructureView_viewNotAvailable);
	}
	
	
	@Override
	public void subcreatePartControl(Composite parent) {
		structureComposite = 
			new StructureComposite(pageBook, SWT.NULL);
	}
		
	public Selection getSelection() {
		if (structureComposite.isVisible()) {
			return structureComposite.getSelection(); 
		}
		else {
			return Selection.NULL_SELECTION;
		}
	}
	
	@Override
	public void select(Selection newSelection) {
		Selection currentSelection = getSelection();
		
		if (newSelection.equals(currentSelection)) {
			return;
		}
		
		if (newSelection == Selection.NULL_SELECTION) {
			showDefaultPage();
		}
		else {
			pageBook.showPage(structureComposite);
		}
		
		structureComposite.select(newSelection);
	}
	
	
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		structureComposite.viewer.addSelectionChangedListener(listener);
	}
	
	public void removeSelectionChangedListener(ISelectionChangedListener listener) {
		structureComposite.viewer.removeSelectionChangedListener(listener);
	}
	
	
	private class StructureComposite extends Composite 
	{
		/* key: String file content id,  value: IJpaStructureProvider */
		private Map structureProviders;
		
		private TreeViewer viewer;
		
		private StructureComposite(Composite parent, int style) {
			super(parent, style);
			
			structureProviders = new HashMap();
			
			this.setLayout(new FillLayout());
			
			viewer = new TreeViewer(this, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
			viewer.setAutoExpandLevel(2);
			initContextMenu();
		}
		
	    protected void initContextMenu() {
	        // Create dynamic menu mgr.  Dynamic is currently required to
	        // support action contributions.
	        MenuManager mgr = new MenuManager();
	        mgr.setRemoveAllWhenShown(true);
	        mgr.addMenuListener(new IMenuListener() {
	            public void menuAboutToShow(IMenuManager mgr) {
	                fillContextMenu(mgr);
	            }
	        });
	        Menu menu = mgr.createContextMenu(viewer.getControl());
	        viewer.getControl().setMenu(menu);
	        getSite().registerContextMenu(mgr, viewer);
	       	        
	    }	
	    
	    /**
	     * Called when the context menu is about to open.
	     * Delegates to the action group using the viewer's selection as the action context.
	     * @since 2.0
	     */
	    protected void fillContextMenu(IMenuManager manager) {
	        manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	    }
	    
		private Selection getSelection() {
			ITreeSelection viewerSelection = (ITreeSelection) viewer.getSelection();
			
			if (viewerSelection.isEmpty() || viewerSelection.size() > 1) {
				if (viewer.getInput() == null) {
					return Selection.NULL_SELECTION;
				}
				else {
					return new Selection((IJpaContentNode) viewer.getInput());
				}
			}
			
			else {
				return new Selection((IJpaContentNode) viewerSelection.getFirstElement());
			}
			
		}
		
		private void select(Selection selection) {
			// note: checks for null and equals() selection have already been performed
			
			if (selection.equals(Selection.NULL_SELECTION)) {
				clearViewer();
				return;
			}
			
			Selection currentSelection = getSelection();
			IJpaContentNode newNode = selection.getSelectedNode();
			IJpaFile newFile = newNode.getJpaFile();
			IJpaContentNode currentNode = 
				(currentSelection == Selection.NULL_SELECTION) ?
						null : getSelection().getSelectedNode();
			IJpaFile currentFile = 
				(currentNode == null) ? 
						null : currentNode.getJpaFile();
			
			if (newFile.equals(currentFile)) {
				viewer.setSelection(new StructuredSelection(newNode), true);
			}
			else if (currentFile != null &&  newFile.getContentId().equals(currentFile.getContentId())) {
				viewer.setInput(newFile.getContent());
				viewer.setSelection(new StructuredSelection(newNode), true);
			}
			else {
				// new content type
				// replace composite and set selection of tree
				IJpaStructureProvider provider = getStructureProvider(newNode);
				
				if (provider == null) {
					clearViewer();
				}
				else {
					viewer.setContentProvider(provider.buildContentProvider());
					viewer.setLabelProvider(provider.buildLabelProvider());
					viewer.setInput(newFile.getContent());
				}
			}
		}
		
		private void clearViewer() {
			viewer.setContentProvider(NullTreeContentProvider.INSTANCE);
			viewer.setLabelProvider(NullLabelProvider.INSTANCE);
			viewer.setInput(null);
		}
		
		private IJpaStructureProvider getStructureProvider(IJpaContentNode contentNode) {
			String contentId = contentNode.getJpaFile().getContentId();
			IJpaStructureProvider provider = 
				(IJpaStructureProvider) structureProviders.get(contentId);
			
			if (provider == null) {
				String vendorId = contentNode.getJpaProject().getPlatform().getId();
				IJpaPlatformUi jpaPlatformUI = PlatformRegistry.INSTANCE.getJpaPlatform(vendorId);
				for (IJpaStructureProvider p : jpaPlatformUI.structureProviders()) {
					if (p.fileContentType().equals(contentId)) {
						provider = p;
						break;
					}
				}
				
				//TODO this view and the detailsProviders Map is not created on a per project basis.
				//the detailsProviders and their fileContentTypes could overlap across project, this would cause problems with storing this map.				
				if (provider != null) {
					structureProviders.put(contentId, provider);
				}
			}
			
			return provider;	
		}
	}
}

Back to the top