1 package oic.simulator.serviceprovider.view;
3 import java.util.ArrayList;
6 import oic.simulator.serviceprovider.Activator;
7 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
8 import oic.simulator.serviceprovider.manager.ResourceManager;
9 import oic.simulator.serviceprovider.utils.Constants;
10 import oic.simulator.serviceprovider.utils.Utility;
11 import oic.simulator.serviceprovider.view.dialogs.CreateResourceWizard;
12 import oic.simulator.serviceprovider.view.dialogs.DeleteCategory;
13 import oic.simulator.serviceprovider.view.dialogs.DeleteResourceWizard;
14 import oic.simulator.serviceprovider.view.dialogs.ResourceWizardDialog;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.wizard.WizardDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.MenuAdapter;
24 import org.eclipse.swt.events.MenuEvent;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Menu;
35 import org.eclipse.swt.widgets.MenuItem;
36 import org.eclipse.swt.widgets.Tree;
37 import org.eclipse.swt.widgets.TreeItem;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.dialogs.FilteredTree;
40 import org.eclipse.ui.dialogs.PatternFilter;
41 import org.eclipse.ui.part.ViewPart;
43 public class ResourceManagerView extends ViewPart {
45 public static final String VIEW_ID = "oic.simulator.serviceprovider.view.resourcemanager";
47 private Button createButton;
48 private Button deleteButton;
50 private TreeViewer treeViewer;
52 private IResourceListChangedUIListener resourceListChangedListener;
54 private ResourceManager resourceManager;
56 public ResourceManagerView() {
58 resourceManager = Activator.getDefault().getResourceManager();
60 resourceListChangedListener = new IResourceListChangedUIListener() {
63 public void onResourceCreation() {
64 Display.getDefault().asyncExec(new Runnable() {
68 if (null != treeViewer) {
70 treeViewer.expandAll();
73 // Trigger the visibility of delete button
74 changeDeleteVisibility();
80 public void onResourceDeletion() {
81 Display.getDefault().asyncExec(new Runnable() {
85 if (null != treeViewer) {
87 treeViewer.expandAll();
90 // Trigger the visibility of delete button
91 changeDeleteVisibility();
98 public void changeDeleteVisibility() {
99 if (null == treeViewer) {
103 Tree tree = treeViewer.getTree();
104 if (null != tree && !tree.isDisposed() && tree.getItemCount() > 0) {
109 if (null != deleteButton && !deleteButton.isDisposed()) {
110 deleteButton.setEnabled(visibility);
115 public void createPartControl(Composite parent) {
116 Composite compContent = new Composite(parent, SWT.NONE);
117 GridLayout baseLayout = new GridLayout(1, false);
118 compContent.setLayout(baseLayout);
120 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
121 compContent.setLayoutData(gd);
123 Composite buttonComp = new Composite(compContent, SWT.NONE);
124 buttonComp.setLayout(new GridLayout(2, false));
127 gd.horizontalAlignment = SWT.FILL;
128 gd.grabExcessHorizontalSpace = true;
130 buttonComp.setLayoutData(gd);
132 createButton = new Button(buttonComp, SWT.PUSH);
133 createButton.setText("Create");
134 createButton.setToolTipText("Create Simulator Resource(s)");
138 createButton.setLayoutData(gd);
140 deleteButton = new Button(buttonComp, SWT.PUSH);
141 deleteButton.setText("Delete");
142 deleteButton.setToolTipText("Delete Simulator Resource(s)");
146 deleteButton.setLayoutData(gd);
148 Group resourceGroup = new Group(compContent, SWT.NONE);
149 resourceGroup.setText("Created Resources");
151 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
152 resourceGroup.setBackground(color);
154 resourceGroup.setLayout(new GridLayout(1, false));
155 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
156 resourceGroup.setLayoutData(gd);
158 PatternFilter filter = new PatternFilter();
159 FilteredTree filteredTree = new FilteredTree(resourceGroup,
160 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
161 treeViewer = filteredTree.getViewer();
162 treeViewer.getTree().setLayoutData(
163 new GridData(SWT.FILL, SWT.FILL, true, true));
164 treeViewer.setContentProvider(new TreeContentProvider());
165 treeViewer.setLabelProvider(new TreeLabelProvider());
166 treeViewer.setInput(new Object());
170 addManagerListeners();
172 // If there is at least one resource exist, then enable the delete
174 changeDeleteVisibility();
177 private void addUIListeners() {
179 createButton.addSelectionListener(new SelectionAdapter() {
181 public void widgetSelected(SelectionEvent e) {
182 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
186 CreateResourceWizard createWizard = new CreateResourceWizard();
187 ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
188 PlatformUI.getWorkbench().getDisplay()
189 .getActiveShell(), createWizard);
190 int open = wizardDialog.open();
191 if (open == WizardDialog.OK) {
192 String configFilePath;
194 configFilePath = createWizard.getConfigFilePath();
195 count = createWizard.getResourceCount();
198 // Single resource creation
199 resourceManager.createResource(configFilePath);
201 // Multi-resource creation
202 resourceManager.createResource(configFilePath,
211 deleteButton.addSelectionListener(new SelectionAdapter() {
213 public void widgetSelected(SelectionEvent e) {
214 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
218 DeleteResourceWizard deleteWizard = new DeleteResourceWizard();
219 ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
220 PlatformUI.getWorkbench().getDisplay()
221 .getActiveShell(), deleteWizard);
222 int open = wizardDialog.open();
223 if (open == WizardDialog.OK) {
224 DeleteCategory deleteCategory = deleteWizard
225 .getDeleteCategory();
226 if (deleteCategory == DeleteCategory.BY_URI) {
227 String uri = deleteWizard.getDeleteCandidate();
228 boolean completeURI = Utility
231 uri = Utility.displayNameToUri(uri);
233 resourceManager.deleteResourceByURI(uri);
234 } else if (deleteCategory == DeleteCategory.BY_TYPE) {
236 .deleteResourceByType(deleteWizard
237 .getDeleteCandidate());
238 } else if (deleteCategory == DeleteCategory.ALL) {
239 resourceManager.deleteAllResources();
247 if (null != treeViewer) {
248 final Tree resourceTreeHead = treeViewer.getTree();
249 if (null != resourceTreeHead) {
250 // Below code adds a listener to the tree for selection changes
251 // and notifies the resource manager
252 resourceTreeHead.addSelectionListener(new SelectionAdapter() {
254 public void widgetSelected(SelectionEvent e) {
255 TreeItem selectedItem = (TreeItem) e.item;
256 if (null != selectedItem) {
257 String selectedItemText = selectedItem.getText();
258 if (!isSelectedTreeItemACategory(selectedItemText)) {
259 selectedItemText = Utility
260 .displayNameToUri(selectedItemText);
262 // Propagate this selection change event to manager
264 .resourceSelectionChanged(selectedItemText);
268 // Below code creates menu entries and shows them on right
269 // clicking a resource
270 final Menu menu = new Menu(resourceTreeHead);
271 resourceTreeHead.setMenu(menu);
272 menu.addMenuListener(new MenuAdapter() {
274 public void menuShown(MenuEvent e) {
275 // Clear existing menu items
276 MenuItem[] items = menu.getItems();
277 for (int index = 0; index < items.length; index++) {
278 items[index].dispose();
280 final String selectedItem = resourceTreeHead
281 .getSelection()[0].getText();
282 if (isSelectedTreeItemACategory(selectedItem)) {
285 MenuItem startItem = new MenuItem(menu, SWT.NONE);
286 startItem.setText(Constants.START_RESOURCE_AUTOMATION);
287 startItem.addSelectionListener(new SelectionAdapter() {
289 public void widgetSelected(SelectionEvent e) {
290 // Block starting resource level
291 // automation if any attribute level
292 // automation is in progress for the
294 boolean started = resourceManager.isAttributeAutomationStarted(Utility
295 .displayNameToUri(selectedItem));
301 "Attribute automation is in progress",
302 "Attribute level automation for this resource is already in progress!!!\nPlease stop all "
303 + "running attribute level automations to start resource level automation.");
305 boolean status = resourceManager
306 .startResourceAutomationUIRequest(Utility
307 .displayNameToUri(selectedItem));
308 String statusMsg = status ? "Automation started successfully!!!"
309 : "Automation request failed!!!";
310 MessageDialog.openInformation(Display
311 .getDefault().getActiveShell(),
312 "Automation Status", statusMsg);
317 MenuItem stopItem = new MenuItem(menu, SWT.NONE);
318 stopItem.setText(Constants.STOP_RESOURCE_AUTOMATION);
319 stopItem.addSelectionListener(new SelectionAdapter() {
321 public void widgetSelected(SelectionEvent e) {
323 .stopResourceAutomationUIRequest(Utility
324 .displayNameToUri(selectedItem));
325 String statusMsg = "Automation stop requested!!!";
326 MessageDialog.openInformation(Display
327 .getDefault().getActiveShell(),
328 "Automation Status", statusMsg);
332 // Set the initial visibility of menu items
333 boolean status = resourceManager
334 .isResourceAutomationStarted(Utility
335 .displayNameToUri(selectedItem));
336 startItem.setEnabled(!status);
337 stopItem.setEnabled(status);
344 private boolean isSelectedTreeItemACategory(String selectedItem) {
345 boolean category = false;
346 if (null != selectedItem) {
347 category = resourceManager.isTypeExist(selectedItem);
352 public void addManagerListeners() {
354 .addResourceListChangedUIListener(resourceListChangedListener);
358 public void setFocus() {
362 public void dispose() {
363 // Unregister the listener
364 if (null != resourceListChangedListener) {
366 .removeResourceListChangedUIListener(resourceListChangedListener);
367 resourceManager.resourceSelectionChanged(null);
373 class TreeContentProvider implements ITreeContentProvider {
376 public void dispose() {
380 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
384 public Object[] getChildren(Object parent) {
385 String resType = (String) parent;
386 List<String> members;
387 members = Activator.getDefault().getResourceManager()
388 .getURIListOfResourceType(resType);
389 if (null == members) {
390 members = new ArrayList<String>();
392 return members.toArray();
396 public Object[] getElements(Object parent) {
397 List<String> deptTypes;
398 deptTypes = Activator.getDefault().getResourceManager()
399 .getResourceTypeList();
400 if (null == deptTypes) {
401 deptTypes = new ArrayList<String>();
403 return deptTypes.toArray();
407 public Object getParent(Object child) {
412 public boolean hasChildren(Object parent) {
413 String resType = (String) parent;
414 if (Activator.getDefault().getResourceManager().isTypeExist(resType)) {
421 class TreeLabelProvider extends LabelProvider {
423 public String getText(Object element) {
424 String value = (String) element;
425 if (!Activator.getDefault().getResourceManager().isTypeExist(value)) {
426 value = Utility.uriToDisplayName(value);