001: package discRack.presentation.dpanels;
002:
003: import discRack.*;
004: import discRack.presentation.*;
005: import discRack.presentation.delements.*;
006:
007: import java.util.*;
008: import javax.swing.*;
009: import javax.swing.table.*;
010: import java.awt.*;
011: import java.awt.event.*;
012:
013: import org.enhydra.dods.exceptions.AssertionDataObjectException;
014:
015: /**
016: * Creates a table panel.
017: *
018: * @author Sasa Bojanic
019: * @version 1.0
020: */
021: public class DTablePanel extends DPanel {
022:
023: public DTablePanel(DCollection myOwner, String title,
024: boolean hasBorder) {
025:
026: super (myOwner, 2, title, true, hasBorder);
027:
028: // creating a table which do not allow cell editing
029: Vector columnNames = new Vector();
030:
031: Collection c = myOwner.getElementStructure();
032: Iterator it = c.iterator();
033: while (it.hasNext()) {
034: DSimpleElement el = (DSimpleElement) it.next();
035: columnNames.add(el.toName());
036: }
037:
038: JTable allItems = new JTable(new Vector(), columnNames) {
039: public boolean isCellEditable(int row, int col) {
040: return false;
041: }
042:
043: public Class getColumnClass(int c) {
044: return getValueAt(0, c).getClass();
045: }
046:
047: };
048:
049: // setting the first column (ID column) to be invisible
050: TableColumnModel tcm = allItems.getColumnModel();
051: TableColumn column = allItems.getColumnModel().getColumn(0);
052: column.setMinWidth(0);
053: column.setMaxWidth(0);
054: column.setPreferredWidth(0);
055: column.setResizable(false);
056:
057: // setting some table properties
058: allItems.setColumnSelectionAllowed(false);
059: allItems.setRowSelectionAllowed(true);
060: allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
061: allItems.getTableHeader().setReorderingAllowed(false);
062:
063: // fills Table
064: DefaultTableModel dtm = (DefaultTableModel) allItems.getModel();
065: it = myOwner.getTableElements().iterator();
066: while (it.hasNext()) {
067: DCollectionElement elem = (DCollectionElement) it.next();
068: Vector v = new Vector(elem.toComplexTypeValues());
069: dtm.addRow(v);
070: }
071:
072: // mouse listener for editing on double-click
073: allItems.addMouseListener(new MouseAdapter() {
074: public void mouseClicked(MouseEvent me) {
075: if (me.getClickCount() > 1) {
076: editElementDialog();
077: }
078: }
079: });
080:
081: allItems.getInputMap(JComponent.WHEN_FOCUSED).put(
082: KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
083: "edit");
084: allItems.getActionMap().put("edit", new AbstractAction() {
085: public void actionPerformed(ActionEvent e) {
086: editElementDialog();
087: }
088: });
089:
090: allItems.getInputMap(JComponent.WHEN_FOCUSED).put(
091: KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false),
092: "delete");
093: allItems.getActionMap().put("delete", new AbstractAction() {
094: public void actionPerformed(ActionEvent e) {
095: DCollectionElement deleteElement = getSelectedElement();
096: int yn = JOptionPane.showConfirmDialog(getWindow(),
097: "Do you really want to delete selected item?",
098: deleteElement.toName() + " Deleting",
099: JOptionPane.YES_NO_OPTION);
100: if (yn == JOptionPane.YES_OPTION) {
101:
102: try {
103: ((DCollection) getOwner())
104: .onElementDeleted(deleteElement);
105: removeElement(deleteElement);
106: } catch (AssertionDataObjectException ex) {
107: JOptionPane
108: .showMessageDialog(
109: getWindow(),
110: "Read-only cache: delete not allowed.",
111: "Disc error",
112: JOptionPane.ERROR_MESSAGE);
113: } catch (Exception ex) {
114: JOptionPane
115: .showMessageDialog(
116: getWindow(),
117: "Something went wrong, please try again !",
118: "Disc error",
119: JOptionPane.ERROR_MESSAGE);
120: }
121: }
122: }
123: });
124:
125: // creates panel
126: JScrollPane allItemsPane = new JScrollPane();
127: allItemsPane.setViewportView(allItems);
128: Dimension tDim;
129: tDim = new Dimension(tableDimension);
130:
131: allItems.setPreferredScrollableViewportSize(tDim);
132:
133: add(allItemsPane);
134: add(Box.createVerticalGlue());
135: }
136:
137: public JTable getTable() {
138: JScrollPane jsp = (JScrollPane) getComponent(0);
139: JViewport jvp = (JViewport) jsp.getComponent(0);
140: JTable jt = (JTable) jvp.getComponent(0);
141: return jt;
142: }
143:
144: public DCollectionElement getSelectedElement() {
145: JTable t = getTable();
146: int row = t.getSelectedRow();
147: if (row >= 0) {
148: String ID = (String) t.getValueAt(row, 0);
149: Iterator it = ((DCollection) getOwner()).getTableElements()
150: .iterator();
151: while (it.hasNext()) {
152: DCollectionElement e = (DCollectionElement) it.next();
153: if (e.getID().equals(ID)) {
154: return e;
155: }
156: }
157: }
158: return null;
159:
160: }
161:
162: public void modifyElement(DCollectionElement e) {
163: JTable t = getTable();
164: int row = t.getSelectedRow();
165: DefaultTableModel dtm = (DefaultTableModel) t.getModel();
166: Vector v = new Vector(((DCollectionElement) e)
167: .toComplexTypeValues());
168: dtm.removeRow(row);
169: dtm.insertRow(row, v);
170: try {
171: t.setRowSelectionInterval(row, row);
172: } catch (Exception ex) {
173: }
174: }
175:
176: public void removeElement(DCollectionElement e) {
177: ((DCollection) getOwner()).remove(getSelectedElement());
178: JTable t = getTable();
179: int row = t.getSelectedRow();
180: DefaultTableModel dtm = (DefaultTableModel) t.getModel();
181: dtm.removeRow(row);
182: try {
183: if (row == 0) {
184: row++;
185: }
186: t.setRowSelectionInterval(row - 1, row - 1);
187: } catch (Exception ex) {
188: }
189: t.requestFocus();
190: }
191:
192: private void editElementDialog() {
193: DCollectionElement editElement = getSelectedElement();
194: if (editElement != null) {
195: Window w = getWindow();
196: DElementDialog de;
197: if (w instanceof JFrame) {
198: de = new DElementDialog((JFrame) w, editElement
199: .toName()
200: + " Editing");
201: } else {
202: de = new DElementDialog((JDialog) w, editElement
203: .toName()
204: + " Editing");
205: }
206: de.editDElement(editElement.getPanel(), true);
207: if (!de.isCanceled()) {
208: modifyElement(editElement);
209: ((DCollection) getOwner())
210: .onElementModified(editElement);
211: }
212: } else {
213: JOptionPane.showMessageDialog(getWindow(),
214: "Please select valid element!", "DiscRack",
215: JOptionPane.WARNING_MESSAGE);
216: }
217: getTable().requestFocus();
218: }
219:
220: }
|