KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > xmleditor > DataTypeTableFrame


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 JavaDoc;
9 import java.awt.event.ActionListener JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.*;
12 import java.util.Map JavaDoc;
13 import java.util.Vector JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  *
18  * User: eslosse
19  * Date: 28-sep-2005
20  * Time: 10:44:44
21
22  */

23 /*
24  *
25  * Created on 28 de septiembre de 2005, 10:39
26  */

27
28
29 /**
30  *
31  * @author eslosse
32  */

33 public class DataTypeTableFrame extends javax.swing.JDialog JavaDoc {
34
35   private javax.swing.JButton JavaDoc accept;
36   private javax.swing.JButton JavaDoc cancel;
37   private javax.swing.JDialog JavaDoc jDialog1;
38   private javax.swing.JLabel JavaDoc jLabel1;
39   private javax.swing.JPanel JavaDoc jPanel1;
40   private javax.swing.JScrollPane JavaDoc jScrollPane1;
41   private javax.swing.JTable JavaDoc jTable1;
42   private boolean DEBUG = false;
43   private Map JavaDoc map;
44   private Map JavaDoc rowsLatestVersion = new HashMap JavaDoc();
45   private boolean cancelled = false;
46
47     /** Creates new form TablePrueba */
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    /**
58     * Cargo un mapa con la matriz - modelo en su ultimo estado, para poder
59     * actualizar los cambios q el usuario le aplico a la tabla generada con metricas
60     * por default. por ej: cambio de descripcion, cambio de metrica a dimension,
61     * columna no visible, etc
62     * @return map
63     */

64     private Map JavaDoc fillMapWithMatrix(){
65      Map JavaDoc map = new HashMap JavaDoc();
66       for (int i = 0; i < jTable1.getRowCount(); i++) {
67            if ( ((Boolean JavaDoc)jTable1.getValueAt(i,3)).booleanValue()){
68               Vector JavaDoc vector = new Vector JavaDoc(3);
69               vector.add( (String JavaDoc)jTable1.getValueAt(i,1));
70               if ( !((Boolean JavaDoc)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 JavaDoc)jTable1.getValueAt(i,0));
78                 map.put(new Integer JavaDoc(i+1), vector);
79            }
80       }
81     return map;
82    }
83
84   /**
85    * Devuelve un mapa con el ultimo estado de la matriz con el siguiente
86    * formato : map - posicion -> [desc, ("metrica o dimension"), tipo(new Integer), Ext Data("String")]
87    * @return Map
88    */

89      public Map JavaDoc getRowsLatestVersion(){
90         return rowsLatestVersion;
91
92       }
93   /**
94    * Devuelve el valor de cancelled , indica si el boton "Cancelar" fue
95    * presionado o no
96    * @return boolean
97    */

98    public boolean isCancelled(){
99      return cancelled;
100    }
101     public DataTypeTableFrame(JFrame owner, Map JavaDoc map) {
102         super(owner,true);
103         this.map = map;
104         initComponents();
105     }
106
107     /** This method is called from within the constructor to
108      * initialize the form.
109      * WARNING: Do NOT modify this code. The content of this method is
110      * always regenerated by the Form Editor.
111      */

112     private void initComponents() {
113         this.setResizable(false);
114         jPanel1 = new javax.swing.JPanel JavaDoc();
115         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
116         jTable1 = new javax.swing.JTable JavaDoc(new MyTableModel(map));
117         accept = new javax.swing.JButton JavaDoc("Aceptar");
118         jLabel1 = new javax.swing.JLabel JavaDoc();
119         getContentPane().setLayout(new javax.swing.BoxLayout JavaDoc(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 JavaDoc());
133         pack();
134     }
135
136     class MyTableModel extends AbstractTableModel JavaDoc {
137       private Map JavaDoc map;
138       private String JavaDoc[] columnNames = {"Name/Ext Data",
139                                        "Description",
140                                        LanguageTraslator.traslate("546"),
141                                         LanguageTraslator.traslate("547") ,
142                                          LanguageTraslator.traslate("548") };
143       private Object JavaDoc[][] data;
144
145       public MyTableModel(Map JavaDoc map){
146           this.map = map;
147           this.fillMatrixWithMap();
148
149       }
150       private void fillMatrixWithMap(){
151           data = new Object JavaDoc[map.size()][5];
152            for (int j = 0; j < map.size(); j++){
153                   Vector JavaDoc vector = (Vector JavaDoc)map.get(new Integer JavaDoc(j+1));
154                      data[j][0] = (String JavaDoc)vector.get(0);
155                      data[j][1] = (String JavaDoc)vector.get(0);
156                   if (((String JavaDoc)vector.get(1)).equals("Dimension")){
157                      data[j][2] = new Boolean JavaDoc(false);
158                    } else {
159                      data[j][2] = new Boolean JavaDoc(true);
160                    }
161                      data[j][3] = new Boolean JavaDoc(true);
162                      data[j][4] = getType(((Integer JavaDoc)vector.get(2)).intValue());
163                   }
164         }
165       private String JavaDoc 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 JavaDoc getColumnName(int col) {
183             return columnNames[col];
184         }
185       public Object JavaDoc getValueAt(int row, int col) {
186             return data[row][col];
187         }
188       /*
189          * JTable uses this method to determine the default renderer/
190          * editor for each cell. If we didn't implement this method,
191          * then the last column would contain text ("true"/"false"),
192          * rather than a check box.
193          */

194         public Class JavaDoc getColumnClass(int c) {
195             return getValueAt(0, c).getClass();
196         }
197
198         /*
199          * Don't need to implement this method unless your table's
200          * editable.
201          */

202         public boolean isCellEditable(int row, int col) {
203             //Note that the data/cell address is constant,
204
//no matter where the cell appears onscreen.
205
if (col== 0 || col == 4 || (setEditableFalseToDimensions(row, col))) {
206                 return false;
207             } else {
208                 return true;
209             }
210         }
211
212         /*
213          * Don't need to implement this method unless your table's
214          * data can change.
215          */

216         private boolean setEditableFalseToDimensions(int row , int col){
217           if (col !=2){
218             return false;
219           }
220           Vector JavaDoc vector = (Vector JavaDoc)map.get(new Integer JavaDoc(row + 1));
221           String JavaDoc column = (String JavaDoc)vector.get(1);
222            return (column.equals("Dimension")? true : false);
223           }
224         public void setValueAt(Object JavaDoc 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 JavaDoc {
257
258       public void actionPerformed(ActionEvent JavaDoc e) {
259      if( e.getSource() == accept ) {
260             rowsLatestVersion = fillMapWithMatrix();
261             setCancelled(false);
262             setVisible(false);
263
264         }
265       }
266     }
267
268 }
Popular Tags