001: package org.enhydra.kelp.ant.xmlc;
002:
003: /*
004: * Enhydra Java Application Server Project
005: *
006: * The contents of this file are subject to the Enhydra Public License
007: * Version 1.1 (the "License"); you may not use this file except in
008: * compliance with the License. You may obtain a copy of the License on
009: * the Enhydra web site ( http://www.enhydra.org/ ).
010: *
011: * Software distributed under the License is distributed on an "AS IS"
012: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
013: * the License for the specific terms governing rights and limitations
014: * under the License.
015: *
016: * The Initial Developer of the Enhydra Application Server is Lutris
017: * Technologies, Inc. The Enhydra Application Server and portions created
018: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s):
022: *
023: */
024:
025: // ToolBox imports
026: import org.enhydra.tool.common.DialogPanel;
027: import org.enhydra.tool.common.SwingUtil;
028:
029: // Kelp imports
030: import org.enhydra.kelp.common.PropUtil;
031: import org.enhydra.kelp.common.properties.XMLCParameterEditor;
032:
033: // Standard imports
034: import java.awt.*;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.beans.Beans;
038: import javax.swing.*;
039: import javax.swing.event.ListSelectionEvent;
040: import javax.swing.event.ListSelectionListener;
041: import java.util.ResourceBundle;
042:
043: //
044: public class AntXMLCParamViewer extends DialogPanel {
045:
046: //
047: static ResourceBundle res = ResourceBundle
048: .getBundle("org.enhydra.kelp.common.Res"); // nores
049: private DefaultListModel listModel = null;
050: private LocalListListener listListener = null;
051: private LocalButtonListener buttonListener = null;
052: private BorderLayout layoutRoot = null;
053: private GridBagLayout layoutTab = null;
054: private GridBagLayout layoutEdit = null;
055: private GridBagLayout layoutControl = null;
056: private JTabbedPane tab = null;
057: private JPanel panelTab = null;
058: private JPanel panelEdit = null;
059: private JPanel panelControl = null;
060: private JButton buttonAdd = null;
061: private JButton buttonEdit = null;
062: private JButton buttonRemove = null;
063: private JButton buttonUp = null;
064: private JButton buttonDown = null;
065: private JButton buttonOK = null;
066: private JButton buttonCancel = null;
067: private JScrollPane scroll = null;
068: private JList list = null;
069:
070: public static void main(String[] args) {
071: SwingUtil.setLookAndFeelToSystem();
072: AntXMLCParamViewer viewer = null;
073:
074: viewer = new AntXMLCParamViewer();
075: viewer.showDialog();
076: System.exit(0);
077: }
078:
079: public AntXMLCParamViewer() {
080: try {
081: jbInit();
082: pmInit();
083: } catch (Exception ex) {
084: ex.printStackTrace();
085: }
086: }
087:
088: /*
089: public String getParameters() {
090: String out = new String();
091: String[] params = new String[0];
092:
093: params = new String[listModel.getSize()];
094: for (int i = 0; i < params.length; i++) {
095: params[i] = listModel.getElementAt(i).toString();
096: }
097: params = PropUtil.cleanXMLCParameters(params);
098: out = PropUtil.XMLCParametersToString(params);
099: return out;
100: }
101: */
102: public String[] getParameters() {
103: String[] params = new String[0];
104:
105: params = new String[listModel.getSize()];
106: for (int i = 0; i < params.length; i++) {
107: params[i] = listModel.getElementAt(i).toString();
108: }
109: params = PropUtil.cleanXMLCParameters(params);
110: return params;
111: }
112:
113: /*
114: public void setParameters(String paramLine) {
115: String[] params = new String[0];
116:
117: params = PropUtil.XMLCParametersToArray(paramLine);
118: params = PropUtil.cleanXMLCParameters(params);
119: listModel.removeAllElements();
120: for (int i = 0; i < params.length; i++) {
121: listModel.addElement(params[i]);
122: }
123: list.setModel(listModel);
124: list.updateUI();
125: }
126: */
127: public void setParameters(String[] paramLines) {
128: String[] params = PropUtil.cleanXMLCParameters(paramLines);
129: listModel.removeAllElements();
130: for (int i = 0; i < params.length; i++) {
131: listModel.addElement(params[i]);
132: }
133: list.setModel(listModel);
134: list.updateUI();
135: }
136:
137: //
138: //
139: protected void clearDialog() {
140: super .clearDialog();
141: }
142:
143: //
144: //
145: private void refreshEditButtons() {
146: int index = list.getSelectedIndex();
147:
148: buttonRemove.setEnabled(index > -1);
149: buttonEdit.setEnabled(index > -1);
150: buttonUp.setEnabled(index > 0);
151: buttonDown.setEnabled((index > -1)
152: && (index < (listModel.getSize() - 1)));
153: }
154:
155: private void removeSelection() {
156: int index = list.getSelectedIndex();
157:
158: if (index > -1) {
159: listModel.removeElementAt(index);
160: }
161: }
162:
163: private void moveSelection(int direction) {
164: int index = list.getSelectedIndex();
165:
166: if (index > -1) {
167: String selection = null;
168:
169: selection = listModel.getElementAt(index).toString();
170: listModel.removeElementAt(index);
171: listModel.insertElementAt(selection, index + direction);
172: }
173: }
174:
175: private void add() {
176: XMLCParameterEditor editor = null;
177: String selection = new String();
178:
179: editor = new XMLCParameterEditor();
180: if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
181: selection = editor.getParameter();
182: if (selection.trim().length() > 0) {
183: listModel.addElement(selection);
184: list.setSelectedIndex(listModel.getSize() - 1);
185: }
186: }
187: }
188:
189: private void editSelection() {
190: XMLCParameterEditor editor = null;
191: int index = list.getSelectedIndex();
192:
193: if (index > -1) {
194: String selection = null;
195:
196: selection = listModel.getElementAt(index).toString();
197: editor = new XMLCParameterEditor();
198: editor.setParameter(selection);
199: if (showEditor(editor) == XMLCParameterEditor.OK_OPTION) {
200: selection = editor.getParameter();
201: listModel.setElementAt(selection, index);
202: }
203: }
204: }
205:
206: private int showEditor(XMLCParameterEditor editor) {
207: editor.setOwner((Window) getTopLevelAncestor());
208: editor.setModal(true);
209: return editor.showDialog();
210: }
211:
212: private void pmInit() {
213: listModel = new DefaultListModel();
214: listListener = new LocalListListener();
215: buttonListener = new LocalButtonListener();
216: list.addListSelectionListener(listListener);
217: list.requestDefaultFocus();
218: list.setModel(listModel);
219: list.setCellRenderer(new ToolTipCellRenderer());
220: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
221: buttonAdd.addActionListener(buttonListener);
222: buttonEdit.addActionListener(buttonListener);
223: buttonRemove.addActionListener(buttonListener);
224: buttonUp.addActionListener(buttonListener);
225: buttonDown.addActionListener(buttonListener);
226: buttonOK.addActionListener(buttonListener);
227: buttonCancel.addActionListener(buttonListener);
228: refreshEditButtons();
229: setTitle(res.getString("XMLC_Parameters"));
230: setModal(true);
231: }
232:
233: private void jbInit() throws Exception {
234: layoutRoot = (BorderLayout) Beans.instantiate(getClass()
235: .getClassLoader(), BorderLayout.class.getName());
236: layoutTab = (GridBagLayout) Beans.instantiate(getClass()
237: .getClassLoader(), GridBagLayout.class.getName());
238: layoutEdit = (GridBagLayout) Beans.instantiate(getClass()
239: .getClassLoader(), GridBagLayout.class.getName());
240: layoutControl = (GridBagLayout) Beans.instantiate(getClass()
241: .getClassLoader(), GridBagLayout.class.getName());
242: tab = (JTabbedPane) Beans.instantiate(getClass()
243: .getClassLoader(), JTabbedPane.class.getName());
244: panelTab = (JPanel) Beans.instantiate(getClass()
245: .getClassLoader(), JPanel.class.getName());
246: panelEdit = (JPanel) Beans.instantiate(getClass()
247: .getClassLoader(), JPanel.class.getName());
248: panelControl = (JPanel) Beans.instantiate(getClass()
249: .getClassLoader(), JPanel.class.getName());
250: buttonAdd = (JButton) Beans.instantiate(getClass()
251: .getClassLoader(), JButton.class.getName());
252: buttonEdit = (JButton) Beans.instantiate(getClass()
253: .getClassLoader(), JButton.class.getName());
254: buttonRemove = (JButton) Beans.instantiate(getClass()
255: .getClassLoader(), JButton.class.getName());
256: buttonUp = (JButton) Beans.instantiate(getClass()
257: .getClassLoader(), JButton.class.getName());
258: buttonDown = (JButton) Beans.instantiate(getClass()
259: .getClassLoader(), JButton.class.getName());
260: list = (JList) Beans.instantiate(getClass().getClassLoader(),
261: JList.class.getName());
262: buttonOK = (JButton) Beans.instantiate(getClass()
263: .getClassLoader(), JButton.class.getName());
264: buttonCancel = (JButton) Beans.instantiate(getClass()
265: .getClassLoader(), JButton.class.getName());
266: buttonAdd.setPreferredSize(new Dimension(100, 27));
267: buttonAdd.setMnemonic('A');
268: buttonAdd.setText(res.getString("buttonAdd_Text"));
269: buttonEdit.setPreferredSize(new Dimension(100, 27));
270: buttonEdit.setMnemonic('E');
271: buttonEdit.setText(res.getString("Edit"));
272: buttonRemove.setPreferredSize(new Dimension(100, 27));
273: buttonRemove.setMnemonic('R');
274: buttonRemove.setText(res.getString("Remove"));
275: buttonUp.setPreferredSize(new Dimension(100, 27));
276: buttonUp.setMnemonic('U');
277: buttonUp.setText(res.getString("buttonUp_Text"));
278: buttonDown.setPreferredSize(new Dimension(100, 27));
279: buttonDown.setMnemonic('D');
280: buttonDown.setText(res.getString("buttonDown_Text"));
281: buttonOK.setPreferredSize(new Dimension(100, 27));
282: buttonOK.setText(res.getString("OK"));
283: buttonOK.setDefaultCapable(true);
284: buttonCancel.setPreferredSize(new Dimension(100, 27));
285: buttonCancel.setText(res.getString("Cancel"));
286: scroll = new JScrollPane(list);
287: panelEdit.setLayout(layoutEdit);
288: tab.setPreferredSize(new Dimension(400, 300));
289: panelEdit.add(buttonAdd, new GridBagConstraints(0, 0, 1, 1,
290: 0.0, 0.0, GridBagConstraints.CENTER,
291: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
292: panelEdit.add(buttonEdit, new GridBagConstraints(0, 1, 1, 1,
293: 0.0, 0.0, GridBagConstraints.CENTER,
294: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
295: panelEdit
296: .add(buttonRemove, new GridBagConstraints(0, 2, 1, 1,
297: 0.0, 0.0, GridBagConstraints.CENTER,
298: GridBagConstraints.NONE,
299: new Insets(5, 5, 20, 5), 0, 0));
300: panelEdit
301: .add(buttonUp, new GridBagConstraints(0, 3, 1, 1, 0.0,
302: 0.0, GridBagConstraints.CENTER,
303: GridBagConstraints.NONE,
304: new Insets(20, 5, 5, 5), 0, 0));
305: panelEdit.add(buttonDown, new GridBagConstraints(0, 4, 1, 1,
306: 0.0, 0.0, GridBagConstraints.CENTER,
307: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
308: panelTab.setLayout(layoutTab);
309: panelTab.add(scroll, new GridBagConstraints(0, 0, 1, 1, 0.3,
310: 0.1, GridBagConstraints.CENTER,
311: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
312: panelTab.add(panelEdit, new GridBagConstraints(1, 0, 1, 1, 0.1,
313: 0.1, GridBagConstraints.CENTER,
314: GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
315: tab.add(panelTab, res.getString("Parameters"));
316: panelControl.setLayout(layoutControl);
317: panelControl.add(buttonOK, new GridBagConstraints(0, 0, 1, 1,
318: 0.0, 0.0, GridBagConstraints.CENTER,
319: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
320: panelControl.add(buttonCancel, new GridBagConstraints(1, 0, 1,
321: 1, 0.0, 0.0, GridBagConstraints.CENTER,
322: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 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 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 {
349: public void actionPerformed(ActionEvent e) {
350: Object 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 {
378: public void valueChanged(ListSelectionEvent event) {
379: if (event.getValueIsAdjusting()) {
380:
381: // do nothing
382: } else {
383: refreshEditButtons();
384: }
385: }
386:
387: }
388: }
|