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