1 19 package org.netbeans.modules.xml.tools.actions; 20 21 import java.util.*; 22 import java.awt.datatransfer.StringSelection ; 23 import java.io.*; 24 25 import javax.swing.text.*; 26 27 import org.openide.*; 28 import org.openide.nodes.*; 29 import org.openide.util.*; 30 import org.openide.util.datatransfer.ExClipboard; 31 import org.openide.util.actions.*; 32 import org.openide.cookies.*; 33 import org.openide.loaders.*; 34 import org.openide.filesystems.*; 35 36 import org.netbeans.tax.*; 37 import org.netbeans.modules.xml.core.DTDDataObject; 38 39 import org.netbeans.modules.xml.core.lib.GuiUtil; 40 import org.netbeans.modules.xml.tools.generator.SelectFileDialog; 41 import org.netbeans.modules.xml.core.actions.CollectDTDAction; 42 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie; 43 50 public final class CSSStyleAction extends CookieAction implements CollectDTDAction.DTDAction { 51 52 53 private static final long serialVersionUID = 7867855746468L; 54 55 56 public CSSStyleAction() { 57 } 58 59 public Class [] cookieClasses() { 60 return new Class [] { DTDDataObject.class }; 61 } 62 63 public int mode() { 64 return MODE_ONE; 65 } 66 67 private String css; 68 69 public void performAction(Node[] nodes) { 70 if (nodes == null) return; 71 if (nodes.length != 1) return; 72 73 Node dtd = nodes[0]; 74 75 css = ""; css += "/* Cascade style sheet based on " + dtd.getDisplayName() + " DTD */\n"; 78 DTDDataObject dtdo = (DTDDataObject) dtd.getCookie(DTDDataObject.class); 79 80 ErrorManager emgr = ErrorManager.getDefault(); 81 82 try { 83 84 TreeDocumentRoot result; 85 86 TreeEditorCookie cake = (TreeEditorCookie) dtdo.getCookie(TreeEditorCookie.class); 87 if (cake != null) { 88 result = cake.openDocumentRoot(); 89 } else { 90 throw new TreeException("DTDDataObject:INTERNAL ERROR"); } 92 TreeDTD treeDTD = (TreeDTD) result; 93 94 Iterator it = treeDTD.getElementDeclarations().iterator(); 95 96 while (it.hasNext()) { 97 TreeElementDecl decl = (TreeElementDecl) it.next(); 98 String name = decl.getName(); 99 add(name); 100 } 101 102 104 FileObject primFile = dtdo.getPrimaryFile(); 105 String name = primFile.getName() + Util.THIS.getString("NAME_SUFFIX_Stylesheet"); 106 FileObject folder = primFile.getParent(); 107 108 FileObject generFile = (new SelectFileDialog (folder, name, "css")).getFileObject(); name = generFile.getName(); 110 111 113 DataObject targeto; 114 115 116 try { 117 targeto = DataObject.find(generFile); 118 } catch (DataObjectNotFoundException eex) { 119 return; 120 } 121 122 EditorCookie ec = (EditorCookie) targeto.getCookie(EditorCookie.class); 123 if (ec != null) { 124 Document doc = ec.openDocument(); 125 126 try { 127 doc.remove(0, doc.getLength()); 128 doc.insertString(0, css, null); 129 ec.saveDocument(); 130 } catch (BadLocationException locex) { 131 emgr.annotate(locex, Util.THIS.getString("MSG_Leaving_CSS_in_clipboard")); 132 emgr.notify(locex); 133 134 StringSelection ss = new StringSelection (css); 135 ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup(ExClipboard.class); 136 clipboard.setContents(ss, null); 137 GuiUtil.setStatusText(Util.THIS.getString("MSG_CSS_placed_in_clipboard")); 138 139 } 140 141 142 OpenCookie oc = (OpenCookie) targeto.getCookie(OpenCookie.class); 143 if (oc != null) oc.open(); 144 145 } 146 147 } catch (UserCancelException ex) { 148 150 } catch (IOException ex) { 151 152 emgr.annotate(ex, Util.THIS.getString("MSG_IO_ex_CSS_writing.")); 153 emgr.notify(ex); 154 155 } catch (TreeException ex) { 156 157 GuiUtil.setStatusText(Util.THIS.getString("MSG_CSS_fatal_error")); 158 159 } 160 161 } 162 163 164 private void add(String name) { 165 css += name + " { display: block }\n"; } 167 168 public HelpCtx getHelpCtx() { 169 return new HelpCtx(getClass()); 170 } 171 172 public String getName() { 173 return Util.THIS.getString("NAME_Generate_CSS"); 174 } 175 176 protected boolean asynchronous() { 177 return false; 178 } 179 180 } 181 | Popular Tags |