1 /*************************************************************************
3 * $RCSfile: SwingDialogProvider.java,v $
7 * last change: $Author: hr $ $Date: 2007-07-31 13:57:37 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.uno
.XComponentContext
;
42 import java
.awt
.Component
;
43 import java
.awt
.Container
;
44 import java
.awt
.Dimension
;
45 import java
.awt
.event
.ActionListener
;
46 import java
.awt
.event
.ComponentAdapter
;
47 import java
.awt
.event
.ComponentEvent
;
48 import java
.awt
.event
.KeyEvent
;
49 import java
.awt
.event
.WindowAdapter
;
50 import java
.awt
.event
.WindowEvent
;
51 import javax
.swing
.ButtonGroup
;
52 import javax
.swing
.JCheckBoxMenuItem
;
53 import javax
.swing
.JDialog
;
54 import javax
.swing
.JEditorPane
;
55 import javax
.swing
.JMenu
;
56 import javax
.swing
.JMenuBar
;
57 import javax
.swing
.JMenuItem
;
58 import javax
.swing
.JPanel
;
59 import javax
.swing
.JPopupMenu
;
60 import javax
.swing
.JRadioButtonMenuItem
;
61 import javax
.swing
.JTabbedPane
;
62 import javax
.swing
.KeyStroke
;
66 public class SwingDialogProvider
implements XDialogProvider
{
68 private JPopupMenu m_jPopupMenu
= new JPopupMenu();
69 private XComponentContext m_xComponentContext
;
70 private Inspector
._Inspector m_oInspector
;
71 private JDialog m_jInspectorDialog
= new JDialog();
72 private JTabbedPane m_jTabbedPane1
= new JTabbedPane();
75 private JMenu jMnuOptions
= new JMenu("Options");
76 private JRadioButtonMenuItem jJavaMenuItem
= null;
77 private JRadioButtonMenuItem jCPlusPlusMenuItem
= null;
78 private JRadioButtonMenuItem jBasicMenuItem
= null;
80 /** Creates a new instance of SwingPopupMentuProvider */
81 public SwingDialogProvider(Inspector
._Inspector _oInspector
, String _sTitle
) {
82 m_oInspector
= _oInspector
;
83 m_xComponentContext
= _oInspector
.getXComponentContext();
85 initializePopupMenu();
86 cp
= m_jInspectorDialog
.getContentPane();
87 cp
.setLayout(new java
.awt
.BorderLayout(0, 10));
88 m_jTabbedPane1
.setTabLayoutPolicy(JTabbedPane
.SCROLL_TAB_LAYOUT
);
89 m_jInspectorDialog
.addWindowListener(new InspectorWindowAdapter());
90 m_jInspectorDialog
.addComponentListener(new InspectorComponentAdapter());
91 m_jInspectorDialog
.setTitle(_sTitle
);
92 m_jInspectorDialog
.setLocation(100, 50);
93 m_jInspectorDialog
.getContentPane().add(m_jTabbedPane1
);
98 public JDialog
getDialog(){
99 return m_jInspectorDialog
;
103 public void addMenuBar(JMenuBar _jMenuBar
){
104 getDialog().setJMenuBar(_jMenuBar
);
108 public void removeTabPaneByIndex(int _nIndex
){
110 String sSelInspectorPanelTitle
= m_jTabbedPane1
.getTitleAt(_nIndex
);
111 m_jTabbedPane1
.remove(_nIndex
);
112 m_oInspector
.getInspectorPages().remove(sSelInspectorPanelTitle
);
117 public void selectInspectorPageByIndex(int nTabIndex
){
118 m_jTabbedPane1
.setSelectedIndex(nTabIndex
);
122 public int getInspectorPageCount(){
123 return m_jTabbedPane1
.getTabCount();
127 public JTabbedPane
getTabbedPane(){
128 return m_jTabbedPane1
;
132 public InspectorPane
getSelectedInspectorPage(){
133 int nIndex
= m_jTabbedPane1
.getSelectedIndex();
134 return getInspectorPage(nIndex
);
138 public InspectorPane
getInspectorPage(int _nIndex
){
139 InspectorPane oInspectorPane
= null;
141 JPanel jPnlContainerInspectorPanel
= (JPanel
) m_jTabbedPane1
.getComponentAt(_nIndex
);
142 String sInspectorPanelTitle
= m_jTabbedPane1
.getTitleAt(_nIndex
);
143 oInspectorPane
= (InspectorPane
) m_oInspector
.getInspectorPages().get(sInspectorPanelTitle
);
145 return oInspectorPane
;
149 public void removeTabPanes(){
150 int nCount
= m_jTabbedPane1
.getTabCount();
152 for (int i
= nCount
-1; i
>= 0; i
--){
153 removeTabPaneByIndex(i
);
158 public void removeSelectedTabPane(){
159 int nIndex
= getTabbedPane().getSelectedIndex();
160 removeTabPaneByIndex(nIndex
);
164 private class InspectorComponentAdapter
extends ComponentAdapter
{
165 public void componentHidden(ComponentEvent e
){
166 m_jInspectorDialog
.pack();
167 m_jInspectorDialog
.invalidate();
171 public void componentShown(ComponentEvent e
){
172 m_jInspectorDialog
.pack();
173 m_jInspectorDialog
.invalidate();
177 private class InspectorWindowAdapter
extends WindowAdapter
{
178 public void windowClosed(WindowEvent e
){
180 m_oInspector
.disposeHiddenDocuments();
183 public void windowClosing(WindowEvent e
){
185 m_oInspector
.disposeHiddenDocuments();
191 private void initializePopupMenu(){
192 m_jPopupMenu
.add(getInspectMenuItem("Inspect"));
193 m_jPopupMenu
.add(getSourceCodeMenuItem(SADDTOSOURCECODE
));
194 m_jPopupMenu
.add(getInvokeMenuItem(SINVOKE
));
195 m_jPopupMenu
.addSeparator();
196 m_jPopupMenu
.add(getHelpMenuItem("Help"));
200 private void addOpenDocumentMenu(JMenu _jMnuRoot
){
201 ActionListener oActionListener
= new ActionListener(){
202 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
203 String sTDocUrl
= evt
.getActionCommand();
204 m_oInspector
.inspectOpenDocument(sTDocUrl
);
207 String
[] sTDocUrls
= m_oInspector
.getTDocUrls();
208 String
[] sTDocTitles
= m_oInspector
.getTDocTitles(sTDocUrls
);
209 for (int i
= 0; i
< sTDocUrls
.length
; i
++){
210 addSingleMenuItem(_jMnuRoot
, sTDocTitles
[i
], sTDocUrls
[i
], oActionListener
);
215 private void addApplicationDocumentMenu(JMenu _jMnuRoot
){
216 ActionListener oActionListener
= new ActionListener(){
217 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
218 String sApplicationDocUrl
= evt
.getActionCommand();
219 m_oInspector
.inspectOpenEmptyDocument(sApplicationDocUrl
);
222 String
[][] sApplUrls
= m_oInspector
.getApplicationUrls();
223 for (int i
= 0; i
< sApplUrls
.length
; i
++){
224 addSingleMenuItem(_jMnuRoot
, sApplUrls
[i
][1], sApplUrls
[i
][0], oActionListener
);
229 private void addSingleMenuItem(JMenu _jMnuOpenDocs
, String _sTitle
, String _sActionCommand
, ActionListener _oActionListener
){
230 javax
.swing
.JMenuItem jMnuItemOpenDoc
= new javax
.swing
.JMenuItem(_sTitle
);
231 jMnuItemOpenDoc
.setActionCommand(_sActionCommand
);
232 jMnuItemOpenDoc
.addActionListener(_oActionListener
);
233 _jMnuOpenDocs
.add(jMnuItemOpenDoc
);
237 private void addHelpMenu(JMenuBar _jInspectMenuBar
){
238 JMenu jMnuHelp
= new JMenu("Help");
239 jMnuHelp
.add(getHelpMenuItem("Idl-Help"));
240 _jInspectMenuBar
.add(jMnuHelp
);
244 private JMenuItem
getHelpMenuItem(String _sMenuTitle
){
245 JMenuItem jMnuHelpItem
= new JMenuItem(_sMenuTitle
);
246 jMnuHelpItem
.setAccelerator(KeyStroke
.getKeyStroke(KeyEvent
.VK_F1
, 0));
247 jMnuHelpItem
.setMnemonic('H');
248 jMnuHelpItem
.addActionListener(new ActionListener(){
249 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
250 m_oInspector
.openIdlFileforSelectedNode();
256 private void addFileMenu(JMenuBar _jInspectMenuBar
){
257 JMenu jMnuFile
= new JMenu("File");
258 JMenuItem jMnuItemRemoveInspector
= new JMenuItem("Remove");
259 jMnuItemRemoveInspector
.addActionListener(new ActionListener(){
260 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
261 removeSelectedTabPane();
264 jMnuFile
.add(jMnuItemRemoveInspector
);
265 JMenuItem jMnuItemExit
= new JMenuItem("Exit");
266 jMnuItemExit
.addActionListener(new ActionListener(){
267 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
268 getDialog().dispose();
271 jMnuFile
.add(jMnuItemExit
);
272 _jInspectMenuBar
.add(jMnuFile
);
275 private JMenuItem
getInspectMenuItem(String _sLabel
){
276 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
277 jMnuSelectedObject
.addActionListener(new ActionListener(){
278 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
279 m_oInspector
.inspectSelectedNode();
282 return jMnuSelectedObject
;
286 private JMenuItem
getSourceCodeMenuItem(String _sLabel
){
287 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
288 jMnuSelectedObject
.addActionListener(new ActionListener(){
289 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
290 m_oInspector
.addSourceCodeOfSelectedNode();
293 return jMnuSelectedObject
;
296 private JMenuItem
getInvokeMenuItem(String _sLabel
){
297 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
298 jMnuSelectedObject
.addActionListener(new ActionListener(){
299 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
300 m_oInspector
.invokeSelectedMethod();
303 return jMnuSelectedObject
;
307 private void addInspectMenu(JMenuBar _jInspectMenuBar
){
308 JMenu jMnuInspect
= new JMenu("Inspect");
309 addApplicationDocumentMenu(jMnuInspect
);
310 jMnuInspect
.addSeparator();
311 addGlobalServiceManagerMenu(jMnuInspect
);
312 jMnuInspect
.addSeparator();
313 jMnuInspect
.add(getInspectMenuItem("Selected Object"));
314 jMnuInspect
.addSeparator();
315 addOpenDocumentMenu(jMnuInspect
);
316 _jInspectMenuBar
.add(jMnuInspect
);
319 public int getLanguage(){
320 return XLanguageSourceCodeGenerator
.nJAVA
;
324 public void selectSourceCodeLanguage(int _nLanguage
){
326 case XLanguageSourceCodeGenerator
.nJAVA
:
327 jJavaMenuItem
.setSelected(true);
329 case XLanguageSourceCodeGenerator
.nCPLUSPLUS
:
330 jCPlusPlusMenuItem
.setSelected(true);
332 case XLanguageSourceCodeGenerator
.nBASIC
:
333 jBasicMenuItem
.setSelected(true);
336 System
.out
.println("Warning: Sourcecode language is not defined!");
340 private JRadioButtonMenuItem
addLanguageMenuItem(ButtonGroup _group
, String _sLanguageTitle
, boolean _bSelect
, char _sMnemonic
, final int _nLanguage
){
341 JRadioButtonMenuItem jMenuItem
= new JRadioButtonMenuItem(_sLanguageTitle
, _bSelect
);
342 jMenuItem
.setMnemonic(_sMnemonic
);
343 _group
.add(jMenuItem
);
344 jMenuItem
.addActionListener(new ActionListener(){
345 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
346 m_oInspector
.setSourceCodeLanguage(_nLanguage
);
353 public String
getIDLPath(){
354 return this.m_oInspector
.getIDLPath();
357 private void addOptionsMenu(JMenuBar _jInspectMenuBar
){
358 ButtonGroup oButtonGroup
= new ButtonGroup();
359 jJavaMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate Java Sourcecode", true, 'J', XLanguageSourceCodeGenerator
.nJAVA
);
360 jMnuOptions
.add(jJavaMenuItem
);
361 jCPlusPlusMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate C++ Sourcecode", false, 'C', XLanguageSourceCodeGenerator
.nCPLUSPLUS
);
362 jMnuOptions
.add(jCPlusPlusMenuItem
);
363 jBasicMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate OpenOffice.org Basic Sourcecode", false, 'B', XLanguageSourceCodeGenerator
.nBASIC
);
364 jMnuOptions
.add(jBasicMenuItem
);
365 jMnuOptions
.addSeparator();
366 JMenuItem jMenuItem
= new JMenuItem("Path to SDK-Installation");
367 jMenuItem
.setMnemonic('I');
368 jMenuItem
.addActionListener(new ActionListener(){
369 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
370 m_oInspector
.assignSDKPath();
374 jMnuOptions
.add(jMenuItem
);
375 _jInspectMenuBar
.add(jMnuOptions
);
379 private void insertMenus(){
380 JMenuBar jMenuBar1
= new javax
.swing
.JMenuBar();
381 addFileMenu(jMenuBar1
);
382 addInspectMenu(jMenuBar1
);
383 JMenu jMnuEdit
= new JMenu("Edit");
384 JMenu jMnuView
= new JMenu("View");
385 addOptionsMenu(jMenuBar1
);
386 jMenuBar1
.setFont(new java
.awt
.Font("Dialog", 0, 12));
387 jMenuBar1
.add(jMnuEdit
);
388 jMenuBar1
.add(jMnuView
);
389 addHelpMenu(jMenuBar1
);
390 addMenuBar(jMenuBar1
);
393 private void addGlobalServiceManagerMenu(JMenu _jMnuRoot
){
394 JMenuItem jMnuGlobalServiceManager
= new JMenuItem("Global Service Manager");
395 jMnuGlobalServiceManager
.addActionListener(new ActionListener(){
396 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
397 m_oInspector
.inspect(m_xComponentContext
.getServiceManager(), "Global ServiceManager");
400 _jMnuRoot
.add(jMnuGlobalServiceManager
);
405 public void enablePopupMenuItem(String _sMenuTitle
, boolean _bdoEnable
){
406 Component
[] oComponents
= m_jPopupMenu
.getComponents();
407 for (int i
= 0; i
< oComponents
.length
; i
++){
408 if (oComponents
[i
] instanceof JMenuItem
){
409 JMenuItem jMenuItem
= (JMenuItem
) oComponents
[i
];
410 if (jMenuItem
.getText().equals(_sMenuTitle
)){
411 jMenuItem
.setEnabled(_bdoEnable
);
418 public void showPopUpMenu(Object _invoker
, int x
, int y
) throws ClassCastException
{
419 if (_invoker
instanceof Component
){
420 m_jPopupMenu
.show((Component
) _invoker
, x
, y
);
425 public void show(int _nPageIndex
){
426 Dimension aDimension
= m_jInspectorDialog
.getSize();
427 selectInspectorPageByIndex(_nPageIndex
);
428 if (_nPageIndex
> 0){
429 m_jInspectorDialog
.setSize(aDimension
);
432 m_jInspectorDialog
.pack();
434 // m_jInspectorDialog.paint(m_jInspectorDialog.getGraphics());
435 m_jInspectorDialog
.setVisible(true);
439 m_jTabbedPane1
.paintImmediately(m_jTabbedPane1
.getBounds());
443 public void addInspectorPage(String _sTitle
, Object _oContainer
) throws ClassCastException
{
444 if (_oContainer
instanceof Component
){
445 m_jTabbedPane1
.addTab(_sTitle
, (Component
) _oContainer
);