update dev300-m58
[ooovba.git] / odk / examples / java / Inspector / MethodParametersDialog.java
blobf37ba1961ce44d631aac66706a43d76636fb4379
1 /*************************************************************************
3 * $RCSfile: MethodParametersDialog.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: hr $ $Date: 2007-07-31 13:57:21 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.reflection.ParamInfo;
42 import com.sun.star.reflection.XIdlMethod;
43 import com.sun.star.uno.TypeClass;
44 import java.awt.BorderLayout;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.KeyAdapter;
48 import java.awt.event.KeyEvent;
49 import java.awt.event.WindowAdapter;
50 import java.awt.event.WindowEvent;
51 import java.util.Vector;
52 import javax.swing.JButton;
53 import javax.swing.JComboBox;
54 import javax.swing.JComponent;
55 import javax.swing.JDialog;
56 import javax.swing.JFrame;
57 import javax.swing.JLabel;
58 import javax.swing.JPanel;
59 import javax.swing.JTextField;
61 /**
62 * @author bc93774
64 public class MethodParametersDialog extends JDialog{
65 private javax.swing.JPanel jPnlParamContainer;
66 private ParameterPanel[] m_aParameterPanels;
67 private ParamInfo[] m_aParamInfo;
68 private XIdlMethod m_xIdlMethod;
69 private ActionListener oActionListener;
70 private JButton jHelpButton = new JButton("Help");
71 private JButton jOKButton = new JButton("Ok");
72 private JButton jInvokeButton = new JButton("Invoke");
73 private Object m_oReturnButton = null;
74 private Object m_oUnoObject = null;
75 private Object m_oUnoReturnObject = null;
76 private JLabel jLblResult;
77 private JPanel jResultPanel = null;
78 private boolean bisdiposed = false;
79 private XUnoMethodNode m_xUnoMethodNode;
82 public MethodParametersDialog(XUnoMethodNode _xUnoMethodNode){
83 m_xUnoMethodNode = _xUnoMethodNode;
84 m_xIdlMethod = _xUnoMethodNode.getXIdlMethod();
85 m_aParamInfo = m_xIdlMethod.getParameterInfos();
86 m_oUnoObject = m_xUnoMethodNode.getUnoObject();
87 Object[] m_aParameterObjects = new Object[m_aParamInfo.length];
91 public Vector getMethodObjects() {
92 super.setModal(true);
93 addBorderPanel(getContentPane(), BorderLayout.NORTH);
94 addBorderPanel(getContentPane(), BorderLayout.WEST);
95 addBorderPanel(getContentPane(), BorderLayout.EAST);
96 jPnlParamContainer = new JPanel();
97 jPnlParamContainer.setLayout(new javax.swing.BoxLayout(jPnlParamContainer, javax.swing.BoxLayout.Y_AXIS));
98 JPanel jHeaderPanel = new JPanel(new BorderLayout());
99 JLabel jLblHeader = new JLabel();
100 jLblHeader.setText("Please insert the values for the given Parameters of the method '" + m_xIdlMethod.getName() + "'");
101 jHeaderPanel.add(jLblHeader,BorderLayout.WEST);
102 jPnlParamContainer.add(jHeaderPanel);
104 m_aParameterPanels = new ParameterPanel[m_aParamInfo.length];
105 for (int i = 0; i < m_aParameterPanels.length; i++){
106 m_aParameterPanels[i] = new ParameterPanel(m_aParamInfo[i]);
107 jPnlParamContainer.add(m_aParameterPanels[i]);
109 jPnlParamContainer.add(new ButtonPanel(), java.awt.BorderLayout.SOUTH);
110 getContentPane().add(jPnlParamContainer, java.awt.BorderLayout.CENTER);
111 pack();
112 setLocation(350, 350);
113 setTitle("Object Inspector - Parameter Values of '" + m_xIdlMethod.getName() + "'");
114 super.setFocusable(true);
115 super.setFocusableWindowState(true);
116 super.requestFocus();
117 m_aParameterPanels[0].getInputComponent().requestFocusInWindow();
118 setVisible(true);
119 if (!bisdiposed){
120 Vector aMethodObjects = new Vector();
121 for (int i = 0; i < m_aParameterPanels.length; i++){
122 aMethodObjects.add(m_aParameterPanels[i].getValue());
124 aMethodObjects.add(m_oUnoReturnObject);
125 return aMethodObjects;
127 else{
128 return null;
133 private void insertResultPanel(Exception _oInvocationException){
134 boolean bAddPanel = false;
135 if (jResultPanel == null){
136 jResultPanel = new JPanel(new BorderLayout());
137 bAddPanel = true;
139 else{
140 jResultPanel.removeAll();
142 jLblResult = new JLabel();
143 jLblResult.setMaximumSize(new java.awt.Dimension(getSize().width - 20, 57));
144 if (_oInvocationException != null){
145 jLblResult.setText("<html>Invoking the method cause an exception: <br>" + _oInvocationException.toString() + "</html>");
147 else{
148 jLblResult.setText("<html>The invocation of the method did not produce any error</html>");
150 jResultPanel.add(jLblResult,BorderLayout.WEST);
151 if (bAddPanel){
152 int nPos = jPnlParamContainer.getComponentCount() - 1;
153 jPnlParamContainer.add(jResultPanel, nPos);
155 super.pack();
156 super.validate();
160 private Object[] getParameterValues(){
161 Object[] oParameterValues = new Object[m_aParameterPanels.length];
162 for (int i = 0; i < m_aParameterPanels.length; i++){
163 oParameterValues[i] = m_aParameterPanels[i].getValue();
165 return oParameterValues;
169 private boolean isCompleted(){
170 boolean bIsCompleted = true;
171 for (int i = 0; i < m_aParameterPanels.length; i++){
172 bIsCompleted = m_aParameterPanels[i].isCompleted();
173 if (!bIsCompleted){
174 break;
177 return bIsCompleted;
181 private void addBorderPanel(java.awt.Container _jContainer, String _sLayout){
182 JPanel jPnlBorder = new JPanel();
183 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10));
184 _jContainer.add(jPnlBorder, _sLayout);
187 private void addGapPanel(java.awt.Container _jContainer){
188 JPanel jPnlBorder = new JPanel();
189 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10));
190 jPnlBorder.setMaximumSize(new java.awt.Dimension(10, 10));
191 _jContainer.add(jPnlBorder);
195 private class ParameterPanel extends JPanel{
196 private JComponent m_jComponent;
197 private TypeClass m_aTypeClass = null;
199 public ParameterPanel(ParamInfo _aParamInfo){
200 JTextField jTextField = new JTextField();
201 JComboBox jComboBox = new JComboBox();
202 m_aTypeClass = _aParamInfo.aType.getTypeClass();
203 setLayout(new java.awt.BorderLayout());
204 addBorderPanel(this, BorderLayout.NORTH);
205 addBorderPanel(this, BorderLayout.SOUTH);
206 JPanel jPnlCenter1 = new javax.swing.JPanel();
207 jPnlCenter1.setLayout(new javax.swing.BoxLayout(jPnlCenter1, javax.swing.BoxLayout.X_AXIS));
208 JLabel jLabel1 = new JLabel();
209 jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
210 String sParamText = _aParamInfo.aName + " (" + _aParamInfo.aType.getName() +")";
211 jLabel1.setText(sParamText);
212 jPnlCenter1.add(jLabel1);
213 addGapPanel(jPnlCenter1);
214 switch (m_aTypeClass.getValue()){
215 case TypeClass.BOOLEAN_value:
216 jComboBox.setBackground(new java.awt.Color(255, 255, 255));
217 jComboBox.setPreferredSize(new java.awt.Dimension(50, 19));
218 jComboBox.addItem("True");
219 jComboBox.addItem("False");
220 jComboBox.addKeyListener(new UpdateUIAdapter());
221 jPnlCenter1.add(jComboBox);
222 m_jComponent = jComboBox;
223 break;
224 case TypeClass.BYTE_value:
225 case TypeClass.CHAR_value:
226 case TypeClass.DOUBLE_value:
227 case TypeClass.ENUM_value:
228 case TypeClass.FLOAT_value:
229 case TypeClass.HYPER_value:
230 case TypeClass.LONG_value:
231 case TypeClass.SHORT_value:
232 case TypeClass.STRING_value:
233 case TypeClass.UNSIGNED_HYPER_value:
234 case TypeClass.UNSIGNED_LONG_value:
235 case TypeClass.UNSIGNED_SHORT_value:
236 jTextField.setPreferredSize(new java.awt.Dimension(50, 19));
237 jTextField.addKeyListener(new UpdateUIAdapter());
238 jPnlCenter1.add(jTextField);
239 m_jComponent = jTextField;
240 break;
241 default:
242 System.out.println("Type " + m_aTypeClass.getValue() + " not yet defined in 'ParameterPanel()'");
244 add(jPnlCenter1, java.awt.BorderLayout.CENTER);
245 JPanel jPnlEast = new JPanel();
246 add(jPnlEast, BorderLayout.EAST);
249 private JComponent getInputComponent(){
250 return m_jComponent;
253 public Object getValue(){
254 Object oReturn = null;
255 if (m_jComponent instanceof JTextField){
256 String sText = ((JTextField) m_jComponent).getText();
257 oReturn = Introspector.getIntrospector().getValueOfText(m_aTypeClass, sText);
259 else{
260 JComboBox jComboBox = ((JComboBox) m_jComponent);
261 oReturn = Boolean.valueOf(jComboBox.getSelectedIndex() == 0);
263 return oReturn;
267 public boolean isCompleted(){
268 if (m_jComponent instanceof JTextField){
269 return !((JTextField) m_jComponent).getText().equals("");
271 else{
272 return true;
278 private class UpdateUIAdapter extends KeyAdapter{
279 public void keyReleased(KeyEvent e){
280 boolean bIsCompleted = isCompleted();
281 jOKButton.setEnabled(bIsCompleted);
282 jInvokeButton.setEnabled(bIsCompleted);
283 if (jLblResult != null){
284 jLblResult.setEnabled(false);
285 jLblResult.invalidate();
292 private class ButtonPanel extends JPanel{
293 public ButtonPanel(){
294 super();
295 setLayout(new BorderLayout());
296 addBorderPanel(this, BorderLayout.NORTH);
297 addBorderPanel(this, BorderLayout.SOUTH);
298 JPanel jPnlBottomCenter = new JPanel();
299 jPnlBottomCenter.setLayout(new javax.swing.BoxLayout(jPnlBottomCenter, javax.swing.BoxLayout.X_AXIS));
300 jHelpButton.addActionListener(new ActionListener() {
301 public void actionPerformed(ActionEvent e) {
302 oActionListener.actionPerformed(e);
304 });
305 jHelpButton.setEnabled(oActionListener != null);
306 jPnlBottomCenter.add(jHelpButton);
307 addGapPanel(jPnlBottomCenter);
308 jOKButton.setEnabled(false);
309 jOKButton.addActionListener(new ActionListener() {
310 public void actionPerformed(ActionEvent e) {
311 invokeParameterMethod();
312 dispose();
314 });
315 jOKButton.setEnabled(isCompleted());
316 jInvokeButton.setEnabled(isCompleted());
317 jInvokeButton.addActionListener(new ActionListener() {
318 public void actionPerformed(ActionEvent e) {
319 invokeParameterMethod();
323 jPnlBottomCenter.add(jOKButton);
324 addGapPanel(jPnlBottomCenter);
325 jPnlBottomCenter.add(jInvokeButton);
326 addGapPanel(jPnlBottomCenter);
327 JButton jCancelButton = new JButton("Cancel");
328 jCancelButton.setFocusCycleRoot(true);
329 jCancelButton.setFocusPainted(true);
330 jCancelButton.addActionListener(new ActionListener(){
331 public void actionPerformed(java.awt.event.ActionEvent evt) {
332 bisdiposed = true;
333 dispose();
337 jPnlBottomCenter.add(jCancelButton);
338 add(jPnlBottomCenter);
343 public void addActionListener(ActionListener _oActionListener){
344 oActionListener = _oActionListener;
345 jHelpButton.setEnabled(oActionListener != null);
349 public void invokeParameterMethod(){
350 try{
351 Object[] oParameters = getParameterValues();
352 m_oUnoReturnObject = m_xUnoMethodNode.invoke(m_oUnoObject, oParameters);
353 insertResultPanel(null);
354 } catch (Exception ex) {
355 insertResultPanel(ex);
356 m_oUnoReturnObject = null;