Skip to main content
summaryrefslogtreecommitdiffstats
blob: db12cd660626aa59a6a13b400a7ef5fa8090ac73 (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
/*******************************************************************************
 * 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.xsd.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.xsd.ui.internal.XSDEditorPlugin;


public class XSDNewFilePage extends WizardNewFileCreationPage
{
  public XSDNewFilePage(IStructuredSelection selection) 
  {
    super(XSDEditorPlugin.getXSDString("_UI_CREATEXSD"), selection);
    setTitle(XSDEditorPlugin.getXSDString("_UI_NEW_XML_SCHEMA_TITLE"));
    setDescription(XSDEditorPlugin.getXSDString("_UI_CREATE_A_NEW_XML_SCHEMA_DESC"));
  }

  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("xsd")) 
    {
      setErrorMessage(XSDEditorPlugin.getXSDString("_ERROR_FILENAME_MUST_END_XSD"));
      return false;
    }
    else 
    {
      setErrorMessage(null);
    }

    // check for file should be case insensitive
    String sameName = existsFileAnyCase(fullFileName);
    if (sameName != null) 
    {
      setErrorMessage(XSDEditorPlugin.getPlugin().getString("_ERROR_FILE_ALREADY_EXISTS", sameName)); //$NON-NLS-1$
      return false;
    }

    return super.validatePage();
  }

  public String defaultName = "NewXMLSchema"; //$NON-NLS-1$
  public String defaultFileExtension = ".xsd"; //$NON-NLS-1$
  public String[] filterExtensions = { "*.xsd"}; //$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))
    {
      //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