Commit | Line | Data |
---|---|---|
c1a40998 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 MontaVista Software | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Yufen Kuo (ykuo@mvista.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.ui.project.dialogs; | |
14 | ||
15 | import java.io.File; | |
16 | ||
17 | import org.eclipse.core.resources.IProject; | |
18 | import org.eclipse.core.resources.IResource; | |
5945cec9 | 19 | import org.eclipse.linuxtools.internal.lttng.core.TraceHelper; |
c1a40998 FC |
20 | import org.eclipse.swt.SWT; |
21 | import org.eclipse.swt.events.ModifyEvent; | |
22 | import org.eclipse.swt.events.ModifyListener; | |
23 | import org.eclipse.swt.events.SelectionAdapter; | |
24 | import org.eclipse.swt.events.SelectionEvent; | |
25 | import org.eclipse.swt.graphics.Font; | |
26 | import org.eclipse.swt.layout.GridData; | |
27 | import org.eclipse.swt.layout.GridLayout; | |
28 | import org.eclipse.swt.widgets.Button; | |
29 | import org.eclipse.swt.widgets.Composite; | |
30 | import org.eclipse.swt.widgets.Control; | |
31 | import org.eclipse.swt.widgets.DirectoryDialog; | |
32 | import org.eclipse.swt.widgets.Display; | |
33 | import org.eclipse.swt.widgets.Label; | |
34 | import org.eclipse.swt.widgets.Text; | |
35 | import org.eclipse.ui.dialogs.PropertyPage; | |
36 | ||
37 | public class TraceLibraryPathPropertyPage extends PropertyPage { | |
38 | ||
39 | private static final String LTTVTRACEREAD_LOADER_LIBNAME = "lttvtraceread_loader"; //$NON-NLS-1$ | |
40 | private Button browsePathButton; | |
41 | private Text traceLibraryPath; | |
42 | ||
43 | @Override | |
44 | protected Control createContents(Composite parent) { | |
45 | Composite client = new Composite(parent, SWT.NONE); | |
46 | client.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
47 | ||
48 | GridLayout layout = new GridLayout(3, false); | |
49 | layout.marginHeight = 0; | |
50 | layout.marginWidth = 0; | |
51 | client.setLayout(layout); | |
52 | ||
53 | Label label = new Label(client, SWT.NONE); | |
54 | label.setText(Messages.TraceLibraryPath_label); | |
55 | traceLibraryPath = new Text(client, SWT.BORDER); | |
56 | traceLibraryPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, | |
57 | false)); | |
58 | traceLibraryPath.addModifyListener(new ModifyListener() { | |
59 | ||
60 | @Override | |
61 | public void modifyText(ModifyEvent e) { | |
62 | setValid(validateInputs()); | |
63 | } | |
64 | ||
65 | }); | |
66 | browsePathButton = new Button(client, SWT.PUSH); | |
67 | browsePathButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, | |
68 | false)); | |
69 | browsePathButton.setText(Messages.TraceLibraryPath_browseBtn); | |
70 | browsePathButton.addSelectionListener(new SelectionAdapter() { | |
71 | ||
72 | @Override | |
73 | public void widgetSelected(SelectionEvent e) { | |
74 | String dir = new DirectoryDialog(Display.getDefault() | |
75 | .getActiveShell()).open(); | |
76 | if (dir != null) { | |
77 | traceLibraryPath.setText(dir); | |
78 | } | |
79 | ||
80 | } | |
81 | ||
82 | }); | |
83 | ||
84 | Label noLabel = new Label(client, SWT.NONE); | |
85 | noLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false)); | |
86 | ||
87 | Label descTextLabel = new Label(client, SWT.WRAP); | |
88 | descTextLabel.setText(Messages.TraceLibraryPathProperty_Message); | |
89 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
90 | gd.widthHint = 400; | |
91 | gd.horizontalSpan = 2; | |
92 | descTextLabel.setLayoutData(gd); | |
93 | ||
94 | Label noteBoldLabel = new Label(client, SWT.BOLD); | |
95 | noteBoldLabel.setText(Messages.TraceLibraryPath_Note); | |
96 | noteBoldLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, | |
97 | false)); | |
98 | Font font = noteBoldLabel.getFont(); | |
99 | if (font.getFontData().length > 0) | |
100 | noteBoldLabel.setFont(new Font(client.getDisplay(), font | |
101 | .getFontData()[0].getName(), font.getFontData()[0] | |
102 | .getHeight(), SWT.BOLD)); | |
103 | ||
104 | Label noteTextLabel = new Label(client, SWT.WRAP); | |
105 | noteTextLabel.setText(Messages.TraceLibraryPath_Message); | |
106 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
107 | gd.widthHint = 400; | |
108 | gd.horizontalSpan = 2; | |
109 | noteTextLabel.setLayoutData(gd); | |
110 | ||
111 | performDefaults(); | |
112 | return client; | |
113 | } | |
114 | ||
115 | public boolean validateInputs() { | |
116 | String path = traceLibraryPath.getText(); | |
117 | if (path != null && !path.trim().isEmpty()) { | |
118 | File file = new File(path); | |
119 | if (file.exists() && file.isDirectory()) { | |
120 | File loaderLib = new File(path, | |
121 | System.mapLibraryName(LTTVTRACEREAD_LOADER_LIBNAME)); | |
122 | if (!loaderLib.exists()) { | |
123 | setErrorMessage(Messages.TraceLibraryPathWizardPage_TraceLoaderLibrary_notExists); | |
124 | return false; | |
125 | } | |
126 | } else { | |
127 | setErrorMessage(Messages.TraceLibraryPathWizardPage_SpecifiedTraceLibraryLocation_notExists); | |
128 | return false; | |
129 | } | |
130 | } | |
131 | setErrorMessage(null); | |
132 | return true; | |
133 | } | |
134 | ||
135 | @Override | |
136 | protected void performDefaults() { | |
137 | IResource resource = (IResource) getElement().getAdapter( | |
138 | IResource.class); | |
139 | IProject project = resource.getProject(); | |
140 | if (project != null) { | |
141 | String traceLibDir = TraceHelper.getTraceLibDirFromProject(project); | |
142 | if (traceLibDir != null) { | |
143 | traceLibraryPath.setText(traceLibDir); | |
144 | } | |
145 | } | |
146 | super.performDefaults(); | |
147 | } | |
148 | ||
149 | @Override | |
150 | public boolean performOk() { | |
151 | IResource resource = (IResource) getElement().getAdapter( | |
152 | IResource.class); | |
153 | IProject project = resource.getProject(); | |
154 | boolean ok = false; | |
155 | if (project != null) { | |
156 | String libPath = traceLibraryPath.getText(); | |
157 | if (libPath == null || libPath.trim().isEmpty()) | |
158 | ok = TraceHelper.removeProjectPreference(project, "traceLibraryPath"); //$NON-NLS-1$ | |
159 | else | |
160 | ok = TraceHelper.setProjectPreference(project, "traceLibraryPath", traceLibraryPath.getText()); //$NON-NLS-1$ | |
161 | } | |
162 | return ok && super.performOk(); | |
163 | } | |
164 | ||
165 | } |