KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > properties > PrincipalTableModel


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PrincipalTableModel.java
26  *
27  * Created on June 27, 2002, 4:50 PM
28  */

29
30 package com.sun.enterprise.tools.common.properties;
31
32 import java.util.*;
33 import javax.swing.*;
34 import java.awt.*;
35 import java.awt.event.*;
36 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
37
38 /**
39  * @author shirleyc
40  */

41 public class PrincipalTableModel extends javax.swing.table.AbstractTableModel JavaDoc{
42
43     Vector principals = null;
44     
45     private static java.util.ResourceBundle JavaDoc bundle =
46         java.util.ResourceBundle.getBundle("com.sun.enterprise.tools.common.properties.Bundle"); //NOI18N
47

48     /** Creates new RoleMapElementTableModel */
49     public PrincipalTableModel(Vector values) {
50         if (values == null)
51             this.principals = new Vector();
52         else
53             this.principals = (Vector)values.clone();
54     }
55
56     public java.lang.Object JavaDoc getValueAt(int row, int column) {
57         if (row < principals.size())
58             return ((String JavaDoc[])principals.elementAt(row))[column];
59         else
60             return ""; //NOI18N
61
}
62     
63     public int getRowCount() {
64         return principals.size() + 1;
65     }
66     
67     public int getColumnCount() {
68         return 2;
69     }
70     
71     public Vector getPrincipals() {
72         return principals;
73     }
74     
75     public boolean isCellEditable(int row, int col) {
76         return true;
77     }
78     
79     public void setValueAt(Object JavaDoc val, int row, int col) {
80 // System.out.println("val = " + (String)val + ", row = " + new Integer(row) + ", col = " + new Integer(col));
81

82         int pre = principals.size();
83         if (row >= pre) {
84             String JavaDoc[] newPrincipal = new String JavaDoc[2];
85             principals.add(newPrincipal);
86         }
87              
88         if (!(val instanceof String JavaDoc)) {
89             throw new IllegalArgumentException JavaDoc();
90         }
91         String JavaDoc input = (String JavaDoc)val;
92         if (col == 0 && (input == null || input.trim().length() == 0)) {
93             Reporter.info("row has no value (" + input + ")"); //NOI18N
94
principals.removeElementAt(row);
95         } else {
96             Reporter.info("(" + input.trim() + ")"); //NOI18N
97
((String JavaDoc[])principals.elementAt(row))[col] = input.trim();
98         }
99         
100         if (principals.size() < pre) {
101 // System.out.println("fireTableStructureChanged");
102
fireTableStructureChanged();
103         }
104     }
105     
106     public String JavaDoc getColumnName(int col) {
107         if (0 == col)
108             return bundle.getString("COL_HEADER_PRINCIPAL"); //NOI18N
109
if (1 == col)
110             return bundle.getString("COL_HEADER_DESCRIPTION"); //NOI18N
111

112         throw new RuntimeException JavaDoc(bundle.getString("COL_HEADER_ERR_ERR_ERR")); //NOI18N
113
}
114     
115     public static void main(String JavaDoc args[]) {
116         String JavaDoc[] principal = {"user-name", "description"}; //NOI18N
117
Vector principals = new Vector();
118         principals.add(principal);
119         javax.swing.JTable JavaDoc table = new javax.swing.JTable JavaDoc(new PrincipalTableModel(principals));
120         javax.swing.JScrollPane JavaDoc sp = new javax.swing.JScrollPane JavaDoc(table);
121 /*
122         javax.swing.JFrame f = new javax.swing.JFrame();
123         f.addWindowListener(new CloseTestWindow(principals));
124         f.getContentPane().add(sp);
125         f.show();
126  */

127         final JDialog d = new JDialog();
128         d.setSize(200, 150);
129         d.getContentPane().setLayout(new BorderLayout());
130         d.getContentPane().add(sp, BorderLayout.CENTER);
131         JButton okButton = new JButton("OK"); //NOI18N
132
okButton.addActionListener(new ActionListener() {
133             public void actionPerformed(ActionEvent ev) {
134                 d.setVisible(false);
135                 d.dispose();
136             }
137         });
138         JPanel buttonsPane = new JPanel();
139         buttonsPane.add(okButton);
140         d.getContentPane().add(buttonsPane, BorderLayout.SOUTH);
141         d.setVisible(true);
142     }
143     
144     static class CloseTestWindow extends java.awt.event.WindowAdapter JavaDoc {
145  
146         private Vector principals = null;
147         
148         public CloseTestWindow(Vector vec) {
149             principals = vec;
150         }
151         
152         public void windowClosing(java.awt.event.WindowEvent JavaDoc e) {
153             System.exit(0);
154         }
155     }
156     
157 }
158
Popular Tags