KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > actions > SchemaTransformAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.core.actions;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27 import org.netbeans.modules.xml.axi.AXIModel;
28 import org.netbeans.modules.xml.axi.AXIModelFactory;
29 import org.netbeans.modules.xml.schema.abe.wizard.SchemaTransformWizard;
30 import org.netbeans.modules.xml.schema.core.SchemaDataObject;
31 import org.netbeans.modules.xml.schema.core.SchemaEditorSupport;
32 import org.netbeans.modules.xml.schema.model.SchemaModel;
33 import org.netbeans.modules.xml.schema.ui.basic.SchemaModelCookie;
34 import org.netbeans.modules.xml.xam.ui.XAMUtils;
35 import org.openide.nodes.Node;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.CookieAction;
39
40 /**
41  * An action on the SchemaDataObject node (SchemaNode)
42  * to "Transform" the schema (from one design pattern to another)
43  *
44  * @author Ayub Khan
45  */

46 public class SchemaTransformAction extends CookieAction {
47     private static final long serialVersionUID = 1L;
48     
49     private static final Class JavaDoc[] COOKIE_ARRAY =
50             new Class JavaDoc[] {SchemaModelCookie.class};
51     
52     private static final String JavaDoc EMPTY_DOC =
53             "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"/>";
54     
55     SchemaDataObject sdo = null;
56     
57     /** Creates a new instance of SchemaViewOpenAction */
58     public SchemaTransformAction() {
59         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
60
}
61     
62     protected void performAction(Node[] node) {
63         assert node.length==1:
64             "Length of nodes array should be 1";
65         try {
66             //at this point forceResetDoc
67
SchemaModel sm = getSchemaModel(node, true);
68             if(sm != null) {
69                 AXIModel am = AXIModelFactory.getDefault().getModel(sm);
70                 SchemaTransformWizard wizard = new SchemaTransformWizard(sm);
71                 wizard.show();
72                 if(!wizard.isCancelled() && sdo != null)
73                     sdo.setModified(true);
74             }
75         } catch (IOException JavaDoc ex) {
76 // ex.printStackTrace();
77
}
78     }
79     
80     private SchemaModel getSchemaModel(final Node[] node, boolean forceResetDoc) throws IOException JavaDoc {
81         if(node == null || node.length == 0 || node[0] == null)
82             return null;
83         
84         sdo = (SchemaDataObject)node[0].getLookup().lookup(
85                 SchemaDataObject.class);
86         if (sdo != null){
87             SchemaEditorSupport editor = sdo.getSchemaEditorSupport();
88             SchemaModel model = null;
89             if(editor != null) {
90                 model = editor.getModel();
91                 StyledDocument JavaDoc doc = editor.getDocument();
92                 //Do this only when the document is not opened
93
if(forceResetDoc &&
94                         model != null && doc != null &&
95                             (editor.getOpenedPanes() == null ||
96                                 editor.getOpenedPanes().length == 0)) {
97                     try {
98                         // Reset the model state by forcing it to sync again.
99
boolean saveState = sdo.isModified();
100                         String JavaDoc saved = doc.getText(0, doc.getLength());
101                         String JavaDoc emptyDoc = EMPTY_DOC;
102                         if(saved != null) {
103                             int schemaStart = saved.indexOf("schema");
104                             int schemaEnd = saved.lastIndexOf("schema");
105                             if(schemaStart != -1 && schemaEnd != -1) {
106                                 int ss = saved.indexOf(">", schemaStart);
107                                 emptyDoc = saved.substring(0, ss+1);
108                                 emptyDoc += "\n";
109                                 int se = saved.lastIndexOf("<", schemaEnd);
110                                 emptyDoc += saved.substring(se, saved.length());
111                             }
112                         }
113                         // Remove undo manager as a listener (IZ# 96476).
114
boolean undoValue = editor.suspendUndoRedo();
115                         doc.remove(0, doc.getLength());
116                         doc.insertString(0, emptyDoc, null);
117                         model.sync();
118                         doc.remove(0, doc.getLength());
119                         doc.insertString(0, saved, null);
120                         model.sync();
121                         AXIModel am = AXIModelFactory.getDefault().getModel(model);
122                         am.sync();
123                         sdo.setModified(saveState);
124                         editor.resumeUndoRedo(undoValue);
125                     } catch(BadLocationException JavaDoc e) {
126                         Logger.getLogger(SchemaTransformAction.class.getName()).log(
127                                 Level.FINE, "forceResetDocument", e);
128                     } catch(IOException JavaDoc e) {
129                         Logger.getLogger(SchemaTransformAction.class.getName()).log(
130                                 Level.FINE, "forceResetDocument", e);
131                     }
132                 }
133             }
134             return model;
135         }
136         return null;
137     }
138     
139     protected boolean enable(Node[] node) {
140         if(super.enable(node)) {
141             SchemaModel sm = null;
142             try {
143                 sm = getSchemaModel(node, false);
144             } catch (IOException JavaDoc ex) {
145             }
146             return XAMUtils.isWritable(sm);
147         }
148         return false;
149     }
150     
151     public String JavaDoc getName() {
152         return NbBundle.getMessage(
153                 SchemaViewOpenAction.class, "LBL_ApplyDesignPattern_Name");
154     }
155     
156     public HelpCtx getHelpCtx() {
157         return HelpCtx.DEFAULT_HELP;
158     }
159     
160     protected boolean asynchronous() {
161         
162         return false;
163     }
164     
165     protected int mode() {
166         return CookieAction.MODE_EXACTLY_ONE;
167     }
168     
169     protected Class JavaDoc[] cookieClasses() {
170         return COOKIE_ARRAY;
171     }
172 }
173
Popular Tags