KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > actions > CssRuleCreateAction


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  * CssRuleCreateAction.java
21  * Created on September 23, 2005, 5:05 PM
22  */

23
24
25 package org.netbeans.modules.css.actions;
26
27 import java.awt.event.ActionEvent JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.Caret JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import org.openide.util.NbBundle;
32 import org.netbeans.editor.BaseAction;
33 import org.netbeans.editor.BaseDocument;
34
35 /**
36  * Action to create a new CSS Rule
37  * @author Winston Prakash
38  */

39 public class CssRuleCreateAction extends BaseAction{
40
41     public static final String JavaDoc createRuleAction = "create-rule"; // NOI18N
42
/** Creates a new instance of CssRuleCreateAction */
43     public CssRuleCreateAction() {
44         super(createRuleAction);
45         putValue("helpID", CssRuleCreateAction.class.getName()); // NOI18N
46
putValue(SHORT_DESCRIPTION, NbBundle.getMessage(CssRuleCreateAction.class, "Create_Rule"));
47         putValue(BaseAction.ICON_RESOURCE_PROPERTY, "org/netbeans/modules/css/resources/new_rule.png"); // NOI18N
48
}
49
50     public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
51         if(target == null) {
52             return;
53         }
54         CssRuleCreateActionDialog cssRuleCreateActionDialog = new CssRuleCreateActionDialog();
55         cssRuleCreateActionDialog.showDialog();
56         String JavaDoc styleRuleName = cssRuleCreateActionDialog.getStyleRuleName();
57         if((styleRuleName != null) && !styleRuleName.equals("")){
58             Caret JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc exc){
121                 exc.printStackTrace();
122             }
123         }
124     }
125     
126 }
127
Popular Tags