1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.XComponentContext;
25 import java.awt.Component;
26 import java.awt.Container;
27 import java.awt.Dimension;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.KeyEvent;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import javax.swing.ButtonGroup;
35 import javax.swing.JCheckBoxMenuItem;
36 import javax.swing.JDialog;
37 import javax.swing.JEditorPane;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuBar;
40 import javax.swing.JMenuItem;
41 import javax.swing.JPanel;
42 import javax.swing.JPopupMenu;
43 import javax.swing.JRadioButtonMenuItem;
44 import javax.swing.JTabbedPane;
45 import javax.swing.KeyStroke;
46 
47 
48 
49 public class SwingDialogProvider implements XDialogProvider{
50 
51     private JPopupMenu m_jPopupMenu = new JPopupMenu();
52     private XComponentContext m_xComponentContext;
53     private Inspector._Inspector m_oInspector;
54     private JDialog m_jInspectorDialog = new JDialog();
55     private JTabbedPane m_jTabbedPane1 = new JTabbedPane();
56     private Container cp;
57 
58     private JMenu jMnuOptions = new JMenu("Options");
59     private JRadioButtonMenuItem jJavaMenuItem = null;
60     private JRadioButtonMenuItem jCPlusPlusMenuItem = null;
61     private JRadioButtonMenuItem jBasicMenuItem = null;
62 
63     /** Creates a new instance of SwingPopupMentuProvider */
SwingDialogProvider(Inspector._Inspector _oInspector, String _sTitle)64     public SwingDialogProvider(Inspector._Inspector _oInspector, String _sTitle) {
65         m_oInspector = _oInspector;
66         m_xComponentContext = _oInspector.getXComponentContext();
67         insertMenus();
68         initializePopupMenu();
69         cp = m_jInspectorDialog.getContentPane();
70         cp.setLayout(new java.awt.BorderLayout(0, 10));
71         m_jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
72         m_jInspectorDialog.addWindowListener(new InspectorWindowAdapter());
73         m_jInspectorDialog.addComponentListener(new InspectorComponentAdapter());
74         m_jInspectorDialog.setTitle(_sTitle);
75         m_jInspectorDialog.setLocation(100, 50);
76         m_jInspectorDialog.getContentPane().add(m_jTabbedPane1);
77     }
78 
79 
80 
getDialog()81         public JDialog getDialog(){
82             return m_jInspectorDialog;
83         }
84 
85 
addMenuBar(JMenuBar _jMenuBar)86         public void addMenuBar(JMenuBar _jMenuBar){
87             getDialog().setJMenuBar(_jMenuBar);
88         }
89 
90 
removeTabPaneByIndex(int _nIndex)91         public void  removeTabPaneByIndex(int _nIndex){
92             if (_nIndex > -1){
93                 String sSelInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
94                 m_jTabbedPane1.remove(_nIndex);
95                 m_oInspector.getInspectorPages().remove(sSelInspectorPanelTitle);
96             }
97         }
98 
99 
selectInspectorPageByIndex(int nTabIndex)100         public void selectInspectorPageByIndex(int nTabIndex){
101             m_jTabbedPane1.setSelectedIndex(nTabIndex);
102         }
103 
104 
getInspectorPageCount()105         public int getInspectorPageCount(){
106             return  m_jTabbedPane1.getTabCount();
107         }
108 
109 
getTabbedPane()110         public JTabbedPane getTabbedPane(){
111             return m_jTabbedPane1;
112         }
113 
114 
getSelectedInspectorPage()115         public InspectorPane getSelectedInspectorPage(){
116             int nIndex = m_jTabbedPane1.getSelectedIndex();
117             return getInspectorPage(nIndex);
118         }
119 
120 
getInspectorPage(int _nIndex)121         public InspectorPane getInspectorPage(int _nIndex){
122             InspectorPane oInspectorPane = null;
123             if (_nIndex > -1){
124                 JPanel jPnlContainerInspectorPanel = (JPanel) m_jTabbedPane1.getComponentAt(_nIndex);
125                 String sInspectorPanelTitle = m_jTabbedPane1.getTitleAt(_nIndex);
126                 oInspectorPane = (InspectorPane) m_oInspector.getInspectorPages().get(sInspectorPanelTitle);
127             }
128             return oInspectorPane;
129         }
130 
131 
removeTabPanes()132         public void removeTabPanes(){
133             int nCount = m_jTabbedPane1.getTabCount();
134             if (nCount > 0){
135                 for (int i = nCount-1; i >= 0; i--){
136                     removeTabPaneByIndex(i);
137                 }
138             }
139         }
140 
removeSelectedTabPane()141         public void removeSelectedTabPane(){
142             int nIndex = getTabbedPane().getSelectedIndex();
143             removeTabPaneByIndex(nIndex);
144         }
145 
146 
147         private class InspectorComponentAdapter extends ComponentAdapter{
componentHidden(ComponentEvent e)148             public void componentHidden(ComponentEvent e){
149                 m_jInspectorDialog.pack();
150                 m_jInspectorDialog.invalidate();
151 
152             }
153 
componentShown(ComponentEvent e)154             public void componentShown(ComponentEvent e){
155                 m_jInspectorDialog.pack();
156                 m_jInspectorDialog.invalidate();
157             }
158         }
159 
160         private class InspectorWindowAdapter extends WindowAdapter{
windowClosed(WindowEvent e)161             public void windowClosed(WindowEvent e){
162                 removeTabPanes();
163                 m_oInspector.disposeHiddenDocuments();
164             }
165 
windowClosing(WindowEvent e)166             public void windowClosing(WindowEvent e){
167                 removeTabPanes();
168                 m_oInspector.disposeHiddenDocuments();
169             }
170         }
171 
172 
173 
initializePopupMenu()174         private void initializePopupMenu(){
175             m_jPopupMenu.add(getInspectMenuItem("Inspect"));
176             m_jPopupMenu.add(getSourceCodeMenuItem(SADDTOSOURCECODE));
177             m_jPopupMenu.add(getInvokeMenuItem(SINVOKE));
178             m_jPopupMenu.addSeparator();
179             m_jPopupMenu.add(getHelpMenuItem("Help"));
180         }
181 
182 
addOpenDocumentMenu(JMenu _jMnuRoot)183         private void addOpenDocumentMenu(JMenu _jMnuRoot){
184             ActionListener oActionListener = new ActionListener(){
185                 public void actionPerformed(java.awt.event.ActionEvent evt) {
186                     String sTDocUrl = evt.getActionCommand();
187                     m_oInspector.inspectOpenDocument(sTDocUrl);
188                 }
189             };
190             String[] sTDocUrls = m_oInspector.getTDocUrls();
191             String[] sTDocTitles = m_oInspector.getTDocTitles(sTDocUrls);
192             for (int i = 0; i < sTDocUrls.length; i++){
193                 addSingleMenuItem(_jMnuRoot, sTDocTitles[i], sTDocUrls[i], oActionListener);
194             }
195         }
196 
197 
addApplicationDocumentMenu(JMenu _jMnuRoot)198         private void addApplicationDocumentMenu(JMenu _jMnuRoot){
199             ActionListener oActionListener = new ActionListener(){
200                 public void actionPerformed(java.awt.event.ActionEvent evt) {
201                     String sApplicationDocUrl = evt.getActionCommand();
202                     m_oInspector.inspectOpenEmptyDocument(sApplicationDocUrl);
203                 }
204             };
205             String[][] sApplUrls = m_oInspector.getApplicationUrls();
206             for (int i = 0; i < sApplUrls.length; i++){
207                 addSingleMenuItem(_jMnuRoot, sApplUrls[i][1], sApplUrls[i][0], oActionListener);
208             }
209         }
210 
211 
addSingleMenuItem(JMenu _jMnuOpenDocs, String _sTitle, String _sActionCommand, ActionListener _oActionListener)212         private void addSingleMenuItem(JMenu _jMnuOpenDocs, String _sTitle, String _sActionCommand, ActionListener _oActionListener){
213             javax.swing.JMenuItem jMnuItemOpenDoc = new javax.swing.JMenuItem(_sTitle);
214             jMnuItemOpenDoc.setActionCommand(_sActionCommand);
215             jMnuItemOpenDoc.addActionListener(_oActionListener);
216             _jMnuOpenDocs.add(jMnuItemOpenDoc);
217         }
218 
219 
addHelpMenu(JMenuBar _jInspectMenuBar)220         private void addHelpMenu(JMenuBar _jInspectMenuBar){
221             JMenu jMnuHelp = new JMenu("Help");
222             jMnuHelp.add(getHelpMenuItem("Idl-Help"));
223             _jInspectMenuBar.add(jMnuHelp);
224         }
225 
226 
getHelpMenuItem(String _sMenuTitle)227         private JMenuItem getHelpMenuItem(String _sMenuTitle){
228             JMenuItem jMnuHelpItem = new JMenuItem(_sMenuTitle);
229             jMnuHelpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
230             jMnuHelpItem.setMnemonic('H');
231             jMnuHelpItem.addActionListener(new ActionListener(){
232                 public void actionPerformed(java.awt.event.ActionEvent evt) {
233                     m_oInspector.openIdlFileforSelectedNode();
234                 }
235             });
236             return jMnuHelpItem;
237         }
238 
addFileMenu(JMenuBar _jInspectMenuBar)239         private void addFileMenu(JMenuBar _jInspectMenuBar){
240             JMenu jMnuFile = new JMenu("File");
241             JMenuItem jMnuItemRemoveInspector = new JMenuItem("Remove");
242             jMnuItemRemoveInspector.addActionListener(new ActionListener(){
243                 public void actionPerformed(java.awt.event.ActionEvent evt) {
244                     removeSelectedTabPane();
245                 }
246             });
247             jMnuFile.add(jMnuItemRemoveInspector);
248             JMenuItem jMnuItemExit = new JMenuItem("Exit");
249             jMnuItemExit.addActionListener(new ActionListener(){
250                 public void actionPerformed(java.awt.event.ActionEvent evt) {
251                     getDialog().dispose();
252                 }
253             });
254             jMnuFile.add(jMnuItemExit);
255             _jInspectMenuBar.add(jMnuFile);
256         }
257 
getInspectMenuItem(String _sLabel)258         private JMenuItem getInspectMenuItem(String _sLabel){
259             JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
260             jMnuSelectedObject.addActionListener(new ActionListener(){
261                 public void actionPerformed(java.awt.event.ActionEvent evt) {
262                     m_oInspector.inspectSelectedNode();
263                 }
264             });
265             return jMnuSelectedObject;
266         }
267 
268 
getSourceCodeMenuItem(String _sLabel)269         private JMenuItem getSourceCodeMenuItem(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.addSourceCodeOfSelectedNode();
274                 }
275             });
276             return jMnuSelectedObject;
277         }
278 
getInvokeMenuItem(String _sLabel)279         private JMenuItem getInvokeMenuItem(String _sLabel){
280             JMenuItem jMnuSelectedObject = new JMenuItem(_sLabel);
281             jMnuSelectedObject.addActionListener(new ActionListener(){
282                 public void actionPerformed(java.awt.event.ActionEvent evt) {
283                     m_oInspector.invokeSelectedMethod();
284                 }
285             });
286             return jMnuSelectedObject;
287         }
288 
289 
addInspectMenu(JMenuBar _jInspectMenuBar)290         private void addInspectMenu(JMenuBar _jInspectMenuBar){
291             JMenu jMnuInspect = new JMenu("Inspect");
292             addApplicationDocumentMenu(jMnuInspect);
293             jMnuInspect.addSeparator();
294             addGlobalServiceManagerMenu(jMnuInspect);
295             jMnuInspect.addSeparator();
296             jMnuInspect.add(getInspectMenuItem("Selected Object"));
297             jMnuInspect.addSeparator();
298             addOpenDocumentMenu(jMnuInspect);
299             _jInspectMenuBar.add(jMnuInspect);
300         }
301 
getLanguage()302         public int getLanguage(){
303             return XLanguageSourceCodeGenerator.nJAVA;
304         }
305 
306 
selectSourceCodeLanguage(int _nLanguage)307         public void selectSourceCodeLanguage(int _nLanguage){
308             switch (_nLanguage){
309                 case XLanguageSourceCodeGenerator.nJAVA:
310                     jJavaMenuItem.setSelected(true);
311                     break;
312                 case XLanguageSourceCodeGenerator.nCPLUSPLUS:
313                     jCPlusPlusMenuItem.setSelected(true);
314                     break;
315                 case XLanguageSourceCodeGenerator.nBASIC:
316                     jBasicMenuItem.setSelected(true);
317                     break;
318                 default:
319                     System.out.println("Warning: Sourcecode language is not defined!");
320             }
321         }
322 
addLanguageMenuItem(ButtonGroup _group, String _sLanguageTitle, boolean _bSelect, char _sMnemonic, final int _nLanguage)323         private JRadioButtonMenuItem addLanguageMenuItem(ButtonGroup _group, String _sLanguageTitle, boolean _bSelect, char _sMnemonic, final int _nLanguage){
324             JRadioButtonMenuItem jMenuItem = new JRadioButtonMenuItem(_sLanguageTitle, _bSelect);
325             jMenuItem.setMnemonic(_sMnemonic);
326             _group.add(jMenuItem);
327             jMenuItem.addActionListener(new ActionListener(){
328                 public void actionPerformed(java.awt.event.ActionEvent evt) {
329                     m_oInspector.setSourceCodeLanguage(_nLanguage);
330                 }
331             });
332             return jMenuItem;
333         }
334 
335 
getIDLPath()336         public String getIDLPath(){
337             return this.m_oInspector.getIDLPath();
338         }
339 
addOptionsMenu(JMenuBar _jInspectMenuBar)340         private void addOptionsMenu(JMenuBar _jInspectMenuBar){
341             ButtonGroup oButtonGroup = new ButtonGroup();
342             jJavaMenuItem = addLanguageMenuItem(oButtonGroup, "Generate Java Sourcecode", true, 'J', XLanguageSourceCodeGenerator.nJAVA);
343             jMnuOptions.add(jJavaMenuItem);
344             jCPlusPlusMenuItem = addLanguageMenuItem(oButtonGroup, "Generate C++ Sourcecode", false, 'C', XLanguageSourceCodeGenerator.nCPLUSPLUS);
345             jMnuOptions.add(jCPlusPlusMenuItem);
346             jBasicMenuItem = addLanguageMenuItem(oButtonGroup, "Generate OpenOffice.org Basic Sourcecode", false, 'B', XLanguageSourceCodeGenerator.nBASIC);
347             jMnuOptions.add(jBasicMenuItem);
348             jMnuOptions.addSeparator();
349             JMenuItem jMenuItem = new JMenuItem("Path to SDK-Installation");
350             jMenuItem.setMnemonic('I');
351             jMenuItem.addActionListener(new ActionListener(){
352                 public void actionPerformed(java.awt.event.ActionEvent evt) {
353                     m_oInspector.assignSDKPath();
354                 }
355             });
356 
357             jMnuOptions.add(jMenuItem);
358             _jInspectMenuBar.add(jMnuOptions);
359         }
360 
361 
insertMenus()362         private void insertMenus(){
363             JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
364             addFileMenu(jMenuBar1);
365             addInspectMenu(jMenuBar1);
366             JMenu jMnuEdit = new JMenu("Edit");
367             JMenu jMnuView = new JMenu("View");
368             addOptionsMenu(jMenuBar1);
369             jMenuBar1.setFont(new java.awt.Font("Dialog", 0, 12));
370             jMenuBar1.add(jMnuEdit);
371             jMenuBar1.add(jMnuView);
372             addHelpMenu(jMenuBar1);
373             addMenuBar(jMenuBar1);
374         }
375 
addGlobalServiceManagerMenu(JMenu _jMnuRoot)376         private void addGlobalServiceManagerMenu(JMenu _jMnuRoot){
377             JMenuItem jMnuGlobalServiceManager = new JMenuItem("Global Service Manager");
378             jMnuGlobalServiceManager.addActionListener(new ActionListener(){
379                 public void actionPerformed(java.awt.event.ActionEvent evt) {
380                     m_oInspector.inspect(m_xComponentContext.getServiceManager(), "Global ServiceManager");
381                 }
382             });
383             _jMnuRoot.add(jMnuGlobalServiceManager);
384         }
385 
386 
387 
enablePopupMenuItem(String _sMenuTitle, boolean _bdoEnable)388         public void enablePopupMenuItem(String _sMenuTitle, boolean _bdoEnable){
389             Component[] oComponents = m_jPopupMenu.getComponents();
390             for (int i = 0; i < oComponents.length; i++){
391                 if (oComponents[i] instanceof JMenuItem){
392                     JMenuItem jMenuItem = (JMenuItem) oComponents[i];
393                     if (jMenuItem.getText().equals(_sMenuTitle)){
394                         jMenuItem.setEnabled(_bdoEnable);
395                     }
396                 }
397             }
398         }
399 
400 
showPopUpMenu(Object _invoker, int x, int y)401         public void showPopUpMenu(Object _invoker, int x, int y) throws ClassCastException{
402             if (_invoker instanceof Component){
403                 m_jPopupMenu.show((Component) _invoker, x, y);
404             }
405         }
406 
407 
show(int _nPageIndex)408     public void show(int _nPageIndex){
409         Dimension aDimension = m_jInspectorDialog.getSize();
410         selectInspectorPageByIndex(_nPageIndex);
411         if (_nPageIndex > 0){
412             m_jInspectorDialog.setSize(aDimension);
413         }
414         else{
415             m_jInspectorDialog.pack();
416         }
417 //            m_jInspectorDialog.paint(m_jInspectorDialog.getGraphics());
418         m_jInspectorDialog.setVisible(true);
419     }
420 
paint()421         public void paint(){
422             m_jTabbedPane1.paintImmediately(m_jTabbedPane1.getBounds());
423         }
424 
425 
addInspectorPage(String _sTitle, Object _oContainer)426         public void addInspectorPage(String _sTitle, Object _oContainer) throws ClassCastException{
427             if (_oContainer instanceof Component){
428                 m_jTabbedPane1.addTab(_sTitle, (Component) _oContainer);
429             }
430         }
431 }
432