KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > xmlc > AntXMLCParamViewer


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

24
25 // ToolBox imports
26
import org.enhydra.tool.common.DialogPanel;
27 import org.enhydra.tool.common.SwingUtil;
28
29 // Kelp imports
30
import org.enhydra.kelp.common.PropUtil;
31 import org.enhydra.kelp.common.properties.XMLCParameterEditor;
32
33 // Standard imports
34
import java.awt.*;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.beans.Beans JavaDoc;
38 import java.util.Vector JavaDoc;
39 import javax.swing.*;
40 import javax.swing.event.ListSelectionEvent JavaDoc;
41 import javax.swing.event.ListSelectionListener JavaDoc;
42 import java.util.ResourceBundle JavaDoc;
43
44 //
45
public class AntXMLCParamViewer extends DialogPanel {
46
47     //
48
static ResourceBundle JavaDoc res =
49         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
50
private DefaultListModel listModel = null;
51     private LocalListListener listListener = null;
52     private LocalButtonListener buttonListener = null;
53     private BorderLayout layoutRoot = null;
54     private GridBagLayout layoutTab = null;
55     private GridBagLayout layoutEdit = null;
56     private GridBagLayout layoutControl = null;
57     private JTabbedPane tab = null;
58     private JPanel panelTab = null;
59     private JPanel panelEdit = null;
60     private JPanel panelControl = null;
61     private JButton buttonAdd = null;
62     private JButton buttonEdit = null;
63     private JButton buttonRemove = null;
64     private JButton buttonUp = null;
65     private JButton buttonDown = null;
66     private JButton buttonOK = null;
67     private JButton buttonCancel = null;
68     private JScrollPane scroll = null;
69     private JList list = null;
70
71     public static void main(String JavaDoc[] args) {
72         SwingUtil.setLookAndFeelToSystem();
73         AntXMLCParamViewer viewer = null;
74
75         viewer = new AntXMLCParamViewer();
76         viewer.showDialog();
77         System.exit(0);
78     }
79
80     public AntXMLCParamViewer() {
81         try {
82             jbInit();
83             pmInit();
84         } catch (Exception JavaDoc ex) {
85             ex.printStackTrace();
86         }
87     }
88
89 /*
90     public String getParameters() {
91         String out = new String();
92         String[] params = new String[0];
93
94         params = new String[listModel.getSize()];
95         for (int i = 0; i < params.length; i++) {
96             params[i] = listModel.getElementAt(i).toString();
97         }
98         params = PropUtil.cleanXMLCParameters(params);
99         out = PropUtil.XMLCParametersToString(params);
100         return out;
101     }
102 */

103     public String JavaDoc[] getParameters() {
104         String JavaDoc[] params = new String JavaDoc[0];
105
106         params = new String JavaDoc[listModel.getSize()];
107         for (int i = 0; i < params.length; i++) {
108             params[i] = listModel.getElementAt(i).toString();
109         }
110         params = PropUtil.cleanXMLCParameters(params);
111         return params;
112     }
113
114 /*
115     public void setParameters(String paramLine) {
116         String[] params = new String[0];
117
118         params = PropUtil.XMLCParametersToArray(paramLine);
119         params = PropUtil.cleanXMLCParameters(params);
120         listModel.removeAllElements();
121         for (int i = 0; i < params.length; i++) {
122             listModel.addElement(params[i]);
123         }
124         list.setModel(listModel);
125         list.updateUI();
126     }
127 */

128     public void setParameters(String JavaDoc[] paramLines) {
129         String JavaDoc[] params = PropUtil.cleanXMLCParameters(paramLines);
130         listModel.removeAllElements();
131         for (int i = 0; i < params.length; i++) {
132             listModel.addElement(params[i]);
133         }
134         list.setModel(listModel);
135         list.updateUI();
136     }
137
138     //
139
//
140
protected void clearDialog() {
141         super.clearDialog();
142     }
143
144     //
145
//
146
private void refreshEditButtons() {
147         int index = list.getSelectedIndex();
148
149         buttonRemove.setEnabled(index > -1);
150         buttonEdit.setEnabled(index > -1);
151         buttonUp.setEnabled(index > 0);
152         buttonDown.setEnabled((index > -1)
153                               && (index < (listModel.getSize() - 1)));
154     }
155
156     private void removeSelection() {
157         int index = list.getSelectedIndex();
158
159         if (index > -1) {
160             listModel.removeElementAt(index);
161         }
162     }
163
164     private void moveSelection(int direction) {
165         int index = list.getSelectedIndex();
166
167         if (index > -1) {
168             String JavaDoc selection = null;
169
170             selection = listModel.getElementAt(index).toString();
171             listModel.removeElementAt(index);
172             listModel.insertElementAt(selection, index + direction);
173         }
174     }
175
176     private void add() {
177         XMLCParameterEditor editor = null;
178         String JavaDoc selection = new String JavaDoc();
179
180         editor = new XMLCParameterEditor();
181         if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
182             selection = editor.getParameter();
183             if (selection.trim().length() > 0) {
184                 listModel.addElement(selection);
185                 list.setSelectedIndex(listModel.getSize() - 1);
186             }
187         }
188     }
189
190     private void editSelection() {
191         XMLCParameterEditor editor = null;
192         int index = list.getSelectedIndex();
193
194         if (index > -1) {
195             String JavaDoc selection = null;
196
197             selection = listModel.getElementAt(index).toString();
198             editor = new XMLCParameterEditor();
199             editor.setParameter(selection);
200             if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
201                 selection = editor.getParameter();
202                 listModel.setElementAt(selection, index);
203             }
204         }
205     }
206
207     private int showEditor(XMLCParameterEditor editor) {
208         int option = XMLCParameterEditor.CANCEL_OPTION;
209
210         editor.setOwner((Window) getTopLevelAncestor());
211         editor.setModal(true);
212         return editor.showDialog();
213     }
214
215     private void pmInit() {
216         listModel = new DefaultListModel();
217         listListener = new LocalListListener();
218         buttonListener = new LocalButtonListener();
219         list.addListSelectionListener(listListener);
220         list.requestDefaultFocus();
221         list.setModel(listModel);
222         list.setCellRenderer(new ToolTipCellRenderer());
223         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
224         buttonAdd.addActionListener(buttonListener);
225         buttonEdit.addActionListener(buttonListener);
226         buttonRemove.addActionListener(buttonListener);
227         buttonUp.addActionListener(buttonListener);
228         buttonDown.addActionListener(buttonListener);
229         buttonOK.addActionListener(buttonListener);
230         buttonCancel.addActionListener(buttonListener);
231         refreshEditButtons();
232         setTitle(res.getString("XMLC_Parameters"));
233         setModal(true);
234     }
235
236     private void jbInit() throws Exception JavaDoc {
237         layoutRoot =
238             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
239                                              BorderLayout.class.getName());
240         layoutTab =
241             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
242                                               GridBagLayout.class.getName());
243         layoutEdit =
244             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
245                                               GridBagLayout.class.getName());
246         layoutControl =
247             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
248                                               GridBagLayout.class.getName());
249         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
250                                               JTabbedPane.class.getName());
251         panelTab = (JPanel) Beans.instantiate(getClass().getClassLoader(),
252                                               JPanel.class.getName());
253         panelEdit = (JPanel) Beans.instantiate(getClass().getClassLoader(),
254                                                JPanel.class.getName());
255         panelControl = (JPanel) Beans.instantiate(getClass().getClassLoader(),
256                                                   JPanel.class.getName());
257         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
258                                                 JButton.class.getName());
259         buttonEdit = (JButton) Beans.instantiate(getClass().getClassLoader(),
260                                                  JButton.class.getName());
261         buttonRemove =
262             (JButton) Beans.instantiate(getClass().getClassLoader(),
263                                         JButton.class.getName());
264         buttonUp = (JButton) Beans.instantiate(getClass().getClassLoader(),
265                                                JButton.class.getName());
266         buttonDown = (JButton) Beans.instantiate(getClass().getClassLoader(),
267                                                  JButton.class.getName());
268         list = (JList) Beans.instantiate(getClass().getClassLoader(),
269                                          JList.class.getName());
270         buttonOK = (JButton) Beans.instantiate(getClass().getClassLoader(),
271                                                JButton.class.getName());
272         buttonCancel =
273             (JButton) Beans.instantiate(getClass().getClassLoader(),
274                                         JButton.class.getName());
275         buttonAdd.setPreferredSize(new Dimension(100, 27));
276         buttonAdd.setMnemonic('A');
277         buttonAdd.setText(res.getString("buttonAdd_Text"));
278         buttonEdit.setPreferredSize(new Dimension(100, 27));
279         buttonEdit.setMnemonic('E');
280         buttonEdit.setText(res.getString("Edit"));
281         buttonRemove.setPreferredSize(new Dimension(100, 27));
282         buttonRemove.setMnemonic('R');
283         buttonRemove.setText(res.getString("Remove"));
284         buttonUp.setPreferredSize(new Dimension(100, 27));
285         buttonUp.setMnemonic('U');
286         buttonUp.setText(res.getString("buttonUp_Text"));
287         buttonDown.setPreferredSize(new Dimension(100, 27));
288         buttonDown.setMnemonic('D');
289         buttonDown.setText(res.getString("buttonDown_Text"));
290         buttonOK.setPreferredSize(new Dimension(100, 27));
291         buttonOK.setText(res.getString("OK"));
292         buttonOK.setDefaultCapable(true);
293         buttonCancel.setPreferredSize(new Dimension(100, 27));
294         buttonCancel.setText(res.getString("Cancel"));
295         scroll = new JScrollPane(list);
296         panelEdit.setLayout(layoutEdit);
297         tab.setPreferredSize(new Dimension(400, 300));
298         panelEdit.add(buttonAdd,
299                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
300                                              GridBagConstraints.CENTER,
301                                              GridBagConstraints.NONE,
302                                              new Insets(5, 5, 5, 5), 0, 0));
303         panelEdit.add(buttonEdit,
304                       new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
305                                              GridBagConstraints.CENTER,
306                                              GridBagConstraints.NONE,
307                                              new Insets(5, 5, 5, 5), 0, 0));
308         panelEdit.add(buttonRemove,
309                       new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
310                                              GridBagConstraints.CENTER,
311                                              GridBagConstraints.NONE,
312                                              new Insets(5, 5, 20, 5), 0, 0));
313         panelEdit.add(buttonUp,
314                       new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
315                                              GridBagConstraints.CENTER,
316                                              GridBagConstraints.NONE,
317                                              new Insets(20, 5, 5, 5), 0, 0));
318         panelEdit.add(buttonDown,
319                       new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
320                                              GridBagConstraints.CENTER,
321                                              GridBagConstraints.NONE,
322                                              new Insets(5, 5, 5, 5), 0, 0));
323         panelTab.setLayout(layoutTab);
324         panelTab.add(scroll,
325                      new GridBagConstraints(0, 0, 1, 1, 0.3, 0.1,
326                                             GridBagConstraints.CENTER,
327                                             GridBagConstraints.BOTH,
328                                             new Insets(0, 0, 0, 0), 0, 0));
329         panelTab.add(panelEdit,
330                      new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1,
331                                             GridBagConstraints.CENTER,
332                                             GridBagConstraints.BOTH,
333                                             new Insets(5, 5, 5, 5), 0, 0));
334         tab.add(panelTab, res.getString("Parameters"));
335         panelControl.setLayout(layoutControl);
336         panelControl.add(buttonOK,
337                          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
338                                                 GridBagConstraints.CENTER,
339                                                 GridBagConstraints.NONE,
340                                                 new Insets(5, 5, 5, 5), 0,
341                                                 0));
342         panelControl.add(buttonCancel,
343                          new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
344                                                 GridBagConstraints.CENTER,
345                                                 GridBagConstraints.NONE,
346                                                 new Insets(5, 5, 5, 5), 0,
347                                                 0));
348         this.setLayout(layoutRoot);
349         this.add(tab, BorderLayout.NORTH);
350         this.add(panelControl, BorderLayout.SOUTH);
351     }
352
353     //
354
private class ToolTipCellRenderer extends DefaultListCellRenderer {
355         public Component getListCellRendererComponent(JList list,
356                 Object JavaDoc value, int index, boolean isSelected,
357                 boolean cellHasFocus) {
358             Component c = null;
359
360             c = super.getListCellRendererComponent(list, value, index,
361                                                    isSelected, cellHasFocus);
362             if (c instanceof JComponent) {
363                 JComponent jc = (JComponent) c;
364
365                 jc.setToolTipText(value.toString());
366             }
367             return c;
368         }
369
370     }
371
372     //
373
private class LocalButtonListener implements ActionListener JavaDoc {
374         public void actionPerformed(ActionEvent JavaDoc e) {
375             Object JavaDoc source = e.getSource();
376
377             if (source == buttonAdd) {
378                 add();
379             } else if (source == buttonEdit) {
380                 editSelection();
381             } else if (source == buttonRemove) {
382                 removeSelection();
383                 refreshEditButtons();
384             } else if (source == buttonUp) {
385                 moveSelection(-1);
386                 refreshEditButtons();
387             } else if (source == buttonDown) {
388                 moveSelection(1);
389                 refreshEditButtons();
390             } else if (source == buttonOK) {
391                 setOption(DialogPanel.OK_OPTION);
392                 clearDialog();
393             } else if (source == buttonCancel) {
394                 setOption(DialogPanel.CANCEL_OPTION);
395                 clearDialog();
396             }
397         }
398
399     }
400
401     //
402
private class LocalListListener implements ListSelectionListener JavaDoc {
403         public void valueChanged(ListSelectionEvent JavaDoc event) {
404             if (event.getValueIsAdjusting()) {
405
406                 // do nothing
407
} else {
408                 refreshEditButtons();
409             }
410         }
411
412     }
413 }
414
Popular Tags