KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > actions > CopyStyleAction


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.css.actions;
20
21 import java.awt.datatransfer.StringSelection JavaDoc;
22 import java.net.*;
23
24 import org.openide.*;
25 import org.openide.awt.StatusDisplayer;
26 import org.openide.nodes.Node;
27 import org.openide.util.*;
28 import org.openide.util.datatransfer.ExClipboard;
29 import org.openide.util.actions.*;
30 import org.openide.filesystems.*;
31
32 import org.netbeans.modules.css.*;
33
34 /**
35 * Action that put XML style processing instruction in clipboard.
36 * TODO add PI flavor.
37 *
38 * @author Petr Kuzel
39 */

40 public abstract class CopyStyleAction extends CookieAction {
41
42     protected static final String JavaDoc comment = Util.THIS.getString("Style-Comment") + "\n"; // NOI18N
43

44     /** Serial Version UID */
45     private static final long serialVersionUID = -6638807099960633334L;
46     
47     /** What triggers me?
48     * @return MODE_EXACTLY_ONE.
49     */

50     public int mode() {
51         return MODE_EXACTLY_ONE;
52     }
53
54     /** What triggers me?
55     * @return {CSSObject.class}
56     */

57     public Class JavaDoc[] cookieClasses() {
58         return new Class JavaDoc[] {CSSObject.class};
59     }
60
61     /** Action.
62     */

63     protected void performAction(final Node[] nodes) {
64         if (nodes.length != 1) return;
65         if (nodes[0] == null) return;
66
67         CSSObject csso = (CSSObject) nodes[0].getCookie(CSSObject.class);
68
69         if (csso != null) {
70             String JavaDoc pi = createText(csso);
71             StringSelection JavaDoc ss = new StringSelection JavaDoc(pi);
72             ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup(ExClipboard.class);
73             clipboard.setContents(ss, null);
74             StatusDisplayer.getDefault().setStatusText(Util.THIS.getString("MSG_Style_tag_in_clipboard")); // NOI18N
75
}
76
77     }
78
79     /** A method that creates particular clipboard text.
80     * @return text to be placed to clip board.
81     */

82     protected abstract String JavaDoc createText(CSSObject node);
83
84     /** Get help context for the action.
85     */

86     public HelpCtx getHelpCtx() {
87         return new HelpCtx(getClass());
88     }
89
90     /** Converts CSS fileobject to its href that is valid during IDE runtime. */
91     protected String JavaDoc getHref(FileObject fo) {
92         URL u = URLMapper.findURL(fo, URLMapper.NETWORK);
93         if (u != null) {
94             return u.toExternalForm();
95         } else {
96             return fo.getPath();
97         }
98     }
99
100     /** Produces XML PI text. */
101     public final static class XML extends CopyStyleAction {
102
103         /** Serial Version UID */
104         private static final long serialVersionUID = -6638807099960633335L;
105         
106         protected String JavaDoc createText(CSSObject csso) {
107             return comment + "<?xml-stylesheet type=\"text/css\" HREF=\"" + this.getHref(csso.getPrimaryFile()) + "\" ?>"; // NOI18N
108
}
109
110         public String JavaDoc getName() {
111             return Util.THIS.getString("Copy-XML-Style"); // NOI18N
112
}
113     }
114
115     /** Produces HTML style text. */
116     public final static class HTML extends CopyStyleAction {
117
118         /** Serial Version UID */
119         private static final long serialVersionUID = -6638807099960633336L;
120         
121         protected String JavaDoc createText(CSSObject csso) {
122             return comment + "<link rel=\"StyleSheet\" type=\"text/css\" HREF=\"" + this.getHref(csso.getPrimaryFile()) + "\" media=\"screen\" >"; // NOI18N
123
}
124
125         public String JavaDoc getName() {
126             return Util.THIS.getString("Copy-HTML-Style"); // NOI18N
127
}
128     }
129 }
130
Popular Tags