]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/preferences/NativeLibrariesConfigurationBlock.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / preferences / NativeLibrariesConfigurationBlock.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.preferences;
12
13import org.eclipse.swt.SWT;
14import org.eclipse.swt.layout.GridData;
15import org.eclipse.swt.layout.GridLayout;
16import org.eclipse.swt.widgets.Composite;
17import org.eclipse.swt.widgets.Control;
18import org.eclipse.swt.widgets.DirectoryDialog;
19import org.eclipse.swt.widgets.Label;
20import org.eclipse.swt.widgets.Shell;
21
22import org.eclipse.core.runtime.IPath;
23import org.eclipse.core.runtime.IStatus;
24import org.eclipse.core.runtime.Path;
25
26import org.eclipse.core.resources.IContainer;
27import org.eclipse.core.resources.IFolder;
28import org.eclipse.core.resources.IProject;
29import org.eclipse.core.resources.IResource;
30import org.eclipse.core.resources.IWorkspaceRoot;
31import org.eclipse.core.resources.ResourcesPlugin;
32
33import org.eclipse.jface.layout.PixelConverter;
34import org.eclipse.jface.viewers.ILabelProvider;
35import org.eclipse.jface.viewers.ITreeContentProvider;
36import org.eclipse.jface.viewers.ViewerFilter;
37import org.eclipse.jface.window.Window;
38
39import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
40import org.eclipse.ui.model.WorkbenchContentProvider;
41import org.eclipse.ui.model.WorkbenchLabelProvider;
42import org.eclipse.ui.views.navigator.ResourceComparator;
43
44import org.eclipse.jdt.core.IClasspathEntry;
45
46import org.eclipse.jdt.internal.corext.util.Messages;
47
48import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
50import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
51import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
52import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
53import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
54import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
55import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
56import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
57import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
58import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
59
60
61public class NativeLibrariesConfigurationBlock {
62
63 private class NativeLibrariesAdapter implements IDialogFieldListener {
64 public void dialogFieldChanged(DialogField field) {
65 doFieldChanged(field);
66 }
67 }
68
69 private StringDialogField fPathField;
70 private SelectionButtonDialogField fBrowseWorkspace;
71 private SelectionButtonDialogField fBrowseExternal;
72 private final IClasspathEntry fEntry;
73 private Shell fShell;
74 private final IStatusChangeListener fListener;
75 private final String fOrginalValue;
76
77 public NativeLibrariesConfigurationBlock(IStatusChangeListener listener, Shell parent, String nativeLibPath, IClasspathEntry parentEntry) {
78 fListener= listener;
79 fEntry= parentEntry;
80
81 NativeLibrariesAdapter adapter= new NativeLibrariesAdapter();
82
83 fPathField= new StringDialogField();
84 fPathField.setLabelText(NewWizardMessages.NativeLibrariesDialog_location_label);
85 fPathField.setDialogFieldListener(adapter);
86
87 fBrowseWorkspace= new SelectionButtonDialogField(SWT.PUSH);
88 fBrowseWorkspace.setLabelText(NewWizardMessages.NativeLibrariesDialog_workspace_browse);
89 fBrowseWorkspace.setDialogFieldListener(adapter);
90
91 fBrowseExternal= new SelectionButtonDialogField(SWT.PUSH);
92 fBrowseExternal.setLabelText(NewWizardMessages.NativeLibrariesDialog_external_browse);
93 fBrowseExternal.setDialogFieldListener(adapter);
94
95 if (nativeLibPath != null) {
96 fPathField.setText(Path.fromPortableString(nativeLibPath).toString());
97 fOrginalValue= nativeLibPath;
98 } else {
99 fOrginalValue= ""; //$NON-NLS-1$
100 }
101 }
102
103 public Control createContents(Composite parent) {
104 fShell= parent.getShell();
105
106 Composite inner= new Composite(parent, SWT.NONE);
107 inner.setFont(parent.getFont());
108 inner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
109
110 int nColumns= 3;
111
112 GridLayout layout= new GridLayout(nColumns, false);
113 layout.marginWidth= 0;
114 layout.marginWidth= 0;
115 inner.setLayout(layout);
116
117 PixelConverter converter= new PixelConverter(parent);
118
119 Label desc= new Label(inner, SWT.WRAP);
120 desc.setFont(inner.getFont());
121 desc.setText(Messages.format(NewWizardMessages.NativeLibrariesDialog_description, new String[] { BasicElementLabels.getResourceName(fEntry.getPath().lastSegment()) }));
122 GridData gridData= new GridData(GridData.FILL, GridData.CENTER, false, false, 3, 1);
123 gridData.widthHint= converter.convertWidthInCharsToPixels(80);
124 desc.setLayoutData(gridData);
125
126 fPathField.doFillIntoGrid(inner, 2);
127 LayoutUtil.setHorizontalGrabbing(fPathField.getTextControl(null));
128 LayoutUtil.setWidthHint(fPathField.getTextControl(null), converter.convertWidthInCharsToPixels(50));
129
130 fBrowseExternal.doFillIntoGrid(inner, 1);
131
132 DialogField.createEmptySpace(inner, 2);
133 fBrowseWorkspace.doFillIntoGrid(inner, 1);
134
135 fPathField.setFocus();
136
137 return parent;
138 }
139
140 public String getNativeLibraryPath() {
141 String val= fPathField.getText();
142 if (val.length() == 0) {
143 return null;
144 }
145 return new Path(val).toPortableString();
146 }
147
148 final void doFieldChanged(DialogField field) {
149 if (field == fBrowseExternal) {
150 String res= chooseExternal();
151 if (res != null) {
152 fPathField.setText(res);
153 }
154 } else if (field == fBrowseWorkspace) {
155 String res= chooseInternal();
156 if (res != null) {
157 fPathField.setText(res);
158 }
159 } else if (field == fPathField) {
160 fListener.statusChanged(validatePath());
161 }
162 }
163
164 private IStatus validatePath() {
165 StatusInfo status= new StatusInfo();
166 String val= fPathField.getText();
167 if (val.length() == 0) {
168 return status;
169 }
170 Path path= new Path(val);
171 if (path.isAbsolute()) {
172 if (!path.toFile().isDirectory()) {
173 status.setWarning(NewWizardMessages.NativeLibrariesDialog_error_external_not_existing);
174 return status;
175 }
176 } else {
177 if (!(ResourcesPlugin.getWorkspace().getRoot().findMember(path) instanceof IContainer)) {
178 status.setWarning(NewWizardMessages.NativeLibrariesDialog_error_internal_not_existing);
179 return status;
180 }
181 }
182 return status;
183 }
184
185 private String chooseExternal() {
186 IPath currPath= new Path(fPathField.getText());
187 if (currPath.isEmpty()) {
188 currPath= fEntry.getPath();
189 } else {
190 currPath= currPath.removeLastSegments(1);
191 }
192
193 DirectoryDialog dialog= new DirectoryDialog(fShell);
194 dialog.setMessage(NewWizardMessages.NativeLibrariesDialog_external_message);
195 dialog.setText(NewWizardMessages.NativeLibrariesDialog_extfiledialog_text);
196 dialog.setFilterPath(currPath.toOSString());
197 String res= dialog.open();
198 if (res != null) {
199 return res;
200 }
201 return null;
202 }
203
204 /*
205 * Opens a dialog to choose an internal file.
206 */
207 private String chooseInternal() {
208 String initSelection= fPathField.getText();
209
210 ILabelProvider lp= new WorkbenchLabelProvider();
211 ITreeContentProvider cp= new WorkbenchContentProvider();
212 Class<?>[] acceptedClasses= new Class[] { IProject.class, IFolder.class };
213 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true);
214 ViewerFilter filter= new TypedViewerFilter(acceptedClasses);
215
216 IResource initSel= null;
217 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
218 if (initSelection.length() > 0) {
219 initSel= root.findMember(new Path(initSelection));
220 }
221 if (initSel == null) {
222 initSel= root.findMember(fEntry.getPath());
223 }
224
225 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, lp, cp);
226 dialog.setAllowMultiple(false);
227 dialog.setValidator(validator);
228 dialog.addFilter(filter);
229 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
230 dialog.setTitle(NewWizardMessages.NativeLibrariesDialog_intfiledialog_title);
231 dialog.setMessage(NewWizardMessages.NativeLibrariesDialog_intfiledialog_message);
232 dialog.setInput(root);
233 dialog.setInitialSelection(initSel);
234 dialog.setHelpAvailable(false);
235 if (dialog.open() == Window.OK) {
236 IResource res= (IResource) dialog.getFirstResult();
237 return res.getFullPath().makeRelative().toString();
238 }
239 return null;
240 }
241
242 public void performDefaults() {
243 fPathField.setText(fOrginalValue);
244 }
245
246}