1 19 20 package org.netbeans.modules.html.palette.items; 21 import javax.swing.text.BadLocationException ; 22 import javax.swing.text.JTextComponent ; 23 import org.netbeans.modules.html.palette.HTMLPaletteUtilities; 24 import org.openide.text.ActiveEditorDrop; 25 26 27 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 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 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 targetComponent) { 51 52 TABLECustomizer c = new TABLECustomizer(this); 53 boolean accept = c.showDialog(); 54 if (accept) { 55 String body = createBody(); 56 try { 57 HTMLPaletteUtilities.insert(body, targetComponent); 58 } catch (BadLocationException ble) { 59 accept = false; 60 } 61 } 62 63 return accept; 64 } 65 66 private String createBody() { 67 68 String tHead = generateTHead(); 69 String tBody = generateTBody(); 70 71 String strBorder = " border=\"" + border + "\""; 73 String strWidth = ""; 74 if (!width.equals(WIDTH_DEFAULT)) 75 strWidth = " width=\"" + width + "\""; 77 String strCspac = ""; 78 if (cspac != CSPAC_DEFAULT) 79 strCspac = " cellspacing=\"" + cspac + "\""; 81 String strCpadd = ""; 82 if (cpadd != CPADD_DEFAULT) 83 strCpadd = " cellpadding=\"" + cpadd + "\""; 85 86 String body = 87 "<table" + strBorder + strWidth + strCspac + strCpadd + ">\n" + "<thead>\n" + tHead + "</thead>\n" + "<tbody>\n" + tBody + "</tbody>\n" + "</table>\n"; 92 return body; 93 } 94 95 private String generateTHead() { 96 97 StringBuffer sb = new StringBuffer (); 98 for (int i = 0; i < cols; i++) 99 sb.append("<th></th>\n"); 101 String thead = "<tr>\n" + sb.toString() + "</tr>\n"; 103 return thead; 104 } 105 106 private String generateTBody() { 107 108 StringBuffer sb = new StringBuffer (); 109 for (int i = 0; i < rows; i++) { 110 sb.append("<tr>\n"); for (int j = 0; j < cols; j++) 112 sb.append("<td></td>\n"); sb.append("</tr>\n"); } 115 116 String 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 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 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 |