1 package com.calipso.xmleditor; 2 3 import com.calipso.reportgenerator.common.AbsoluteLayout; 4 import com.calipso.reportgenerator.common.AbsoluteConstraints; 5 import com.calipso.reportgenerator.common.LanguageTraslator; 6 7 import javax.swing.*; 8 import javax.swing.table.AbstractTableModel ; 9 import java.awt.event.ActionListener ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.*; 12 import java.util.Map ; 13 import java.util.Vector ; 14 import java.util.HashMap ; 15 16 23 27 28 29 33 public class DataTypeTableFrame extends javax.swing.JDialog { 34 35 private javax.swing.JButton accept; 36 private javax.swing.JButton cancel; 37 private javax.swing.JDialog jDialog1; 38 private javax.swing.JLabel jLabel1; 39 private javax.swing.JPanel jPanel1; 40 private javax.swing.JScrollPane jScrollPane1; 41 private javax.swing.JTable jTable1; 42 private boolean DEBUG = false; 43 private Map map; 44 private Map rowsLatestVersion = new HashMap (); 45 private boolean cancelled = false; 46 47 48 public DataTypeTableFrame(JDialog owner) { 49 super(owner,true); 50 initComponents(); 51 } 52 53 private void setCancelled(boolean b) { 54 this.cancelled = b; 55 } 56 57 64 private Map fillMapWithMatrix(){ 65 Map map = new HashMap (); 66 for (int i = 0; i < jTable1.getRowCount(); i++) { 67 if ( ((Boolean )jTable1.getValueAt(i,3)).booleanValue()){ 68 Vector vector = new Vector (3); 69 vector.add( (String )jTable1.getValueAt(i,1)); 70 if ( !((Boolean )jTable1.getValueAt(i,2)).booleanValue()){ 71 vector.add("Dimension"); 72 vector.add(jTable1.getValueAt(i,4)); 73 } else{ 74 vector.add("Metric"); 75 vector.add(jTable1.getValueAt(i,4)); 76 } 77 vector.add((String )jTable1.getValueAt(i,0)); 78 map.put(new Integer (i+1), vector); 79 } 80 } 81 return map; 82 } 83 84 89 public Map getRowsLatestVersion(){ 90 return rowsLatestVersion; 91 92 } 93 98 public boolean isCancelled(){ 99 return cancelled; 100 } 101 public DataTypeTableFrame(JFrame owner, Map map) { 102 super(owner,true); 103 this.map = map; 104 initComponents(); 105 } 106 107 112 private void initComponents() { 113 this.setResizable(false); 114 jPanel1 = new javax.swing.JPanel (); 115 jScrollPane1 = new javax.swing.JScrollPane (); 116 jTable1 = new javax.swing.JTable (new MyTableModel(map)); 117 accept = new javax.swing.JButton ("Aceptar"); 118 jLabel1 = new javax.swing.JLabel (); 119 getContentPane().setLayout(new javax.swing.BoxLayout (getContentPane(), javax.swing.BoxLayout.X_AXIS)); 120 jTable1.setRowSelectionAllowed( true ); 121 jTable1.setColumnSelectionAllowed( true ); 122 jTable1.setSelectionForeground( Color.blue ); 123 jTable1.setSelectionBackground( Color.red ); 124 jPanel1.setLayout(new AbsoluteLayout()); 125 jScrollPane1.setViewportView(jTable1); 126 jPanel1.add(jScrollPane1, new AbsoluteConstraints(0, 20, 720, 350)); 127 accept.setText(LanguageTraslator.traslate("112")); 128 jPanel1.add(accept, new AbsoluteConstraints(340, 380, -1, -1)); 129 jLabel1.setText(LanguageTraslator.traslate("545")); 130 jPanel1.add(jLabel1, new AbsoluteConstraints(0, 0, 170, 20)); 131 getContentPane().add(jPanel1); 132 accept.addActionListener(new Listener ()); 133 pack(); 134 } 135 136 class MyTableModel extends AbstractTableModel { 137 private Map map; 138 private String [] columnNames = {"Name/Ext Data", 139 "Description", 140 LanguageTraslator.traslate("546"), 141 LanguageTraslator.traslate("547") , 142 LanguageTraslator.traslate("548") }; 143 private Object [][] data; 144 145 public MyTableModel(Map map){ 146 this.map = map; 147 this.fillMatrixWithMap(); 148 149 } 150 private void fillMatrixWithMap(){ 151 data = new Object [map.size()][5]; 152 for (int j = 0; j < map.size(); j++){ 153 Vector vector = (Vector )map.get(new Integer (j+1)); 154 data[j][0] = (String )vector.get(0); 155 data[j][1] = (String )vector.get(0); 156 if (((String )vector.get(1)).equals("Dimension")){ 157 data[j][2] = new Boolean (false); 158 } else { 159 data[j][2] = new Boolean (true); 160 } 161 data[j][3] = new Boolean (true); 162 data[j][4] = getType(((Integer )vector.get(2)).intValue()); 163 } 164 } 165 private String getType(int type) { 166 switch(type){ 167 case 4 : return "INTEGER"; 168 case 6 : return "FLOAT"; 169 case 3: return "DECIMAL"; 170 case 16: return "BOOLEAN"; 171 case 91: return "DATETIME"; 172 default: return "STRING"; 173 } 174 } 175 176 public int getColumnCount() { 177 return columnNames.length; 178 } 179 public int getRowCount() { 180 return data.length; 181 } 182 public String getColumnName(int col) { 183 return columnNames[col]; 184 } 185 public Object getValueAt(int row, int col) { 186 return data[row][col]; 187 } 188 194 public Class getColumnClass(int c) { 195 return getValueAt(0, c).getClass(); 196 } 197 198 202 public boolean isCellEditable(int row, int col) { 203 if (col== 0 || col == 4 || (setEditableFalseToDimensions(row, col))) { 206 return false; 207 } else { 208 return true; 209 } 210 } 211 212 216 private boolean setEditableFalseToDimensions(int row , int col){ 217 if (col !=2){ 218 return false; 219 } 220 Vector vector = (Vector )map.get(new Integer (row + 1)); 221 String column = (String )vector.get(1); 222 return (column.equals("Dimension")? true : false); 223 } 224 public void setValueAt(Object value, int row, int col) { 225 if (DEBUG) { 226 System.out.println("Setting value at " + row + "," + col 227 + " to " + value 228 + " (an instance of " 229 + value.getClass() + ")"); 230 } 231 232 data[row][col] = value; 233 fireTableCellUpdated(row, col); 234 235 if (DEBUG) { 236 System.out.println("New value of data:"); 237 printDebugData(); 238 } 239 } 240 241 private void printDebugData() { 242 int numRows = getRowCount(); 243 int numCols = getColumnCount(); 244 245 for (int i=0; i < numRows; i++) { 246 System.out.print(" row " + i + ":"); 247 for (int j=0; j < numCols; j++) { 248 System.out.print(" " + data[i][j]); 249 } 250 System.out.println(); 251 } 252 System.out.println("--------------------------"); 253 } 254 } 255 256 class Listener implements ActionListener { 257 258 public void actionPerformed(ActionEvent e) { 259 if( e.getSource() == accept ) { 260 rowsLatestVersion = fillMapWithMatrix(); 261 setCancelled(false); 262 setVisible(false); 263 264 } 265 } 266 } 267 268 } | Popular Tags |