1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 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. 20 * 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. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.reflection.ParamInfo; 36 import com.sun.star.reflection.XIdlMethod; 37 import com.sun.star.uno.TypeClass; 38 import java.awt.BorderLayout; 39 import java.awt.event.ActionEvent; 40 import java.awt.event.ActionListener; 41 import java.awt.event.KeyAdapter; 42 import java.awt.event.KeyEvent; 43 import java.awt.event.WindowAdapter; 44 import java.awt.event.WindowEvent; 45 import java.util.Vector; 46 import javax.swing.JButton; 47 import javax.swing.JComboBox; 48 import javax.swing.JComponent; 49 import javax.swing.JDialog; 50 import javax.swing.JFrame; 51 import javax.swing.JLabel; 52 import javax.swing.JPanel; 53 import javax.swing.JTextField; 54 55 /** 56 * @author bc93774 57 */ 58 public class MethodParametersDialog extends JDialog{ 59 private javax.swing.JPanel jPnlParamContainer; 60 private ParameterPanel[] m_aParameterPanels; 61 private ParamInfo[] m_aParamInfo; 62 private XIdlMethod m_xIdlMethod; 63 private ActionListener oActionListener; 64 private JButton jHelpButton = new JButton("Help"); 65 private JButton jOKButton = new JButton("Ok"); 66 private JButton jInvokeButton = new JButton("Invoke"); 67 private Object m_oReturnButton = null; 68 private Object m_oUnoObject = null; 69 private Object m_oUnoReturnObject = null; 70 private JLabel jLblResult; 71 private JPanel jResultPanel = null; 72 private boolean bisdiposed = false; 73 private XUnoMethodNode m_xUnoMethodNode; 74 75 76 public MethodParametersDialog(XUnoMethodNode _xUnoMethodNode){ 77 m_xUnoMethodNode = _xUnoMethodNode; 78 m_xIdlMethod = _xUnoMethodNode.getXIdlMethod(); 79 m_aParamInfo = m_xIdlMethod.getParameterInfos(); 80 m_oUnoObject = m_xUnoMethodNode.getUnoObject(); 81 Object[] m_aParameterObjects = new Object[m_aParamInfo.length]; 82 } 83 84 85 public Vector getMethodObjects() { 86 super.setModal(true); 87 addBorderPanel(getContentPane(), BorderLayout.NORTH); 88 addBorderPanel(getContentPane(), BorderLayout.WEST); 89 addBorderPanel(getContentPane(), BorderLayout.EAST); 90 jPnlParamContainer = new JPanel(); 91 jPnlParamContainer.setLayout(new javax.swing.BoxLayout(jPnlParamContainer, javax.swing.BoxLayout.Y_AXIS)); 92 JPanel jHeaderPanel = new JPanel(new BorderLayout()); 93 JLabel jLblHeader = new JLabel(); 94 jLblHeader.setText("Please insert the values for the given Parameters of the method '" + m_xIdlMethod.getName() + "'"); 95 jHeaderPanel.add(jLblHeader,BorderLayout.WEST); 96 jPnlParamContainer.add(jHeaderPanel); 97 98 m_aParameterPanels = new ParameterPanel[m_aParamInfo.length]; 99 for (int i = 0; i < m_aParameterPanels.length; i++){ 100 m_aParameterPanels[i] = new ParameterPanel(m_aParamInfo[i]); 101 jPnlParamContainer.add(m_aParameterPanels[i]); 102 } 103 jPnlParamContainer.add(new ButtonPanel(), java.awt.BorderLayout.SOUTH); 104 getContentPane().add(jPnlParamContainer, java.awt.BorderLayout.CENTER); 105 pack(); 106 setLocation(350, 350); 107 setTitle("Object Inspector - Parameter Values of '" + m_xIdlMethod.getName() + "'"); 108 super.setFocusable(true); 109 super.setFocusableWindowState(true); 110 super.requestFocus(); 111 m_aParameterPanels[0].getInputComponent().requestFocusInWindow(); 112 setVisible(true); 113 if (!bisdiposed){ 114 Vector aMethodObjects = new Vector(); 115 for (int i = 0; i < m_aParameterPanels.length; i++){ 116 aMethodObjects.add(m_aParameterPanels[i].getValue()); 117 } 118 aMethodObjects.add(m_oUnoReturnObject); 119 return aMethodObjects; 120 } 121 else{ 122 return null; 123 } 124 } 125 126 127 private void insertResultPanel(Exception _oInvocationException){ 128 boolean bAddPanel = false; 129 if (jResultPanel == null){ 130 jResultPanel = new JPanel(new BorderLayout()); 131 bAddPanel = true; 132 } 133 else{ 134 jResultPanel.removeAll(); 135 } 136 jLblResult = new JLabel(); 137 jLblResult.setMaximumSize(new java.awt.Dimension(getSize().width - 20, 57)); 138 if (_oInvocationException != null){ 139 jLblResult.setText("<html>Invoking the method cause an exception: <br>" + _oInvocationException.toString() + "</html>"); 140 } 141 else{ 142 jLblResult.setText("<html>The invocation of the method did not produce any error</html>"); 143 } 144 jResultPanel.add(jLblResult,BorderLayout.WEST); 145 if (bAddPanel){ 146 int nPos = jPnlParamContainer.getComponentCount() - 1; 147 jPnlParamContainer.add(jResultPanel, nPos); 148 } 149 super.pack(); 150 super.validate(); 151 } 152 153 154 private Object[] getParameterValues(){ 155 Object[] oParameterValues = new Object[m_aParameterPanels.length]; 156 for (int i = 0; i < m_aParameterPanels.length; i++){ 157 oParameterValues[i] = m_aParameterPanels[i].getValue(); 158 } 159 return oParameterValues; 160 } 161 162 163 private boolean isCompleted(){ 164 boolean bIsCompleted = true; 165 for (int i = 0; i < m_aParameterPanels.length; i++){ 166 bIsCompleted = m_aParameterPanels[i].isCompleted(); 167 if (!bIsCompleted){ 168 break; 169 } 170 } 171 return bIsCompleted; 172 } 173 174 175 private void addBorderPanel(java.awt.Container _jContainer, String _sLayout){ 176 JPanel jPnlBorder = new JPanel(); 177 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10)); 178 _jContainer.add(jPnlBorder, _sLayout); 179 } 180 181 private void addGapPanel(java.awt.Container _jContainer){ 182 JPanel jPnlBorder = new JPanel(); 183 jPnlBorder.setPreferredSize(new java.awt.Dimension(10, 10)); 184 jPnlBorder.setMaximumSize(new java.awt.Dimension(10, 10)); 185 _jContainer.add(jPnlBorder); 186 } 187 188 189 private class ParameterPanel extends JPanel{ 190 private JComponent m_jComponent; 191 private TypeClass m_aTypeClass = null; 192 193 public ParameterPanel(ParamInfo _aParamInfo){ 194 JTextField jTextField = new JTextField(); 195 JComboBox jComboBox = new JComboBox(); 196 m_aTypeClass = _aParamInfo.aType.getTypeClass(); 197 setLayout(new java.awt.BorderLayout()); 198 addBorderPanel(this, BorderLayout.NORTH); 199 addBorderPanel(this, BorderLayout.SOUTH); 200 JPanel jPnlCenter1 = new javax.swing.JPanel(); 201 jPnlCenter1.setLayout(new javax.swing.BoxLayout(jPnlCenter1, javax.swing.BoxLayout.X_AXIS)); 202 JLabel jLabel1 = new JLabel(); 203 jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); 204 String sParamText = _aParamInfo.aName + " (" + _aParamInfo.aType.getName() +")"; 205 jLabel1.setText(sParamText); 206 jPnlCenter1.add(jLabel1); 207 addGapPanel(jPnlCenter1); 208 switch (m_aTypeClass.getValue()){ 209 case TypeClass.BOOLEAN_value: 210 jComboBox.setBackground(new java.awt.Color(255, 255, 255)); 211 jComboBox.setPreferredSize(new java.awt.Dimension(50, 19)); 212 jComboBox.addItem("True"); 213 jComboBox.addItem("False"); 214 jComboBox.addKeyListener(new UpdateUIAdapter()); 215 jPnlCenter1.add(jComboBox); 216 m_jComponent = jComboBox; 217 break; 218 case TypeClass.BYTE_value: 219 case TypeClass.CHAR_value: 220 case TypeClass.DOUBLE_value: 221 case TypeClass.ENUM_value: 222 case TypeClass.FLOAT_value: 223 case TypeClass.HYPER_value: 224 case TypeClass.LONG_value: 225 case TypeClass.SHORT_value: 226 case TypeClass.STRING_value: 227 case TypeClass.UNSIGNED_HYPER_value: 228 case TypeClass.UNSIGNED_LONG_value: 229 case TypeClass.UNSIGNED_SHORT_value: 230 jTextField.setPreferredSize(new java.awt.Dimension(50, 19)); 231 jTextField.addKeyListener(new UpdateUIAdapter()); 232 jPnlCenter1.add(jTextField); 233 m_jComponent = jTextField; 234 break; 235 default: 236 System.out.println("Type " + m_aTypeClass.getValue() + " not yet defined in 'ParameterPanel()'"); 237 } 238 add(jPnlCenter1, java.awt.BorderLayout.CENTER); 239 JPanel jPnlEast = new JPanel(); 240 add(jPnlEast, BorderLayout.EAST); 241 } 242 243 private JComponent getInputComponent(){ 244 return m_jComponent; 245 } 246 247 public Object getValue(){ 248 Object oReturn = null; 249 if (m_jComponent instanceof JTextField){ 250 String sText = ((JTextField) m_jComponent).getText(); 251 oReturn = Introspector.getIntrospector().getValueOfText(m_aTypeClass, sText); 252 } 253 else{ 254 JComboBox jComboBox = ((JComboBox) m_jComponent); 255 oReturn = Boolean.valueOf(jComboBox.getSelectedIndex() == 0); 256 } 257 return oReturn; 258 } 259 260 261 public boolean isCompleted(){ 262 if (m_jComponent instanceof JTextField){ 263 return !((JTextField) m_jComponent).getText().equals(""); 264 } 265 else{ 266 return true; 267 } 268 } 269 } 270 271 272 private class UpdateUIAdapter extends KeyAdapter{ 273 public void keyReleased(KeyEvent e){ 274 boolean bIsCompleted = isCompleted(); 275 jOKButton.setEnabled(bIsCompleted); 276 jInvokeButton.setEnabled(bIsCompleted); 277 if (jLblResult != null){ 278 jLblResult.setEnabled(false); 279 jLblResult.invalidate(); 280 } 281 } 282 } 283 284 285 286 private class ButtonPanel extends JPanel{ 287 public ButtonPanel(){ 288 super(); 289 setLayout(new BorderLayout()); 290 addBorderPanel(this, BorderLayout.NORTH); 291 addBorderPanel(this, BorderLayout.SOUTH); 292 JPanel jPnlBottomCenter = new JPanel(); 293 jPnlBottomCenter.setLayout(new javax.swing.BoxLayout(jPnlBottomCenter, javax.swing.BoxLayout.X_AXIS)); 294 jHelpButton.addActionListener(new ActionListener() { 295 public void actionPerformed(ActionEvent e) { 296 oActionListener.actionPerformed(e); 297 } 298 }); 299 jHelpButton.setEnabled(oActionListener != null); 300 jPnlBottomCenter.add(jHelpButton); 301 addGapPanel(jPnlBottomCenter); 302 jOKButton.setEnabled(false); 303 jOKButton.addActionListener(new ActionListener() { 304 public void actionPerformed(ActionEvent e) { 305 invokeParameterMethod(); 306 dispose(); 307 } 308 }); 309 jOKButton.setEnabled(isCompleted()); 310 jInvokeButton.setEnabled(isCompleted()); 311 jInvokeButton.addActionListener(new ActionListener() { 312 public void actionPerformed(ActionEvent e) { 313 invokeParameterMethod(); 314 } 315 }); 316 317 jPnlBottomCenter.add(jOKButton); 318 addGapPanel(jPnlBottomCenter); 319 jPnlBottomCenter.add(jInvokeButton); 320 addGapPanel(jPnlBottomCenter); 321 JButton jCancelButton = new JButton("Cancel"); 322 jCancelButton.setFocusCycleRoot(true); 323 jCancelButton.setFocusPainted(true); 324 jCancelButton.addActionListener(new ActionListener(){ 325 public void actionPerformed(java.awt.event.ActionEvent evt) { 326 bisdiposed = true; 327 dispose(); 328 } 329 }); 330 331 jPnlBottomCenter.add(jCancelButton); 332 add(jPnlBottomCenter); 333 } 334 } 335 336 337 public void addActionListener(ActionListener _oActionListener){ 338 oActionListener = _oActionListener; 339 jHelpButton.setEnabled(oActionListener != null); 340 } 341 342 343 public void invokeParameterMethod(){ 344 try{ 345 Object[] oParameters = getParameterValues(); 346 m_oUnoReturnObject = m_xUnoMethodNode.invoke(m_oUnoObject, oParameters); 347 insertResultPanel(null); 348 } catch (Exception ex) { 349 insertResultPanel(ex); 350 m_oUnoReturnObject = null; 351 }} 352 353 } 354