Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e94a0013070e072503d4047a071203f52ba8c41 (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
/*******************************************************************************
* Copyright (c) 2010 Oracle. 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:
*     Oracle - initial API and implementation
*******************************************************************************/
package org.eclipse.jpt.jaxb.ui.internal.wizards.classesgen;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jpt.jaxb.ui.internal.JptJaxbUiMessages;
import org.eclipse.jpt.jaxb.ui.internal.wizards.ProjectWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;

/**
 *  SchemaWizardPage
 */
public class SchemaWizardPage extends WizardPage {
	
	private final IStructuredSelection initialSelection;
	private IProject targetProject;
	
	protected SelectFileOrXMLCatalogIdPanel selectSourcePanel;

	protected static final String[] browseXSDFilterExtensions = {".xsd"}; //$NON-NLS-1$

	// ********** static method **********
	
    public static IFile getSourceSchemaFromSelection(IStructuredSelection selection) {
		Object firstElement = selection.getFirstElement();
		if(firstElement instanceof IFile) {
			String elementExtension = ((IFile)firstElement).getFileExtension();
			if(elementExtension != null) {
				if(browseXSDFilterExtensions[0].endsWith(elementExtension)) {
					return ((IFile)firstElement);
				}
			}
		}
		return null;
    }

	// ********** constructor **********
    
	SchemaWizardPage(IStructuredSelection selection) {
		super("SchemaWizardPage"); //$NON-NLS-1$
		
		this.initialSelection = selection;
	}
	
	// ********** IDialogPage implementation  **********
	
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
//		PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, HELP_CONTEXT_ID);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		this.setControl(composite);

		this.selectSourcePanel = new SelectFileOrXMLCatalogIdPanel(composite, this.initialSelection);
		this.selectSourcePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		SelectFileOrXMLCatalogIdPanel.PanelListener listener = new SelectFileOrXMLCatalogIdPanel.PanelListener() {
			public void completionStateChanged() {
				selectFileOrXMLCatalogIdPanelChanged();
			}
		};
		this.selectSourcePanel.setListener(listener);
		Dialog.applyDialogFont(parent);
	}

    @Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if(visible) {

	    	if(this.getSourceSchema() != null) {
	    		this.selectSourcePanel.setSingleFileViewDefaultSelection(new StructuredSelection(this.getSourceSchema()));
	    	}
	    	else {
	    		this.updateTargetProject();
		    	IFile schema = getSourceSchemaFromSelection(this.initialSelection);
		    	if(schema != null) {
		    		this.selectSourcePanel.setSingleFileViewDefaultSelection(new StructuredSelection(schema));
		    	}
		    	else {
		    		this.selectSourcePanel.setSingleFileViewDefaultSelection(new StructuredSelection(this.targetProject));
		    	}
	    	}
	    	this.selectSourcePanel.update();
			
			this.setTitle(JptJaxbUiMessages.SchemaWizardPage_title);
			this.setDescription(JptJaxbUiMessages.SchemaWizardPage_desc);
			this.selectSourcePanel.setFilterExtensions(browseXSDFilterExtensions);
		}
		this.selectSourcePanel.setVisibleHelper(visible);
	}
    
	// ********** IWizardPage implementation  **********
    
    @Override
	public boolean isPageComplete() {

		return this.schemaOrUriSelected() && (this.getErrorMessage() == null);
	}

	// ********** intra-wizard methods **********
	
	public IFile getSourceSchema() {
		return this.selectSourcePanel.getFile();
	}

	public String getSourceURI() {
		String uri = this.selectSourcePanel.getXMLCatalogURI();
		if(uri == null) {
			IFile file = this.selectSourcePanel.getFile();
			if(file != null) {
				uri = URIHelper.getPlatformURI(file);
			}
		}
		return uri;
	}

	public String getXMLCatalogId() {
		return this.selectSourcePanel.getXMLCatalogId();
	}

	// ********** internal methods **********

	private void updateTargetProject() {
    	IWizardPage previousPage = this.getPreviousPage();
    	
		if(previousPage instanceof ProjectWizardPage) {
			// get project from previousPage
			this.targetProject = (((ProjectWizardPage)previousPage).getJavaProject()).getProject();
		}
		else if(initialSelection != null && ! this.initialSelection.isEmpty()) {
			// no previousPage - get project from initialSelection
			this.targetProject = this.getProjectFromInitialSelection();
		}		
	}
	
    private IProject getProjectFromInitialSelection() {
		Object firstElement = initialSelection.getFirstElement();
		if(firstElement instanceof IProject) {
			return (IProject)firstElement;
		}
		else if(firstElement instanceof IResource) {
			return ((IResource) firstElement).getProject();
		}
		else if(firstElement instanceof IJavaElement) {
			return ((IJavaElement)firstElement).getJavaProject().getProject();
		}
		return null;
    }

	private boolean schemaOrUriSelected() {
		return ((this.getSourceSchema() != null) || (this.getSourceURI() != null));
	}

	private String computeErrorMessage() {
		String errorMessage = null;
		String uri = this.getSourceURI();
		if(uri != null) {
			if( ! URIHelper.isReadableURI(uri, false)) {
				errorMessage = JptJaxbUiMessages.SchemaWizardPage_errorUriCannotBeLocated;
			}
		}
		return errorMessage;
	}

	private void selectFileOrXMLCatalogIdPanelChanged() {
		String errorMessage = this.computeErrorMessage();
		this.setErrorMessage(errorMessage);
		this.setPageComplete(this.isPageComplete());
	}
}

Back to the top