KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > actions > CSSStyleAction


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.actions;
20
21 import java.util.*;
22 import java.awt.datatransfer.StringSelection JavaDoc;
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 /**
44  * Creates a CSS draft upon a standalone DTD. Stores it into css file.
45  * It does work only with standalone DTD (it is feature).
46  *
47  * @author Petr Kuzel
48  * @version 1.0
49  */

50 public final class CSSStyleAction extends CookieAction implements CollectDTDAction.DTDAction {
51
52     /** Serial Version UID */
53     private static final long serialVersionUID = 7867855746468L;
54     
55     /** Creates new CSSStyleAction */
56     public CSSStyleAction() {
57     }
58
59     public Class JavaDoc[] cookieClasses() {
60         return new Class JavaDoc[] { DTDDataObject.class };
61     }
62
63     public int mode() {
64         return MODE_ONE;
65     }
66
67     private String JavaDoc 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 = ""; //"@charset \"UTF-8\";\n"; // NOI18N
76
css += "/* Cascade style sheet based on " + dtd.getDisplayName() + " DTD */\n"; // NOI18N
77

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"); // NOI18N
91
}
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 JavaDoc name = decl.getName();
99                 add(name);
100             }
101             
102             // ask for data object location
103

104             FileObject primFile = dtdo.getPrimaryFile();
105             String JavaDoc name = primFile.getName() + Util.THIS.getString("NAME_SUFFIX_Stylesheet");
106             FileObject folder = primFile.getParent();
107
108             FileObject generFile = (new SelectFileDialog (folder, name, "css")).getFileObject(); // NOI18N
109
name = generFile.getName();
110             
111             // create and open data object
112

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 JavaDoc ss = new StringSelection JavaDoc(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             //user cancelled do nothing
149

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     /** adds a new name to just created CSS. */
164     private void add(String JavaDoc name) {
165         css += name + " { display: block }\n"; // NOI18N
166
}
167
168     public HelpCtx getHelpCtx() {
169         return new HelpCtx(getClass());
170     }
171
172     public String JavaDoc getName() {
173         return Util.THIS.getString("NAME_Generate_CSS");
174     }
175     
176     protected boolean asynchronous() {
177         return false;
178     }
179
180 }
181
Popular Tags