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
;
34 import javax
.swing
.event
.DocumentEvent
;
35 import javax
.swing
.event
.DocumentListener
;
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
;
45 * @author Konstantin Bulenkov
47 public class GenerateInstanceDocumentFromSchemaDialog
extends DialogWrapper
{
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
) {
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());
75 setTitle(XmlBundle
.message("generate.instance.document.from.schema.dialog.title"));
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
) {
91 public void removeUpdate(DocumentEvent e
) {
95 public void changedUpdate(DocumentEvent e
) {
99 } else if (component
instanceof JComboBox
) {
100 JComboBox jComboBox
= ((JComboBox
) component
);
102 jComboBox
.addActionListener(new ActionListener() {
103 public void actionPerformed(ActionEvent e
) {
108 ((JTextField
)jComboBox
.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {
109 public void insertUpdate(DocumentEvent e
) {
114 public void removeUpdate(DocumentEvent e
) {
119 public void changedUpdate(DocumentEvent e
) {
125 if (jComboBox
.isEditable()) {
126 jComboBox
.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
127 public void keyTyped(KeyEvent e
) {
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
);
157 final XmlTag rootTag
= getRootTag(psifile
);
159 if (rootTag
== null) {
160 configureComboBox(rootElementChooser
, Collections
.EMPTY_LIST
);
161 rootElementChooser
.setSelectedIndex(-1);
166 myRootValues
= Xsd2InstanceUtils
.addVariantsFromRootTag(rootTag
);
168 Object selectedItem
= rootElementChooser
.getSelectedItem();
169 configureComboBox(rootElementChooser
, myRootValues
);
172 rootElementChooser
.setSelectedItem(selectedItem
);
174 rootElementChooser
.setSelectedIndex(myRootValues
.size() > 0 ?
0:-1);
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() {
194 return rootElementChooser
.getSelectedItem() != null;
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
);
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");
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() {
241 protected JLabel
getStatusField() {
245 protected JComponent
createCenterPanel() {
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
;
266 protected void doOKAction() {
268 if (myOkAction
!= null) {