KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > text > CSSEditorSupport


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.text;
20
21 import java.io.*;
22 import java.awt.event.*;
23 import javax.swing.Timer JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.event.*;
26 import javax.swing.text.*;
27
28 import org.openide.text.EditorSupport;
29 import org.openide.text.Line;
30 import org.openide.windows.*;
31 import org.openide.loaders.*;
32 import org.openide.cookies.*;
33
34 import org.netbeans.modules.css.*;
35
36 /**
37  * Editor OAPI integration stuff.
38  *
39  * @author Petr Kuzel
40  * @version 1.0
41  */

42 public class CSSEditorSupport extends EditorSupport {
43
44     public CSSEditorSupport(MultiDataObject.Entry entry) {
45         super (entry);
46         setMIMEType (CSSObject.MIME_TYPE);
47     }
48
49     /* A method to create a new component. Overridden in subclasses.
50      * @return the {@link Editor} for this support
51      */

52     protected CloneableTopComponent createCloneableTopComponent () {
53         // initializes the document if not initialized
54
prepareDocument ();
55
56         return createCSSEditorComponent();
57     }
58
59     /** Method for creation of the java editor component
60      * - accessible from the innerclass.
61      */

62     CSSEditorComponent createCSSEditorComponent() {
63         CSSEditorComponent editor = new CSSEditorComponent (findDataObject());
64
65         // dock into editor mode if possible
66
Workspace current = WindowManager.getDefault().getCurrentWorkspace();
67         Mode editorMode = current.findMode (EDITOR_MODE);
68         if (editorMode != null)
69             editorMode.dockInto (editor);
70
71         return editor;
72     }
73
74 //~~~~~~~~~~~~~~~~~~~~~~~~~ ENCODING HANDLING ~~~~~~~~~~~~~~~~~~~~~~~~~~~
75

76     /** Opens the css checking for corect encoding.
77     public void open() {
78
79         try {
80
81             openDocument(); //sync call - prepare encodingErr
82
83             if (encodingErr) {
84                 String pattern = Util.THIS.getString("TEXT_WRONG_ENCODING");
85                 String msg = MessageFormat.format(pattern, new Object[] {entry.getFile().toString()});
86                 TopManager.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE));
87             } else {
88                 super.open();
89             }
90
91         } catch (IOException ex) {
92             String pattern = Util.THIS.getString("TEXT_LOADING_ERROR");
93             String msg = MessageFormat.format(pattern, new Object[] {entry.getFile().toString()});
94             TopManager.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE));
95         }
96         
97     }
98
99     //indicates than document has wrong encoding
100     private volatile boolean encodingErr;
101
102     /** Read the file from the stream, detect right encoding.
103     
104     protected void loadFromStreamToKit (StyledDocument doc, InputStream in, EditorKit kit) throws IOException, BadLocationException {
105         try {
106             encodingErr = false;
107             Reader reader = new CSSReader(in);
108             kit.read(reader, doc, 0);
109         } catch (CharConversionException ex) {
110             encodingErr = true;
111         } catch (UnsupportedEncodingException ex) {
112             encodingErr = true;
113         }
114
115     }
116
117     /** Store the document in proper encoding.
118     
119     protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream out) throws IOException, BadLocationException {
120         try {
121             Writer writer = new CSSWriter(out);
122             kit.write(writer, doc, 0, doc.getLength());
123         } catch (UnsupportedEncodingException ex) {
124             //!!! just write nothing //?? save say as UTF-8
125         }
126     }
127
128 */

129     
130     /////////////////////////////
131
/// class XMLEditorComponent
132
public static class CSSEditorComponent extends EditorSupport.Editor {
133         /** The support, subclass of EditorSupport */
134         CSSEditorSupport support;
135
136 private static final long serialVersionUID = 1997409673385969462L;
137
138         /** Only for externalization */
139         public CSSEditorComponent () {
140             super();
141         }
142
143         /** Creates new editor */
144         public CSSEditorComponent (DataObject obj) {
145             super (obj);
146             initialize();
147         }
148
149         /** Obtain a support for this component */
150         private void initialize () {
151             support = (CSSEditorSupport)obj.getCookie (CSSEditorSupport.class);
152         }
153
154 // /** Returns Editor pane for private use.
155
// * @return Editor pane for private use.
156
// */
157
// private JEditorPane getEditorPane () {
158
// return pane;
159
// }
160

161         /* Is called from the clone method to create new component from this one.
162          * This implementation only clones the object by calling super.clone method.
163          * @return the copy of this object
164          */

165         protected CloneableTopComponent createClonedObject () {
166             return support.createCSSEditorComponent();
167         }
168
169
170         /* This method is called when parent window of this component has focus,
171          * and this component is preferred one in it. This implementation adds
172          * performer to the ToggleBreakpointAction.
173          */

174         protected void componentActivated () {
175             super.componentActivated();
176         }
177
178         /*
179          * This method is called when parent window of this component losts focus,
180          * or when this component losts preferrence in the parent window. This
181          * implementation removes performer from the ToggleBreakpointAction.
182          */

183         protected void componentDeactivated () {
184             super.componentDeactivated();
185         }
186
187         /** Deserialize this top component.
188          * @param in the stream to deserialize from
189          */

190         public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
191             super.readExternal (in);
192             initialize();
193         }
194     }
195
196 }
197
Popular Tags