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