1 19 20 package org.netbeans.modules.xml.schema.core.actions; 21 22 import java.io.IOException ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.StyledDocument ; 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 46 public class SchemaTransformAction extends CookieAction { 47 private static final long serialVersionUID = 1L; 48 49 private static final Class [] COOKIE_ARRAY = 50 new Class [] {SchemaModelCookie.class}; 51 52 private static final String EMPTY_DOC = 53 "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"/>"; 54 55 SchemaDataObject sdo = null; 56 57 58 public SchemaTransformAction() { 59 putValue("noIconInMenu", Boolean.TRUE); } 61 62 protected void performAction(Node[] node) { 63 assert node.length==1: 64 "Length of nodes array should be 1"; 65 try { 66 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 ex) { 76 } 78 } 79 80 private SchemaModel getSchemaModel(final Node[] node, boolean forceResetDoc) throws IOException { 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 doc = editor.getDocument(); 92 if(forceResetDoc && 94 model != null && doc != null && 95 (editor.getOpenedPanes() == null || 96 editor.getOpenedPanes().length == 0)) { 97 try { 98 boolean saveState = sdo.isModified(); 100 String saved = doc.getText(0, doc.getLength()); 101 String 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 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 e) { 126 Logger.getLogger(SchemaTransformAction.class.getName()).log( 127 Level.FINE, "forceResetDocument", e); 128 } catch(IOException 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 ex) { 145 } 146 return XAMUtils.isWritable(sm); 147 } 148 return false; 149 } 150 151 public String 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 [] cookieClasses() { 170 return COOKIE_ARRAY; 171 } 172 } 173 | Popular Tags |