update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / xml / actions / xmlbeans / GenerateInstanceDocumentFromSchemaDialog.java
blobf980bbf17664fae5b09355571b9bad54de896ee4
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.xml.actions.xmlbeans;
19 import com.intellij.javaee.ExternalResourceManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.DialogWrapper;
22 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
23 import com.intellij.openapi.vfs.VfsUtil;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.psi.PsiManager;
27 import com.intellij.psi.xml.XmlFile;
28 import com.intellij.psi.xml.XmlTag;
29 import com.intellij.xml.XmlBundle;
30 import com.intellij.xml.XmlElementDescriptor;
31 import org.jetbrains.annotations.Nullable;
33 import javax.swing.*;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import java.awt.*;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyAdapter;
40 import java.awt.event.KeyEvent;
41 import java.util.Collections;
42 import java.util.List;
44 /**
45 * @author Konstantin Bulenkov
47 public class GenerateInstanceDocumentFromSchemaDialog extends DialogWrapper {
48 private JPanel panel;
49 private TextFieldWithBrowseButton generateFromUrl;
50 private JLabel status;
51 private JLabel statusText;
52 private JLabel generateFromUrlText;
53 private JComboBox rootElementChooser;
54 private JLabel rootElementChooserText;
55 private JCheckBox enableRestrictionCheck;
56 private JCheckBox enableUniqueCheck;
57 private JTextField outputFileName;
58 private JLabel outputFileNameText;
59 private String previousUri;
60 private Runnable myOkAction;
61 private Project myProject;
63 public GenerateInstanceDocumentFromSchemaDialog(Project project, VirtualFile file) {
64 super(project, true);
65 myProject = project;
67 UIUtils.configureBrowseButton(project, generateFromUrl, new String[] {"xsd"}, XmlBundle.message("select.xsd.schema.dialog.title"), false);
69 doInitFor(rootElementChooserText, rootElementChooser);
70 doInitFor(generateFromUrlText, generateFromUrl.getTextField());
71 doInitFor(outputFileNameText, outputFileName);
72 generateFromUrl.setText(file.getPresentableUrl());
73 updateFile();
75 setTitle(XmlBundle.message("generate.instance.document.from.schema.dialog.title"));
77 init();
79 outputFileName.setText(file.getName()+ ".xml");
82 public void doInitFor(JLabel textComponent, JComponent component) {
83 textComponent.setLabelFor(component);
85 if (component instanceof JTextField) {
86 ((JTextField)component).getDocument().addDocumentListener(new DocumentListener() {
87 public void insertUpdate(DocumentEvent e) {
88 validateData();
91 public void removeUpdate(DocumentEvent e) {
92 validateData();
95 public void changedUpdate(DocumentEvent e) {
96 validateData();
98 });
99 } else if (component instanceof JComboBox) {
100 JComboBox jComboBox = ((JComboBox) component);
102 jComboBox.addActionListener(new ActionListener() {
103 public void actionPerformed(ActionEvent e) {
104 validateData();
108 ((JTextField)jComboBox.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {
109 public void insertUpdate(DocumentEvent e) {
110 validateData();
111 updateFile();
114 public void removeUpdate(DocumentEvent e) {
115 validateData();
116 updateFile();
119 public void changedUpdate(DocumentEvent e) {
120 validateData();
121 updateFile();
125 if (jComboBox.isEditable()) {
126 jComboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
127 public void keyTyped(KeyEvent e) {
128 validateData();
135 private void validateData() {
136 String msg = doValidateWithData();
137 setOKActionEnabled(msg == null);
138 status.setText(msg == null ? "" : msg);
139 status.setForeground(Color.RED);
142 public void configureComboBox(JComboBox combo, List<String> lastValues) { // without -editor.selectAll- no focus
143 combo.setModel(new DefaultComboBoxModel(lastValues.toArray(new String[lastValues.size()])));
146 private void updateFile() {
147 String uri = generateFromUrl.getText();
148 boolean hasPrevious = (previousUri != null && previousUri.equals(uri));
149 final PsiFile psifile = findFile(uri);
150 List<String> myRootValues;
152 if (psifile == null) {
153 configureComboBox(rootElementChooser, Collections.EMPTY_LIST);
154 return;
157 final XmlTag rootTag = getRootTag(psifile);
159 if (rootTag == null) {
160 configureComboBox(rootElementChooser, Collections.EMPTY_LIST);
161 rootElementChooser.setSelectedIndex(-1);
162 previousUri = uri;
163 return;
166 myRootValues = Xsd2InstanceUtils.addVariantsFromRootTag(rootTag);
168 Object selectedItem = rootElementChooser.getSelectedItem();
169 configureComboBox(rootElementChooser, myRootValues);
171 if (hasPrevious) {
172 rootElementChooser.setSelectedItem(selectedItem);
173 } else {
174 rootElementChooser.setSelectedIndex(myRootValues.size() > 0 ? 0:-1);
176 previousUri = uri;
179 private XmlTag getRootTag(PsiFile psifile) {
180 return ((XmlFile) psifile).getDocument().getRootTag();
183 private PsiFile findFile(String uri) {
184 final VirtualFile file = uri != null ? VfsUtil.findRelativeFile(ExternalResourceManager.getInstance().getResourceLocation(uri), null):null;
185 return file != null ? PsiManager.getInstance(myProject).findFile(file):null;
188 public String getOutputFileName() {
189 return outputFileName.getText();
192 public Boolean areCurrentParametersStillValid() {
193 updateFile();
194 return rootElementChooser.getSelectedItem() != null;
197 @Nullable
198 protected String doValidateWithData() {
199 String rootElementName = getElementName();
200 if (rootElementName == null || rootElementName.length() == 0) {
201 return XmlBundle.message("schema2.instance.no.valid.root.element.name.validation.error");
204 final PsiFile psiFile = findFile(getUrl().getText());
205 if (psiFile instanceof XmlFile) {
206 final XmlTag tag = getRootTag(psiFile);
207 if (tag != null) {
208 final XmlElementDescriptor descriptor = Xsd2InstanceUtils.getDescriptor(tag, rootElementName);
210 if (descriptor == null) {
211 return XmlBundle.message("schema2.instance.no.valid.root.element.name.validation.error");
216 final String fileName = getOutputFileName();
217 if (fileName == null || fileName.length() == 0) {
218 return XmlBundle.message("schema2.instance.output.file.name.is.empty.validation.problem");
220 return null;
224 protected boolean isAcceptableFile(VirtualFile virtualFile) {
225 return GenerateInstanceDocumentFromSchemaAction.isAcceptableFileForGenerateSchemaFromInstanceDocument(virtualFile);
229 protected TextFieldWithBrowseButton getUrl() {
230 return generateFromUrl;
233 protected JLabel getUrlText() {
234 return generateFromUrlText;
237 protected JLabel getStatusTextField() {
238 return statusText;
241 protected JLabel getStatusField() {
242 return status;
245 protected JComponent createCenterPanel() {
246 return panel;
249 boolean enableUniquenessCheck() {
250 return enableUniqueCheck.isSelected();
253 boolean enableRestrictionCheck() {
254 return enableRestrictionCheck.isSelected();
257 String getElementName() {
258 return (String)rootElementChooser.getSelectedItem();
261 public void setOkAction(Runnable runnable) {
262 myOkAction = runnable;
265 @Override
266 protected void doOKAction() {
267 super.doOKAction();
268 if (myOkAction != null) {
269 myOkAction.run();