KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > properties > XMLCParameterViewer


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.kelp.common.properties;
23
24 // ToolBox imports
25
import org.enhydra.tool.common.DialogPanel;
26 import org.enhydra.tool.common.SwingUtil;
27
28 // Kelp imports
29
import org.enhydra.kelp.common.PropUtil;
30
31 // Standard imports
32
import java.awt.*;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.beans.Beans JavaDoc;
36 import java.util.Vector JavaDoc;
37 import javax.swing.*;
38 import javax.swing.event.ListSelectionEvent JavaDoc;
39 import javax.swing.event.ListSelectionListener JavaDoc;
40 import java.util.ResourceBundle JavaDoc;
41
42 //
43
public class XMLCParameterViewer extends DialogPanel {
44
45     //
46
static ResourceBundle JavaDoc res =
47         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
48
private DefaultListModel listModel = null;
49     private LocalListListener listListener = null;
50     private LocalButtonListener buttonListener = null;
51     private BorderLayout layoutRoot = null;
52     private GridBagLayout layoutTab = null;
53     private GridBagLayout layoutEdit = null;
54     private GridBagLayout layoutControl = null;
55     private JTabbedPane tab = null;
56     private JPanel panelTab = null;
57     private JPanel panelEdit = null;
58     private JPanel panelControl = null;
59     private JButton buttonAdd = null;
60     private JButton buttonEdit = null;
61     private JButton buttonRemove = null;
62     private JButton buttonUp = null;
63     private JButton buttonDown = null;
64     private JButton buttonOK = null;
65     private JButton buttonCancel = null;
66     private JScrollPane scroll = null;
67     private JList list = null;
68
69     public static void main(String JavaDoc[] args) {
70         SwingUtil.setLookAndFeelToSystem();
71         XMLCParameterViewer viewer = null;
72
73         viewer = new XMLCParameterViewer();
74         viewer.showDialog();
75         System.exit(0);
76     }
77
78     public XMLCParameterViewer() {
79         try {
80             jbInit();
81             pmInit();
82         } catch (Exception JavaDoc ex) {
83             ex.printStackTrace();
84         }
85     }
86
87     public String JavaDoc getParameters() {
88         String JavaDoc out = new String JavaDoc();
89         String JavaDoc[] params = new String JavaDoc[0];
90
91         params = new String JavaDoc[listModel.getSize()];
92         for (int i = 0; i < params.length; i++) {
93             params[i] = listModel.getElementAt(i).toString();
94         }
95         params = PropUtil.cleanXMLCParameters(params);
96         out = PropUtil.XMLCParametersToString(params);
97         return out;
98     }
99
100     public void setParameters(String JavaDoc paramLine) {
101         String JavaDoc[] params = new String JavaDoc[0];
102
103         params = PropUtil.XMLCParametersToArray(paramLine);
104         params = PropUtil.cleanXMLCParameters(params);
105         listModel.removeAllElements();
106         for (int i = 0; i < params.length; i++) {
107             listModel.addElement(params[i]);
108         }
109         list.setModel(listModel);
110         list.updateUI();
111     }
112
113     //
114
//
115
protected void clearDialog() {
116         super.clearDialog();
117     }
118
119     //
120
//
121
private void refreshEditButtons() {
122         int index = list.getSelectedIndex();
123
124         buttonRemove.setEnabled(index > -1);
125         buttonEdit.setEnabled(index > -1);
126         buttonUp.setEnabled(index > 0);
127         buttonDown.setEnabled((index > -1)
128                               && (index < (listModel.getSize() - 1)));
129     }
130
131     private void removeSelection() {
132         int index = list.getSelectedIndex();
133
134         if (index > -1) {
135             listModel.removeElementAt(index);
136         }
137     }
138
139     private void moveSelection(int direction) {
140         int index = list.getSelectedIndex();
141
142         if (index > -1) {
143             String JavaDoc selection = null;
144
145             selection = listModel.getElementAt(index).toString();
146             listModel.removeElementAt(index);
147             listModel.insertElementAt(selection, index + direction);
148         }
149     }
150
151     private void add() {
152         XMLCParameterEditor editor = null;
153         String JavaDoc selection = new String JavaDoc();
154
155         editor = new XMLCParameterEditor();
156         if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
157             selection = editor.getParameter();
158             if (selection.trim().length() > 0) {
159                 listModel.addElement(selection);
160                 list.setSelectedIndex(listModel.getSize() - 1);
161             }
162         }
163     }
164
165     private void editSelection() {
166         XMLCParameterEditor editor = null;
167         int index = list.getSelectedIndex();
168
169         if (index > -1) {
170             String JavaDoc selection = null;
171
172             selection = listModel.getElementAt(index).toString();
173             editor = new XMLCParameterEditor();
174             editor.setParameter(selection);
175             if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
176                 selection = editor.getParameter();
177                 listModel.setElementAt(selection, index);
178             }
179         }
180     }
181
182     private int showEditor(XMLCParameterEditor editor) {
183         int option = XMLCParameterEditor.CANCEL_OPTION;
184
185         editor.setOwner((Window) getTopLevelAncestor());
186         editor.setModal(true);
187         return editor.showDialog();
188     }
189
190     private void pmInit() {
191         listModel = new DefaultListModel();
192         listListener = new LocalListListener();
193         buttonListener = new LocalButtonListener();
194         list.addListSelectionListener(listListener);
195         list.requestDefaultFocus();
196         list.setModel(listModel);
197         list.setCellRenderer(new ToolTipCellRenderer());
198         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
199         buttonAdd.addActionListener(buttonListener);
200         buttonEdit.addActionListener(buttonListener);
201         buttonRemove.addActionListener(buttonListener);
202         buttonUp.addActionListener(buttonListener);
203         buttonDown.addActionListener(buttonListener);
204         buttonOK.addActionListener(buttonListener);
205         buttonCancel.addActionListener(buttonListener);
206         refreshEditButtons();
207         setTitle(res.getString("XMLC_Parameters"));
208         setModal(true);
209     }
210
211     private void jbInit() throws Exception JavaDoc {
212         layoutRoot =
213             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
214                                              BorderLayout.class.getName());
215         layoutTab =
216             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
217                                               GridBagLayout.class.getName());
218         layoutEdit =
219             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
220                                               GridBagLayout.class.getName());
221         layoutControl =
222             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
223                                               GridBagLayout.class.getName());
224         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
225                                               JTabbedPane.class.getName());
226         panelTab = (JPanel) Beans.instantiate(getClass().getClassLoader(),
227                                               JPanel.class.getName());
228         panelEdit = (JPanel) Beans.instantiate(getClass().getClassLoader(),
229                                                JPanel.class.getName());
230         panelControl = (JPanel) Beans.instantiate(getClass().getClassLoader(),
231                                                   JPanel.class.getName());
232         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
233                                                 JButton.class.getName());
234         buttonEdit = (JButton) Beans.instantiate(getClass().getClassLoader(),
235                                                  JButton.class.getName());
236         buttonRemove =
237             (JButton) Beans.instantiate(getClass().getClassLoader(),
238                                         JButton.class.getName());
239         buttonUp = (JButton) Beans.instantiate(getClass().getClassLoader(),
240                                                JButton.class.getName());
241         buttonDown = (JButton) Beans.instantiate(getClass().getClassLoader(),
242                                                  JButton.class.getName());
243         list = (JList) Beans.instantiate(getClass().getClassLoader(),
244                                          JList.class.getName());
245         buttonOK = (JButton) Beans.instantiate(getClass().getClassLoader(),
246                                                JButton.class.getName());
247         buttonCancel =
248             (JButton) Beans.instantiate(getClass().getClassLoader(),
249                                         JButton.class.getName());
250         buttonAdd.setPreferredSize(new Dimension(100, 27));
251         buttonAdd.setMnemonic('A');
252         buttonAdd.setText(res.getString("buttonAdd_Text"));
253         buttonEdit.setPreferredSize(new Dimension(100, 27));
254         buttonEdit.setMnemonic('E');
255         buttonEdit.setText(res.getString("Edit"));
256         buttonRemove.setPreferredSize(new Dimension(100, 27));
257         buttonRemove.setMnemonic('R');
258         buttonRemove.setText(res.getString("Remove"));
259         buttonUp.setPreferredSize(new Dimension(100, 27));
260         buttonUp.setMnemonic('U');
261         buttonUp.setText(res.getString("buttonUp_Text"));
262         buttonDown.setPreferredSize(new Dimension(100, 27));
263         buttonDown.setMnemonic('D');
264         buttonDown.setText(res.getString("buttonDown_Text"));
265         buttonOK.setPreferredSize(new Dimension(100, 27));
266         buttonOK.setText(res.getString("OK"));
267         buttonOK.setDefaultCapable(true);
268         buttonCancel.setPreferredSize(new Dimension(100, 27));
269         buttonCancel.setText(res.getString("Cancel"));
270         scroll = new JScrollPane(list);
271         panelEdit.setLayout(layoutEdit);
272         tab.setPreferredSize(new Dimension(400, 300));
273         panelEdit.add(buttonAdd,
274                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
275                                              GridBagConstraints.CENTER,
276                                              GridBagConstraints.NONE,
277                                              new Insets(5, 5, 5, 5), 0, 0));
278         panelEdit.add(buttonEdit,
279                       new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
280                                              GridBagConstraints.CENTER,
281                                              GridBagConstraints.NONE,
282                                              new Insets(5, 5, 5, 5), 0, 0));
283         panelEdit.add(buttonRemove,
284                       new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
285                                              GridBagConstraints.CENTER,
286                                              GridBagConstraints.NONE,
287                                              new Insets(5, 5, 20, 5), 0, 0));
288         panelEdit.add(buttonUp,
289                       new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
290                                              GridBagConstraints.CENTER,
291                                              GridBagConstraints.NONE,
292                                              new Insets(20, 5, 5, 5), 0, 0));
293         panelEdit.add(buttonDown,
294                       new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
295                                              GridBagConstraints.CENTER,
296                                              GridBagConstraints.NONE,
297                                              new Insets(5, 5, 5, 5), 0, 0));
298         panelTab.setLayout(layoutTab);
299         panelTab.add(scroll,
300                      new GridBagConstraints(0, 0, 1, 1, 0.3, 0.1,
301                                             GridBagConstraints.CENTER,
302                                             GridBagConstraints.BOTH,
303                                             new Insets(0, 0, 0, 0), 0, 0));
304         panelTab.add(panelEdit,
305                      new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1,
306                                             GridBagConstraints.CENTER,
307                                             GridBagConstraints.BOTH,
308                                             new Insets(5, 5, 5, 5), 0, 0));
309         tab.add(panelTab, res.getString("Parameters"));
310         panelControl.setLayout(layoutControl);
311         panelControl.add(buttonOK,
312                          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
313                                                 GridBagConstraints.CENTER,
314                                                 GridBagConstraints.NONE,
315                                                 new Insets(5, 5, 5, 5), 0,
316                                                 0));
317         panelControl.add(buttonCancel,
318                          new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
319                                                 GridBagConstraints.CENTER,
320                                                 GridBagConstraints.NONE,
321                                                 new Insets(5, 5, 5, 5), 0,
322                                                 0));
323         this.setLayout(layoutRoot);
324         this.add(tab, BorderLayout.NORTH);
325         this.add(panelControl, BorderLayout.SOUTH);
326     }
327
328     //
329
private class ToolTipCellRenderer extends DefaultListCellRenderer {
330         public Component getListCellRendererComponent(JList list,
331                 Object JavaDoc value, int index, boolean isSelected,
332                 boolean cellHasFocus) {
333             Component c = null;
334
335             c = super.getListCellRendererComponent(list, value, index,
336                                                    isSelected, cellHasFocus);
337             if (c instanceof JComponent) {
338                 JComponent jc = (JComponent) c;
339
340                 jc.setToolTipText(value.toString());
341             }
342             return c;
343         }
344
345     }
346
347     //
348
private class LocalButtonListener implements ActionListener JavaDoc {
349         public void actionPerformed(ActionEvent JavaDoc e) {
350             Object JavaDoc source = e.getSource();
351
352             if (source == buttonAdd) {
353                 add();
354             } else if (source == buttonEdit) {
355                 editSelection();
356             } else if (source == buttonRemove) {
357                 removeSelection();
358                 refreshEditButtons();
359             } else if (source == buttonUp) {
360                 moveSelection(-1);
361                 refreshEditButtons();
362             } else if (source == buttonDown) {
363                 moveSelection(1);
364                 refreshEditButtons();
365             } else if (source == buttonOK) {
366                 setOption(DialogPanel.OK_OPTION);
367                 clearDialog();
368             } else if (source == buttonCancel) {
369                 setOption(DialogPanel.CANCEL_OPTION);
370                 clearDialog();
371             }
372         }
373
374     }
375
376     //
377
private class LocalListListener implements ListSelectionListener JavaDoc {
378         public void valueChanged(ListSelectionEvent JavaDoc event) {
379             if (event.getValueIsAdjusting()) {
380
381                 // do nothing
382
} else {
383                 refreshEditButtons();
384             }
385         }
386
387     }
388 }
389
Popular Tags