1
/*******************************************************************************
2
* Copyright (c) 2012 Red Hat Inc. 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
9
* Red Hat Inc. - Initial Wizard and related API
10
*******************************************************************************/
11
package org.eclipse.linuxtools.systemtap.ui.ide.wizards;
13
import java.util.ResourceBundle;
15
import org.eclipse.core.resources.IContainer;
16
import org.eclipse.core.resources.IResource;
17
import org.eclipse.core.runtime.IPath;
18
import org.eclipse.core.runtime.Path;
19
import org.eclipse.jface.viewers.ISelection;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.wizard.WizardPage;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.ModifyEvent;
24
import org.eclipse.swt.events.ModifyListener;
25
import org.eclipse.swt.events.SelectionAdapter;
26
import org.eclipse.swt.events.SelectionEvent;
27
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Button;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.DirectoryDialog;
32
import org.eclipse.swt.widgets.Label;
33
import org.eclipse.swt.widgets.Text;
36
* The "New" wizard page allows setting the container for the new file as well
37
* as the file name. The page will only accept file name without the extension
38
* OR with the extension that matches the expected one (mpe).
41
public class StapNewWizardPage extends WizardPage {
42
private Text fileText;
44
private Text containerText;
46
private ISelection selection;
48
private static final ResourceBundle resourceBundle = ResourceBundle.getBundle("org.eclipse.linuxtools.systemtap.ui.ide.wizards.stap_strings");
51
* Constructor for StapNewWizardPage.
55
public StapNewWizardPage(ISelection selection) {
56
super(resourceBundle.getString("StapNewWizardPage.WizardPage"));
57
setTitle(resourceBundle.getString("StapNewWizardPage.Title"));
58
setDescription(resourceBundle.getString("StapNewWizardPage.setDescription"));
59
this.selection = selection;
63
* @see IDialogPage#createControl(Composite)
65
public void createControl(Composite parent) {
66
Composite container = new Composite(parent, SWT.NULL);
67
GridLayout layout = new GridLayout();
68
container.setLayout(layout);
69
layout.numColumns = 3;
70
layout.verticalSpacing = 9;
72
Label label = new Label(container, SWT.NULL);
73
label.setText(resourceBundle.getString("StapNewWizardPage.ScriptName")); //$NON-NLS-1$
75
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
76
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
77
fileText.setLayoutData(gd);
78
fileText.addModifyListener(new ModifyListener() {
79
public void modifyText(ModifyEvent e) {
83
label = new Label(container, SWT.NULL); // XXX just create a new layout with different width
85
label = new Label(container, SWT.NULL);
86
label.setText(resourceBundle.getString("StapNewWizardPage.Directory")); //$NON-NLS-1$
88
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
89
gd = new GridData(GridData.FILL_HORIZONTAL);
90
containerText.setLayoutData(gd);
91
containerText.addModifyListener(new ModifyListener() {
92
public void modifyText(ModifyEvent e) {
97
Button button = new Button(container, SWT.PUSH);
98
button.setText(resourceBundle.getString("StapNewWizardPage.Browse"));
99
button.addSelectionListener(new SelectionAdapter() {
100
public void widgetSelected(SelectionEvent e) {
106
setControl(container);
110
* Tests if the current workbench selection is a suitable container to use.
113
private void initialize() {
114
if (selection != null && selection.isEmpty() == false
115
&& selection instanceof IStructuredSelection) {
116
IStructuredSelection ssel = (IStructuredSelection) selection;
119
Object obj = ssel.getFirstElement();
120
if (obj instanceof IResource) {
121
IContainer container;
122
if (obj instanceof IContainer)
123
container = (IContainer) obj;
125
container = ((IResource) obj).getParent();
126
containerText.setText(container.getFullPath().toString());
129
fileText.setText(".stp");
133
* Uses the standard container selection dialog to choose the new value for
134
* the container field.
137
private void handleBrowse() {
138
DirectoryDialog dialog = new DirectoryDialog(getShell());
141
String result = dialog.getFilterPath();
142
containerText.setText(result);
143
} catch (Exception e) {
144
System.out.println("Error: " + e);
149
* Ensures that both text fields are set.
152
private void dialogChanged() {
153
IPath container = Path.fromOSString(getContainerName());
154
String fileName = getFileName();
155
container.isValidPath(getContainerName());
156
if (fileName.length() == 0 || fileName.equals(".stp")) {
157
updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus1"));
160
if (getContainerName().length() == 0) {
161
updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus2"));
164
if (container == null
165
|| !container.isValidPath(getContainerName())) {
166
updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus3"));
169
if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
170
updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus4"));
173
int dotLoc = fileName.lastIndexOf('.');
175
String ext = fileName.substring(dotLoc + 1);
176
if (ext.equalsIgnoreCase("stp") == false) {
177
updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus.5"));
184
private void updateStatus(String message) {
185
setErrorMessage(message);
186
setPageComplete(message == null);
189
public String getContainerName() {
190
return containerText.getText();
193
public String getFileName() {
194
return fileText.getText();
b'\\ No newline at end of file'