001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021
022 package org.apache.qpid.management.ui.views;
023
024 import static org.apache.qpid.management.ui.Constants.BUTTON_REFRESH;
025 import static org.apache.qpid.management.ui.Constants.FONT_BOLD;
026 import static org.apache.qpid.management.ui.Constants.FONT_ITALIC;
027 import static org.apache.qpid.management.ui.Constants.FONT_NORMAL;
028
029 import java.util.Collections;
030 import java.util.HashMap;
031
032 import org.apache.qpid.management.ui.ApplicationRegistry;
033 import org.apache.qpid.management.ui.ManagedBean;
034 import org.apache.qpid.management.ui.jmx.MBeanUtility;
035 import org.apache.qpid.management.ui.model.AttributeData;
036 import org.eclipse.swt.SWT;
037 import org.eclipse.swt.events.SelectionAdapter;
038 import org.eclipse.swt.events.SelectionEvent;
039 import org.eclipse.swt.layout.GridData;
040 import org.eclipse.swt.layout.GridLayout;
041 import org.eclipse.swt.widgets.Button;
042 import org.eclipse.swt.widgets.Composite;
043 import org.eclipse.swt.widgets.Control;
044 import org.eclipse.swt.widgets.Label;
045 import org.eclipse.swt.widgets.List;
046 import org.eclipse.swt.widgets.TabFolder;
047 import org.eclipse.ui.IWorkbenchWindow;
048 import org.eclipse.ui.PlatformUI;
049 import org.eclipse.ui.forms.widgets.Form;
050 import org.eclipse.ui.forms.widgets.FormToolkit;
051
052 /**
053 * Abstract class to be extended by the Controller classes for different MBean types (Connection, Queue, Exchange)
054 */
055 public abstract class MBeanTypeTabControl
056 {
057 private FormToolkit _toolkit = null;
058 private Form _form = null;
059 private TabFolder _tabFolder = null;
060 private Composite _composite = null;
061 private Composite _headerComposite = null;
062 private Composite _listComposite = null;
063 private Label _labelName = null;
064 private Label _labelDesc = null;
065 private Label _labelList = null;
066
067 private org.eclipse.swt.widgets.List _list = null;
068 private Button _refreshButton = null;
069 private Button _addButton = null;
070
071 private String _type = null;
072
073 // maps an mbean name with the mbean object. Required to get mbean object when an mbean
074 // is to be added to the navigation view.
075 private HashMap<String, ManagedBean> _objectsMap = new HashMap<String, ManagedBean>();
076 private Sorter _sorterByName = new Sorter();
077
078 public MBeanTypeTabControl(TabFolder tabFolder, String type)
079 {
080 _type = type;
081 _tabFolder = tabFolder;
082 _toolkit = new FormToolkit(_tabFolder.getDisplay());
083 _form = _toolkit.createForm(_tabFolder);
084 createFormComposite();
085 }
086
087 public FormToolkit getToolkit()
088 {
089 return _toolkit;
090 }
091
092 public Control getControl()
093 {
094 return _form;
095 }
096
097 public String getType()
098 {
099 return _type;
100 }
101
102 protected List getListWidget()
103 {
104 return _list;
105 }
106
107 protected HashMap<String, ManagedBean> getMBeansMap()
108 {
109 return _objectsMap;
110 }
111
112 public Sorter getMBeanNameSorter()
113 {
114 return _sorterByName;
115 }
116
117 public Button getAddButton()
118 {
119 return _addButton;
120 }
121
122 public Button getRefreshButton()
123 {
124 return _refreshButton;
125 }
126
127 /**
128 * Creates the main Composite, which will contain all other Composites and Widgets
129 */
130 protected void createFormComposite()
131 {
132 _form.getBody().setLayout(new GridLayout());
133 _composite = _toolkit.createComposite(_form.getBody(), SWT.NONE);
134 _composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
135 GridLayout layout = new GridLayout();
136 layout.verticalSpacing = 10;
137 layout.horizontalSpacing = 0;
138 _composite.setLayout(layout);
139 }
140
141 protected Composite getFormComposite()
142 {
143 return _composite;
144 }
145
146 /**
147 * Creates the header composite, which has MBean type name and description
148 * @param parentComposite
149 */
150 protected void createHeaderComposite(Composite parentComposite)
151 {
152 _headerComposite = _toolkit.createComposite(parentComposite);
153 _headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
154 GridLayout layout = new GridLayout();
155 layout.verticalSpacing = 10;
156 layout.horizontalSpacing = 0;
157 _headerComposite.setLayout(layout);
158
159 _labelName = _toolkit.createLabel(_headerComposite, "Type:", SWT.NONE);
160 GridData gridData = new GridData(SWT.CENTER, SWT.TOP, true, false);
161 _labelName.setLayoutData(gridData);
162 _labelName.setFont(ApplicationRegistry.getFont(FONT_BOLD));
163
164 _labelDesc = _toolkit.createLabel(_headerComposite, " ", SWT.NONE);
165 _labelDesc.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
166 _labelDesc.setFont(ApplicationRegistry.getFont(FONT_ITALIC));
167
168 _headerComposite.layout();
169 }
170
171 /**
172 * Creates Composite, which contains the common buttons - Add and Refresh.
173 * @param parentComposite
174 */
175 protected void createButtonsComposite(Composite parentComposite)
176 {
177 Composite composite = _toolkit.createComposite(parentComposite);
178 composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
179 GridLayout layout = new GridLayout(2, true);
180 layout.verticalSpacing = 10;
181 layout.horizontalSpacing = 20;
182 composite.setLayout(layout);
183
184 createAddButton(composite);
185 createRefreshButton(composite);
186 }
187
188 /**
189 * Creates the Add button, which adds the selected item to the navigation view
190 * @param parentComposite
191 */
192 protected void createAddButton(Composite parentComposite)
193 {
194 Button _addButton = _toolkit.createButton(parentComposite, "<- Add to Navigation", SWT.PUSH);
195 GridData gridData = new GridData(SWT.CENTER, SWT.CENTER, false, false);
196 _addButton.setLayoutData(gridData);
197 _addButton.addSelectionListener(new SelectionAdapter(){
198 public void widgetSelected(SelectionEvent e)
199 {
200 if (_list.getSelectionCount() == 0)
201 return;
202
203 String[] selectedItems = _list.getSelection();
204 for (int i = 0; i < selectedItems.length; i++)
205 {
206 String name = selectedItems[i];
207 int nameEnd = name.indexOf(" (");
208 if (nameEnd != -1)
209 {
210 name = name.substring(0, nameEnd);
211 }
212 // pass the ManagedBean to the navigation view to be added
213 ManagedBean mbean = _objectsMap.get(name);
214 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
215 NavigationView view = (NavigationView)window.getActivePage().findView(NavigationView.ID);
216 try
217 {
218 view.addManagedBean(mbean);
219 }
220 catch (Exception ex)
221 {
222 MBeanUtility.handleException(mbean, ex);
223 }
224 }
225 }
226 });
227 }
228
229 /**
230 * Creates the Refresh button, which gets syncs the items with the broker server
231 * @param parentComposite
232 */
233 protected void createRefreshButton(Composite parentComposite)
234 {
235 Button _refreshButton = _toolkit.createButton(parentComposite, BUTTON_REFRESH, SWT.PUSH);
236 GridData gridData = new GridData(SWT.CENTER, SWT.CENTER, false, false);
237 gridData.widthHint = 120;
238 _refreshButton.setLayoutData(gridData);
239 _refreshButton.addSelectionListener(new SelectionAdapter(){
240 public void widgetSelected(SelectionEvent e)
241 {
242 try
243 {
244 // refresh the list from the broker server
245 populateList();
246 }
247 catch (Exception ex)
248 {
249 MBeanUtility.handleException(ex);
250 }
251 }
252 });
253 }
254
255 /**
256 * Creates the Composite, which contains the items ( Connections, Exchanges or Queues)
257 * @param parentComposite
258 */
259 protected void createListComposite(Composite parentComposite)
260 {
261 // Composite to contain the item list
262 _listComposite = _toolkit.createComposite(parentComposite);
263 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
264 _listComposite.setLayoutData(gridData);
265 GridLayout layout = new GridLayout();
266 layout.verticalSpacing = 0;
267 _listComposite.setLayout(layout);
268
269 // Label for item name
270 _labelList = _toolkit.createLabel(_listComposite, " ", SWT.CENTER);
271 gridData = new GridData(SWT.CENTER, SWT.TOP, true, false, 1, 1);
272 _labelList.setLayoutData(gridData);
273 _labelList.setFont(ApplicationRegistry.getFont(FONT_NORMAL));
274
275 _list = new List(_listComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
276 gridData = new GridData(SWT.FILL, SWT.FILL, true, true,1, 1);
277 _list.setLayoutData(gridData);
278
279 }
280
281 /**
282 * This is called from MBean View to refresh the tab contents
283 * @throws Exception
284 */
285 public void refresh() throws Exception
286 {
287 setLabelValues();
288 populateList();
289 layout();
290 }
291
292 protected void setLabelValues()
293 {
294 _labelName.setText("Type : " + _type);
295 _labelDesc.setText("Select the " + _type + "(s) to add in the Navigation View");
296 _labelList.setText("-- List of " + _type + "s --");
297 }
298
299 protected abstract void populateList() throws Exception;
300
301 public void layout()
302 {
303 _form.layout(true);
304 _form.getBody().layout(true, true);
305 }
306
307 // sets the map with appropriate mbean and name
308 protected String[] getItems(java.util.List<ManagedBean> list)
309 {
310 if (list == null)
311 return new String[0];
312
313 Collections.sort(list, _sorterByName);
314 String[] items = new String[list.size()];
315 int i = 0;
316 for (ManagedBean mbean : list)
317 {
318 items[i++] = mbean.getName();
319 _objectsMap.put(mbean.getName(), mbean);
320 }
321 return items;
322 }
323
324 protected class ComparatorImpl implements java.util.Comparator<AttributeData>
325 {
326 public int compare(AttributeData data1, AttributeData data2)
327 {
328 Integer int1 = Integer.parseInt(data1.getValue().toString());
329 Integer int2 = Integer.parseInt(data2.getValue().toString());
330 return int1.compareTo(int2) * -1;
331 }
332 }
333
334 protected class Sorter implements java.util.Comparator<ManagedBean>
335 {
336 public int compare(ManagedBean mbean1, ManagedBean mbean2)
337 {
338 return mbean1.getName().compareTo(mbean2.getName());
339 }
340 }
341 }
|