Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / SwingDialogProvider.java
blob42ae6db387ce12c98f1cdc5d0cea1decacaa707b
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 com.sun.star.uno.XComponentContext;
36 import java.awt.Component;
37 import java.awt.Container;
38 import java.awt.Dimension;
39 import java.awt.event.ActionListener;
40 import java.awt.event.ComponentAdapter;
41 import java.awt.event.ComponentEvent;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.WindowAdapter;
44 import java.awt.event.WindowEvent;
45 import javax.swing.ButtonGroup;
46 import javax.swing.JDialog;
47 import javax.swing.JMenu;
48 import javax.swing.JMenuBar;
49 import javax.swing.JMenuItem;
50 import javax.swing.JPopupMenu;
51 import javax.swing.JRadioButtonMenuItem;
52 import javax.swing.JTabbedPane;
53 import javax.swing.KeyStroke;
57 public class SwingDialogProvider implements XDialogProvider{
59 private JPopupMenu m_jPopupMenu = new JPopupMenu();
60 private XComponentContext m_xComponentContext;
61 private Inspector._Inspector m_oInspector;
62 private JDialog m_jInspectorDialog = new JDialog();
63 private JTabbedPane m_jTabbedPane1 = new JTabbedPane();
64 private Container cp;
66 private JMenu jMnuOptions = new JMenu("Options");
67 private JRadioButtonMenuItem jJavaMenuItem = null;
68 private JRadioButtonMenuItem jCPlusPlusMenuItem = null;
69 private JRadioButtonMenuItem jBasicMenuItem = null;
71 /** Creates a new instance of SwingPopupMentuProvider */
72 public SwingDialogProvider(Inspector._Inspector _oInspector, String _sTitle) {
73 m_oInspector = _oInspector;
74 m_xComponentContext = _oInspector.getXComponentContext();
75 insertMenus();
76 initializePopupMenu();
77 cp = m_jInspectorDialog.getContentPane();
78 cp.setLayout(new java.awt.BorderLayout(0, 10));
79 m_jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
80 m_jInspectorDialog.addWindowListener(new InspectorWindowAdapter());
81 m_jInspectorDialog.addComponentListener(new InspectorComponentAdapter());
82 m_jInspectorDialog.setTitle(_sTitle);
83 m_jInspectorDialog.setLocation(100, 50);
84 m_jInspectorDialog.getContentPane().add(m_jTabbedPane1);
89 public JDialog getDialog(){
90 return m_jInspectorDialog;
94 private void addMenuBar(JMenuBar _jMenuBar){
95 getDialog().setJMenuBar(_jMenuBar);
99 private void removeTabPaneByIndex(int _nIndex){
100 if (_nIndex > -1){
101 String sSelInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
102 m_jTabbedPane1.remove(_nIndex);
103 m_oInspector.getInspectorPages().remove(sSelInspectorPanelTitle);
108 public void selectInspectorPageByIndex(int nTabIndex){
109 m_jTabbedPane1.setSelectedIndex(nTabIndex);
113 public int getInspectorPageCount(){
114 return m_jTabbedPane1.getTabCount();
118 public JTabbedPane getTabbedPane(){
119 return m_jTabbedPane1;
123 public InspectorPane getSelectedInspectorPage(){
124 int nIndex = m_jTabbedPane1.getSelectedIndex();
125 return getInspectorPage(nIndex);
129 public InspectorPane getInspectorPage(int _nIndex){
130 InspectorPane oInspectorPane = null;
131 if (_nIndex > -1){
132 String sInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
133 oInspectorPane = m_oInspector.getInspectorPages().get(sInspectorPanelTitle);
135 return oInspectorPane;
139 private void removeTabPanes(){
140 int nCount = m_jTabbedPane1.getTabCount();
141 if (nCount > 0){
142 for (int i = nCount-1; i >= 0; i--){
143 removeTabPaneByIndex(i);
148 private void removeSelectedTabPane(){
149 int nIndex = getTabbedPane().getSelectedIndex();
150 removeTabPaneByIndex(nIndex);
154 private class InspectorComponentAdapter extends ComponentAdapter{
155 @Override
156 public void componentHidden(ComponentEvent e){
157 m_jInspectorDialog.pack();
158 m_jInspectorDialog.invalidate();
162 @Override
163 public void componentShown(ComponentEvent e){
164 m_jInspectorDialog.pack();
165 m_jInspectorDialog.invalidate();
169 private class InspectorWindowAdapter extends WindowAdapter{
170 @Override
171 public void windowClosed(WindowEvent e){
172 removeTabPanes();
173 m_oInspector.disposeHiddenDocuments();
176 @Override
177 public void windowClosing(WindowEvent e){
178 removeTabPanes();
179 m_oInspector.disposeHiddenDocuments();
185 private void initializePopupMenu(){
186 m_jPopupMenu.add(getInspectMenuItem("Inspect"));
187 m_jPopupMenu.add(getSourceCodeMenuItem(SADDTOSOURCECODE));
188 m_jPopupMenu.add(getInvokeMenuItem(SINVOKE));
189 m_jPopupMenu.addSeparator();
190 m_jPopupMenu.add(getHelpMenuItem("Help"));
194 private void addOpenDocumentMenu(JMenu _jMnuRoot){
195 ActionListener oActionListener = new ActionListener(){
196 public void actionPerformed(java.awt.event.ActionEvent evt) {
197 String sTDocUrl = evt.getActionCommand();
198 m_oInspector.inspectOpenDocument(sTDocUrl);
201 String[] sTDocUrls = m_oInspector.getTDocUrls();
202 String[] sTDocTitles = m_oInspector.getTDocTitles(sTDocUrls);
203 for (int i = 0; i < sTDocUrls.length; i++){
204 addSingleMenuItem(_jMnuRoot, sTDocTitles[i], sTDocUrls[i], oActionListener);
209 private void addApplicationDocumentMenu(JMenu _jMnuRoot){
210 ActionListener oActionListener = new ActionListener(){
211 public void actionPerformed(java.awt.event.ActionEvent evt) {
212 String sApplicationDocUrl = evt.getActionCommand();
213 m_oInspector.inspectOpenEmptyDocument(sApplicationDocUrl);
216 String[][] sApplUrls = m_oInspector.getApplicationUrls();
217 for (int i = 0; i < sApplUrls.length; i++){
218 addSingleMenuItem(_jMnuRoot, sApplUrls[i][1], sApplUrls[i][0], oActionListener);
223 private void addSingleMenuItem(JMenu _jMnuOpenDocs, String _sTitle, String _sActionCommand, ActionListener _oActionListener){
224 javax.swing.JMenuItem jMnuItemOpenDoc = new javax.swing.JMenuItem(_sTitle);
225 jMnuItemOpenDoc.setActionCommand(_sActionCommand);
226 jMnuItemOpenDoc.addActionListener(_oActionListener);
227 _jMnuOpenDocs.add(jMnuItemOpenDoc);
231 private void addHelpMenu(JMenuBar _jInspectMenuBar){
232 JMenu jMnuHelp = new JMenu("Help");
233 jMnuHelp.add(getHelpMenuItem("Idl-Help"));
234 _jInspectMenuBar.add(jMnuHelp);
238 private JMenuItem getHelpMenuItem(String _sMenuTitle){
239 JMenuItem jMnuHelpItem = new JMenuItem(_sMenuTitle);
240 jMnuHelpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
241 jMnuHelpItem.setMnemonic('H');
242 jMnuHelpItem.addActionListener(new ActionListener(){
243 public void actionPerformed(java.awt.event.ActionEvent evt) {
244 m_oInspector.openIdlFileforSelectedNode();
247 return jMnuHelpItem;
250 private void addFileMenu(JMenuBar _jInspectMenuBar){
251 JMenu jMnuFile = new JMenu("File");
252 JMenuItem jMnuItemRemoveInspector = new JMenuItem("Remove");
253 jMnuItemRemoveInspector.addActionListener(new ActionListener(){
254 public void actionPerformed(java.awt.event.ActionEvent evt) {
255 removeSelectedTabPane();
258 jMnuFile.add(jMnuItemRemoveInspector);
259 JMenuItem jMnuItemExit = new JMenuItem("Exit");
260 jMnuItemExit.addActionListener(new ActionListener(){
261 public void actionPerformed(java.awt.event.ActionEvent evt) {
262 getDialog().dispose();
265 jMnuFile.add(jMnuItemExit);
266 _jInspectMenuBar.add(jMnuFile);
269 private JMenuItem getInspectMenuItem(String _sLabel){
270 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
271 jMnuSelectedObject.addActionListener(new ActionListener(){
272 public void actionPerformed(java.awt.event.ActionEvent evt) {
273 m_oInspector.inspectSelectedNode();
276 return jMnuSelectedObject;
280 private JMenuItem getSourceCodeMenuItem(String _sLabel){
281 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
282 jMnuSelectedObject.addActionListener(new ActionListener(){
283 public void actionPerformed(java.awt.event.ActionEvent evt) {
284 m_oInspector.addSourceCodeOfSelectedNode();
287 return jMnuSelectedObject;
290 private JMenuItem getInvokeMenuItem(String _sLabel){
291 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
292 jMnuSelectedObject.addActionListener(new ActionListener(){
293 public void actionPerformed(java.awt.event.ActionEvent evt) {
294 m_oInspector.invokeSelectedMethod();
297 return jMnuSelectedObject;
301 private void addInspectMenu(JMenuBar _jInspectMenuBar){
302 JMenu jMnuInspect = new JMenu("Inspect");
303 addApplicationDocumentMenu(jMnuInspect);
304 jMnuInspect.addSeparator();
305 addGlobalServiceManagerMenu(jMnuInspect);
306 jMnuInspect.addSeparator();
307 jMnuInspect.add(getInspectMenuItem("Selected Object"));
308 jMnuInspect.addSeparator();
309 addOpenDocumentMenu(jMnuInspect);
310 _jInspectMenuBar.add(jMnuInspect);
313 public int getLanguage(){
314 return XLanguageSourceCodeGenerator.nJAVA;
318 public void selectSourceCodeLanguage(int _nLanguage){
319 switch (_nLanguage){
320 case XLanguageSourceCodeGenerator.nJAVA:
321 jJavaMenuItem.setSelected(true);
322 break;
323 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
324 jCPlusPlusMenuItem.setSelected(true);
325 break;
326 case XLanguageSourceCodeGenerator.nBASIC:
327 jBasicMenuItem.setSelected(true);
328 break;
329 default:
330 System.out.println("Warning: Sourcecode language is not defined!");
334 private JRadioButtonMenuItem addLanguageMenuItem(ButtonGroup _group, String _sLanguageTitle, boolean _bSelect, char _sMnemonic, final int _nLanguage){
335 JRadioButtonMenuItem jMenuItem = new JRadioButtonMenuItem(_sLanguageTitle, _bSelect);
336 jMenuItem.setMnemonic(_sMnemonic);
337 _group.add(jMenuItem);
338 jMenuItem.addActionListener(new ActionListener(){
339 public void actionPerformed(java.awt.event.ActionEvent evt) {
340 m_oInspector.setSourceCodeLanguage(_nLanguage);
343 return jMenuItem;
347 public String getIDLPath(){
348 return this.m_oInspector.getIDLPath();
351 private void addOptionsMenu(JMenuBar _jInspectMenuBar){
352 ButtonGroup oButtonGroup = new ButtonGroup();
353 jJavaMenuItem = addLanguageMenuItem(oButtonGroup, "Generate Java Sourcecode", true, 'J', XLanguageSourceCodeGenerator.nJAVA);
354 jMnuOptions.add(jJavaMenuItem);
355 jCPlusPlusMenuItem = addLanguageMenuItem(oButtonGroup, "Generate C++ Sourcecode", false, 'C', XLanguageSourceCodeGenerator.nCPLUSPLUS);
356 jMnuOptions.add(jCPlusPlusMenuItem);
357 jBasicMenuItem = addLanguageMenuItem(oButtonGroup, "Generate OpenOffice.org Basic Sourcecode", false, 'B', XLanguageSourceCodeGenerator.nBASIC);
358 jMnuOptions.add(jBasicMenuItem);
359 jMnuOptions.addSeparator();
360 JMenuItem jMenuItem = new JMenuItem("Path to SDK-Installation");
361 jMenuItem.setMnemonic('I');
362 jMenuItem.addActionListener(new ActionListener(){
363 public void actionPerformed(java.awt.event.ActionEvent evt) {
364 m_oInspector.assignSDKPath();
368 jMnuOptions.add(jMenuItem);
369 _jInspectMenuBar.add(jMnuOptions);
373 private void insertMenus(){
374 JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
375 addFileMenu(jMenuBar1);
376 addInspectMenu(jMenuBar1);
377 JMenu jMnuEdit = new JMenu("Edit");
378 JMenu jMnuView = new JMenu("View");
379 addOptionsMenu(jMenuBar1);
380 jMenuBar1.setFont(new java.awt.Font("Dialog", 0, 12));
381 jMenuBar1.add(jMnuEdit);
382 jMenuBar1.add(jMnuView);
383 addHelpMenu(jMenuBar1);
384 addMenuBar(jMenuBar1);
387 private void addGlobalServiceManagerMenu(JMenu _jMnuRoot){
388 JMenuItem jMnuGlobalServiceManager = new JMenuItem("Global Service Manager");
389 jMnuGlobalServiceManager.addActionListener(new ActionListener(){
390 public void actionPerformed(java.awt.event.ActionEvent evt) {
391 m_oInspector.inspect(m_xComponentContext.getServiceManager(), "Global ServiceManager");
394 _jMnuRoot.add(jMnuGlobalServiceManager);
399 public void enablePopupMenuItem(String _sMenuTitle, boolean _bdoEnable){
400 Component[] oComponents = m_jPopupMenu.getComponents();
401 for (int i = 0; i < oComponents.length; i++){
402 if (oComponents[i] instanceof JMenuItem){
403 JMenuItem jMenuItem = (JMenuItem) oComponents[i];
404 if (jMenuItem.getText().equals(_sMenuTitle)){
405 jMenuItem.setEnabled(_bdoEnable);
412 public void showPopUpMenu(Object _invoker, int x, int y) throws ClassCastException{
413 if (_invoker instanceof Component){
414 m_jPopupMenu.show((Component) _invoker, x, y);
419 public void show(int _nPageIndex){
420 Dimension aDimension = m_jInspectorDialog.getSize();
421 selectInspectorPageByIndex(_nPageIndex);
422 if (_nPageIndex > 0){
423 m_jInspectorDialog.setSize(aDimension);
425 else{
426 m_jInspectorDialog.pack();
428 m_jInspectorDialog.setVisible(true);
431 public void paint(){
432 m_jTabbedPane1.paintImmediately(m_jTabbedPane1.getBounds());
436 public void addInspectorPage(String _sTitle, Object _oContainer) throws ClassCastException{
437 if (_oContainer instanceof Component){
438 m_jTabbedPane1.addTab(_sTitle, (Component) _oContainer);