cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Inspector / SwingDialogProvider.java
blobe66bedce8c5497f916fe2cdcb6319fb523f3aaea
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.uno.XComponentContext;
37 import java.awt.Component;
38 import java.awt.Container;
39 import java.awt.Dimension;
40 import java.awt.event.ActionListener;
41 import java.awt.event.ComponentAdapter;
42 import java.awt.event.ComponentEvent;
43 import java.awt.event.KeyEvent;
44 import java.awt.event.WindowAdapter;
45 import java.awt.event.WindowEvent;
46 import javax.swing.ButtonGroup;
47 import javax.swing.JDialog;
48 import javax.swing.JMenu;
49 import javax.swing.JMenuBar;
50 import javax.swing.JMenuItem;
51 import javax.swing.JPopupMenu;
52 import javax.swing.JRadioButtonMenuItem;
53 import javax.swing.JTabbedPane;
54 import javax.swing.KeyStroke;
58 public class SwingDialogProvider implements XDialogProvider{
60 private JPopupMenu m_jPopupMenu = new JPopupMenu();
61 private XComponentContext m_xComponentContext;
62 private Inspector._Inspector m_oInspector;
63 private JDialog m_jInspectorDialog = new JDialog();
64 private JTabbedPane m_jTabbedPane1 = new JTabbedPane();
65 private Container cp;
67 private JMenu jMnuOptions = new JMenu("Options");
68 private JRadioButtonMenuItem jJavaMenuItem = null;
69 private JRadioButtonMenuItem jCPlusPlusMenuItem = null;
70 private JRadioButtonMenuItem jBasicMenuItem = null;
72 /** Creates a new instance of SwingPopupMentuProvider */
73 public SwingDialogProvider(Inspector._Inspector _oInspector, String _sTitle) {
74 m_oInspector = _oInspector;
75 m_xComponentContext = _oInspector.getXComponentContext();
76 insertMenus();
77 initializePopupMenu();
78 cp = m_jInspectorDialog.getContentPane();
79 cp.setLayout(new java.awt.BorderLayout(0, 10));
80 m_jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
81 m_jInspectorDialog.addWindowListener(new InspectorWindowAdapter());
82 m_jInspectorDialog.addComponentListener(new InspectorComponentAdapter());
83 m_jInspectorDialog.setTitle(_sTitle);
84 m_jInspectorDialog.setLocation(100, 50);
85 m_jInspectorDialog.getContentPane().add(m_jTabbedPane1);
90 public JDialog getDialog(){
91 return m_jInspectorDialog;
95 private void addMenuBar(JMenuBar _jMenuBar){
96 getDialog().setJMenuBar(_jMenuBar);
100 private void removeTabPaneByIndex(int _nIndex){
101 if (_nIndex > -1){
102 String sSelInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
103 m_jTabbedPane1.remove(_nIndex);
104 m_oInspector.getInspectorPages().remove(sSelInspectorPanelTitle);
109 public void selectInspectorPageByIndex(int nTabIndex){
110 m_jTabbedPane1.setSelectedIndex(nTabIndex);
114 public int getInspectorPageCount(){
115 return m_jTabbedPane1.getTabCount();
119 public JTabbedPane getTabbedPane(){
120 return m_jTabbedPane1;
124 public InspectorPane getSelectedInspectorPage(){
125 int nIndex = m_jTabbedPane1.getSelectedIndex();
126 return getInspectorPage(nIndex);
130 public InspectorPane getInspectorPage(int _nIndex){
131 InspectorPane oInspectorPane = null;
132 if (_nIndex > -1){
133 String sInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
134 oInspectorPane = m_oInspector.getInspectorPages().get(sInspectorPanelTitle);
136 return oInspectorPane;
140 private void removeTabPanes(){
141 int nCount = m_jTabbedPane1.getTabCount();
142 if (nCount > 0){
143 for (int i = nCount-1; i >= 0; i--){
144 removeTabPaneByIndex(i);
149 private void removeSelectedTabPane(){
150 int nIndex = getTabbedPane().getSelectedIndex();
151 removeTabPaneByIndex(nIndex);
155 private class InspectorComponentAdapter extends ComponentAdapter{
156 @Override
157 public void componentHidden(ComponentEvent e){
158 m_jInspectorDialog.pack();
159 m_jInspectorDialog.invalidate();
163 @Override
164 public void componentShown(ComponentEvent e){
165 m_jInspectorDialog.pack();
166 m_jInspectorDialog.invalidate();
170 private class InspectorWindowAdapter extends WindowAdapter{
171 @Override
172 public void windowClosed(WindowEvent e){
173 removeTabPanes();
174 m_oInspector.disposeHiddenDocuments();
177 @Override
178 public void windowClosing(WindowEvent e){
179 removeTabPanes();
180 m_oInspector.disposeHiddenDocuments();
186 private void initializePopupMenu(){
187 m_jPopupMenu.add(getInspectMenuItem("Inspect"));
188 m_jPopupMenu.add(getSourceCodeMenuItem(SADDTOSOURCECODE));
189 m_jPopupMenu.add(getInvokeMenuItem(SINVOKE));
190 m_jPopupMenu.addSeparator();
191 m_jPopupMenu.add(getHelpMenuItem("Help"));
195 private void addOpenDocumentMenu(JMenu _jMnuRoot){
196 ActionListener oActionListener = new ActionListener(){
197 public void actionPerformed(java.awt.event.ActionEvent evt) {
198 String sTDocUrl = evt.getActionCommand();
199 m_oInspector.inspectOpenDocument(sTDocUrl);
202 String[] sTDocUrls = m_oInspector.getTDocUrls();
203 String[] sTDocTitles = m_oInspector.getTDocTitles(sTDocUrls);
204 for (int i = 0; i < sTDocUrls.length; i++){
205 addSingleMenuItem(_jMnuRoot, sTDocTitles[i], sTDocUrls[i], oActionListener);
210 private void addApplicationDocumentMenu(JMenu _jMnuRoot){
211 ActionListener oActionListener = new ActionListener(){
212 public void actionPerformed(java.awt.event.ActionEvent evt) {
213 String sApplicationDocUrl = evt.getActionCommand();
214 m_oInspector.inspectOpenEmptyDocument(sApplicationDocUrl);
217 String[][] sApplUrls = m_oInspector.getApplicationUrls();
218 for (int i = 0; i < sApplUrls.length; i++){
219 addSingleMenuItem(_jMnuRoot, sApplUrls[i][1], sApplUrls[i][0], oActionListener);
224 private void addSingleMenuItem(JMenu _jMnuOpenDocs, String _sTitle, String _sActionCommand, ActionListener _oActionListener){
225 javax.swing.JMenuItem jMnuItemOpenDoc = new javax.swing.JMenuItem(_sTitle);
226 jMnuItemOpenDoc.setActionCommand(_sActionCommand);
227 jMnuItemOpenDoc.addActionListener(_oActionListener);
228 _jMnuOpenDocs.add(jMnuItemOpenDoc);
232 private void addHelpMenu(JMenuBar _jInspectMenuBar){
233 JMenu jMnuHelp = new JMenu("Help");
234 jMnuHelp.add(getHelpMenuItem("Idl-Help"));
235 _jInspectMenuBar.add(jMnuHelp);
239 private JMenuItem getHelpMenuItem(String _sMenuTitle){
240 JMenuItem jMnuHelpItem = new JMenuItem(_sMenuTitle);
241 jMnuHelpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
242 jMnuHelpItem.setMnemonic('H');
243 jMnuHelpItem.addActionListener(new ActionListener(){
244 public void actionPerformed(java.awt.event.ActionEvent evt) {
245 m_oInspector.openIdlFileforSelectedNode();
248 return jMnuHelpItem;
251 private void addFileMenu(JMenuBar _jInspectMenuBar){
252 JMenu jMnuFile = new JMenu("File");
253 JMenuItem jMnuItemRemoveInspector = new JMenuItem("Remove");
254 jMnuItemRemoveInspector.addActionListener(new ActionListener(){
255 public void actionPerformed(java.awt.event.ActionEvent evt) {
256 removeSelectedTabPane();
259 jMnuFile.add(jMnuItemRemoveInspector);
260 JMenuItem jMnuItemExit = new JMenuItem("Exit");
261 jMnuItemExit.addActionListener(new ActionListener(){
262 public void actionPerformed(java.awt.event.ActionEvent evt) {
263 getDialog().dispose();
266 jMnuFile.add(jMnuItemExit);
267 _jInspectMenuBar.add(jMnuFile);
270 private JMenuItem getInspectMenuItem(String _sLabel){
271 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
272 jMnuSelectedObject.addActionListener(new ActionListener(){
273 public void actionPerformed(java.awt.event.ActionEvent evt) {
274 m_oInspector.inspectSelectedNode();
277 return jMnuSelectedObject;
281 private JMenuItem getSourceCodeMenuItem(String _sLabel){
282 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
283 jMnuSelectedObject.addActionListener(new ActionListener(){
284 public void actionPerformed(java.awt.event.ActionEvent evt) {
285 m_oInspector.addSourceCodeOfSelectedNode();
288 return jMnuSelectedObject;
291 private JMenuItem getInvokeMenuItem(String _sLabel){
292 JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
293 jMnuSelectedObject.addActionListener(new ActionListener(){
294 public void actionPerformed(java.awt.event.ActionEvent evt) {
295 m_oInspector.invokeSelectedMethod();
298 return jMnuSelectedObject;
302 private void addInspectMenu(JMenuBar _jInspectMenuBar){
303 JMenu jMnuInspect = new JMenu("Inspect");
304 addApplicationDocumentMenu(jMnuInspect);
305 jMnuInspect.addSeparator();
306 addGlobalServiceManagerMenu(jMnuInspect);
307 jMnuInspect.addSeparator();
308 jMnuInspect.add(getInspectMenuItem("Selected Object"));
309 jMnuInspect.addSeparator();
310 addOpenDocumentMenu(jMnuInspect);
311 _jInspectMenuBar.add(jMnuInspect);
314 public int getLanguage(){
315 return XLanguageSourceCodeGenerator.nJAVA;
319 public void selectSourceCodeLanguage(int _nLanguage){
320 switch (_nLanguage){
321 case XLanguageSourceCodeGenerator.nJAVA:
322 jJavaMenuItem.setSelected(true);
323 break;
324 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
325 jCPlusPlusMenuItem.setSelected(true);
326 break;
327 case XLanguageSourceCodeGenerator.nBASIC:
328 jBasicMenuItem.setSelected(true);
329 break;
330 default:
331 System.out.println("Warning: Sourcecode language is not defined!");
335 private JRadioButtonMenuItem addLanguageMenuItem(ButtonGroup _group, String _sLanguageTitle, boolean _bSelect, char _sMnemonic, final int _nLanguage){
336 JRadioButtonMenuItem jMenuItem = new JRadioButtonMenuItem(_sLanguageTitle, _bSelect);
337 jMenuItem.setMnemonic(_sMnemonic);
338 _group.add(jMenuItem);
339 jMenuItem.addActionListener(new ActionListener(){
340 public void actionPerformed(java.awt.event.ActionEvent evt) {
341 m_oInspector.setSourceCodeLanguage(_nLanguage);
344 return jMenuItem;
348 public String getIDLPath(){
349 return this.m_oInspector.getIDLPath();
352 private void addOptionsMenu(JMenuBar _jInspectMenuBar){
353 ButtonGroup oButtonGroup = new ButtonGroup();
354 jJavaMenuItem = addLanguageMenuItem(oButtonGroup, "Generate Java Sourcecode", true, 'J', XLanguageSourceCodeGenerator.nJAVA);
355 jMnuOptions.add(jJavaMenuItem);
356 jCPlusPlusMenuItem = addLanguageMenuItem(oButtonGroup, "Generate C++ Sourcecode", false, 'C', XLanguageSourceCodeGenerator.nCPLUSPLUS);
357 jMnuOptions.add(jCPlusPlusMenuItem);
358 jBasicMenuItem = addLanguageMenuItem(oButtonGroup, "Generate OpenOffice.org Basic Sourcecode", false, 'B', XLanguageSourceCodeGenerator.nBASIC);
359 jMnuOptions.add(jBasicMenuItem);
360 jMnuOptions.addSeparator();
361 JMenuItem jMenuItem = new JMenuItem("Path to SDK-Installation");
362 jMenuItem.setMnemonic('I');
363 jMenuItem.addActionListener(new ActionListener(){
364 public void actionPerformed(java.awt.event.ActionEvent evt) {
365 m_oInspector.assignSDKPath();
369 jMnuOptions.add(jMenuItem);
370 _jInspectMenuBar.add(jMnuOptions);
374 private void insertMenus(){
375 JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
376 addFileMenu(jMenuBar1);
377 addInspectMenu(jMenuBar1);
378 JMenu jMnuEdit = new JMenu("Edit");
379 JMenu jMnuView = new JMenu("View");
380 addOptionsMenu(jMenuBar1);
381 jMenuBar1.setFont(new java.awt.Font("Dialog", 0, 12));
382 jMenuBar1.add(jMnuEdit);
383 jMenuBar1.add(jMnuView);
384 addHelpMenu(jMenuBar1);
385 addMenuBar(jMenuBar1);
388 private void addGlobalServiceManagerMenu(JMenu _jMnuRoot){
389 JMenuItem jMnuGlobalServiceManager = new JMenuItem("Global Service Manager");
390 jMnuGlobalServiceManager.addActionListener(new ActionListener(){
391 public void actionPerformed(java.awt.event.ActionEvent evt) {
392 m_oInspector.inspect(m_xComponentContext.getServiceManager(), "Global ServiceManager");
395 _jMnuRoot.add(jMnuGlobalServiceManager);
400 public void enablePopupMenuItem(String _sMenuTitle, boolean _bdoEnable){
401 Component[] oComponents = m_jPopupMenu.getComponents();
402 for (int i = 0; i < oComponents.length; i++){
403 if (oComponents[i] instanceof JMenuItem){
404 JMenuItem jMenuItem = (JMenuItem) oComponents[i];
405 if (jMenuItem.getText().equals(_sMenuTitle)){
406 jMenuItem.setEnabled(_bdoEnable);
413 public void showPopUpMenu(Object _invoker, int x, int y) throws ClassCastException{
414 if (_invoker instanceof Component){
415 m_jPopupMenu.show((Component) _invoker, x, y);
420 public void show(int _nPageIndex){
421 Dimension aDimension = m_jInspectorDialog.getSize();
422 selectInspectorPageByIndex(_nPageIndex);
423 if (_nPageIndex > 0){
424 m_jInspectorDialog.setSize(aDimension);
426 else{
427 m_jInspectorDialog.pack();
429 m_jInspectorDialog.setVisible(true);
432 public void paint(){
433 m_jTabbedPane1.paintImmediately(m_jTabbedPane1.getBounds());
437 public void addInspectorPage(String _sTitle, Object _oContainer) throws ClassCastException{
438 if (_oContainer instanceof Component){
439 m_jTabbedPane1.addTab(_sTitle, (Component) _oContainer);
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */