Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4a5251abdde9c3e3381273aa0e8894d6f8f23695 (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
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.wsdl.ui.internal.wizards;

import org.eclipse.core.resources.IContainer;
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.swt.widgets.Composite;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.wst.wsdl.ui.internal.Messages;

public class WSDLNewFilePage extends WizardNewFileCreationPage
{
  public WSDLNewFilePage(IStructuredSelection selection) 
  {
    super(Messages.getString("_UI_TITLE_NEW_WSDL_FILE"), selection); //$NON-NLS-1$
    setTitle(Messages.getString("_UI_TITLE_NEW_WSDL_FILE")); //$NON-NLS-1$
    setDescription(Messages.getString("_UI_LABEL_CREATE_NEW_WSDL_FILE")); //$NON-NLS-1$
  }

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

    setPageComplete(validatePage());
  }
  
  protected boolean validatePage()
  {
    Path newName = new Path(getFileName());
    String fullFileName = getFileName();
    String extension = newName.getFileExtension();
    if (extension == null || !extension.equalsIgnoreCase("wsdl"))  //$NON-NLS-1$
    {
      setErrorMessage(Messages.getString("_UI_ERROR_FILE_MUST_END_WITH_WSDL")); //$NON-NLS-1$
      return false;
    }
    else 
    {
      setErrorMessage(null);
    }

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

    
    return super.validatePage();
  }

  public String defaultName = "NewWSDLFile"; //$NON-NLS-1$
  public String defaultFileExtension = ".wsdl"; //$NON-NLS-1$
  public String[] filterExtensions = { "*.wsdl"}; //$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 (WorkbenchUtility.getWorkspace().getRoot().exists(path))
        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;
  }

}

Back to the top