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