1ddf8ce6b330b87f52e4b9540d8934ee07c2e242
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / adapters / InterfacesAdapter.java
1 package org.netxms.ui.android.main.adapters;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import org.netxms.client.constants.Severity;
9 import org.netxms.client.objects.Interface;
10 import org.netxms.ui.android.R;
11
12 import android.content.Context;
13 import android.view.LayoutInflater;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.widget.BaseExpandableListAdapter;
17 import android.widget.ListView;
18 import android.widget.TextView;
19
20 /**
21 * @author Marco Incalcaterra (marco.incalcaterra@thinksoft.it)
22 *
23 * Expandable list adapter for browsing interfaces
24 *
25 */
26
27 public class InterfacesAdapter extends BaseExpandableListAdapter
28 {
29 private final Context context;
30 private final ArrayList<String> groups;
31 private final ArrayList<ArrayList<Interface>> children;
32
33 /**
34 *
35 * @param context
36 */
37 public InterfacesAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<Interface>> children)
38 {
39 this.context = context;
40 this.groups = new ArrayList<String>(0);
41 this.children = new ArrayList<ArrayList<Interface>>(0);
42 }
43
44 /**
45 * Set graphs
46 *
47 * @param graphs
48 */
49 public void setValues(List<Interface> interfaces)
50 {
51 groups.clear();
52 children.clear();
53 if (interfaces != null)
54 {
55 Collections.sort(interfaces, new Comparator<Interface>()
56 {
57 @Override
58 public int compare(Interface object1, Interface object2)
59 {
60 return object1.getObjectName().compareTo(object2.getObjectName());
61 }
62 });
63 for (int i = 0; i < interfaces.size(); i++)
64 {
65 groups.add(interfaces.get(i).getObjectName());
66 children.add(new ArrayList<Interface>());
67 children.get(i).add(interfaces.get(i));
68 }
69 }
70 }
71
72 @Override
73 public Object getChild(int groupPosition, int childPosition)
74 {
75 if (groupPosition >= 0 && groupPosition < children.size())
76 if (childPosition >= 0 && childPosition < children.get(groupPosition).size())
77 return children.get(groupPosition).get(childPosition);
78 return null;
79 }
80
81 @Override
82 public long getChildId(int groupPosition, int childPosition)
83 {
84 return childPosition;
85 }
86
87 private ViewGroup getViewGroupChild(View convertView, ViewGroup parent)
88 {
89 // The parent will be our ListView from the ListActivity
90 if (convertView instanceof ViewGroup)
91 {
92 return (ViewGroup)convertView;
93 }
94 Context context = parent.getContext();
95 LayoutInflater inflater = LayoutInflater.from(context);
96 ViewGroup item = (ViewGroup)inflater.inflate(R.layout.interfaces_child, null);
97 return item;
98 }
99
100 @Override
101 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
102 {
103 ViewGroup item = getViewGroupChild(convertView, parent);
104 ListView label = (ListView)item.findViewById(R.id.interfacesChild);
105 InterfaceDetailsAdapter idAdapter = new InterfaceDetailsAdapter(parent.getContext());
106 idAdapter.setValues((Interface)getChild(groupPosition, childPosition));
107 label.setAdapter(idAdapter);
108
109 //////// TODO: FIX ASAP, REMOVE HARDCODED VALUES!!!
110 final int rowHeightDp = 24;
111 final int rowCount = 13;
112 // convert the dp values to pixels
113 final float ROW_HEIGHT = context.getResources().getDisplayMetrics().density * rowHeightDp;
114 // set the height of the current grid
115 label.getLayoutParams().height = Math.round(rowCount * ROW_HEIGHT);
116 //////// TODO: FIX ASAP, REMOVE HARDCODED VALUES!!!
117
118 return item;
119 }
120
121 @Override
122 public int getChildrenCount(int groupPosition)
123 {
124 if (groupPosition < children.size())
125 return children.get(groupPosition).size();
126 return 0;
127 }
128
129 @Override
130 public Object getGroup(int groupPosition)
131 {
132 return groups.get(groupPosition);
133 }
134
135 @Override
136 public int getGroupCount()
137 {
138 return groups.size();
139 }
140
141 @Override
142 public long getGroupId(int groupPosition)
143 {
144 return groupPosition;
145 }
146
147 @Override
148 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
149 {
150 if (convertView == null)
151 {
152 LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
153 convertView = layoutInflater.inflate(R.layout.interfaces_group, null);
154 }
155 Interface i = (Interface)getChild(groupPosition, 0);
156 TextView tv = (TextView)convertView.findViewById(R.id.interface_text);
157 tv.setText(" " + i.getObjectName());
158 tv.setCompoundDrawablesWithIntrinsicBounds(parent.getResources().getDrawable(getInterfaceStatusIcon(i.getStatus())), null, null, null);
159 return convertView;
160 }
161
162 @Override
163 public boolean hasStableIds()
164 {
165 return true;
166 }
167
168 @Override
169 public boolean isChildSelectable(int groupPosition, int childPosition)
170 {
171 return true;
172 }
173
174 private int getInterfaceStatusIcon(int status)
175 {
176 switch (status)
177 {
178 case Severity.NORMAL:
179 return R.drawable.status_normal;
180 case Severity.WARNING:
181 return R.drawable.status_warning;
182 case Severity.MINOR:
183 return R.drawable.status_minor;
184 case Severity.MAJOR:
185 return R.drawable.status_major;
186 case Severity.CRITICAL:
187 return R.drawable.status_critical;
188 case Severity.UNKNOWN:
189 return R.drawable.status_unknown;
190 case Severity.UNMANAGED:
191 return R.drawable.status_unmanaged;
192 case Severity.DISABLED:
193 return R.drawable.status_disabled;
194 }
195 return R.drawable.status_unknown;
196 }
197 }