KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* XMLListControlPanel.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
16 import java.util.*;
17 import javax.swing.*;
18 import java.awt.*;
19 import java.awt.event.*;
20
21 /**
22 * Creates a control panel for table panel.
23 */

24 public class XMLListControlPanel extends XMLControlPanel {
25
26    //public DTDListControlPanel (DTDCollection myOwner,DTDCollection defineNew,
27
public XMLListControlPanel (XMLCollection myOwner,
28       String JavaDoc title, boolean isVertical,boolean hasBorder,
29       boolean editButton) {
30
31       super(myOwner,title,isVertical,hasBorder);
32
33       controlledPanel=((XMLCollection)myOwner).getControlledPanel();
34
35       String JavaDoc a=XMLUtil.getLanguageDependentString("AddKey");
36       String JavaDoc r=XMLUtil.getLanguageDependentString("RemoveKey");
37       String JavaDoc e=XMLUtil.getLanguageDependentString("EditKey");
38
39       Dimension dim;
40       if (editButton) {
41          dim=getPreferredDimension(new String JavaDoc[]{a,r});
42       } else {
43          dim=getPreferredDimension(new String JavaDoc[]{a,r,e});
44       }
45
46       final XMLButton buttonAdd=new XMLButton(a,"Add",dim);
47       final XMLButton buttonRemove=new XMLButton(r,"Remove",dim);
48       final XMLButton buttonEdit=new XMLButton(e,"EditCellSmall",dim);
49
50       buttonAdd.setEnabled(!myOwner.isReadOnly());
51       buttonRemove.setEnabled(!myOwner.isReadOnly());
52       add(Box.createRigidArea(new Dimension(0,10)));
53       add(buttonAdd);
54       add(Box.createRigidArea(new Dimension(0,5)));
55       add(buttonRemove);
56       if (editButton) {
57          add(Box.createRigidArea(new Dimension(0,5)));
58          add(buttonEdit);
59       }
60
61       buttonRemove.addActionListener(new ActionListener() {
62          public void actionPerformed(ActionEvent e) {
63             JList l=((XMLListPanel)controlledPanel).getList();
64             Object JavaDoc obj=null;
65             int ind=0;
66             try {
67                obj=l.getSelectedValue();
68                ind=l.getSelectedIndex();
69             } catch (Exception JavaDoc ex) {}
70
71             if (obj!=null) {
72                if (((XMLCollection)getOwner()).canRemoveElement((XMLElement)obj)) {
73                   int yn=JOptionPane.showConfirmDialog(getDialog(),
74                         XMLUtil.getLanguageDependentString(
75                         "MessageDoYouReallyWantToRemoveSelectedItem"),
76                         ((XMLElement)obj).toLabel()+" - "+
77                         XMLUtil.getLanguageDependentString("DeletingKey"),
78                         JOptionPane.YES_NO_OPTION);
79                   if (yn==JOptionPane.YES_OPTION) {
80                      DefaultListModel listModel=(DefaultListModel)l.getModel();
81                      listModel.removeElement(obj);
82                      ((XMLCollection)getOwner()).remove(obj);
83                      ((XMLCollection)getOwner()).onElementRemoved((XMLElement)obj);
84                   } else {
85                      l.requestFocus();
86                      return;
87                   }
88                } else {
89                   String JavaDoc message="";
90                   if (obj instanceof XMLComplexElement) {
91                      message=XMLUtil.getLanguageDependentString(((XMLCollection)getOwner()).
92                         getInUseMessageName((XMLComplexElement)obj));
93                   }
94                   JOptionPane.showMessageDialog(getDialog(),
95                      message,XMLUtil.getLanguageDependentString("Title"),
96                      JOptionPane.ERROR_MESSAGE);
97                }
98             } else {
99                JOptionPane.showMessageDialog(getDialog(),
100                   XMLUtil.getLanguageDependentString("WarningEmptySelectionToEditOrDelete"),
101                   XMLUtil.getLanguageDependentString("Title"),
102                   JOptionPane.WARNING_MESSAGE);
103             }
104             try {
105                if (ind==0) {
106                   ind++;
107                }
108                l.setSelectedIndex(ind-1);
109             } catch (Exception JavaDoc ex) {}
110             l.requestFocus();
111          }
112       });
113
114       buttonAdd.addActionListener(new ActionListener() {
115          public void actionPerformed(ActionEvent e) {
116             JList l=((XMLListPanel)controlledPanel).getList();
117             DefaultListModel listModel=(DefaultListModel)l.getModel();
118             int size = listModel.getSize();
119
120             XMLElement newElement=((XMLCollection)getOwner()).generateNewElement();
121             String JavaDoc label=newElement.toLabel()+" - "+XMLUtil.getLanguageDependentString("SelectKey");
122             XMLElementDialog de=new XMLElementDialog(getDialog(),label);
123             de.editXMLElement(newElement.getPanel(),true,false);
124             // if cancel hasn't been pressed
125
if (!de.isCanceled()) {
126                if (!((XMLCollection)getOwner()).canInsertElement(newElement)) {
127                   return;
128                }
129                listModel.addElement(newElement); //.toValue());
130
l.setSelectedIndex(size);
131                ((XMLCollection)getOwner()).add(newElement);
132                ((XMLCollection)getOwner()).onElementInserted(newElement);
133             }
134             l.requestFocus();
135          }
136       });
137
138       // action listener for modifying selected element
139
buttonEdit.addActionListener(new ActionListener() {
140          public void actionPerformed(ActionEvent e) {
141             JList l=((XMLListPanel)controlledPanel).getList();
142             XMLElement editElement=null;
143             try {
144                editElement=(XMLElement)l.getSelectedValue();
145             } catch (Exception JavaDoc ex) {}
146
147             if (editElement!=null) {
148                XMLElementDialog de=new XMLElementDialog(getDialog(),
149                   editElement.toLabel()+" '"+editElement.toString()+"' - "+
150                   XMLUtil.getLanguageDependentString("EditingKey"));
151                de.editXMLElement(editElement.getPanel(),true,false);
152                if (!de.isCanceled()) {
153                   ((XMLCollection)getOwner()).onElementModified(editElement);
154                   controlledPanel.paintAll(controlledPanel.getGraphics());
155                }
156             } else {
157                JOptionPane.showMessageDialog(getDialog(),
158                   XMLUtil.getLanguageDependentString("WarningEmptySelectionToEditOrDelete"),
159                   XMLUtil.getLanguageDependentString("Title"),
160                   JOptionPane.WARNING_MESSAGE);
161             }
162             l.requestFocus();
163          }
164       });
165
166    }
167
168 }
169
170
Popular Tags