1 19 23 24 25 package org.netbeans.modules.css.actions; 26 27 import java.awt.event.ActionEvent ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.Caret ; 30 import javax.swing.text.JTextComponent ; 31 import org.openide.util.NbBundle; 32 import org.netbeans.editor.BaseAction; 33 import org.netbeans.editor.BaseDocument; 34 35 39 public class CssRuleCreateAction extends BaseAction{ 40 41 public static final String createRuleAction = "create-rule"; 43 public CssRuleCreateAction() { 44 super(createRuleAction); 45 putValue("helpID", CssRuleCreateAction.class.getName()); putValue(SHORT_DESCRIPTION, NbBundle.getMessage(CssRuleCreateAction.class, "Create_Rule")); 47 putValue(BaseAction.ICON_RESOURCE_PROPERTY, "org/netbeans/modules/css/resources/new_rule.png"); } 49 50 public void actionPerformed(ActionEvent evt, JTextComponent target) { 51 if(target == null) { 52 return; 53 } 54 CssRuleCreateActionDialog cssRuleCreateActionDialog = new CssRuleCreateActionDialog(); 55 cssRuleCreateActionDialog.showDialog(); 56 String styleRuleName = cssRuleCreateActionDialog.getStyleRuleName(); 57 if((styleRuleName != null) && !styleRuleName.equals("")){ 58 Caret caret = target.getCaret(); 59 int searchPos = target.getCaret().getDot() -1 ; 60 BaseDocument doc = (BaseDocument)target.getDocument(); 61 int insertPos = doc.getLength() + 1; 62 try{ 63 if(searchPos > 0){ 64 String txtBefore = doc.getText(searchPos, 1); 65 while(!(txtBefore.equals("{") || txtBefore.equals("}"))){ 66 if(txtBefore.equals("/")){ 67 if ((searchPos-1) >= 0){ 68 if (doc.getText(searchPos-1, 1).equals("*")){ 69 break; 70 }else if((searchPos+1) <= doc.getLength()){ 71 if (doc.getText(searchPos+1, 1).equals("*")){ 72 break; 73 } 74 } 75 } 76 } 77 searchPos--; 78 if (searchPos < 0) break; 79 txtBefore = doc.getText(searchPos, 1); 80 } 81 if(searchPos < 0){ 82 insertPos = 0; 83 }else if(txtBefore.equals("}")){ 84 insertPos = searchPos + 1; 85 }else if(txtBefore.equals("/")){ 86 if ((searchPos-1) >= 0){ 87 if (doc.getText(searchPos-1, 1).equals("*")){ 88 insertPos = searchPos + 1; 89 }else if((searchPos+1) <= doc.getLength()){ 90 if (doc.getText(searchPos+1, 1).equals("*")){ 91 insertPos = searchPos - 1; 92 } 93 } 94 } 95 } else if(txtBefore.equals("{")){ 96 searchPos = target.getCaret().getDot(); 97 String txtAfter = doc.getText(searchPos, 1); 98 while(!txtAfter.equals("}")){ 99 searchPos++; 100 if(searchPos > doc.getLength()) break; 101 txtAfter = doc.getText(searchPos, 1); 102 } 103 if(txtAfter.equals("}")){ 104 insertPos = searchPos + 1; 105 } 106 } 107 }else{ 108 insertPos = 0; 109 } 110 doc.insertString(insertPos,"\n" + styleRuleName + " {\n\n}", null); 111 searchPos = insertPos; 112 String txtAfter = doc.getText(searchPos, 1); 113 while(!txtAfter.equals("{")){ 114 searchPos++; 115 if(searchPos > doc.getLength()) break; 116 txtAfter = doc.getText(searchPos, 1); 117 } 118 searchPos += 2; 119 target.setCaretPosition(searchPos); 120 }catch(BadLocationException exc){ 121 exc.printStackTrace(); 122 } 123 } 124 } 125 126 } 127 | Popular Tags |