001: /*
002: * SkeletonPropertyEditor.java
003: *
004: * Created on 14 août 2003, 00:47
005: */
006:
007: package net.charabia.jsmoothgen.skeleton;
008:
009: import javax.swing.table.*;
010: import javax.swing.*;
011: import java.util.*;
012: import java.awt.*;
013:
014: /**
015: *
016: * @author Rodrigo
017: */
018: public class SkeletonPropertyEditor extends javax.swing.JPanel {
019: private SkeletonPropertyEditor.SkelPropTableModel m_model;
020: private java.util.Vector m_columnNames = new Vector();
021:
022: public static class SkelPropTableModel extends AbstractTableModel {
023: Vector m_props = new Vector();
024:
025: public SkelPropTableModel(SkeletonProperty[] props) {
026: for (int i = 0; i < props.length; i++) {
027: m_props.add(props[i]);
028: }
029: }
030:
031: public SkelPropTableModel() {
032: m_props.add(new SkeletonProperty());
033: }
034:
035: public void add(SkeletonProperty prop) {
036: m_props.addElement(prop);
037: fireTableRowsInserted(m_props.size() - 1, m_props.size());
038: fireTableStructureChanged();
039: }
040:
041: public void add(SkeletonProperty prop, int row) {
042: m_props.insertElementAt(prop, row);
043: fireTableRowsInserted(row, row);
044: fireTableStructureChanged();
045: }
046:
047: public void removeRow(int row) {
048: m_props.removeElementAt(row);
049: fireTableRowsDeleted(row, row);
050: }
051:
052: public Class getColumnClass(int columnIndex) {
053: return Object.class;
054: }
055:
056: public String getColumnName(int column) {
057: return "TEST";
058: }
059:
060: public int getColumnCount() {
061: return 5;
062: }
063:
064: public int getRowCount() {
065: return m_props.size();
066: }
067:
068: public boolean isCellEditable(int rowIndex, int columnIndex) {
069: return true;
070: }
071:
072: public Object getValueAt(int rowIndex, int columnIndex) {
073: SkeletonProperty sp = (SkeletonProperty) m_props
074: .get(rowIndex);
075: switch (columnIndex) {
076: case 0:
077: return sp.getIdName();
078: case 1:
079: return sp.getLabel();
080: case 2:
081: return sp.getDescription();
082: case 3:
083: return sp.getType();
084: case 4:
085: return sp.getValue();
086: default:
087: return "";
088: }
089: }
090:
091: public void setValueAt(Object aValue, int rowIndex,
092: int columnIndex) {
093: SkeletonProperty sp = (SkeletonProperty) m_props
094: .get(rowIndex);
095: switch (columnIndex) {
096: case 0:
097: sp.setIdName(aValue.toString());
098: break;
099: case 1:
100: sp.setLabel(aValue.toString());
101: break;
102: case 2:
103: sp.setDescription(aValue.toString());
104: break;
105: case 3:
106: sp.setType(aValue.toString());
107: break;
108: case 4:
109: sp.setValue(aValue.toString());
110: break;
111: }
112: }
113:
114: }
115:
116: public String getColumnName(int columnIndex) {
117: switch (columnIndex) {
118: case 0:
119: return "Id";
120: case 1:
121: return "Label";
122: case 2:
123: return "Description";
124: case 3:
125: return "Type";
126: case 4:
127: return "Value";
128: default:
129: return "pouet";
130: }
131: }
132:
133: /** Creates new form BeanForm */
134: public SkeletonPropertyEditor() {
135: m_model = new SkeletonPropertyEditor.SkelPropTableModel();
136: initComponents();
137: m_table.setModel(m_model);
138: }
139:
140: public void setModel(SkeletonProperty[] sprops) {
141: if (sprops == null)
142: sprops = new SkeletonProperty[0];
143:
144: SkeletonPropertyEditor.SkelPropTableModel model = new SkeletonPropertyEditor.SkelPropTableModel(
145: sprops);
146: m_table.setModel(model);
147: m_model = model;
148: m_table.createDefaultColumnsFromModel();
149:
150: // model.addColumn("DescriptiveString");
151: // model.addColumn("DescriptiveString");
152: // model.addColumn("DescriptiveString");
153: // model.addColumn("DescriptiveString");
154: // model.addColumn("DescriptiveString");
155:
156: TableColumn type = m_table.getColumnModel().getColumn(3);
157: type.setHeaderValue("POUET");
158: JComboBox typeCombo = new JComboBox();
159: typeCombo.addItem(SkeletonProperty.TYPE_STRING);
160: typeCombo.addItem(SkeletonProperty.TYPE_TEXTAREA);
161: typeCombo.addItem(SkeletonProperty.TYPE_BOOLEAN);
162: type.setCellEditor(new DefaultCellEditor(typeCombo));
163: }
164:
165: /** This method is called from within the constructor to
166: * initialize the form.
167: * WARNING: Do NOT modify this code. The content of this method is
168: * always regenerated by the Form Editor.
169: */
170: private void initComponents()//GEN-BEGIN:initComponents
171: {
172: m_popupMenu = new javax.swing.JPopupMenu();
173: m_menuAddItem = new javax.swing.JMenuItem();
174: m_menuRemoveItem = new javax.swing.JMenuItem();
175: jScrollPane1 = new javax.swing.JScrollPane();
176: m_table = new javax.swing.JTable();
177:
178: m_menuAddItem.setText("Add item");
179: m_menuAddItem
180: .addActionListener(new java.awt.event.ActionListener() {
181: public void actionPerformed(
182: java.awt.event.ActionEvent evt) {
183: m_menuAddItemActionPerformed(evt);
184: }
185: });
186:
187: m_popupMenu.add(m_menuAddItem);
188:
189: m_menuRemoveItem.setText("RemoveItems");
190: m_menuRemoveItem
191: .addActionListener(new java.awt.event.ActionListener() {
192: public void actionPerformed(
193: java.awt.event.ActionEvent evt) {
194: m_menuRemoveItemActionPerformed(evt);
195: }
196: });
197:
198: m_popupMenu.add(m_menuRemoveItem);
199:
200: setLayout(new java.awt.BorderLayout());
201:
202: jScrollPane1.setBorder(null);
203: m_table.setModel(new javax.swing.table.DefaultTableModel(
204: new Object[][] { { null, null, null, null },
205: { null, null, null, null },
206: { null, null, null, null },
207: { null, null, null, null } }, new String[] {
208: "Title 1", "Title 2", "Title 3", "Title 4" }));
209: m_table.setTableHeader(null);
210: m_table.addMouseListener(new java.awt.event.MouseAdapter() {
211: public void mouseClicked(java.awt.event.MouseEvent evt) {
212: tableMousePressed(evt);
213: }
214:
215: public void mousePressed(java.awt.event.MouseEvent evt) {
216: tableMousePressed(evt);
217: }
218:
219: public void mouseReleased(java.awt.event.MouseEvent evt) {
220: tableMousePressed(evt);
221: }
222: });
223:
224: jScrollPane1.setViewportView(m_table);
225:
226: add(jScrollPane1, java.awt.BorderLayout.CENTER);
227:
228: }//GEN-END:initComponents
229:
230: private void m_menuRemoveItemActionPerformed(
231: java.awt.event.ActionEvent evt)//GEN-FIRST:event_m_menuRemoveItemActionPerformed
232: {//GEN-HEADEREND:event_m_menuRemoveItemActionPerformed
233: // Add your handling code here:
234: int selr = m_table.getSelectedRow();
235: if (selr != -1) {
236: m_model.removeRow(selr);
237: }
238: }//GEN-LAST:event_m_menuRemoveItemActionPerformed
239:
240: private void m_menuAddItemActionPerformed(
241: java.awt.event.ActionEvent evt)//GEN-FIRST:event_m_menuAddItemActionPerformed
242: {//GEN-HEADEREND:event_m_menuAddItemActionPerformed
243: // Add your handling code here:
244: SkeletonProperty prop = new SkeletonProperty();
245: int selr = m_table.getSelectedRow();
246: if (selr >= 0)
247: m_model.add(prop, selr);
248: else
249: m_model.add(prop);
250: }//GEN-LAST:event_m_menuAddItemActionPerformed
251:
252: private void tableMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tableMousePressed
253: {//GEN-HEADEREND:event_tableMousePressed
254: System.out.println("pressed !");
255: // Add your handling code here:
256: if (evt.isPopupTrigger()) {
257: System.out.println("trigger !");
258: showPopupMenu(evt);
259: }
260: }//GEN-LAST:event_tableMousePressed
261:
262: public void showPopupMenu(java.awt.event.MouseEvent evt) {
263: int r = m_table.rowAtPoint(new Point(evt.getX(), evt.getY()));
264: if (r != -1) {
265: m_table.setRowSelectionInterval(r, r);
266: }
267: m_popupMenu.show(this , evt.getX(), evt.getY());
268: }
269:
270: // Variables declaration - do not modify//GEN-BEGIN:variables
271: private javax.swing.JScrollPane jScrollPane1;
272: private javax.swing.JMenuItem m_menuAddItem;
273: private javax.swing.JMenuItem m_menuRemoveItem;
274: private javax.swing.JPopupMenu m_popupMenu;
275: private javax.swing.JTable m_table;
276: // End of variables declaration//GEN-END:variables
277:
278: }
|