KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > editor > CssCustomEditor


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

19
20 /*
21  * CssCustomEditor.java
22  *
23  * Created on February 25, 2005, 12:47 PM
24  */

25
26 package org.netbeans.modules.css.editor;
27
28 import org.netbeans.modules.css.visual.model.CssMetaModel;
29 import org.netbeans.modules.css.visual.model.CssMetaModel.CssModelChangeEvent;
30 import org.netbeans.modules.css.visual.model.CssMetaModel.CssModelListener;
31 import org.netbeans.modules.css.visual.model.CssRuleData;
32 import org.netbeans.modules.css.visual.model.CssStyleData;
33 import org.netbeans.modules.css.visual.ui.StyleBuilderPanel;
34 import java.awt.Component JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Graphics JavaDoc;
37 import java.awt.Graphics2D JavaDoc;
38 import java.awt.font.FontRenderContext JavaDoc;
39 import java.awt.geom.Rectangle2D JavaDoc;
40 import java.awt.image.BufferedImage JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JEditorPane JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import org.netbeans.editor.Utilities;
46 import org.openide.loaders.DataObject;
47 import org.openide.util.NbBundle;
48 //import org.xhtmlrenderer.simple.XHTMLPanel;
49
//import org.xhtmlrenderer.util.XMLUtil;
50

51
52 /**
53  * Custom Editor for CSS document. Adds the Style Builder and
54  * Preview panel to the Original Editor Component
55  * @author Winston Prakash
56  * @version 1.0
57  */

