Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1361e5f0907c0eb3a93b6f181b5af7833620aa9 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 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.prefs;

import java.io.IOException;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.preference.IPersistentPreferenceStore;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.internal.prefs.JpaPreferenceConstants;
import org.eclipse.jpt.ui.internal.JptUiMessages;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

public class JpaPreferencePage extends PreferencePage 
	implements IWorkbenchPreferencePage
{
	public static final String ID = "org.eclipse.jpt.ui.jpaPreferencePage";
	
	
	private IPersistentPreferenceStore preferences;
	
	private IPreferenceStore userLibPreferences;
	
	private Label jpaLibLabel;
		
	private Combo jpaLibCombo;
	
	private Link userLibsLink;
		
	
	public JpaPreferencePage() {
		super();
		preferences = 
			new ScopedPreferenceStore(
				new InstanceScope(),
				JptCorePlugin.instance().getBundle().getSymbolicName());
		userLibPreferences =
			new ScopedPreferenceStore(
				new InstanceScope(),
				JavaCore.getPlugin().getBundle().getSymbolicName());
	}
	
	public void init(IWorkbench workbench) {}
	
	public Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		
		jpaLibLabel = createLabel(container, 1, JptUiMessages.JpaPreferencePage_defaultJpaLib);
			
		jpaLibCombo = createCombo(container, true);
		
		userLibsLink =  new Link(container, SWT.NONE);
		GridData data = new GridData(GridData.END, GridData.CENTER, false, false);
		data.horizontalSpan = 2;
		userLibsLink.setLayoutData(data);
		userLibsLink.setText(JptUiMessages.JpaPreferencePage_userLibsLink);
		userLibsLink.addSelectionListener(
			new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					((IWorkbenchPreferenceContainer) getContainer())
						.openPage(JavaUI.ID_USER_LIBRARY_PREFERENCE_PAGE, null);
				}
			}
		);
			
		performDefaults();
		return container;
	}
	
	private Label createLabel(Composite container, int span, String text) {
		Label label = new Label(container, SWT.NONE);
		label.setText(text);
		GridData gd = new GridData();
		gd.horizontalSpan = span;
		label.setLayoutData(gd);
		return label;
	}
	
	private Combo createCombo(Composite container, boolean fillHorizontal) {
		Combo combo = new Combo(container, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
		if (fillHorizontal) {
			combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
		else {
			combo.setLayoutData(new GridData());
		}
		return combo;
	}
	
	private void fillJpaLibs() {
		int index = jpaLibCombo.getSelectionIndex();
		String selectedJpaLib = null;
		if (index >= 0) {
			selectedJpaLib = jpaLibCombo.getItem(jpaLibCombo.getSelectionIndex());
		}
		
		jpaLibCombo.clearSelection();
		jpaLibCombo.setItems(JavaCore.getUserLibraryNames());
		
		if (selectedJpaLib != null) {
			int newIndex = CollectionTools.indexOf(jpaLibCombo.getItems(), selectedJpaLib);
			if (newIndex >= 0) {
				jpaLibCombo.select(newIndex);
			}
		}
	}
	
	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (visible) {
			fillJpaLibs();
		}
	}
	
	public void performDefaults() {
		fillJpaLibs();
		String defaultLib = preferences.getString(JpaPreferenceConstants.PREF_DEFAULT_JPA_LIB);
		int index = -1;
		if (! StringTools.stringIsEmpty(defaultLib)) {
			index = CollectionTools.indexOf(jpaLibCombo.getItems(), defaultLib);
		}
		if (index >= 0) {
			jpaLibCombo.select(index);
		}
		
		super.performDefaults();
	}

	public boolean performOk() {
		int index = jpaLibCombo.getSelectionIndex();
		String defaultLib = (index >= 0) ? jpaLibCombo.getItem(index) : null;
		if (! StringTools.stringIsEmpty(defaultLib)) {
			preferences.setValue(JpaPreferenceConstants.PREF_DEFAULT_JPA_LIB, defaultLib);
		}
		try {
			preferences.save();
		}
		catch (IOException ioe) {
			JptCorePlugin.log(ioe);
		}
		return true;
	}
	
	public void dispose() {
		// null pointer check - bug 168337
		if (jpaLibLabel != null) jpaLibLabel.dispose();
		if (jpaLibCombo != null) jpaLibCombo.dispose();
		super.dispose();
	}
	
	
		
//		private boolean libContainsJpaClasses() {
//			return true;
//			String jarLocation = getStringValue();
//			String errorMessage = JptUiMessages.JpaPreferencePage_invalidJpaLib;
//			boolean hasError = false;
//			
//			try {
//				JarFile jarFile = new JarFile(jarLocation);
//				hasError = jarFile.getEntry("javax/persistence/EntityManager.class") == null;
//			}
//			catch (IOException ioe) {
//				hasError = true;
//			}
//			
//			if (hasError) {
//				showErrorMessage(errorMessage);
//			}
//			else {
//				clearErrorMessage();
//			}
//			
//			return ! hasError;
//		}
//	}
}

Back to the top