1 19 20 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 ; 35 import java.awt.Dimension ; 36 import java.awt.Graphics ; 37 import java.awt.Graphics2D ; 38 import java.awt.font.FontRenderContext ; 39 import java.awt.geom.Rectangle2D ; 40 import java.awt.image.BufferedImage ; 41 import javax.swing.JComponent ; 42 import javax.swing.JEditorPane ; 43 import javax.swing.JPanel ; 44 import javax.swing.JScrollPane ; 45 import org.netbeans.editor.Utilities; 46 import org.openide.loaders.DataObject; 47 import org.openide.util.NbBundle; 48 51 52 58 public class CssCustomEditor extends JPanel implements CssModelListener{ 59 60 boolean styleEditorShown = false; 61 StyleBuilderPanel styleBuilderPanel = new StyleBuilderPanel(); 62 JScrollPane styleBuilderScroll = new JScrollPane (); 63 JScrollPane stylePreviewScroll = new JScrollPane (); 65 66 BufferedImage previewImage; 67 String noPreviewLabel = NbBundle.getMessage(CssCustomEditor.class, "No_preview_available"); 68 69 CssStyleData cssStyleData = null; 70 71 Component editorComp; 72 73 JEditorPane editorPane; 74 75 DataObject dataObject = null; 76 77 78 public CssCustomEditor(JEditorPane editorPane) { 79 this.editorPane = editorPane; 80 initComponents(); 81 editorComp = Utilities.getEditorUI(editorPane).getExtComponent(); 82 ((JComponent )editorComp).setMinimumSize(new Dimension (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.setMinimumSize(new Dimension (250,250)); 101 styleBuilderScroll.setViewportView(styleBuilderPanel); 102 103 109 112 styleBuilderScroll.setPreferredSize(new Dimension (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 176 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 209 210 this.cssStyleData = cssStyleData; 211 styleBuilderPanel.setCssStyleData(cssStyleData); 212 } 221 222 227 private void initComponents() { 229 editorSplitPane = new javax.swing.JSplitPane (); 230 styleBuilderSplitPane = new javax.swing.JSplitPane (); 231 232 setLayout(new java.awt.BorderLayout ()); 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 (400, 250)); 239 styleBuilderSplitPane.setPreferredSize(new java.awt.Dimension (600, 250)); 240 editorSplitPane.setBottomComponent(styleBuilderSplitPane); 241 242 add(editorSplitPane, java.awt.BorderLayout.CENTER); 243 244 } 246 class PreviewPanel extends JPanel { 247 248 public void paintComponent(Graphics graphics) { 249 super.paintComponent(graphics); 250 Graphics2D g2d = (Graphics2D ) 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 frc = g2d.getFontRenderContext(); 263 Rectangle2D 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 private javax.swing.JSplitPane editorSplitPane; 273 private javax.swing.JSplitPane styleBuilderSplitPane; 274 276 } 277 | Popular Tags |