58 public class CssCustomEditor extends JPanel JavaDoc implements CssModelListener{
59
60     boolean styleEditorShown = false;
61     StyleBuilderPanel styleBuilderPanel = new StyleBuilderPanel();
62     JScrollPane JavaDoc styleBuilderScroll = new JScrollPane JavaDoc();
63 // XHTMLPanel stylePreviewPanel = new XHTMLPanel();
64
JScrollPane JavaDoc stylePreviewScroll = new JScrollPane JavaDoc();
65
66     BufferedImage JavaDoc previewImage;
67     String JavaDoc noPreviewLabel = NbBundle.getMessage(CssCustomEditor.class, "No_preview_available");
68
69     CssStyleData cssStyleData = null;
70
71     Component JavaDoc editorComp;
72
73     JEditorPane JavaDoc editorPane;
74
75     DataObject dataObject = null;
76
77     /** Creates new form NewJPanel */
78     public CssCustomEditor(JEditorPane JavaDoc editorPane) {
79         this.editorPane = editorPane;
80         initComponents();
81         editorComp = Utilities.getEditorUI(editorPane).getExtComponent();
82         ((JComponent JavaDoc)editorComp).setMinimumSize(new Dimension JavaDoc(300,200));
83         addStyleBuilder(false);
84     }
85
86     public void setDataObject(DataObject dObject){
87         if(dataObject == null){
88             dataObject = dObject;
89             CssMetaModel.setDataObject(dataObject);
90             CssMetaModel.getInstance().addModelListener(this);
91         }
92     }
93
94     public void addStyleBuilder(boolean repaint){
95         int caretPos = editorPane.getCaret().getDot();
96         remove(editorComp);
97         editorSplitPane.setTopComponent(editorComp);
98
99 // styleBuilderScroll.setPreferredSize(new Dimension(450,450));
100
styleBuilderScroll.setMinimumSize(new Dimension JavaDoc(250,250));
101         styleBuilderScroll.setViewportView(styleBuilderPanel);
102
103 // stylePreviewScroll.setPreferredSize(new Dimension(350,250));
104
// stylePreviewScroll.setMinimumSize(new Dimension(250,250));
105
// stylePreviewScroll.setViewportView(stylePreviewPanel);
106
//
107
// stylePreviewPanel.setPreferredSize(new Dimension(100,100));
108

109 // styleBuilderSplitPane.setLeftComponent(styleBuilderScroll);
110
// styleBuilderSplitPane.setRightComponent(stylePreviewScroll);
111

112         //temporary hack - delete after preview panel is back
113
styleBuilderScroll.setPreferredSize(new Dimension JavaDoc(600,250));
114         editorSplitPane.setBottomComponent(styleBuilderScroll);
115         
116         add(editorSplitPane, java.awt.BorderLayout.CENTER);
117         if(repaint){
118             validate();
119             repaint();
120         }
121         editorPane.requestFocus();
122         editorPane.getCaret().setDot(caretPos);
123     }
124
125     public void removeStyleBuilder(boolean repaint){
126         int caretPos = editorPane.getCaret().getDot();
127         remove(editorSplitPane);
128         add(editorComp, java.awt.BorderLayout.CENTER);
129         if(repaint){
130             validate();
131             repaint();
132         }
133         editorPane.requestFocus();
134         editorPane.getCaret().setDot(caretPos);
135     }
136     
137 // private void createPreviewImage(CssStyleData cssStyleData){
138
// URL fileUrl = null;
139
// try{
140
// FileObject fo = dataObject.getPrimaryFile();
141
// try {
142
// fileUrl = fo.getURL();
143
// } catch (Exception e) {
144
// fileUrl = FileUtil.toFile(fo).toURI().toURL();
145
// }
146
// } catch (Exception exc){
147
// exc.printStackTrace();
148
// }
149
// previewImage = (BufferedImage) cssService.getCssPreviewImage(cssStyleData.getPropertyMap(), fileUrl, 300,150);
150
// if (previewImage == null){
151
// ColorModel colorModel = new ColorModel();
152
// FontModel fontModel = new FontModel();
153
// previewImage = new BufferedImage(200,50,BufferedImage.TYPE_INT_RGB);
154
// Graphics2D g2d = (Graphics2D)previewImage.getGraphics();
155
// String bgColor = cssStyleData.getProperty(CssProperties.BACKGROUND_COLOR);
156
// if(bgColor != null){
157
// Color originalCol = g2d.getColor();
158
// colorModel.setColor(bgColor);
159
// g2d.setColor(colorModel.getColor());
160
// g2d.fillRect(0,0,previewImage.getWidth(),previewImage.getHeight());
161
// g2d.setColor(originalCol);
162
// }
163
// String fgColor = cssStyleData.getProperty(CssProperties.COLOR);
164
// if(fgColor != null){
165
// colorModel.setColor(fgColor);
166
// g2d.setColor(colorModel.getColor());
167
// }
168
// g2d.setFont(fontModel.resolveFont(cssStyleData, g2d.getFont()));
169
// g2d.drawString(NbBundle.getMessage(CssCustomEditor.class, "Sample_Text"),(float)50.,(float)30.);
170
// }
171
// if(previewImage != null){
172
//// stylePreviewPanel.setPreferredSize(new Dimension(previewImage.getWidth(this)+10,previewImage.getHeight(this)+10));
173
// }
174
// }
175

176     // Implementation of CssMetaModelListener
177

178     public void ruleAdded(CssModelChangeEvent evt) {}
179     
180     public void ruleRemoved(CssModelChangeEvent evt) {}
181     
182     public void selectionChanged(CssModelChangeEvent evt) {
183         CssRuleData ruleData = evt.getRule();
184         if(ruleData.getCssStyleData() != null){
185             setCssStyleData(ruleData.getCssStyleData());
186         }
187     }
188     
189     public void viewChanged(CssModelChangeEvent evt) {
190         if(CssMetaModel.getInstance().getStyleBuilderVisibility()){
191             addStyleBuilder(true);
192         }else{
193             removeStyleBuilder(true);
194         }
195     }
196     
197     public void setCssStyleData(final CssStyleData cssStyleData){
198 // //prepare the XHTML document from the cssStyleData
199
// String fakeCSScontent = ...;
200
// try {
201
// stylePreviewPanel.setDocument(XMLUtil.documentFromString(fakeCSScontent));
202
// }catch(Exception e) {
203
// System.out.println("error!!!!!!!!!!!!!!!!!!!!!");
204
// e.printStackTrace();
205
// }
206
//
207
// stylePreviewPanel.repaint();
208

209         
210         this.cssStyleData = cssStyleData;
211         styleBuilderPanel.setCssStyleData(cssStyleData);
212 // cssStyleData.addPropertyChangeListener(new PropertyChangeListener(){
213
// public void propertyChange(PropertyChangeEvent evt){
214
// createPreviewImage(cssStyleData);
215
// stylePreviewScroll.repaint();
216
// }
217
// });
218
// createPreviewImage(cssStyleData);
219
// stylePreviewScroll.repaint();
220
}
221     
222     /** This method is called from within the constructor to
223      * initialize the form.
224      * WARNING: Do NOT modify this code. The content of this method is
225      * always regenerated by the Form Editor.
226      */

227     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
228
private void initComponents() {
229         editorSplitPane = new javax.swing.JSplitPane JavaDoc();
230         styleBuilderSplitPane = new javax.swing.JSplitPane JavaDoc();
231
232         setLayout(new java.awt.BorderLayout JavaDoc());
233
234         editorSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
235         editorSplitPane.setResizeWeight(1.0);
236         editorSplitPane.setAutoscrolls(true);
237         styleBuilderSplitPane.setResizeWeight(1.0);
238         styleBuilderSplitPane.setMinimumSize(new java.awt.Dimension JavaDoc(400, 250));
239         styleBuilderSplitPane.setPreferredSize(new java.awt.Dimension JavaDoc(600, 250));
240         editorSplitPane.setBottomComponent(styleBuilderSplitPane);
241
242         add(editorSplitPane, java.awt.BorderLayout.CENTER);
243
244     }// </editor-fold>//GEN-END:initComponents
245

246     class PreviewPanel extends JPanel JavaDoc{
247         
248         public void paintComponent(Graphics JavaDoc graphics) {
249             super.paintComponent(graphics);
250             Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) graphics;
251             if(previewImage != null){
252                 int imgX = 3;
253                 int imgY = 3;
254                 int imgWidth = previewImage.getWidth(this);
255                 int imgHeight = previewImage.getHeight(this);
256                 if(imgWidth < this.getWidth()){
257                     imgX = (getWidth() - imgWidth)/2;
258                     imgY = (getHeight() - imgHeight)/2;
259                 }
260                 g2d.drawImage(previewImage, imgX, imgY,this);
261             }else{
262                 FontRenderContext JavaDoc frc = g2d.getFontRenderContext();
263                 Rectangle2D JavaDoc bounds = g2d.getFont().getStringBounds(noPreviewLabel,frc);
264                 int labelX = (getWidth() - (int)bounds.getWidth())/2;
265                 int labelY = (getHeight() - (int)bounds.getHeight())/2;
266                 g2d.drawString(noPreviewLabel,labelX,labelY);
267             }
268         }
269     }
270     
271     // Variables declaration - do not modify//GEN-BEGIN:variables
272
private javax.swing.JSplitPane JavaDoc editorSplitPane;
273     private javax.swing.JSplitPane JavaDoc styleBuilderSplitPane;
274     // End of variables declaration//GEN-END:variables
275

276 }
277
Popular Tags