Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7e04abe37f58ca44db5248af11f8781fc7d70ede (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
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.test.manager.wizards;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

/**
 * The "New" wizard page allows setting the container for the new file as well as the file name. The page will only
 * accept file name without the extension OR with the extension that matches the expected one (mpe).
 */

public class TestManagerNewWizardPage extends WizardPage {
   private Text containerText;

   private Text fileText;

   private final ISelection selection;

   /**
    * Constructor for SampleNewWizardPage.
    * 
    * @param selection
    */
   public TestManagerNewWizardPage(ISelection selection) {
      super("wizardPage");
      setTitle("Test Manager Editor File");
      setDescription("This wizard creates a new Test Manager file.");
      this.selection = selection;
   }

   /**
    * @see IDialogPage#createControl(Composite)
    */
   @Override
   public void createControl(Composite parent) {
      Composite container = new Composite(parent, SWT.NULL);
      GridLayout layout = new GridLayout();
      container.setLayout(layout);
      layout.numColumns = 3;
      layout.verticalSpacing = 9;
      Label label = new Label(container, SWT.NULL);
      label.setText("&Container:");

      containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
      GridData gd = new GridData(GridData.FILL_HORIZONTAL);
      containerText.setLayoutData(gd);
      containerText.addModifyListener(new ModifyListener() {
         @Override
         public void modifyText(ModifyEvent e) {
            dialogChanged();
         }
      });

      Button button = new Button(container, SWT.PUSH);
      button.setText("Browse...");
      button.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            handleBrowse();
         }
      });
      label = new Label(container, SWT.NULL);
      label.setText("&File name:");

      fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
      gd = new GridData(GridData.FILL_HORIZONTAL);
      fileText.setLayoutData(gd);
      fileText.addModifyListener(new ModifyListener() {
         @Override
         public void modifyText(ModifyEvent e) {
            dialogChanged();
         }
      });
      initialize();
      dialogChanged();
      setControl(container);
   }

   public String getContainerName() {
      return containerText.getText();
   }

   public String getFileName() {
      return fileText.getText();
   }

   /**
    * Ensures that both text fields are set.
    */

   private void dialogChanged() {
      String container = getContainerName();
      String fileName = getFileName();
      IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
      IResource resource = root.findMember(new Path(container));
      IContainer rcontainer = (IContainer) resource;

      if (container.length() == 0) {
         updateStatus("File container must be specified");
         return;
      }
      if (fileName.length() == 0) {
         updateStatus("File name must be specified");
         return;
      }

      // verify extension isn't there
      if (fileName.endsWith("tmc") == true || fileName.endsWith(".") == true) {
         updateStatus("Do not add .tmc extenstion.");
         return;
      }
      // verify title is alpha-numeric with spaces and dashes
      Matcher m = Pattern.compile("^[\\w]+([\\w_]*[\\w])?$").matcher(fileName);
      boolean match = false;
      while (m.find()) {
         match = true;
      }
      if (!match) {
         updateStatus("Filename must be alpha-numeric with \"_\" \n" + "And can not begin or end with a space.");
         return;
      }
      // verify existing file doesn't exist
      final IFile file = rcontainer.getFile(new Path(fileName + ".tmc"));
      if (file.exists()) {
         updateStatus("File with this name already exists.");
         return;
      }
      int dotLoc = fileName.lastIndexOf('.');
      if (dotLoc != -1) {
         String ext = fileName.substring(dotLoc + 1);
         if (ext.equalsIgnoreCase("tmc") == false) {
            updateStatus("File extension must be \"tmc\"");
            return;
         }
      }
      updateStatus(null);
   }

   /**
    * Uses the standard container selection dialog to choose the new value for the container field.
    */

   private void handleBrowse() {
      ContainerSelectionDialog dialog =
         new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
            "Select new file container");
      if (dialog.open() == Window.OK) {
         Object[] result = dialog.getResult();
         if (result.length == 1) {
            containerText.setText(((Path) result[0]).toOSString());
         }
      }
   }

   /**
    * Tests if the current workbench selection is a suitable container to use.
    */

   private void initialize() {
      if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
         IStructuredSelection ssel = (IStructuredSelection) selection;
         if (ssel.size() > 1) {
            return;
         }
         Object obj = ssel.getFirstElement();
         if (obj instanceof IResource) {
            IContainer container;
            if (obj instanceof IContainer) {
               container = (IContainer) obj;
            } else {
               container = ((IResource) obj).getParent();
            }
            containerText.setText(container.getFullPath().toString());
         }
      }
   }

   private void updateStatus(String message) {
      setErrorMessage(message);
      setPageComplete(message == null);
   }
}

Back to the top