1 23 24 package org.infoglue.cms.plugins.codeeditor; 25 26 import java.awt.Color ; 27 import java.awt.Component ; 28 import java.awt.Dimension ; 29 import java.awt.Font ; 30 import java.awt.Frame ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import javax.swing.JScrollPane ; 36 import javax.swing.JTextPane ; 37 import javax.swing.event.DocumentEvent ; 38 import javax.swing.event.DocumentListener ; 39 import javax.swing.text.SimpleAttributeSet ; 40 import javax.swing.text.Style ; 41 import javax.swing.text.StyleConstants ; 42 import javax.swing.text.StyleContext ; 43 import javax.swing.text.TabSet ; 44 import javax.swing.text.TabStop ; 45 46 49 50 public class InfoGlueTextArea extends JTextPane implements DocumentListener { 52 protected InfoGlueCodeEditorController controller = null; 53 protected JScrollPane scrollPane = null; 54 private Font standardFont = new Font ("Courier", Font.PLAIN, 12); 55 private CodeHelperDialog methodsCodeHelperDialog = null; 56 private CodeHelperDialog velocityCodeHelperDialog = null; 57 private CodeHelperDialog velocityVariableCodeHelperDialog = null; 58 59 62 63 public InfoGlueTextArea(InfoGlueCodeEditorController infoGlueCodeEditorController) 64 { 65 Style styDefault = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 66 StyleConstants.setFontFamily(styDefault,"Courier"); 67 StyleConstants.setFontSize(styDefault, 14); 68 StyleConstants.setForeground(styDefault, Color.black); 69 this.setLogicalStyle(styDefault); 70 SimpleAttributeSet attribs = new SimpleAttributeSet (this.getParagraphAttributes()); 71 StyleConstants.setBackground(attribs, new Color (230, 230, 230)); 72 this.setCharacterAttributes(attribs, false); 73 74 setTabs(); 75 this.addKeyListener(new TextAreaInputListener(this)); 76 this.controller = infoGlueCodeEditorController; 77 } 78 79 82 83 private void setTabs() 84 { 85 List list = new ArrayList (); 86 87 float pos = 0; 88 for(int i=0; i<20; i++) 89 { 90 pos += 20; 91 int align = TabStop.ALIGN_LEFT; 92 int leader = TabStop.LEAD_NONE; 93 TabStop tstop = new TabStop (pos, align, leader); 94 list.add(tstop); 95 } 96 97 TabStop [] tstops = (TabStop [])list.toArray(new TabStop [0]); 98 TabSet tabs = new TabSet (tstops); 99 100 Style style = this.getLogicalStyle(); 101 StyleConstants.setTabSet(style, tabs); 102 this.setLogicalStyle(style); 103 } 104 105 109 private List getTemplateLogicMethodList() 110 { 111 List methods = new ArrayList (); 112 113 try 114 { 115 String xml = HttpUtilities.getUrlContent(this.controller.getDeliverySettingsUrl()); 116 118 int offset = 0; 119 int methodIndex = xml.indexOf("<method>", offset); 120 int methodEndIndex = xml.indexOf("</method>", offset); 121 122 while(methodIndex > -1 && methodEndIndex > -1) 123 { 124 String methodDescription = xml.substring(methodIndex + 8, methodEndIndex); 125 methods.add(methodDescription); 126 127 offset = methodEndIndex + 9; 128 methodIndex = xml.indexOf("<method>", offset); 129 methodEndIndex = xml.indexOf("</method>", offset); 130 } 131 } 132 catch(Exception e) 133 { 134 System.out.println("Exception:" + e); 135 e.printStackTrace(); 136 } 137 138 return methods; 139 } 140 141 public void openTemplateLogicCodeHelper(int xPosition, int yPosition) 142 { 143 if(this.methodsCodeHelperDialog == null) 144 { 145 List methods = getTemplateLogicMethodList(); 146 List listItems = new ArrayList (); 147 Iterator i = methods.iterator(); 148 while(i.hasNext()) 149 { 150 String method = "" + (String )i.next(); 151 listItems.add(method); 152 } 153 154 methodsCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray()); 155 } 156 157 int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10; 158 int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24; 159 160 if(xLocation > this.scrollPane.getWidth()) 161 xLocation = this.scrollPane.getWidth(); 162 163 if(yLocation > this.scrollPane.getHeight()) 164 yLocation = this.scrollPane.getHeight(); 165 166 this.methodsCodeHelperDialog.setLocation(xLocation, yLocation); 167 this.methodsCodeHelperDialog.setVisible(true); 168 } 169 170 171 public void openVelocityTemplateCodeHelper(int xPosition, int yPosition) 172 { 173 if(this.velocityCodeHelperDialog == null) 174 { 175 List listItems = new ArrayList (); 176 listItems.add("if()"); 177 listItems.add("else()"); 178 listItems.add("elseif()"); 179 listItems.add("end()"); 180 listItems.add("foreach()"); 181 listItems.add("if()"); 182 listItems.add("include()"); 183 listItems.add("macro()"); 184 listItems.add("parse()"); 185 listItems.add("set()"); 186 listItems.add("stop()"); 187 188 this.velocityCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray()); 189 } 190 191 int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10; 192 int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24; 193 194 if(xLocation > this.scrollPane.getWidth()) 195 xLocation = this.scrollPane.getWidth(); 196 197 if(yLocation > this.scrollPane.getHeight()) 198 yLocation = this.scrollPane.getHeight(); 199 200 201 this.velocityCodeHelperDialog.setLocation(xLocation, yLocation); 202 this.velocityCodeHelperDialog.setVisible(true); 203 } 204 205 public void openVelocityVariableTemplateCodeHelper(int xPosition, int yPosition) 206 { 207 if(this.velocityVariableCodeHelperDialog == null) 208 { 209 List listItems = new ArrayList (); 210 listItems.add("templateLogic"); 211 212 this.velocityVariableCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray()); 213 } 214 215 int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10; 216 int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24; 217 218 if(xLocation > this.scrollPane.getWidth()) 219 xLocation = this.scrollPane.getWidth(); 220 221 if(yLocation > this.scrollPane.getHeight()) 222 yLocation = this.scrollPane.getHeight(); 223 224 this.velocityVariableCodeHelperDialog.setLocation(xLocation, yLocation); 225 this.velocityVariableCodeHelperDialog.setVisible(true); 226 } 227 228 229 private Frame getFrame(Component parent) 230 { 231 while (!(parent instanceof Frame )) 232 { 233 parent = ((Component )parent).getParent(); 234 } 236 237 return (Frame )parent; 240 } 241 242 243 public void setScrollPane(JScrollPane scrollPane) 244 { 245 this.scrollPane = scrollPane; 246 247 Rule columnView = new Rule(Rule.HORIZONTAL, true); 248 columnView.setPreferredWidth(500); 249 Rule rowView = new Rule(Rule.VERTICAL, true); 250 rowView.setPreferredHeight(20000); 251 scrollPane.setRowHeaderView(rowView); 252 } 253 254 255 public void insertUpdate( DocumentEvent event ) 257 { 258 } 259 260 public void removeUpdate( DocumentEvent event ) 262 { 263 } 264 265 public void changedUpdate( DocumentEvent event ) 267 { 268 } 269 270 275 276 public boolean getScrollableTracksViewportWidth() 277 { 278 return false; 279 } 280 281 287 288 public void setSize(Dimension d) 289 { 290 if (d.width < getParent().getSize().width) 291 { 292 d.width = getParent().getSize().width; 293 } 294 super.setSize(d); 295 } 296 297 } 298 | Popular Tags |