1 /*
2    D-Bus Java Viewer
3    Copyright (c) 2006 Peter Cox
4 
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of either the GNU Lesser General Public License Version 2 or the
7    Academic Free Licence Version 2.1.
8 
9    Full licence texts are included in the COPYING file with this program.
10 */
11 package org.freedesktop.dbus.viewer;
12 
13 import java.util.ArrayList;
14 import java.util.List;
15 
16 import javax.swing.table.AbstractTableModel;
17 
18 @SuppressWarnings("serial")
19 class DBusTableModel extends AbstractTableModel
20 {
21 	private static final String INTROSPECTABLE = "introspectable?";
22 
23 	private static final String OWNER = "owner";
24 
25 	private static final String USER = "user";
26 
27 	private static final String NAME = "name";
28 
29 	private static final String PATH = "path";
30 
31 	final String[] columns = { NAME, PATH, USER, OWNER, INTROSPECTABLE };
32 
33 	private List<DBusEntry> entries = new ArrayList<DBusEntry>();
34 
35 	/** {@inheritDoc} */
getRowCount()36 	public int getRowCount()
37 	{
38 		return entries.size();
39 	}
40 	/** Add a row to the table model
41 	 *
42 	 * @param entry The dbus entry to add
43 	 */
add(DBusEntry entry)44 	public void add(DBusEntry entry)
45 	{
46 		entries.add(entry);
47 	}
48 
49 	/** {@inheritDoc} */
getColumnCount()50 	public int getColumnCount()
51 	{
52 		return columns.length;
53 	}
54 
55 	/** {@inheritDoc} */
56 	@Override
getColumnName(int column)57 	public String getColumnName(int column)
58 	{
59 		return columns[column];
60 	}
61 
62 	/** Get a row of the table
63 	 * @param row The row index
64 	 * @return The table row
65 	 */
getEntry(int row)66 	public DBusEntry getEntry(int row)
67 	{
68 		return entries.get(row);
69 	}
70 
71 	/** {@inheritDoc} */
72 	@Override
getColumnClass(int columnIndex)73 	public Class<?> getColumnClass(int columnIndex)
74 	{
75 		String columnName = getColumnName(columnIndex);
76 		if (columnName.equals(NAME))
77 		{
78 			return String.class;
79 		}
80 		if (columnName.equals(PATH))
81 		{
82 			return String.class;
83 		}
84 		else if (columnName.equals(USER))
85 		{
86 			return Object.class;
87 		}
88 		else if (columnName.equals(OWNER))
89 		{
90 			return String.class;
91 		}
92 		else if (columnName.equals(INTROSPECTABLE))
93 		{
94 			return Boolean.class;
95 		}
96 		return super.getColumnClass(columnIndex);
97 	}
98 
99 	/** {@inheritDoc} */
getValueAt(int rowIndex, int columnIndex)100 	public Object getValueAt(int rowIndex, int columnIndex)
101 	{
102 		DBusEntry entry = getEntry(rowIndex);
103 		String columnName = getColumnName(columnIndex);
104 		if (columnName.equals(NAME))
105 		{
106 			return entry.getName();
107 		}
108 		if (columnName.equals(PATH))
109 		{
110 			return entry.getPath();
111 		}
112 		else if (columnName.equals(USER))
113 		{
114 			return entry.getUser();
115 		}
116 		else if (columnName.equals(OWNER))
117 		{
118 			return entry.getOwner();
119 		}
120 		else if (columnName.equals(INTROSPECTABLE))
121 		{
122 			return entry.getIntrospectable() != null;
123 		}
124 		return null;
125 	}
126 
127 }
128