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 control panel for table panel.
016: *
017: * @author Sasa Bojanic
018: * @version 1.0
019: */
020: public class DTableControlPanel extends DControlPanel {
021: DButton buttonNew;
022: DButton buttonEdit;
023: DButton buttonDelete;
024:
025: public DTableControlPanel(DCollection myOwner, String title,
026: boolean isVertical, boolean hasBorder) {
027:
028: super (myOwner, title, isVertical, hasBorder);
029:
030: controlledPanel = ((DCollection) myOwner).getControlledPanel();
031:
032: String n = "New";
033: String e = "Edit";
034: String d = "Delete";
035:
036: Dimension dim = getPreferredDimension(new String[] { n, e, d });
037:
038: buttonNew = new DButton(n, "NewSmall", dim);
039: buttonEdit = new DButton(e, "EditSmall", dim);
040: buttonDelete = new DButton(d, "DeleteSmall", dim);
041: if (isVertical) {
042: add(Box.createRigidArea(new Dimension(0, 10)));
043: }
044: add(buttonNew);
045: add(Box.createRigidArea(new Dimension((isVertical) ? 0 : 5,
046: (isVertical) ? 5 : 0)));
047: add(buttonEdit);
048: add(Box.createRigidArea(new Dimension((isVertical) ? 0 : 5,
049: (isVertical) ? 5 : 0)));
050: add(buttonDelete);
051: if (isVertical) {
052: add(Box.createVerticalGlue());
053: }
054:
055: buttonNew.setEnabled(!myOwner.isReadOnly());
056: buttonDelete.setEnabled(!myOwner.isReadOnly());
057:
058: buttonNew.getInputMap(JComponent.WHEN_FOCUSED).put(
059: KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
060: "edit");
061: buttonNew.getActionMap().put("edit", new AbstractAction() {
062: public void actionPerformed(ActionEvent e) {
063: if (buttonNew.hasFocus()) {
064: buttonNew.doClick();
065: }
066: }
067: });
068:
069: buttonEdit.getInputMap(JComponent.WHEN_FOCUSED).put(
070: KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
071: "edit");
072: buttonEdit.getActionMap().put("edit", new AbstractAction() {
073: public void actionPerformed(ActionEvent e) {
074: if (buttonEdit.hasFocus()) {
075: buttonEdit.doClick();
076: }
077: }
078: });
079:
080: buttonDelete.getInputMap(JComponent.WHEN_FOCUSED).put(
081: KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
082: "edit");
083: buttonDelete.getActionMap().put("edit", new AbstractAction() {
084: public void actionPerformed(ActionEvent e) {
085: if (buttonDelete.hasFocus()) {
086: buttonDelete.doClick();
087: }
088: }
089: });
090:
091: // action listener for defining new element
092: buttonNew.addActionListener(new ActionListener() {
093: public void actionPerformed(ActionEvent ae) {
094: DSimpleElement newElement = ((DCollection) getOwner())
095: .generateNewElement();
096: Window w = getWindow();
097: DElementDialog de;
098: if (w instanceof JFrame) {
099: de = new DElementDialog((JFrame) w, newElement
100: .toName()
101: + " - Enter new");
102: } else {
103: de = new DElementDialog((JDialog) w, newElement
104: .toName()
105: + " - Enter new");
106: }
107: de.editDElement(newElement.getPanel(), true);
108: // if cancel hasn't been pressed
109: if (!de.isCanceled()) {
110: addElement((DCollectionElement) newElement);
111: ((DCollection) getOwner())
112: .onElementCreated(newElement);
113: } else {
114: ((DCollection) getOwner()).onActionCanceled(
115: newElement, 0);
116: }
117:
118: }
119: });
120:
121: // action listener for modifying selected element
122: buttonEdit.addActionListener(new ActionListener() {
123: public void actionPerformed(ActionEvent ae) {
124: DCollectionElement editElement = getSelectedElement();
125: if (editElement != null) {// && !editElement.isReadOnly()) {
126: Window w = getWindow();
127: DElementDialog de;
128: if (w instanceof JFrame) {
129: de = new DElementDialog((JFrame) w, editElement
130: .toName()
131: + " Editing");
132: } else {
133: de = new DElementDialog((JDialog) w,
134: editElement.toName() + " Editing");
135: }
136:
137: de.editDElement(editElement.getPanel(), true);
138: if (!de.isCanceled()) {
139: modifyElement(editElement);
140: ((DCollection) getOwner())
141: .onElementModified(editElement);
142: } else {
143: ((DCollection) getOwner()).onActionCanceled(
144: editElement, 1);
145: }
146:
147: } else {
148: complainLoudly();
149: }
150: ((DTablePanel) controlledPanel).getTable()
151: .requestFocus();
152: }
153: });
154:
155: // action listener for removing selected element
156: buttonDelete.addActionListener(new ActionListener() {
157: public void actionPerformed(ActionEvent ae) {
158: DCollectionElement deleteElement = getSelectedElement();
159: if (deleteElement == null) {
160: complainLoudly();
161: } else {
162: int yn = JOptionPane
163: .showConfirmDialog(
164: getWindow(),
165: "Do you really want to delete selected item?",
166: deleteElement.toName()
167: + " Deleting",
168: JOptionPane.YES_NO_OPTION);
169: if (yn == JOptionPane.YES_OPTION) {
170: try {
171: ((DCollection) getOwner())
172: .onElementDeleted(deleteElement);
173: removeElement(deleteElement);
174: } catch (AssertionDataObjectException ex) {
175: JOptionPane
176: .showMessageDialog(
177: getWindow(),
178: "Read-only cache: delete not allowed.",
179: "Disc error",
180: JOptionPane.ERROR_MESSAGE);
181: } catch (Exception ex) {
182: JOptionPane
183: .showMessageDialog(
184: getWindow(),
185: "Something went wrong, please try again !",
186: "Disc error",
187: JOptionPane.ERROR_MESSAGE);
188: }
189: }
190: }
191: }
192: });
193: }
194:
195: public DCollectionElement getSelectedElement() {
196: return ((DTablePanel) controlledPanel).getSelectedElement();
197: }
198:
199: private void addElement(DCollectionElement e) {
200: ((DCollection) getOwner()).add(e);
201: JTable t = ((DTablePanel) controlledPanel).getTable();
202: DefaultTableModel dtm = (DefaultTableModel) t.getModel();
203: Vector v = new Vector(((DCollectionElement) e)
204: .toComplexTypeValues());
205: dtm.addRow(v);
206: try {
207: t.setRowSelectionInterval(t.getRowCount() - 1, t
208: .getRowCount() - 1);
209: } catch (Exception ex) {
210: }
211: t.requestFocus();
212: }
213:
214: private void modifyElement(DCollectionElement e) {
215: ((DTablePanel) controlledPanel).modifyElement(e);
216: }
217:
218: private void removeElement(DCollectionElement e) {
219: ((DTablePanel) controlledPanel).removeElement(e);
220: }
221:
222: /**
223: * Pops message into message dialog.
224: */
225: public void complainLoudly() {
226: JOptionPane.showMessageDialog(getWindow(),
227: "Please select valid element", "JtaDiscRack",
228: JOptionPane.WARNING_MESSAGE);
229: }
230: }
|