summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.dbus-java/src/main/java/org/freedesktop/dbus/viewer/IntrospectAction.java
blob: d2110e9460c283251e65590c257c946ab8149423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
   D-Bus Java Viewer
   Copyright (c) 2006 Peter Cox

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus.viewer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.StringReader;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.freedesktop.DBus.Introspectable;
import org.freedesktop.dbus.bin.CreateInterface;

@SuppressWarnings("serial")
final class IntrospectAction extends AbstractAction implements ListSelectionListener
{
	private final JTable table;

	IntrospectAction(JTable table)
	{
		super("Introspect");
		setEnabled(false);
		this.table = table;
		

		ListSelectionModel selectionModel = table.getSelectionModel();
		selectionModel.addListSelectionListener(this);
		selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
	
	/** {@inheritDoc} */
	public void valueChanged(ListSelectionEvent e)
	{
		if (!e.getValueIsAdjusting())
		{
			DBusTableModel model = (DBusTableModel) table.getModel();
			int selection = table.getSelectedRow();
			if (selection > -1 && selection < model.getRowCount())
			{
				DBusEntry entry = model.getEntry(selection);
				final Introspectable introspectable = entry.getIntrospectable();
				setEnabled(introspectable != null);
			}
		}
	}			

	/** {@inheritDoc} */
	public void actionPerformed(ActionEvent e) 
	{
	
		int row = table.getSelectedRow();
		DBusTableModel model = (DBusTableModel) table.getModel();
		if (row > -1 && row < model.getRowCount())
		{
			DBusEntry entry = model.getEntry(row);
			final String xmlFile = entry.getName() + ".xml";
			final Introspectable introspectable = entry.getIntrospectable();
			new Thread(new Runnable()
			{
				public void run()
				{
					
					StringStreamFactory factory = new StringStreamFactory();
					CreateInterface createInterface = new CreateInterface(factory, false);
					try
					{
						String xml = introspectable.Introspect();
						
						String docType = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">";
	
						createInterface.createInterface(new StringReader(xml.replace(docType, "")));
						final JTabbedPane tabbedPane = new JTabbedPane();
						
						tabbedPane.addTab(xmlFile, createSourceTab(xmlFile, xml));
						
						for (String file : factory.streamMap.keySet())
						{
							final String source = factory.streamMap.get(file).toString();
							
							tabbedPane.addTab(file, createSourceTab(file, source));
						}
						tabbedPane.setPreferredSize(new Dimension(600, 400));
						
						final JPanel introspectionPanel = new JPanel(new BorderLayout());
						introspectionPanel.add(tabbedPane, BorderLayout.CENTER);

						JPanel southPanel = new JPanel();
						southPanel.add(new JButton(new SaveFileAction(tabbedPane)));
						southPanel.add(new JButton(new SaveAllAction(tabbedPane)));
						introspectionPanel.add(southPanel, BorderLayout.SOUTH);
						
						SwingUtilities.invokeLater(new Runnable()
						{
							@SuppressWarnings("synthetic-access")
							public void run()
							{
								JOptionPane.showMessageDialog(table, introspectionPanel, "Introspection", JOptionPane.PLAIN_MESSAGE);
							}
						});
						
					}
					catch (final Exception e)
					{
						e.printStackTrace();
						SwingUtilities.invokeLater(new Runnable()
						{
							@SuppressWarnings("synthetic-access")
							public void run()
							{
								JOptionPane.showMessageDialog(table, e.getMessage(), "Introspection Failed", JOptionPane.ERROR_MESSAGE);
							}
						});
					}
				}
	
				private JScrollPane createSourceTab(String file, final String source)
				{
					JTextArea area = new JTextArea(source);
					area.setLineWrap(true);
					area.setWrapStyleWord(true);
					return new JScrollPane(area, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				}
			}).start();
			
		}
	}
}