1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
.JPanel
;
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();
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();
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 public void addMenuBar(JMenuBar _jMenuBar
){
96 getDialog().setJMenuBar(_jMenuBar
);
100 public void removeTabPaneByIndex(int _nIndex
){
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;
133 JPanel jPnlContainerInspectorPanel
= (JPanel
) m_jTabbedPane1
.getComponentAt(_nIndex
);
134 String sInspectorPanelTitle
= m_jTabbedPane1
.getTitleAt(_nIndex
);
135 oInspectorPane
= m_oInspector
.getInspectorPages().get(sInspectorPanelTitle
);
137 return oInspectorPane
;
141 public void removeTabPanes(){
142 int nCount
= m_jTabbedPane1
.getTabCount();
144 for (int i
= nCount
-1; i
>= 0; i
--){
145 removeTabPaneByIndex(i
);
150 public void removeSelectedTabPane(){
151 int nIndex
= getTabbedPane().getSelectedIndex();
152 removeTabPaneByIndex(nIndex
);
156 private class InspectorComponentAdapter
extends ComponentAdapter
{
157 public void componentHidden(ComponentEvent e
){
158 m_jInspectorDialog
.pack();
159 m_jInspectorDialog
.invalidate();
163 public void componentShown(ComponentEvent e
){
164 m_jInspectorDialog
.pack();
165 m_jInspectorDialog
.invalidate();
169 private class InspectorWindowAdapter
extends WindowAdapter
{
170 public void windowClosed(WindowEvent e
){
172 m_oInspector
.disposeHiddenDocuments();
175 public void windowClosing(WindowEvent e
){
177 m_oInspector
.disposeHiddenDocuments();
183 private void initializePopupMenu(){
184 m_jPopupMenu
.add(getInspectMenuItem("Inspect"));
185 m_jPopupMenu
.add(getSourceCodeMenuItem(SADDTOSOURCECODE
));
186 m_jPopupMenu
.add(getInvokeMenuItem(SINVOKE
));
187 m_jPopupMenu
.addSeparator();
188 m_jPopupMenu
.add(getHelpMenuItem("Help"));
192 private void addOpenDocumentMenu(JMenu _jMnuRoot
){
193 ActionListener oActionListener
= new ActionListener(){
194 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
195 String sTDocUrl
= evt
.getActionCommand();
196 m_oInspector
.inspectOpenDocument(sTDocUrl
);
199 String
[] sTDocUrls
= m_oInspector
.getTDocUrls();
200 String
[] sTDocTitles
= m_oInspector
.getTDocTitles(sTDocUrls
);
201 for (int i
= 0; i
< sTDocUrls
.length
; i
++){
202 addSingleMenuItem(_jMnuRoot
, sTDocTitles
[i
], sTDocUrls
[i
], oActionListener
);
207 private void addApplicationDocumentMenu(JMenu _jMnuRoot
){
208 ActionListener oActionListener
= new ActionListener(){
209 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
210 String sApplicationDocUrl
= evt
.getActionCommand();
211 m_oInspector
.inspectOpenEmptyDocument(sApplicationDocUrl
);
214 String
[][] sApplUrls
= m_oInspector
.getApplicationUrls();
215 for (int i
= 0; i
< sApplUrls
.length
; i
++){
216 addSingleMenuItem(_jMnuRoot
, sApplUrls
[i
][1], sApplUrls
[i
][0], oActionListener
);
221 private void addSingleMenuItem(JMenu _jMnuOpenDocs
, String _sTitle
, String _sActionCommand
, ActionListener _oActionListener
){
222 javax
.swing
.JMenuItem jMnuItemOpenDoc
= new javax
.swing
.JMenuItem(_sTitle
);
223 jMnuItemOpenDoc
.setActionCommand(_sActionCommand
);
224 jMnuItemOpenDoc
.addActionListener(_oActionListener
);
225 _jMnuOpenDocs
.add(jMnuItemOpenDoc
);
229 private void addHelpMenu(JMenuBar _jInspectMenuBar
){
230 JMenu jMnuHelp
= new JMenu("Help");
231 jMnuHelp
.add(getHelpMenuItem("Idl-Help"));
232 _jInspectMenuBar
.add(jMnuHelp
);
236 private JMenuItem
getHelpMenuItem(String _sMenuTitle
){
237 JMenuItem jMnuHelpItem
= new JMenuItem(_sMenuTitle
);
238 jMnuHelpItem
.setAccelerator(KeyStroke
.getKeyStroke(KeyEvent
.VK_F1
, 0));
239 jMnuHelpItem
.setMnemonic('H');
240 jMnuHelpItem
.addActionListener(new ActionListener(){
241 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
242 m_oInspector
.openIdlFileforSelectedNode();
248 private void addFileMenu(JMenuBar _jInspectMenuBar
){
249 JMenu jMnuFile
= new JMenu("File");
250 JMenuItem jMnuItemRemoveInspector
= new JMenuItem("Remove");
251 jMnuItemRemoveInspector
.addActionListener(new ActionListener(){
252 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
253 removeSelectedTabPane();
256 jMnuFile
.add(jMnuItemRemoveInspector
);
257 JMenuItem jMnuItemExit
= new JMenuItem("Exit");
258 jMnuItemExit
.addActionListener(new ActionListener(){
259 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
260 getDialog().dispose();
263 jMnuFile
.add(jMnuItemExit
);
264 _jInspectMenuBar
.add(jMnuFile
);
267 private JMenuItem
getInspectMenuItem(String _sLabel
){
268 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
269 jMnuSelectedObject
.addActionListener(new ActionListener(){
270 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
271 m_oInspector
.inspectSelectedNode();
274 return jMnuSelectedObject
;
278 private JMenuItem
getSourceCodeMenuItem(String _sLabel
){
279 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
280 jMnuSelectedObject
.addActionListener(new ActionListener(){
281 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
282 m_oInspector
.addSourceCodeOfSelectedNode();
285 return jMnuSelectedObject
;
288 private JMenuItem
getInvokeMenuItem(String _sLabel
){
289 JMenuItem jMnuSelectedObject
= new JMenuItem(_sLabel
);
290 jMnuSelectedObject
.addActionListener(new ActionListener(){
291 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
292 m_oInspector
.invokeSelectedMethod();
295 return jMnuSelectedObject
;
299 private void addInspectMenu(JMenuBar _jInspectMenuBar
){
300 JMenu jMnuInspect
= new JMenu("Inspect");
301 addApplicationDocumentMenu(jMnuInspect
);
302 jMnuInspect
.addSeparator();
303 addGlobalServiceManagerMenu(jMnuInspect
);
304 jMnuInspect
.addSeparator();
305 jMnuInspect
.add(getInspectMenuItem("Selected Object"));
306 jMnuInspect
.addSeparator();
307 addOpenDocumentMenu(jMnuInspect
);
308 _jInspectMenuBar
.add(jMnuInspect
);
311 public int getLanguage(){
312 return XLanguageSourceCodeGenerator
.nJAVA
;
316 public void selectSourceCodeLanguage(int _nLanguage
){
318 case XLanguageSourceCodeGenerator
.nJAVA
:
319 jJavaMenuItem
.setSelected(true);
321 case XLanguageSourceCodeGenerator
.nCPLUSPLUS
:
322 jCPlusPlusMenuItem
.setSelected(true);
324 case XLanguageSourceCodeGenerator
.nBASIC
:
325 jBasicMenuItem
.setSelected(true);
328 System
.out
.println("Warning: Sourcecode language is not defined!");
332 private JRadioButtonMenuItem
addLanguageMenuItem(ButtonGroup _group
, String _sLanguageTitle
, boolean _bSelect
, char _sMnemonic
, final int _nLanguage
){
333 JRadioButtonMenuItem jMenuItem
= new JRadioButtonMenuItem(_sLanguageTitle
, _bSelect
);
334 jMenuItem
.setMnemonic(_sMnemonic
);
335 _group
.add(jMenuItem
);
336 jMenuItem
.addActionListener(new ActionListener(){
337 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
338 m_oInspector
.setSourceCodeLanguage(_nLanguage
);
345 public String
getIDLPath(){
346 return this.m_oInspector
.getIDLPath();
349 private void addOptionsMenu(JMenuBar _jInspectMenuBar
){
350 ButtonGroup oButtonGroup
= new ButtonGroup();
351 jJavaMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate Java Sourcecode", true, 'J', XLanguageSourceCodeGenerator
.nJAVA
);
352 jMnuOptions
.add(jJavaMenuItem
);
353 jCPlusPlusMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate C++ Sourcecode", false, 'C', XLanguageSourceCodeGenerator
.nCPLUSPLUS
);
354 jMnuOptions
.add(jCPlusPlusMenuItem
);
355 jBasicMenuItem
= addLanguageMenuItem(oButtonGroup
, "Generate OpenOffice.org Basic Sourcecode", false, 'B', XLanguageSourceCodeGenerator
.nBASIC
);
356 jMnuOptions
.add(jBasicMenuItem
);
357 jMnuOptions
.addSeparator();
358 JMenuItem jMenuItem
= new JMenuItem("Path to SDK-Installation");
359 jMenuItem
.setMnemonic('I');
360 jMenuItem
.addActionListener(new ActionListener(){
361 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
362 m_oInspector
.assignSDKPath();
366 jMnuOptions
.add(jMenuItem
);
367 _jInspectMenuBar
.add(jMnuOptions
);
371 private void insertMenus(){
372 JMenuBar jMenuBar1
= new javax
.swing
.JMenuBar();
373 addFileMenu(jMenuBar1
);
374 addInspectMenu(jMenuBar1
);
375 JMenu jMnuEdit
= new JMenu("Edit");
376 JMenu jMnuView
= new JMenu("View");
377 addOptionsMenu(jMenuBar1
);
378 jMenuBar1
.setFont(new java
.awt
.Font("Dialog", 0, 12));
379 jMenuBar1
.add(jMnuEdit
);
380 jMenuBar1
.add(jMnuView
);
381 addHelpMenu(jMenuBar1
);
382 addMenuBar(jMenuBar1
);
385 private void addGlobalServiceManagerMenu(JMenu _jMnuRoot
){
386 JMenuItem jMnuGlobalServiceManager
= new JMenuItem("Global Service Manager");
387 jMnuGlobalServiceManager
.addActionListener(new ActionListener(){
388 public void actionPerformed(java
.awt
.event
.ActionEvent evt
) {
389 m_oInspector
.inspect(m_xComponentContext
.getServiceManager(), "Global ServiceManager");
392 _jMnuRoot
.add(jMnuGlobalServiceManager
);
397 public void enablePopupMenuItem(String _sMenuTitle
, boolean _bdoEnable
){
398 Component
[] oComponents
= m_jPopupMenu
.getComponents();
399 for (int i
= 0; i
< oComponents
.length
; i
++){
400 if (oComponents
[i
] instanceof JMenuItem
){
401 JMenuItem jMenuItem
= (JMenuItem
) oComponents
[i
];
402 if (jMenuItem
.getText().equals(_sMenuTitle
)){
403 jMenuItem
.setEnabled(_bdoEnable
);
410 public void showPopUpMenu(Object _invoker
, int x
, int y
) throws ClassCastException
{
411 if (_invoker
instanceof Component
){
412 m_jPopupMenu
.show((Component
) _invoker
, x
, y
);
417 public void show(int _nPageIndex
){
418 Dimension aDimension
= m_jInspectorDialog
.getSize();
419 selectInspectorPageByIndex(_nPageIndex
);
420 if (_nPageIndex
> 0){
421 m_jInspectorDialog
.setSize(aDimension
);
424 m_jInspectorDialog
.pack();
426 // m_jInspectorDialog.paint(m_jInspectorDialog.getGraphics());
427 m_jInspectorDialog
.setVisible(true);
431 m_jTabbedPane1
.paintImmediately(m_jTabbedPane1
.getBounds());
435 public void addInspectorPage(String _sTitle
, Object _oContainer
) throws ClassCastException
{
436 if (_oContainer
instanceof Component
){
437 m_jTabbedPane1
.addTab(_sTitle
, (Component
) _oContainer
);