~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/systemtap/ui/ide/wizards/StapNewWizardPage.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-13 21:43:22 UTC
  • mfrom: (1.2.1) (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130513214322-6frgd9du1n0w2uo7
Tags: 1.2.1-1
* Team upload.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     Red Hat Inc. - Initial Wizard and related API
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.systemtap.ui.ide.wizards;
 
12
 
 
13
import java.util.ResourceBundle;
 
14
 
 
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;
 
34
 
 
35
/**
 
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).
 
39
 */
 
40
 
 
41
public class StapNewWizardPage extends WizardPage {
 
42
        private Text fileText;
 
43
        
 
44
        private Text containerText;
 
45
 
 
46
        private ISelection selection;
 
47
        
 
48
        private static final ResourceBundle resourceBundle = ResourceBundle.getBundle("org.eclipse.linuxtools.systemtap.ui.ide.wizards.stap_strings");
 
49
 
 
50
        /**
 
51
         * Constructor for StapNewWizardPage.
 
52
         * 
 
53
         * @param pageName
 
54
         */
 
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;
 
60
        }
 
61
 
 
62
        /**
 
63
         * @see IDialogPage#createControl(Composite)
 
64
         */
 
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;
 
71
 
 
72
                Label label = new Label(container, SWT.NULL);
 
73
                label.setText(resourceBundle.getString("StapNewWizardPage.ScriptName")); //$NON-NLS-1$
 
74
 
 
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) {
 
80
                                dialogChanged();
 
81
                        }
 
82
                });
 
83
                label = new Label(container, SWT.NULL); // XXX just create a new layout with different width
 
84
                
 
85
                label = new Label(container, SWT.NULL);
 
86
                label.setText(resourceBundle.getString("StapNewWizardPage.Directory")); //$NON-NLS-1$
 
87
 
 
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) {
 
93
                                dialogChanged();
 
94
                        }
 
95
                });
 
96
 
 
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) {
 
101
                                handleBrowse();
 
102
                        }
 
103
                });
 
104
                initialize();
 
105
                dialogChanged();
 
106
                setControl(container);
 
107
        }
 
108
 
 
109
        /**
 
110
         * Tests if the current workbench selection is a suitable container to use.
 
111
         */
 
112
 
 
113
        private void initialize() {
 
114
                if (selection != null && selection.isEmpty() == false
 
115
                                && selection instanceof IStructuredSelection) {
 
116
                        IStructuredSelection ssel = (IStructuredSelection) selection;
 
117
                        if (ssel.size() > 1)
 
118
                                return;
 
119
                        Object obj = ssel.getFirstElement();
 
120
                        if (obj instanceof IResource) {
 
121
                                IContainer container;
 
122
                                if (obj instanceof IContainer)
 
123
                                        container = (IContainer) obj;
 
124
                                else
 
125
                                        container = ((IResource) obj).getParent();
 
126
                                containerText.setText(container.getFullPath().toString());
 
127
                        }
 
128
                }
 
129
                fileText.setText(".stp");
 
130
        }
 
131
 
 
132
        /**
 
133
         * Uses the standard container selection dialog to choose the new value for
 
134
         * the container field.
 
135
         */
 
136
 
 
137
        private void handleBrowse() {
 
138
                DirectoryDialog dialog = new DirectoryDialog(getShell());
 
139
                try {
 
140
                        dialog.open();
 
141
                        String result = dialog.getFilterPath();
 
142
                        containerText.setText(result);
 
143
                } catch (Exception e) {
 
144
                        System.out.println("Error: " + e);
 
145
                }
 
146
        }
 
147
 
 
148
        /**
 
149
         * Ensures that both text fields are set.
 
150
         */
 
151
 
 
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"));
 
158
                        return;
 
159
                }
 
160
                if (getContainerName().length() == 0) {
 
161
                        updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus2"));
 
162
                        return;
 
163
                }
 
164
                if (container == null
 
165
                                || !container.isValidPath(getContainerName())) {
 
166
                        updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus3"));
 
167
                        return;
 
168
                }
 
169
                if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
 
170
                        updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus4"));
 
171
                        return;
 
172
                }
 
173
                int dotLoc = fileName.lastIndexOf('.');
 
174
                if (dotLoc != -1) {
 
175
                        String ext = fileName.substring(dotLoc + 1);
 
176
                        if (ext.equalsIgnoreCase("stp") == false) {
 
177
                                updateStatus(resourceBundle.getString("StapNewWizardPage.UpdateStatus.5"));
 
178
                                return;
 
179
                        }
 
180
                }
 
181
                updateStatus(null);
 
182
        }
 
183
 
 
184
        private void updateStatus(String message) {
 
185
                setErrorMessage(message);
 
186
                setPageComplete(message == null);
 
187
        }
 
188
 
 
189
        public String getContainerName() {
 
190
                return containerText.getText();
 
191
        }
 
192
 
 
193
        public String getFileName() {
 
194
                return fileText.getText();
 
195
        }
 
196
}
 
 
b'\\ No newline at end of file'