KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* XMLListChoiceControlPanel.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 list choice panel.
23 */

24 public class XMLListChoiceControlPanel extends XMLControlPanel {
25    private XMLCollection mySelection;
26
27    public XMLListChoiceControlPanel (XMLCollection myOwner,
28       XMLCollection mySel,String JavaDoc title,boolean isVertical,boolean hasBorder) {
29
30       super(myOwner,title,isVertical,hasBorder);
31
32       this.mySelection=mySel;
33       controlledPanel=((XMLCollection)myOwner).getControlledPanel();
34
35       String JavaDoc a=XMLUtil.getLanguageDependentString("AddKey");
36       String JavaDoc r=XMLUtil.getLanguageDependentString("RemoveKey");
37       Dimension dim=getPreferredDimension(new String JavaDoc[]{a,r});
38
39       final XMLButton buttonAdd=new XMLButton(a,"Add",dim);
40       final XMLButton buttonRemove=new XMLButton(r,"Remove",dim);
41
42       if (isVertical) {
43          add(Box.createRigidArea(new Dimension(0,10)));
44       }
45       buttonAdd.setEnabled(!myOwner.isReadOnly());
46       buttonRemove.setEnabled(!myOwner.isReadOnly());
47       add(buttonAdd);
48       add(Box.createRigidArea(new Dimension((isVertical)? 0:5,(isVertical)? 5:0)));
49       add(buttonRemove);
50       if (isVertical) {
51          add(Box.createRigidArea(new Dimension(0,10)));
52       }
53
54       buttonRemove.addActionListener(new ActionListener() {
55          public void actionPerformed(ActionEvent e) {
56             JList l=((XMLListChoicePanel)controlledPanel).getList();
57             Object JavaDoc obj=null;
58             int ind=0;
59             try {
60                obj=l.getSelectedValue();
61                ind=l.getSelectedIndex();
62             } catch (Exception JavaDoc ex) {}
63
64             if (obj!=null) {
65                if (((XMLElement)obj).isRequired()) {
66                   JOptionPane.showMessageDialog(getDialog(),
67                      XMLUtil.getLanguageDependentString("ErrorCannotRemoveMandatoryProperty"),
68                      XMLUtil.getLanguageDependentString("Title"),JOptionPane.ERROR_MESSAGE);
69                   return;
70                }
71                else {
72                   int yn=JOptionPane.showConfirmDialog(getDialog(),
73                         XMLUtil.getLanguageDependentString(
74                         "MessageDoYouReallyWantToRemoveSelectedItem"),
75                         ((XMLElement)obj).toLabel()+" - "+
76                         XMLUtil.getLanguageDependentString("DeletingKey"),
77                         JOptionPane.YES_NO_OPTION);
78                   if (yn==JOptionPane.YES_OPTION) {
79                      DefaultListModel listModel=(DefaultListModel)l.getModel();
80                      listModel.removeElement(obj);
81                      ((XMLCollection)getOwner()).remove(obj);
82                      ((XMLCollection)getOwner()).onElementDeleted((XMLElement)obj);
83                   } else {
84                      l.requestFocus();
85                      return;
86                   }
87                }
88             } else {
89                JOptionPane.showMessageDialog(getDialog(),
90                   XMLUtil.getLanguageDependentString("WarningEmptySelectionToEditOrDelete"),
91                   XMLUtil.getLanguageDependentString("Title"),
92                   JOptionPane.WARNING_MESSAGE);
93             }
94             try {
95                if (ind==0) {
96                   ind++;
97                }
98                l.setSelectedIndex(ind-1);
99             } catch (Exception JavaDoc ex) {}
100
101             l.requestFocus();
102          }
103       });
104
105       buttonAdd.addActionListener(new ActionListener() {
106          public void actionPerformed(ActionEvent e) {
107             JList l=((XMLListChoicePanel)controlledPanel).getList();
108             DefaultListModel listModel=(DefaultListModel)l.getModel();
109             int size = listModel.getSize();
110
111             XMLElement[] choices=new XMLElement[mySelection.getChoosable().size()];
112             mySelection.getChoosable().toArray(choices);
113             XMLChoice ch=new XMLChoice("Select",choices) {
114                public XMLPanel getPanel () {
115                   return new XMLComboButtonPanel(this,mySelection);
116                }
117             };
118             ch.setRequired(true);
119             String JavaDoc label=mySelection.toLabel()+" - "+XMLUtil.getLanguageDependentString("SelectKey");
120             XMLElementDialog de=new XMLElementDialog(getDialog(),label);
121             de.editXMLElement(ch.getPanel(),true,false);
122             // if cancel hasn't been pressed
123
if (!de.isCanceled()) {
124                XMLElement newElement=((XMLCollection)getOwner()).generateNewElement();
125                newElement.setValue(ch.getChoosen());
126                listModel.addElement(newElement);
127                l.setSelectedIndex(size);
128                ((XMLCollection)getOwner()).add(newElement);
129                ((XMLCollection)getOwner()).onElementCreated(newElement);
130             }
131             l.requestFocus();
132          }
133       });
134    }
135
136 }
137
138
Popular Tags