KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > palette > items > TABLE


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

19
20 package org.netbeans.modules.html.palette.items;
21 import javax.swing.text.BadLocationException JavaDoc;
22 import javax.swing.text.JTextComponent JavaDoc;
23 import org.netbeans.modules.html.palette.HTMLPaletteUtilities;
24 import org.openide.text.ActiveEditorDrop;
25
26
27 /**
28  *
29  * @author Libor Kotouc
30  */

31 public class TABLE implements ActiveEditorDrop {
32
33     private static final int ROWS_DEFAULT = 2;
34     private static final int COLS_DEFAULT = 2;
35     private static final int BORDER_DEFAULT = 1;
36     private static final String JavaDoc WIDTH_DEFAULT = "";
37     private static final int CSPAC_DEFAULT = 0;
38     private static final int CPADD_DEFAULT = 0;
39     
40     private int rows = ROWS_DEFAULT;
41     private int cols = COLS_DEFAULT;
42     private int border = BORDER_DEFAULT;
43     private String JavaDoc width = WIDTH_DEFAULT;
44     private int cspac = CSPAC_DEFAULT;
45     private int cpadd = CPADD_DEFAULT;
46             
47     public TABLE() {
48     }
49
50     public boolean handleTransfer(JTextComponent JavaDoc targetComponent) {
51
52         TABLECustomizer c = new TABLECustomizer(this);
53         boolean accept = c.showDialog();
54         if (accept) {
55             String JavaDoc body = createBody();
56             try {
57                 HTMLPaletteUtilities.insert(body, targetComponent);
58             } catch (BadLocationException JavaDoc ble) {
59                 accept = false;
60             }
61         }
62         
63         return accept;
64     }
65
66     private String JavaDoc createBody() {
67         
68         String JavaDoc tHead = generateTHead();
69         String JavaDoc tBody = generateTBody();
70         
71         String JavaDoc strBorder = " border=\"" + border + "\""; // NOI18N
72

73         String JavaDoc strWidth = "";
74         if (!width.equals(WIDTH_DEFAULT))
75             strWidth = " width=\"" + width + "\""; // NOI18N
76

77         String JavaDoc strCspac = "";
78         if (cspac != CSPAC_DEFAULT)
79             strCspac = " cellspacing=\"" + cspac + "\""; // NOI18N
80

81         String JavaDoc strCpadd = "";
82         if (cpadd != CPADD_DEFAULT)
83             strCpadd = " cellpadding=\"" + cpadd + "\""; // NOI18N
84

85         
86         String JavaDoc body =
87                 "<table" + strBorder + strWidth + strCspac + strCpadd + ">\n" + // NOI18N
88
"<thead>\n" + tHead + "</thead>\n" + // NOI18N
89
"<tbody>\n" + tBody + "</tbody>\n" + // NOI18N
90
"</table>\n"; // NOI18N
91

92         return body;
93     }
94     
95     private String JavaDoc generateTHead() {
96
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         for (int i = 0; i < cols; i++)
99             sb.append("<th></th>\n"); // NOI18N
100

101         String JavaDoc thead = "<tr>\n" + sb.toString() + "</tr>\n"; // NOI18N
102

103         return thead;
104     }
105     
106     private String JavaDoc generateTBody() {
107         
108         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
109         for (int i = 0; i < rows; i++) {
110             sb.append("<tr>\n"); // NOI18N
111
for (int j = 0; j < cols; j++)
112                 sb.append("<td></td>\n"); // NOI18N
113
sb.append("</tr>\n"); // NOI18N
114
}
115                 
116         String JavaDoc tBody = sb.toString();
117         
118         return tBody;
119     }
120
121     public void setRows(int rows) {
122         this.rows = rows;
123     }
124
125     public void setCols(int cols) {
126         this.cols = cols;
127     }
128
129     public void setBorder(int border) {
130         this.border = border;
131     }
132
133     public void setWidth(String JavaDoc width) {
134         this.width = width;
135     }
136
137     public void setCspac(int cspac) {
138         this.cspac = cspac;
139     }
140
141     public void setCpadd(int cpadd) {
142         this.cpadd = cpadd;
143     }
144
145     public int getRows() {
146         return rows;
147     }
148
149     public int getCols() {
150         return cols;
151     }
152
153     public int getBorder() {
154         return border;
155     }
156
157     public String JavaDoc getWidth() {
158         return width;
159     }
160
161     public int getCspac() {
162         return cspac;
163     }
164
165     public int getCpadd() {
166         return cpadd;
167     }
168     
169 }
170
Popular Tags