KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > presentation > dpanels > DTablePanel


1 package discRack.presentation.dpanels;
2
3 import discRack.*;
4 import discRack.presentation.*;
5 import discRack.presentation.delements.*;
6
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.table.*;
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import org.enhydra.dods.exceptions.AssertionDataObjectException;
14 /**
15  * Creates a table panel.
16  *
17  * @author Sasa Bojanic
18  * @version 1.0
19  */

20 public class DTablePanel extends DPanel {
21
22    public DTablePanel (DCollection myOwner,String JavaDoc title,boolean hasBorder) {
23
24       super(myOwner,2,title,true,hasBorder);
25
26       // creating a table which do not allow cell editing
27
Vector columnNames=new Vector();
28
29       Collection c=myOwner.getElementStructure();
30       Iterator it=c.iterator();
31       while (it.hasNext()) {
32          DSimpleElement el=(DSimpleElement)it.next();
33          columnNames.add(el.toName());
34       }
35
36       JTable allItems=new JTable(new Vector(),columnNames) {
37          public boolean isCellEditable(int row, int col) {
38             return false;
39          }
40
41          public Class JavaDoc getColumnClass(int c) {
42             return getValueAt(0, c).getClass();
43          }
44
45       };
46
47       // setting the first column (ID column) to be invisible
48
TableColumnModel tcm=allItems.getColumnModel();
49       TableColumn column=allItems.getColumnModel().getColumn(0);
50       column.setMinWidth(0);
51       column.setMaxWidth(0);
52       column.setPreferredWidth(0);
53       column.setResizable(false);
54
55       // setting some table properties
56
allItems.setColumnSelectionAllowed(false);
57       allItems.setRowSelectionAllowed(true);
58       allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
59       allItems.getTableHeader().setReorderingAllowed(false);
60
61       // fills Table
62
DefaultTableModel dtm=(DefaultTableModel)allItems.getModel();
63       it=myOwner.getTableElements().iterator();
64       while (it.hasNext()) {
65          DCollectionElement elem=(DCollectionElement)it.next();
66          Vector v=new Vector(elem.toComplexTypeValues());
67          dtm.addRow(v);
68       }
69
70       // mouse listener for editing on double-click
71
allItems.addMouseListener(new MouseAdapter() {
72          public void mouseClicked (MouseEvent me) {
73             if (me.getClickCount()>1) {
74                editElementDialog();
75             }
76          }
77       });
78
79       allItems.getInputMap(JComponent.WHEN_FOCUSED).
80             put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"edit");
81       allItems.getActionMap().put("edit",new AbstractAction() {
82          public void actionPerformed(ActionEvent e) {
83             editElementDialog();
84          }
85       });
86
87       allItems.getInputMap(JComponent.WHEN_FOCUSED).
88             put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0,false),"delete");
89       allItems.getActionMap().put("delete",new AbstractAction() {
90          public void actionPerformed(ActionEvent e) {
91             DCollectionElement deleteElement=getSelectedElement();
92             int yn=JOptionPane.showConfirmDialog(getWindow(),
93                   "Do you really want to delete selected item?",
94                   deleteElement.toName()+" Deleting",
95                   JOptionPane.YES_NO_OPTION);
96             if (yn==JOptionPane.YES_OPTION) {
97
98                try {
99                   ((DCollection)getOwner()).onElementDeleted(deleteElement);
100                   removeElement(deleteElement);
101                } catch (AssertionDataObjectException ex) {
102                   JOptionPane.showMessageDialog(getWindow(),
103                         "Read-only cache: delete not allowed.","Disc error",
104                         JOptionPane.ERROR_MESSAGE);
105                } catch (Exception JavaDoc ex) {
106                   JOptionPane.showMessageDialog(getWindow(),
107                         "Something went wrong, please try again !","Disc error",
108                         JOptionPane.ERROR_MESSAGE);
109                }
110             }
111          }
112       });
113
114
115       // creates panel
116
JScrollPane allItemsPane=new JScrollPane();
117       allItemsPane.setViewportView(allItems);
118       Dimension tDim;
119       tDim=new Dimension(tableDimension);
120
121       allItems.setPreferredScrollableViewportSize(tDim);
122
123       add(allItemsPane);
124       add(Box.createVerticalGlue());
125    }
126
127    public JTable getTable () {
128       JScrollPane jsp=(JScrollPane)getComponent(0);
129       JViewport jvp=(JViewport)jsp.getComponent(0);
130       JTable jt=(JTable)jvp.getComponent(0);
131       return jt;
132    }
133
134    public DCollectionElement getSelectedElement () {
135       JTable t=getTable();
136       int row=t.getSelectedRow();
137       if (row>=0) {
138          String JavaDoc ID=(String JavaDoc)t.getValueAt(row, 0);
139          Iterator it=((DCollection)getOwner()).getTableElements().iterator();
140          while (it.hasNext()) {
141             DCollectionElement e=(DCollectionElement)it.next();
142             if (e.getID().equals(ID)) {
143                return e;
144             }
145          }
146       }
147       return null;
148
149    }
150
151    public void modifyElement(DCollectionElement e) {
152       JTable t=getTable();
153       int row=t.getSelectedRow();
154       DefaultTableModel dtm=(DefaultTableModel)t.getModel();
155       Vector v=new Vector(((DCollectionElement)e).toComplexTypeValues());
156       dtm.removeRow(row);
157       dtm.insertRow(row,v);
158       try {
159          t.setRowSelectionInterval(row,row);
160       } catch (Exception JavaDoc ex) {}
161    }
162
163    public void removeElement(DCollectionElement e) {
164       ((DCollection)getOwner()).remove(getSelectedElement());
165       JTable t=getTable();
166       int row=t.getSelectedRow();
167       DefaultTableModel dtm=(DefaultTableModel)t.getModel();
168       dtm.removeRow(row);
169       try {
170          if (row==0) {
171             row++;
172          }
173          t.setRowSelectionInterval(row-1,row-1);
174       } catch (Exception JavaDoc ex) {}
175       t.requestFocus();
176    }
177
178    private void editElementDialog () {
179       DCollectionElement editElement=getSelectedElement();
180       if (editElement != null) {
181          Window w=getWindow();
182          DElementDialog de;
183          if (w instanceof JFrame) {
184             de=new DElementDialog((JFrame)w,editElement.toName()+" Editing");
185          } else {
186             de=new DElementDialog((JDialog)w,editElement.toName()+" Editing");
187          }
188          de.editDElement(editElement.getPanel(),true);
189          if (!de.isCanceled()) {
190             modifyElement(editElement);
191             ((DCollection)getOwner()).onElementModified(editElement);
192          }
193       } else {
194          JOptionPane.showMessageDialog(getWindow(),
195                "Please select valid element!",
196                "DiscRack",
197                JOptionPane.WARNING_MESSAGE);
198       }
199       getTable().requestFocus();
200    }
201
202 }
203
Popular Tags