1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
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.
20  *
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.
32  *
33  *************************************************************************/
34 
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.JCheckBoxMenuItem;
47 import javax.swing.JDialog;
48 import javax.swing.JEditorPane;
49 import javax.swing.JMenu;
50 import javax.swing.JMenuBar;
51 import javax.swing.JMenuItem;
52 import javax.swing.JPanel;
53 import javax.swing.JPopupMenu;
54 import javax.swing.JRadioButtonMenuItem;
55 import javax.swing.JTabbedPane;
56 import javax.swing.KeyStroke;
57 
58 
59 
60 public class SwingDialogProvider implements XDialogProvider{
61 
62     private JPopupMenu m_jPopupMenu = new JPopupMenu();
63     private XComponentContext m_xComponentContext;
64     private Inspector._Inspector m_oInspector;
65     private JDialog m_jInspectorDialog = new JDialog();
66     private JTabbedPane m_jTabbedPane1 = new JTabbedPane();
67     private Container cp;
68 
69     private JMenu jMnuOptions = new JMenu("Options");
70     private JRadioButtonMenuItem jJavaMenuItem = null;
71     private JRadioButtonMenuItem jCPlusPlusMenuItem = null;
72     private JRadioButtonMenuItem jBasicMenuItem = null;
73 
74     /** Creates a new instance of SwingPopupMentuProvider */
75     public SwingDialogProvider(Inspector._Inspector _oInspector, String _sTitle) {
76         m_oInspector = _oInspector;
77         m_xComponentContext = _oInspector.getXComponentContext();
78         insertMenus();
79         initializePopupMenu();
80         cp = m_jInspectorDialog.getContentPane();
81         cp.setLayout(new java.awt.BorderLayout(0, 10));
82         m_jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
83         m_jInspectorDialog.addWindowListener(new InspectorWindowAdapter());
84         m_jInspectorDialog.addComponentListener(new InspectorComponentAdapter());
85         m_jInspectorDialog.setTitle(_sTitle);
86         m_jInspectorDialog.setLocation(100, 50);
87         m_jInspectorDialog.getContentPane().add(m_jTabbedPane1);
88     }
89 
90 
91 
92         public JDialog getDialog(){
93             return m_jInspectorDialog;
94         }
95 
96 
97         public void addMenuBar(JMenuBar _jMenuBar){
98             getDialog().setJMenuBar(_jMenuBar);
99         }
100 
101 
102         public void  removeTabPaneByIndex(int _nIndex){
103             if (_nIndex > -1){
104                 String sSelInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
105                 m_jTabbedPane1.remove(_nIndex);
106                 m_oInspector.getInspectorPages().remove(sSelInspectorPanelTitle);
107             }
108         }
109 
110 
111         public void selectInspectorPageByIndex(int nTabIndex){
112             m_jTabbedPane1.setSelectedIndex(nTabIndex);
113         }
114 
115 
116         public int getInspectorPageCount(){
117             return  m_jTabbedPane1.getTabCount();
118         }
119 
120 
121         public JTabbedPane getTabbedPane(){
122             return m_jTabbedPane1;
123         }
124 
125 
126         public InspectorPane getSelectedInspectorPage(){
127             int nIndex = m_jTabbedPane1.getSelectedIndex();
128             return getInspectorPage(nIndex);
129         }
130 
131 
132         public InspectorPane getInspectorPage(int _nIndex){
133             InspectorPane oInspectorPane = null;
134             if (_nIndex > -1){
135                 JPanel jPnlContainerInspectorPanel = (JPanel) m_jTabbedPane1.getComponentAt(_nIndex);
136                 String sInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
137                 oInspectorPane = (InspectorPane) m_oInspector.getInspectorPages().get(sInspectorPanelTitle);
138             }
139             return oInspectorPane;
140         }
141 
142 
143         public void removeTabPanes(){
144             int nCount = m_jTabbedPane1.getTabCount();
145             if (nCount > 0){
146                 for (int i = nCount-1; i >= 0; i--){
147                     removeTabPaneByIndex(i);
148                 }
149             }
150         }
151 
152         public void removeSelectedTabPane(){
153             int nIndex = getTabbedPane().getSelectedIndex();
154             removeTabPaneByIndex(nIndex);
155         }
156 
157 
158         private class InspectorComponentAdapter extends ComponentAdapter{
159             public void componentHidden(ComponentEvent e){
160                 m_jInspectorDialog.pack();
161                 m_jInspectorDialog.invalidate();
162 
163             }
164 
165             public void componentShown(ComponentEvent e){
166                 m_jInspectorDialog.pack();
167                 m_jInspectorDialog.invalidate();
168             }
169         }
170 
171         private class InspectorWindowAdapter extends WindowAdapter{
172             public void windowClosed(WindowEvent e){
173                 removeTabPanes();
174                 m_oInspector.disposeHiddenDocuments();
175             }
176 
177             public void windowClosing(WindowEvent e){
178                 removeTabPanes();
179                 m_oInspector.disposeHiddenDocuments();
180             }
181         }
182 
183 
184 
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"));
191         }
192 
193 
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);
199                 }
200             };
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);
205             }
206         }
207 
208 
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);
214                 }
215             };
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);
219             }
220         }
221 
222 
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);
228         }
229 
230 
231         private void addHelpMenu(JMenuBar _jInspectMenuBar){
232             JMenu jMnuHelp = new JMenu("Help");
233             jMnuHelp.add(getHelpMenuItem("Idl-Help"));
234             _jInspectMenuBar.add(jMnuHelp);
235         }
236 
237 
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();
245                 }
246             });
247             return jMnuHelpItem;
248         }
249 
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();
256                 }
257             });
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();
263                 }
264             });
265             jMnuFile.add(jMnuItemExit);
266             _jInspectMenuBar.add(jMnuFile);
267         }
268 
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();
274                 }
275             });
276             return jMnuSelectedObject;
277         }
278 
279 
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();
285                 }
286             });
287             return jMnuSelectedObject;
288         }
289 
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();
295                 }
296             });
297             return jMnuSelectedObject;
298         }
299 
300 
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);
311         }
312 
313         public int getLanguage(){
314             return XLanguageSourceCodeGenerator.nJAVA;
315         }
316 
317 
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!");
331             }
332         }
333 
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);
341                 }
342             });
343             return jMenuItem;
344         }
345 
346 
347         public String getIDLPath(){
348             return this.m_oInspector.getIDLPath();
349         }
350 
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();
365                 }
366             });
367 
368             jMnuOptions.add(jMenuItem);
369             _jInspectMenuBar.add(jMnuOptions);
370         }
371 
372 
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);
385         }
386 
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");
392                 }
393             });
394             _jMnuRoot.add(jMnuGlobalServiceManager);
395         }
396 
397 
398 
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);
406                     }
407                 }
408             }
409         }
410 
411 
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);
415             }
416         }
417 
418 
419     public void show(int _nPageIndex){
420         Dimension aDimension = m_jInspectorDialog.getSize();
421         selectInspectorPageByIndex(_nPageIndex);
422         if (_nPageIndex > 0){
423             m_jInspectorDialog.setSize(aDimension);
424         }
425         else{
426             m_jInspectorDialog.pack();
427         }
428 //            m_jInspectorDialog.paint(m_jInspectorDialog.getGraphics());
429         m_jInspectorDialog.setVisible(true);
430     }
431 
432         public void paint(){
433             m_jTabbedPane1.paintImmediately(m_jTabbedPane1.getBounds());
434         }
435 
436 
437         public void addInspectorPage(String _sTitle, Object _oContainer) throws ClassCastException{
438             if (_oContainer instanceof Component){
439                 m_jTabbedPane1.addTab(_sTitle, (Component) _oContainer);
440             }
441         }
442 }
443