1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package com.sun.star.script.framework.browse;
20 
21 import com.sun.star.awt.XButton;
22 import com.sun.star.awt.XControl;
23 import com.sun.star.awt.XControlContainer;
24 import com.sun.star.awt.XControlModel;
25 import com.sun.star.awt.XDialog;
26 import com.sun.star.awt.XTextComponent;
27 import com.sun.star.awt.XToolkit;
28 import com.sun.star.awt.XWindow;
29 
30 import com.sun.star.beans.XPropertySet;
31 
32 import com.sun.star.container.XNameContainer;
33 
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XMultiComponentFactory;
36 import com.sun.star.lang.XMultiServiceFactory;
37 
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XComponentContext;
40 
41 public class DialogFactory {
42 
43     private static DialogFactory factory;
44     private final XComponentContext xComponentContext;
45 
46     // singleton
DialogFactory(XComponentContext xComponentContext)47     private DialogFactory(XComponentContext xComponentContext) {
48         this.xComponentContext = xComponentContext;
49     }
50 
createDialogFactory(XComponentContext xComponentContext)51     public static void createDialogFactory(XComponentContext xComponentContext) {
52         synchronized (DialogFactory.class) {
53             if (factory == null) {
54                 factory = new DialogFactory(xComponentContext);
55             }
56         }
57     }
58 
getDialogFactory()59     public static DialogFactory getDialogFactory() throws java.lang.Exception {
60         if (factory == null) {
61             throw new java.lang.Exception("DialogFactory not initialized");
62         }
63         return factory;
64     }
65 
showInputDialog(String title, String prompt)66     public String showInputDialog(String title, String prompt) {
67         final XDialog xDialog;
68 
69         try {
70             xDialog = createInputDialog(title, prompt);
71         } catch (com.sun.star.uno.Exception e) {
72             return null;
73         }
74 
75         // add an action listener to the button controls
76         XControlContainer controls =
77             UnoRuntime.queryInterface(XControlContainer.class, xDialog);
78 
79         XButton okButton =
80             UnoRuntime.queryInterface(XButton.class, controls.getControl("Ok"));
81 
82         okButton.setActionCommand("Ok");
83 
84         XButton cancelButton =
85             UnoRuntime.queryInterface(XButton.class, controls.getControl("Cancel"));
86 
87         cancelButton.setActionCommand("Cancel");
88 
89         final XTextComponent textField =
90             UnoRuntime.queryInterface(XTextComponent.class,
91                                       controls.getControl("NameField"));
92 
93         final ResultHolder resultHolder = new ResultHolder();
94 
95         com.sun.star.awt.XActionListener listener =
96         new com.sun.star.awt.XActionListener() {
97             public void actionPerformed(com.sun.star.awt.ActionEvent e) {
98                 if (e.ActionCommand.equals("Cancel")) {
99                     resultHolder.setResult(null);
100                     xDialog.endExecute();
101                 } else {
102                     resultHolder.setResult(textField.getText());
103                     xDialog.endExecute();
104                 }
105             }
106 
107             public void disposing(EventObject o) {
108                 // does nothing
109             }
110         };
111 
112         okButton.addActionListener(listener);
113         cancelButton.addActionListener(listener);
114 
115         xDialog.execute();
116 
117         return (String)resultHolder.getResult();
118     }
119 
setDimensions(Object o, int x, int y, int width, int height)120     private void setDimensions(Object o, int x, int y, int width, int height) throws
121         com.sun.star.uno.Exception {
122 
123         XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, o);
124 
125         props.setPropertyValue("PositionX", Integer.valueOf(x));
126         props.setPropertyValue("PositionY", Integer.valueOf(y));
127         props.setPropertyValue("Height", Integer.valueOf(height));
128         props.setPropertyValue("Width", Integer.valueOf(width));
129     }
130 
createInputDialog(String title, String prompt)131     private XDialog createInputDialog(String title, String prompt) throws
132         com.sun.star.uno.Exception {
133 
134         if (title == null || title.length() == 0) {
135             title = "Scripting Framework";
136         }
137 
138         if (prompt == null || prompt.length() == 0) {
139             prompt = "Enter name";
140         }
141 
142         // get the service manager from the component context
143         XMultiComponentFactory xMultiComponentFactory =
144             xComponentContext.getServiceManager();
145 
146         // create the dialog model and set the properties
147         Object dialogModel = xMultiComponentFactory.createInstanceWithContext(
148                                  "com.sun.star.awt.UnoControlDialogModel",
149                                  xComponentContext);
150 
151         setDimensions(dialogModel, 100, 100, 157, 58);
152 
153         XPropertySet props =
154             UnoRuntime.queryInterface(XPropertySet.class, dialogModel);
155 
156         props.setPropertyValue("Title", title);
157 
158         // get the service manager from the dialog model
159         XMultiServiceFactory xMultiServiceFactory =
160             UnoRuntime.queryInterface(XMultiServiceFactory.class, dialogModel);
161 
162         // create the label model and set the properties
163         Object label = xMultiServiceFactory.createInstance(
164                            "com.sun.star.awt.UnoControlFixedTextModel");
165 
166         setDimensions(label, 15, 5, 134, 12);
167 
168         XPropertySet labelProps =
169             UnoRuntime.queryInterface(XPropertySet.class, label);
170 
171         labelProps.setPropertyValue("Name", "PromptLabel");
172         labelProps.setPropertyValue("Label", prompt);
173 
174         // create the text field
175         Object edit = xMultiServiceFactory.createInstance(
176                           "com.sun.star.awt.UnoControlEditModel");
177 
178         setDimensions(edit, 15, 18, 134, 12);
179 
180         XPropertySet editProps =
181             UnoRuntime.queryInterface(XPropertySet.class, edit);
182 
183         editProps.setPropertyValue("Name", "NameField");
184 
185         // create the OK button
186         Object okButtonModel = xMultiServiceFactory.createInstance(
187                                    "com.sun.star.awt.UnoControlButtonModel");
188 
189         setDimensions(okButtonModel, 40, 39, 38, 15);
190 
191         XPropertySet buttonProps =
192             UnoRuntime.queryInterface(XPropertySet.class, okButtonModel);
193 
194         buttonProps.setPropertyValue("Name", "Ok");
195         buttonProps.setPropertyValue("Label", "Ok");
196 
197         // create the Cancel button
198         Object cancelButtonModel = xMultiServiceFactory.createInstance(
199                                        "com.sun.star.awt.UnoControlButtonModel");
200 
201         setDimensions(cancelButtonModel, 83, 39, 38, 15);
202 
203         buttonProps =
204             UnoRuntime.queryInterface(XPropertySet.class, cancelButtonModel);
205 
206         buttonProps.setPropertyValue("Name", "Cancel");
207         buttonProps.setPropertyValue("Label", "Cancel");
208 
209         // insert the control models into the dialog model
210         XNameContainer xNameCont =
211             UnoRuntime.queryInterface(XNameContainer.class, dialogModel);
212 
213         xNameCont.insertByName("PromptLabel", label);
214         xNameCont.insertByName("NameField", edit);
215         xNameCont.insertByName("Ok", okButtonModel);
216         xNameCont.insertByName("Cancel", cancelButtonModel);
217 
218         // create the dialog control and set the model
219         Object dialog = xMultiComponentFactory.createInstanceWithContext(
220                             "com.sun.star.awt.UnoControlDialog",
221                             xComponentContext);
222 
223         XControl xControl = UnoRuntime.queryInterface(XControl.class, dialog);
224 
225         XControlModel xControlModel =
226             UnoRuntime.queryInterface(XControlModel.class, dialogModel);
227 
228         xControl.setModel(xControlModel);
229 
230         // create a peer
231         Object toolkit = xMultiComponentFactory.createInstanceWithContext(
232                              "com.sun.star.awt.ExtToolkit",
233                              xComponentContext);
234 
235         XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, toolkit);
236         XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xControl);
237         xWindow.setVisible(false);
238         xControl.createPeer(xToolkit, null);
239 
240         return UnoRuntime.queryInterface(XDialog.class, dialog);
241     }
242 
243     private static class ResultHolder {
244 
245         private Object result = null;
246 
getResult()247         public Object getResult() {
248             return result;
249         }
250 
setResult(Object result)251         public void setResult(Object result) {
252             this.result = result;
253         }
254     }
255 }