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