1 7 8 package org.jdesktop.jdnc.markup.attr; 9 10 import java.net.URL ; 11 12 import java.awt.Color ; 13 import java.awt.Dimension ; 14 15 import javax.swing.JTable ; 16 import javax.swing.event.TableModelEvent ; 17 import javax.swing.event.TableModelListener ; 18 import javax.swing.ListSelectionModel ; 19 import javax.swing.table.TableModel ; 20 21 import org.jdesktop.swing.data.DataLoader; 22 import org.jdesktop.swing.data.DefaultTableModelExt; 23 import org.jdesktop.swing.data.TableModelExtTextLoader; 24 import org.jdesktop.swing.utils.LoadOnShowListener; 25 import org.jdesktop.jdnc.JNTable; 26 import org.jdesktop.jdnc.markup.RealizationUtils; 27 import org.jdesktop.jdnc.markup.elem.TableElement; 28 29 import net.openmarkup.ApplierException; 30 import net.openmarkup.AttributeApplier; 31 import net.openmarkup.Realizable; 32 33 37 public class TableAttributes { 38 public static final AttributeApplier hasColumnControlApplier = new AttributeApplier() { 39 public void apply(Realizable target, String namespaceURI, 40 String attributeName, String attributeValue) throws ApplierException { 41 JNTable table = (JNTable) target.getObject(); 42 boolean enabled = Boolean.valueOf(attributeValue).booleanValue(); 43 table.setHasColumnControl(enabled); 44 } 45 }; 46 47 public static final AttributeApplier columnMarginApplier = new AttributeApplier() { 48 public void apply(Realizable target, String namespaceURI, 49 String attributeName, String attributeValue) throws ApplierException { 50 JNTable table = (JNTable) target.getObject(); 51 try { 52 int columnMargin = Integer.parseInt(attributeValue); 53 table.setColumnMargin(columnMargin); 54 } 55 catch (NumberFormatException ex) { 56 throw new ApplierException("Bad format: " + attributeName + "=" + attributeValue, ex); 57 } 58 } 59 }; 60 61 public static final AttributeApplier dataApplier = new AttributeApplier() { 62 public void apply(Realizable target, String namespaceURI, 63 String attributeName, String attributeValue) 64 throws ApplierException { 65 DefaultTableModelExt data = (DefaultTableModelExt)BaseAttribute.getReferencedObject(target, 66 attributeValue); 67 TableElement.setModel(target, data); 68 } 69 }; 70 71 public static final AttributeApplier dataSourceApplier = new AttributeApplier() { 72 public void apply(Realizable target, String namespaceURI, 73 String attributeName, String attributeValue) throws ApplierException { 74 75 URL url = target.getResolvedURL(attributeValue); 77 RealizationUtils.validateURL(url); 78 79 try { 80 DefaultTableModelExt model = new DefaultTableModelExt(); 81 model.setSource(url); 82 JNTable table = (JNTable) target.getObject(); 83 table.setModel(model); 84 table.addHierarchyListener(new LoadOnShowListener(model)); 85 } 86 catch (Exception ex) { 87 throw new ApplierException("Couldn't set data source " + 88 attributeName + "=" + attributeValue, ex); 89 } 90 } 91 }; 92 93 public static final AttributeApplier firstRowHeaderApplier = new AttributeApplier() { 94 public void apply(Realizable target, String namespaceURI, 95 String attributeName, String attributeValue) throws ApplierException { 96 try { 97 JNTable table = (JNTable) target.getObject(); 98 TableModel model = table.getModel(); 99 if (model instanceof DefaultTableModelExt) { 100 TableModelExtTextLoader loader = (TableModelExtTextLoader)((DefaultTableModelExt)model).getLoader(); 101 loader.setFirstRowHeader(Boolean.valueOf(attributeValue).booleanValue()); 102 } 103 } 104 catch (Exception ex) { 105 throw new ApplierException("Couldn't set firstRowHeader " + 106 attributeName + "=" + attributeValue, ex); 107 } 108 } 109 }; 110 111 public static final AttributeApplier gridColorApplier = new AttributeApplier() { 112 public void apply(Realizable target, String namespaceURI, 113 String attributeName, String attributeValue) { 114 JNTable table = (JNTable) target.getObject(); 115 Color color = Decoder.decodeColor(attributeValue); 116 table.setGridColor(color); 117 } 118 }; 119 120 public static final AttributeApplier isColumnHeaderLockedApplier = new AttributeApplier() { 121 public void apply(Realizable target, String namespaceURI, 122 String attributeName, String attributeValue) { 123 JNTable table = (JNTable) target.getObject(); 124 boolean isLocked = Boolean.valueOf(attributeValue).booleanValue(); 125 table.getTable().getTableHeader().setEnabled(!isLocked); 126 127 } 128 }; 129 130 public static final AttributeApplier isRowHeaderLockedApplier = new AttributeApplier() { 131 public void apply(Realizable target, String namespaceURI, 132 String attributeName, String attributeValue) { 133 JNTable table = (JNTable) target.getObject(); 134 boolean isLocked = Boolean.valueOf(attributeValue).booleanValue(); 135 table.setRowHeaderLocked(isLocked); 136 } 137 }; 138 139 public static final AttributeApplier preferredSizeApplier = new AttributeApplier() { 140 public void apply(Realizable target, String namespaceURI, 141 String attributeName, String attributeValue) { 142 String [] args = attributeValue.split("\\s"); 143 if (args.length == 2) { 144 Dimension size = new Dimension ( 145 Integer.parseInt(args[0]), Integer.parseInt(args[1])); 146 JNTable table = (JNTable) target.getObject(); 147 table.setPreferredSize(size); 148 } 149 } 150 }; 151 152 public static final AttributeApplier rowHeightApplier = new AttributeApplier() { 153 public void apply(Realizable target, String namespaceURI, 154 String attributeName, String attributeValue) { 155 JNTable table = (JNTable) target.getObject(); 156 int rowHeight = Integer.parseInt(attributeValue); 157 table.setRowHeight(rowHeight); 158 } 159 }; 160 161 public static final AttributeApplier rowMarginApplier = new AttributeApplier() { 162 public void apply(Realizable target, String namespaceURI, 163 String attributeName, String attributeValue) throws ApplierException { 164 JNTable table = (JNTable) target.getObject(); 165 try { 166 int rowMargin = Integer.parseInt(attributeValue); 167 table.setRowMargin(rowMargin); 168 } 169 catch (NumberFormatException ex) { 170 throw new ApplierException("Bad format: " + attributeName + "=" + attributeValue, ex); 171 } 172 } 173 }; 174 175 public static final AttributeApplier selectionApplier = new AttributeApplier() { 176 public void apply(Realizable target, String namespaceURI, 177 String attributeName, String attributeValue) throws ApplierException { 178 int index = Integer.parseInt(attributeValue); 179 final JTable table = ((JNTable) target.getObject()).getTable(); 180 181 table.getModel().addTableModelListener(new TableModelListener () { 184 public void tableChanged(TableModelEvent e) { 185 if (e.getType() == TableModelEvent.INSERT) { 186 table.getSelectionModel().setLeadSelectionIndex(0); 187 table.getSelectionModel().setAnchorSelectionIndex(0); 188 table.getModel().removeTableModelListener(this); 189 } 190 } 191 }); 192 } 193 }; 194 195 public static final AttributeApplier selectionModeApplier = new AttributeApplier() { 196 public void apply(Realizable target, String namespaceURI, 197 String attributeName, String attributeValue) throws ApplierException { 198 JNTable table = (JNTable) target.getObject(); 199 if (attributeValue.equals("single")) { 200 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 201 } else if (attributeValue.equals("contiguous")) { 202 table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 203 } else if (attributeValue.equals("discontiguous")) { 204 table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 205 } else { 206 207 } 208 } 209 }; 210 211 public static final AttributeApplier showsHorizontalLinesApplier = new AttributeApplier() { 212 public void apply(Realizable target, String namespaceURI, 213 String attributeName, String attributeValue) throws ApplierException { 214 JNTable table = (JNTable) target.getObject(); 215 boolean show = Boolean.valueOf(attributeValue).booleanValue(); 216 table.setShowHorizontalLines(show); 217 } 218 }; 219 220 public static final AttributeApplier showsVerticalLinesApplier = new AttributeApplier() { 221 public void apply(Realizable target, String namespaceURI, 222 String attributeName, String attributeValue) throws ApplierException { 223 JNTable table = (JNTable) target.getObject(); 224 boolean show = Boolean.valueOf(attributeValue).booleanValue(); 225 table.setShowVerticalLines(show); 226 } 227 }; 228 238 239 } 241 | Popular Tags |