Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Inspector / MethodParametersDialog.java
blob621e6228b5da18c584745ca0828ea8b7c42fe2cf
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import java.awt.BorderLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.KeyAdapter;
40 import java.awt.event.KeyEvent;
41 import java.util.ArrayList;
43 import javax.swing.JButton;
44 import javax.swing.JComboBox;
45 import javax.swing.JComponent;
46 import javax.swing.JDialog;
47 import javax.swing.JLabel;
48 import javax.swing.JPanel;
49 import javax.swing.JTextField;
51 import com.sun.star.reflection.ParamInfo;
52 import com.sun.star.reflection.XIdlMethod;
53 import com.sun.star.uno.TypeClass;
55 public class MethodParametersDialog extends JDialog{
56 private javax.swing.JPanel jPnlParamContainer;
57 private ParameterPanel[] m_aParameterPanels;
58 private ParamInfo[] m_aParamInfo;
59 private XIdlMethod m_xIdlMethod;
60 private ActionListener oActionListener;
61 private JButton jHelpButton = new JButton("Help");
62 private JButton jOKButton = new JButton("Ok");
63 private JButton jInvokeButton = new JButton("Invoke");
64 private Object m_oUnoObject = null;
65 private Object m_oUnoReturnObject = null;
66 private JLabel jLblResult;
67 private JPanel jResultPanel = null;
68 private boolean bisdiposed = false;
69 private XUnoMethodNode m_xUnoMethodNode;
72 public MethodParametersDialog(XUnoMethodNode _xUnoMethodNode){
73 m_xUnoMethodNode = _xUnoMethodNode;
74 m_xIdlMethod = _xUnoMethodNode.getXIdlMethod();
75 m_aParamInfo = m_xIdlMethod.getParameterInfos();
76 m_oUnoObject = m_xUnoMethodNode.getUnoObject();
80 public ArrayList<Object> getMethodObjects() {
81 super.setModal(true);
82 addBorderPanel(getContentPane(), BorderLayout.NORTH);
83 addBorderPanel(getContentPane(), BorderLayout.WEST);
84 addBorderPanel(getContentPane(), BorderLayout.EAST);
85 jPnlParamContainer = new JPanel();
86 jPnlParamContainer.setLayout(new javax.swing.BoxLayout(jPnlParamContainer, javax.swing.BoxLayout.Y_AXIS));
87 JPanel jHeaderPanel = new JPanel(new BorderLayout());
88 JLabel jLblHeader = new JLabel();
89 jLblHeader.setText("Please insert the values for the given Parameters of the method '" + m_xIdlMethod.getName() + "'");
90 jHeaderPanel.add(jLblHeader,BorderLayout.WEST);
91 jPnlParamContainer.add(jHeaderPanel);
93 m_aParameterPanels = new ParameterPanel[m_aParamInfo.length];
94 for (int i = 0; i < m_aParameterPanels.length; i++){
95 m_aParameterPanels[i] = new ParameterPanel(m_aParamInfo[i]);
96 jPnlParamContainer.add(m_aParameterPanels[i]);
98 jPnlParamContainer.add(new ButtonPanel(), java.awt.BorderLayout.SOUTH);
99 getContentPane().add(jPnlParamContainer, java.awt.BorderLayout.CENTER);
100 pack();
101 setLocation(350, 350);
102 setTitle("Object Inspector - Parameter Values of '" + m_xIdlMethod.getName() + "'");
103 super.setFocusable(true);
104 super.setFocusableWindowState(true);
105 super.requestFocus();
106 m_aParameterPanels[0].getInputComponent().requestFocusInWindow();
107 setVisible(true);
108 if (!bisdiposed){
109 ArrayList<Object> aMethodObjects = new ArrayList<Object>();
110 for (int i = 0; i < m_aParameterPanels.length; i++){
111 aMethodObjects.add(m_aParameterPanels[i].getValue());
113 aMethodObjects.add(m_oUnoReturnObject);
114 return aMethodObjects;
116 else{
117 return null;
122 private void insertResultPanel(Exception _oInvocationException){
123 boolean bAddPanel = false;
124 if (jResultPanel == null){
125 jResultPanel = new JPanel(new BorderLayout());
126 bAddPanel = true;
128 else{
129 jResultPanel.removeAll();
131 jLblResult = new JLabel();
132 jLblResult.setMaximumSize(new java.awt.Dimension(getSize().width - 20, 57));
133 if (_oInvocationException != null){
134 jLblResult.setText("<html>Invoking the method cause an exception: <br>" + _oInvocationException.toString() + "</html>");
136 else{
137 jLblResult.setText("<html>The invocation of the method did not produce any error</html>");
139 jResultPanel.add(jLblResult,BorderLayout.WEST);
140 if (bAddPanel){
141 int nPos = jPnlParamContainer.getComponentCount() - 1;
142 jPnlParamContainer.add(jResultPanel, nPos);
144 super.pack();
145 super.validate();
149 private Object[] getParameterValues(){
150 Object[] oParameterValues = new Object[m_aParameterPanels.length];
151 for (int i = 0; i < m_aParameterPanels.length; i++){
152 oParameterValues[i] = m_aParameterPanels[i].getValue();
154 return oParameterValues;
158 private boolean isCompleted(){
159 boolean bIsCompleted = true;
160 for (int i = 0; i < m_aParameterPanels.length; i++){
161 bIsCompleted = m_aParameterPanels[i].isCompleted();
162 if (!bIsCompleted){
163 break;
166 return bIsCompleted;
170 private void addBorderPanel(java.awt.Container _jContainer, String _sLayout){
171 JPanel jPnlBorder = new JPanel();
172 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10));
173 _jContainer.add(jPnlBorder, _sLayout);
176 private void addGapPanel(java.awt.Container _jContainer){
177 JPanel jPnlBorder = new JPanel();
178 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10));
179 jPnlBorder.setMaximumSize(new java.awt.Dimension(10, 10));
180 _jContainer.add(jPnlBorder);
184 private class ParameterPanel extends JPanel{
185 private JComponent m_jComponent;
186 private TypeClass m_aTypeClass = null;
188 public ParameterPanel(ParamInfo _aParamInfo){
189 JTextField jTextField = new JTextField();
190 JComboBox jComboBox = new JComboBox();
191 m_aTypeClass = _aParamInfo.aType.getTypeClass();
192 setLayout(new java.awt.BorderLayout());
193 addBorderPanel(this, BorderLayout.NORTH);
194 addBorderPanel(this, BorderLayout.SOUTH);
195 JPanel jPnlCenter1 = new javax.swing.JPanel();
196 jPnlCenter1.setLayout(new javax.swing.BoxLayout(jPnlCenter1, javax.swing.BoxLayout.X_AXIS));
197 JLabel jLabel1 = new JLabel();
198 jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
199 String sParamText = _aParamInfo.aName + " (" + _aParamInfo.aType.getName() +")";
200 jLabel1.setText(sParamText);
201 jPnlCenter1.add(jLabel1);
202 addGapPanel(jPnlCenter1);
203 switch (m_aTypeClass.getValue()){
204 case TypeClass.BOOLEAN_value:
205 jComboBox.setBackground(new java.awt.Color(255, 255, 255));
206 jComboBox.setPreferredSize(new java.awt.Dimension(50, 19));
207 jComboBox.addItem("True");
208 jComboBox.addItem("False");
209 jComboBox.addKeyListener(new UpdateUIAdapter());
210 jPnlCenter1.add(jComboBox);
211 m_jComponent = jComboBox;
212 break;
213 case TypeClass.BYTE_value:
214 case TypeClass.CHAR_value:
215 case TypeClass.DOUBLE_value:
216 case TypeClass.ENUM_value:
217 case TypeClass.FLOAT_value:
218 case TypeClass.HYPER_value:
219 case TypeClass.LONG_value:
220 case TypeClass.SHORT_value:
221 case TypeClass.STRING_value:
222 case TypeClass.UNSIGNED_HYPER_value:
223 case TypeClass.UNSIGNED_LONG_value:
224 case TypeClass.UNSIGNED_SHORT_value:
225 jTextField.setPreferredSize(new java.awt.Dimension(50, 19));
226 jTextField.addKeyListener(new UpdateUIAdapter());
227 jPnlCenter1.add(jTextField);
228 m_jComponent = jTextField;
229 break;
230 default:
231 System.out.println("Type " + m_aTypeClass.getValue() + " not yet defined in 'ParameterPanel()'");
233 add(jPnlCenter1, java.awt.BorderLayout.CENTER);
234 JPanel jPnlEast = new JPanel();
235 add(jPnlEast, BorderLayout.EAST);
238 private JComponent getInputComponent(){
239 return m_jComponent;
242 public Object getValue(){
243 Object oReturn = null;
244 if (m_jComponent instanceof JTextField){
245 String sText = ((JTextField) m_jComponent).getText();
246 oReturn = Introspector.getIntrospector().getValueOfText(m_aTypeClass, sText);
248 else{
249 JComboBox jComboBox = ((JComboBox) m_jComponent);
250 oReturn = Boolean.valueOf(jComboBox.getSelectedIndex() == 0);
252 return oReturn;
256 public boolean isCompleted(){
257 if (m_jComponent instanceof JTextField){
258 return !((JTextField) m_jComponent).getText().equals("");
260 else{
261 return true;
267 private class UpdateUIAdapter extends KeyAdapter{
268 @Override
269 public void keyReleased(KeyEvent e){
270 boolean bIsCompleted = isCompleted();
271 jOKButton.setEnabled(bIsCompleted);
272 jInvokeButton.setEnabled(bIsCompleted);
273 if (jLblResult != null){
274 jLblResult.setEnabled(false);
275 jLblResult.invalidate();
282 private class ButtonPanel extends JPanel{
283 public ButtonPanel(){
284 super();
285 setLayout(new BorderLayout());
286 addBorderPanel(this, BorderLayout.NORTH);
287 addBorderPanel(this, BorderLayout.SOUTH);
288 JPanel jPnlBottomCenter = new JPanel();
289 jPnlBottomCenter.setLayout(new javax.swing.BoxLayout(jPnlBottomCenter, javax.swing.BoxLayout.X_AXIS));
290 jHelpButton.addActionListener(new ActionListener() {
291 public void actionPerformed(ActionEvent e) {
292 oActionListener.actionPerformed(e);
295 jHelpButton.setEnabled(oActionListener != null);
296 jPnlBottomCenter.add(jHelpButton);
297 addGapPanel(jPnlBottomCenter);
298 jOKButton.setEnabled(false);
299 jOKButton.addActionListener(new ActionListener() {
300 public void actionPerformed(ActionEvent e) {
301 invokeParameterMethod();
302 dispose();
305 jOKButton.setEnabled(isCompleted());
306 jInvokeButton.setEnabled(isCompleted());
307 jInvokeButton.addActionListener(new ActionListener() {
308 public void actionPerformed(ActionEvent e) {
309 invokeParameterMethod();
313 jPnlBottomCenter.add(jOKButton);
314 addGapPanel(jPnlBottomCenter);
315 jPnlBottomCenter.add(jInvokeButton);
316 addGapPanel(jPnlBottomCenter);
317 JButton jCancelButton = new JButton("Cancel");
318 jCancelButton.setFocusCycleRoot(true);
319 jCancelButton.setFocusPainted(true);
320 jCancelButton.addActionListener(new ActionListener(){
321 public void actionPerformed(java.awt.event.ActionEvent evt) {
322 bisdiposed = true;
323 dispose();
327 jPnlBottomCenter.add(jCancelButton);
328 add(jPnlBottomCenter);
333 public void addActionListener(ActionListener _oActionListener){
334 oActionListener = _oActionListener;
335 jHelpButton.setEnabled(oActionListener != null);
339 private void invokeParameterMethod(){
340 try{
341 Object[] oParameters = getParameterValues();
342 m_oUnoReturnObject = m_xUnoMethodNode.invoke(m_oUnoObject, oParameters);
343 insertResultPanel(null);
344 } catch (Exception ex) {
345 insertResultPanel(ex);
346 m_oUnoReturnObject = null;
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */