KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > panels > XMLTableControlPanel


1 /* XMLTableControlPanel.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11
12 package org.enhydra.jawe.xml.panels;
13
14 import org.enhydra.jawe.xml.*;
15 import org.enhydra.jawe.xml.panels.tablesorting.*;
16
17 import java.util.*;
18 import javax.swing.*;
19 import javax.swing.table.*;
20 import java.awt.*;
21 import java.awt.event.*;
22
23 /**
24  * Creates a control panel for table panel.
25  */

26 public class XMLTableControlPanel extends XMLControlPanel {
27
28    public XMLTableControlPanel (XMLCollection myOwner,String JavaDoc title,
29                                 boolean isVertical,boolean hasBorder) {
30
31       super(myOwner,title,isVertical,hasBorder);
32
33       controlledPanel=((XMLCollection)myOwner).getControlledPanel();
34
35       String JavaDoc n=XMLUtil.getLanguageDependentString("NewLabel");
36       String JavaDoc e=XMLUtil.getLanguageDependentString("EditKey");
37       String JavaDoc d=XMLUtil.getLanguageDependentString("DeleteLabel");
38       //String m=XMLUtil.getLanguageDependentString("MakeCopyKey");
39

40       Dimension dim=getPreferredDimension(new String JavaDoc[]{n,e,d});
41
42       XMLButton buttonNew=new XMLButton(n,"NewSmall",dim);
43       XMLButton buttonEdit=new XMLButton(e,"EditCellSmall",dim);
44       XMLButton buttonDelete=new XMLButton(d,"DeleteSmall",dim);
45       //XMLButton buttonMakeCopy=new XMLButton(m,"CopySmall",dim);
46

47       if (isVertical) {
48          add(Box.createRigidArea(new Dimension(0,10)));
49       }
50       add(buttonNew);
51       add(Box.createRigidArea(new Dimension((isVertical)? 0:5,(isVertical)? 5:0)));
52       add(buttonEdit);
53       add(Box.createRigidArea(new Dimension((isVertical)? 0:10,(isVertical)? 10:0)));
54       add(buttonDelete);
55       //add(Box.createVerticalGlue());
56
//add(buttonMakeCopy);
57
add(Box.createVerticalGlue());
58
59       buttonNew.setEnabled(!myOwner.isReadOnly());
60       buttonDelete.setEnabled(!myOwner.isReadOnly());
61
62       // action listener for defining new element
63
buttonNew.addActionListener(new ActionListener() {
64                public void actionPerformed(ActionEvent ae) {
65                   XMLElement newElement=((XMLCollection)getOwner()).generateNewElement();
66                   XMLElementDialog de=new XMLElementDialog(getDialog(),
67                                                            newElement.toLabel()+" - "+
68                                                               XMLUtil.getLanguageDependentString("DefiningKey"));
69                   de.editXMLElement(newElement.getPanel(),true,false);
70                   // if cancel hasn't been pressed
71
if (!de.isCanceled()) {
72                      addElement(newElement);
73                      ((XMLCollection)getOwner()).onElementCreated(newElement);
74                   }
75                }
76             });
77
78       // action listener for modifying selected element
79
buttonEdit.addActionListener(new ActionListener() {
80                public void actionPerformed(ActionEvent ae) {
81                   XMLElement editElement=getSelectedElement();
82                   if (editElement != null) {// && !editElement.isReadOnly()) {
83
XMLElementDialog de=new XMLElementDialog(getDialog(),
84                                                               editElement.toLabel()+" '"+editElement.toString()+"' - "+
85                                                                  XMLUtil.getLanguageDependentString("EditingKey"));
86                      de.editXMLElement(editElement.getPanel(),true,false);
87                      if (!de.isCanceled()) {
88                         modifyElement(editElement);
89                         ((XMLCollection)getOwner()).onElementModified(editElement);
90                      }
91                   } else {
92                      complainLoudly("WarningEmptySelectionToEditOrDelete");
93                   }
94                   ((XMLTablePanel)controlledPanel).getTable().requestFocus();
95                }
96             });
97
98       // action listener for removing selected element
99
buttonDelete.addActionListener(new ActionListener() {
100                public void actionPerformed(ActionEvent ae) {
101                   XMLElement deleteElement=getSelectedElement();
102                   if (deleteElement == null) {
103                      complainLoudly("WarningEmptySelectionToEditOrDelete");
104                   } else if (deleteElement.isReadOnly()) {
105                      complainLoudly(((XMLCollection)getOwner()).
106                                        getReadOnlyMessageName(deleteElement));
107                   } else if (!((XMLCollection)getOwner()).canRemoveElement(deleteElement)) {
108                      complainLoudly(((XMLCollection)getOwner()).
109                                        getInUseMessageName(deleteElement));
110                   } else {
111                      int yn=JOptionPane.showConfirmDialog(getDialog(),
112                                                           XMLUtil.getLanguageDependentString(
113                                                              "MessageDoYouReallyWantToRemoveSelectedItem"),
114                                                           deleteElement.toLabel()+" - "+
115                                                              XMLUtil.getLanguageDependentString("DeletingKey"),
116                                                           JOptionPane.YES_NO_OPTION);
117                      if (yn==JOptionPane.YES_OPTION) {
118                         removeElement(deleteElement);
119                         ((XMLCollection)getOwner()).onElementDeleted(deleteElement);
120                         XMLElementDialog.notifyListeners(new XMLElement());
121                      }
122                   }
123                }
124             });
125
126       // action listener for removing selected element
127
/*buttonMakeCopy.addActionListener(new ActionListener() {
128                public void actionPerformed(ActionEvent ae) {
129                   XMLElement selElement=getSelectedElement();
130                   if (selElement == null) {
131                      complainLoudly("WarningEmptySelectionToEditOrDelete");
132                   } else {
133                      if (selElement instanceof XMLCollectionElement) {
134                         XMLElement copyElement=XMLUtil.makeCopyOfCollectionElement((XMLCollectionElement)selElement);
135                         addElement(copyElement);
136                         ((XMLCollection)getOwner()).onElementCreated(copyElement);
137                         XMLElementDialog.notifyListeners(new XMLElement());
138                      }
139                   }
140                }
141             });*/

142
143    }
144
145    public XMLElement getSelectedElement() {
146       return ((XMLTablePanel)controlledPanel).getSelectedElement();
147    }
148
149    private void addElement(XMLElement e) {
150       XMLCollection owncol=(XMLCollection)getOwner();
151       int rowpos=owncol.size();
152       owncol.add(e);
153       JTable t=((XMLTablePanel)controlledPanel).getTable();
154       DefaultTableModel dtm=(DefaultTableModel)t.getModel();
155       Vector v=null;
156       if (e instanceof XMLComplexElement) {
157          v=new Vector(((XMLComplexElement)e).toComplexTypeValues());
158       } else {
159          v=new Vector();
160          v.add(e.toString());
161       }
162
163       // add object itself to the first column (it is invisible column)
164
v.add(0,e);
165       dtm.insertRow(rowpos,v);
166       //dtm.addRow(v);
167
try {
168          //t.setRowSelectionInterval(t.getRowCount()-1,t.getRowCount()-1);
169
t.setRowSelectionInterval(rowpos,rowpos);
170       } catch (Exception JavaDoc ex) {}
171       t.requestFocus();
172       if (t instanceof SortingTable) {
173          ((SortingTable)t).performSorting(null);
174       }
175    }
176
177    private void modifyElement(XMLElement e) {
178       ((XMLTablePanel)controlledPanel).modifyElement(e);
179    }
180
181    private void removeElement(XMLElement e) {
182       ((XMLTablePanel)controlledPanel).removeElement(e);
183    }
184
185    /**
186     * Pops message into message dialog.
187     */

188    public void complainLoudly(String JavaDoc message) {
189       JOptionPane.showMessageDialog(getDialog(),
190                                     XMLUtil.getLanguageDependentString(message),
191                                     XMLUtil.getLanguageDependentString("Title"),
192                                     JOptionPane.WARNING_MESSAGE);
193    }
194 }
195
196
Popular Tags