Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 25a5dba36a5baea1537fc2afad388382e05ace1f (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.ui.internal.wizards;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

public class NewModelWizard extends Wizard implements INewWizard
{
  
  protected IStructuredSelection selection;
  protected IWorkbench workbench;
  protected String currentDirectory;

  public NewModelWizard()
  {
  }

  public void init(IWorkbench workbench, IStructuredSelection selection)
  {
    this.workbench = workbench;
    this.selection = selection;
  }

  public boolean performFinish()
  {
    boolean result = true;
    WizardPage currentPage = (WizardPage)getContainer().getCurrentPage();
    if (currentPage != null)
    {
      result = currentPage.isPageComplete();
    }
    return result;
  }

  /**
   * showFileDialog
   */
  public FileDialog showFileDialog(Shell shell, String defaultDirectory, String defaultFile, String [] filterExtensions)
  {
    FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);

    // Get the last visit directory if we haven't defined a particular one here.
    if (defaultDirectory == null || defaultDirectory.length() == 0)
    {
      //defaultDirectory = Locate.instance(this).getLastSelectedDirectory(getResourcePath());
    }

    if (defaultDirectory != null && defaultDirectory.length() != 0)
    {
      fileDialog.setFilterPath(defaultDirectory);
    }

    fileDialog.setFileName(defaultFile);
    fileDialog.setFilterExtensions(filterExtensions);

    fileDialog.open();

    return fileDialog;
  }


  public void setCurrentDirectory(String currentDirectory)
  {
    this.currentDirectory = currentDirectory;
  }


  public void createWorkbenchResource(IContainer container, String fileName)
  {
    IPath path = container.getFullPath();
    path = path.append(fileName);
    IFile file = container.getWorkspace().getRoot().getFile(path);
    if (!file.exists())
    {
      try
      {
        file.create(null, true, null);
      }
      catch (CoreException e)
      {
        //XMLBuilderPlugin.getPlugin().getMsgLogger().write("Encountered exception creating file: " + e.getMessage());
      }
    }
  }

  /**
   * StartPage
   */
  public class StartPage extends WizardPage implements Listener
  {
    protected int selectedButton;
    protected String[] radioButtonLabel;
    protected Button[] radioButton;

    public StartPage(String pageName, String[] radioButtonLabel)
    {
      super(pageName);
      this.radioButtonLabel = radioButtonLabel;
      radioButton = new Button[radioButtonLabel.length];
    }

    public Button getRadioButtonAtIndex(int i)
    {
      Button result = null;
      if (i >= 0 && i < radioButton.length)
      {
        result = radioButton[i];
      }
      return result;
    }

    public int getSelectedRadioButtonIndex()
    {
      int result = -1;
      for (int i = 0; i < radioButton.length; i++)
      {
        if (radioButton[i].getSelection())
        {
          result = i;
        }
      }
      return result;
    }

    public void createControl(Composite parent)
    {
      Composite base = new Composite(parent, SWT.NONE);
      //TODO... setHelp
      //WorkbenchHelp.setHelp(base, XMLBuilderContextIds.XMLC_CREATE_PAGE);
      base.setLayout(new GridLayout());

      //radio buttons' container	
      Composite radioButtonsGroup = new Composite(base, SWT.NONE);
      GridLayout layout = new GridLayout();
      layout.numColumns = 1;
      layout.makeColumnsEqualWidth = true;
      layout.marginWidth = 0;                

      radioButtonsGroup.setLayout(layout);
      GridData gd = new GridData(GridData.FILL_BOTH);
      gd.heightHint = 300;
      gd.widthHint = 400;
      radioButtonsGroup.setLayoutData(gd);
      //TODO... set help
      //WorkbenchHelp.setHelp(radioButtonsGroup, XMLBuilderContextIds.XMLC_RADIO_GROUP);

      for (int i = 0; i < radioButtonLabel.length; i++)
      {
        radioButton[i] = new Button(radioButtonsGroup, SWT.RADIO);
        radioButton[i].setText(radioButtonLabel[i]);
        radioButton[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        //TODO... set help
        //WorkbenchHelp.setHelp(radioButton[i], new ControlContextComputer(radioButton[i], XMLBuilderContextIds.XMLC_RADIO_BUTTON));
        //radioButton[i].addListener(SWT.Modify, this);
      }
      setControl(base);
      setPageComplete(isPageComplete());
    }

    public void handleEvent(Event event)
    {
      if (event.type == SWT.Modify)
      {
        setPageComplete(isPageComplete());
      }
    }
  }


  /**
   *  NewFilePage
   */
  public class NewFilePage extends WizardNewFileCreationPage
  {
    public String defaultName = "NewFile"; //$NON-NLS-1$
    public String defaultFileExtension = ".txt"; //$NON-NLS-1$
    public String[] filterExtensions = { "*.txt"}; //$NON-NLS-1$

    public NewFilePage(IStructuredSelection selection)
    {
      super("", selection); //$NON-NLS-1$
    }

    protected String computeDefaultFileName()
    {
      int count = 0;
      String fileName = defaultName + defaultFileExtension;
      IPath containerFullPath = getContainerFullPath();
      if (containerFullPath != null)
      {
        while (true)
        {
          IPath path = containerFullPath.append(fileName);
          if (ResourcesPlugin.getWorkspace().getRoot().exists(path))
          {
            count++;
            fileName = defaultName + count + defaultFileExtension;
          }
          else
          {
            break;
          }
        }
      }
      return fileName;
    }

    // returns true if file of specified name exists in any case for selected container
    protected String existsFileAnyCase(String fileName)
    {
      if ( (getContainerFullPath() != null) && (getContainerFullPath().isEmpty() == false)
            && (fileName.compareTo("") != 0)) //$NON-NLS-1$
      {
        //look through all resources at the specified container - compare in upper case
        IResource parent = ResourcesPlugin.getWorkspace().getRoot().findMember(getContainerFullPath());
        if (parent instanceof IContainer)
        {
          IContainer container = (IContainer) parent;
          try
          {
            IResource[] members = container.members();
            String enteredFileUpper = fileName.toUpperCase();
            for (int i=0; i<members.length; i++)
            {
              String resourceUpperName = members[i].getName().toUpperCase();
              if (resourceUpperName.equals(enteredFileUpper))
              {  
                return members[i].getName();    
              }
            }
          }
          catch (CoreException e)
          {
          }
        }
      }
      return null;
    }


    protected boolean validatePage()
    {     
      String fullFileName = getFileName();
      String fileExtension = (new Path(fullFileName)).getFileExtension();
      if (fileExtension != null) 
      {                    
        IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry();
        IEditorDescriptor defaultEditorDescriptor = editorRegistry.getDefaultEditor();   
        IEditorDescriptor[] descriptors =  editorRegistry.getEditors(getFileName());        
        if (descriptors.length == 0)
        {                                                                         
          setErrorMessage(XMLWizardsMessages._ERROR_BAD_FILENAME_EXTENSION);                                                    
          return false;
        }              
      }
      else
      {
        // no fileExtension, let's check for this file with an .xml extension
        fullFileName += ".xml"; //$NON-NLS-1$
        if ( (getContainerFullPath() != null) && (getContainerFullPath().isEmpty() == false)
           && (getFileName().compareTo("") != 0)) //$NON-NLS-1$
        {
          Path fullPath = new Path(getContainerFullPath().toString() + '/' + fullFileName);
          IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(fullPath);
          if (resource != null) 
          {
            setErrorMessage(XMLWizardsMessages._ERROR_FILE_ALREADY_EXISTS);
            return false;    
          }
        }        
      }

      // check for file should be case insensitive
      String sameName = existsFileAnyCase(fullFileName);
      if (sameName != null) 
      {
         String qualifiedFileName = getContainerFullPath().toString() + '/' + fullFileName;
         setErrorMessage(XMLWizardsMessages._ERROR_FILE_ALREADY_EXISTS + " " +  sameName); //$NON-NLS-1$
         return false;
      }
            
      return super.validatePage();
    }

    public void createControl(Composite parent)
    {
      // inherit default container and name specification widgets
      super.createControl(parent);
      this.setFileName(computeDefaultFileName());
      setPageComplete(validatePage());
    }
  }
}

Back to the top