2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.view;
19 import java.util.List;
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
24 import oic.simulator.clientcontroller.manager.ResourceManager;
25 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
26 import oic.simulator.clientcontroller.utils.Constants;
27 import oic.simulator.clientcontroller.view.dialogs.FindResourceWizard;
28 import oic.simulator.clientcontroller.view.dialogs.LoadRAMLDialog;
29 import oic.simulator.clientcontroller.view.dialogs.ResourceWizardDialog;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.viewers.ISelectionChangedListener;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.LabelProvider;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.jface.viewers.TreeViewer;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.jface.wizard.WizardDialog;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.custom.CTabFolder;
43 import org.eclipse.swt.custom.CTabItem;
44 import org.eclipse.swt.events.MenuAdapter;
45 import org.eclipse.swt.events.MenuEvent;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.graphics.Color;
49 import org.eclipse.swt.graphics.Image;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Display;
55 import org.eclipse.swt.widgets.Group;
56 import org.eclipse.swt.widgets.Menu;
57 import org.eclipse.swt.widgets.MenuItem;
58 import org.eclipse.swt.widgets.Tree;
59 import org.eclipse.ui.PlatformUI;
60 import org.eclipse.ui.dialogs.FilteredTree;
61 import org.eclipse.ui.dialogs.PatternFilter;
62 import org.eclipse.ui.part.ViewPart;
65 * This class manages and shows the resource manager view in the perspective.
67 public class ResourceManagerView extends ViewPart {
69 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.resourcemanager";
71 private Button findResButton;
72 private Button refreshButton;
74 private TreeViewer treeViewer;
75 private TreeViewer favTreeViewer;
77 private CTabFolder folder;
78 private CTabItem foundResTab;
79 private CTabItem favResTab;
81 private ResourceManager resourceManager;
83 private IFindResourceUIListener findListener;
85 private Boolean foundResource;
87 private MessageDialog findDialog;
89 private MessageDialog refreshDialog;
91 private Thread sleepThreadHandle;
93 public ResourceManagerView() {
94 resourceManager = Activator.getDefault().getResourceManager();
96 findListener = new IFindResourceUIListener() {
99 public void onNewResourceFound(final RemoteResource resource) {
100 if (null == resource) {
103 // Changing the status of the find operation.
104 setFoundResource(true);
106 // Interrupt the sleep thread.
107 if (null != sleepThreadHandle && sleepThreadHandle.isAlive()) {
108 sleepThreadHandle.interrupt();
111 // Update the tree viewer
112 Display.getDefault().asyncExec(new Runnable() {
115 if (!treeViewer.getControl().isDisposed()) {
116 treeViewer.refresh();
119 if (!favTreeViewer.getControl().isDisposed()) {
120 favTreeViewer.refresh();
123 // Close the find dialog
124 if (null != findDialog) {
128 // Close the refresh dialog
129 if (null != refreshDialog) {
130 refreshDialog.close();
139 public void createPartControl(Composite parent) {
140 Composite compContent = new Composite(parent, SWT.NONE);
141 compContent.setLayout(new GridLayout());
142 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
143 compContent.setLayoutData(gd);
145 Composite buttonComp = new Composite(compContent, SWT.NONE);
146 buttonComp.setLayout(new GridLayout(2, false));
149 gd.horizontalAlignment = SWT.FILL;
150 gd.grabExcessHorizontalSpace = true;
152 buttonComp.setLayoutData(gd);
154 findResButton = new Button(buttonComp, SWT.PUSH);
155 findResButton.setText("Find Resources");
156 findResButton.setToolTipText("Find OIC resources");
160 findResButton.setLayoutData(gd);
162 refreshButton = new Button(buttonComp, SWT.PUSH);
163 refreshButton.setText("Refresh");
164 refreshButton.setToolTipText("Restart the search once again");
168 refreshButton.setLayoutData(gd);
170 // Create a Tab Folder.
171 folder = new CTabFolder(compContent, SWT.BORDER);
172 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
173 folder.setLayoutData(gd);
174 folder.setSimple(false);
175 folder.setUnselectedCloseVisible(false);
176 folder.setUnselectedImageVisible(false);
177 folder.addSelectionListener(new SelectionAdapter() {
179 public void widgetSelected(SelectionEvent e) {
181 treeViewer.setSelection(null);
182 favTreeViewer.setSelection(null);
183 resourceManager.resourceSelectionChanged(null);
187 createFoundResourcesArea();
189 createFavoriteResourcesArea();
191 folder.setSelection(foundResTab);
193 findDialog = new MessageDialog(Display.getDefault().getActiveShell(),
194 "Finding Servers", null,
195 "Finding the requested servers\nPlease wait...",
196 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
197 // findDialog.setBlockOnOpen(false);
199 refreshDialog = new MessageDialog(
200 Display.getDefault().getActiveShell(),
203 "Refreshing the search and finding the requested servers once again\nPlease wait...",
204 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
205 // refreshDialog.setBlockOnOpen(false);
209 addManagerListeners();
211 // Setting the initial visibility of refresh based on the last known
213 Set<String> prevSearchTypes = resourceManager.getLastKnownSearchTypes();
214 if (null == prevSearchTypes || prevSearchTypes.size() < 1) {
215 refreshButton.setEnabled(false);
217 refreshButton.setEnabled(true);
221 private void createFoundResourcesArea() {
222 foundResTab = new CTabItem(folder, SWT.NULL);
223 foundResTab.setText("Found Resources");
225 // Create a group to show all the discovered resources.
226 // Adding the group to the folder.
227 Group resourceGroup = new Group(folder, SWT.NONE);
228 // resourceGroup.setText("Discovered Resources");
230 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
231 resourceGroup.setBackground(color);
233 resourceGroup.setLayout(new GridLayout(1, false));
234 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
235 resourceGroup.setLayoutData(gd);
237 PatternFilter filter = new PatternFilter();
238 FilteredTree filteredTree = new FilteredTree(resourceGroup,
239 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
240 treeViewer = filteredTree.getViewer();
241 treeViewer.getTree().setLayoutData(
242 new GridData(SWT.FILL, SWT.FILL, true, true));
243 treeViewer.setContentProvider(new TreeContentProvider());
244 treeViewer.setLabelProvider(new TreeLabelProvider());
245 treeViewer.setInput(new Object());
247 addMenuToFoundResources();
249 foundResTab.setControl(resourceGroup);
252 private void addMenuToFoundResources() {
254 if (null != treeViewer) {
255 final Tree resourceTreeHead = treeViewer.getTree();
256 if (null != resourceTreeHead) {
257 // Below code creates menu entries and shows them on right
258 // clicking a resource
259 final Menu menu = new Menu(resourceTreeHead);
260 resourceTreeHead.setMenu(menu);
261 menu.addMenuListener(new MenuAdapter() {
263 public void menuShown(MenuEvent e) {
264 // Clear existing menu items
265 MenuItem[] items = menu.getItems();
266 for (int index = 0; index < items.length; index++) {
267 items[index].dispose();
269 MenuItem uploadRAMLItem = new MenuItem(menu, SWT.NONE);
270 uploadRAMLItem.setText("Upload RAML Configuration");
272 .addSelectionListener(new SelectionAdapter() {
274 public void widgetSelected(SelectionEvent e) {
275 // Open the RAML configuration dialog if
276 // RAML file is not yet uploaded for the
277 // currently selected resource
278 RemoteResource resource = resourceManager
279 .getCurrentResourceInSelection();
280 if (null == resource) {
283 if (!resource.isConfigUploaded()) {
284 // Open the dialog in a separate
286 PlatformUI.getWorkbench()
288 .syncExec(new Thread() {
291 LoadRAMLDialog ramlDialog = new LoadRAMLDialog(
295 .open() != Window.OK) {
298 String configFilePath = ramlDialog
299 .getConfigFilePath();
300 if (null == configFilePath
307 "Invalid RAML Config path",
308 "Configuration file path is invalid.");
314 .getCurrentResourceInSelection(),
320 .openInformation(Display
324 "Configuration file for the selected resource is already uploaded");
329 RemoteResource resource = resourceManager
330 .getCurrentResourceInSelection();
331 if (null == resource) {
334 String menuText = !resource.isFavorite() ? "Add to favorites"
335 : "Remove from favorites";
336 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
337 addToFavMenuItem.setText(menuText);
339 .addSelectionListener(new SelectionAdapter() {
341 public void widgetSelected(SelectionEvent e) {
342 RemoteResource resource = (RemoteResource) ((IStructuredSelection) treeViewer
345 if (null == resource) {
348 if (!resource.isFavorite()) {
350 .addResourcetoFavorites(resource);
352 .addResourceURItoFavorites(resource);
355 .removeResourceFromFavorites(resource);
357 .removeResourceURIFromFavorites(resource);
359 favTreeViewer.refresh();
368 private void createFavoriteResourcesArea() {
369 favResTab = new CTabItem(folder, SWT.NULL);
370 favResTab.setText("Favorite Resources");
372 // Create a group to show all the discovered resources.
373 // Adding the group to the folder.
374 Group resourceGroup = new Group(folder, SWT.NONE);
375 // resourceGroup.setText("Discovered Resources");
377 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
378 resourceGroup.setBackground(color);
380 resourceGroup.setLayout(new GridLayout(1, false));
381 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
382 resourceGroup.setLayoutData(gd);
384 PatternFilter filter = new PatternFilter();
385 FilteredTree filteredTree = new FilteredTree(resourceGroup,
386 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
387 favTreeViewer = filteredTree.getViewer();
388 favTreeViewer.getTree().setLayoutData(
389 new GridData(SWT.FILL, SWT.FILL, true, true));
390 favTreeViewer.setContentProvider(new FavTreeContentProvider());
391 favTreeViewer.setLabelProvider(new TreeLabelProvider());
392 favTreeViewer.setInput(new Object());
394 favResTab.setControl(resourceGroup);
396 addMenuToFavResources();
399 private void addMenuToFavResources() {
400 if (null != favTreeViewer) {
401 final Tree resourceTreeHead = favTreeViewer.getTree();
402 if (null != resourceTreeHead) {
403 // Below code creates menu entries and shows them on right
404 // clicking a resource
405 final Menu menu = new Menu(resourceTreeHead);
406 resourceTreeHead.setMenu(menu);
407 menu.addMenuListener(new MenuAdapter() {
409 public void menuShown(MenuEvent e) {
410 // Clear existing menu items
411 MenuItem[] items = menu.getItems();
412 for (int index = 0; index < items.length; index++) {
413 items[index].dispose();
415 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
416 addToFavMenuItem.setText("Remove from favorites");
418 .addSelectionListener(new SelectionAdapter() {
420 public void widgetSelected(SelectionEvent e) {
421 RemoteResource resource = (RemoteResource) ((IStructuredSelection) favTreeViewer
424 if (null == resource) {
428 .removeResourceFromFavorites(resource);
430 .removeResourceURIFromFavorites(resource);
431 favTreeViewer.refresh();
440 private void addUIListeners() {
441 findResButton.addSelectionListener(new SelectionAdapter() {
443 public void widgetSelected(SelectionEvent e) {
444 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
448 FindResourceWizard findWizard = new FindResourceWizard();
449 ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
450 PlatformUI.getWorkbench().getDisplay()
451 .getActiveShell(), findWizard);
452 int open = wizardDialog.open();
453 if (open == WizardDialog.OK) {
454 // Setting initial value on starting the find
456 setFoundResource(false);
458 Set<String> searchTypes = findWizard
460 // Delete cached details of resources based on the
461 // given search types.
462 // If there are no resource types to search, then
465 resourceManager.deleteResources(searchTypes);
468 treeViewer.refresh();
469 favTreeViewer.refresh();
471 // Call native method to find Resources
472 boolean result = resourceManager
473 .findResourceRequest(searchTypes);
475 searchUIOperation(false);
478 .openError(Display.getDefault()
480 "Find Resource status",
481 "Operation failed due to some problems in core layer.");
484 // Store this information for refresh
487 .setLastKnownSearchTypes(searchTypes);
489 // Change the refresh visibility
490 refreshButton.setEnabled(true);
497 refreshButton.addSelectionListener(new SelectionAdapter() {
499 public void widgetSelected(SelectionEvent e) {
500 Set<String> searchTypes = resourceManager
501 .getLastKnownSearchTypes();
502 setFoundResource(false);
504 // Delete cached details of resources based on the given search
506 // If there are no resource types to search, then all resources
508 resourceManager.deleteResources(searchTypes);
511 treeViewer.refresh();
512 favTreeViewer.refresh();
514 // Call native method to find Resources
515 boolean result = resourceManager
516 .findResourceRequest(searchTypes);
518 searchUIOperation(true);
521 .openError(Display.getDefault().getActiveShell(),
522 "Find Resource status",
523 "Operation failed due to some problems in core layer.");
528 // Below code adds a listener to the tree for selection changes
529 // and notifies the resource manager
530 ISelectionChangedListener treeSelectionListener = new ISelectionChangedListener() {
533 public void selectionChanged(SelectionChangedEvent e) {
534 if (e.getSelection().isEmpty()) {
537 if (e.getSelection() instanceof IStructuredSelection) {
538 IStructuredSelection selection = (IStructuredSelection) e
540 RemoteResource resource = (RemoteResource) selection
542 if (null == resource) {
545 resourceManager.resourceSelectionChanged(resource);
550 treeViewer.addSelectionChangedListener(treeSelectionListener);
551 favTreeViewer.addSelectionChangedListener(treeSelectionListener);
554 // If refresh is true, then Refresh Dialog else Find Dialog will be shown.
555 private void searchUIOperation(boolean refresh) {
556 final MessageDialog targetDialog;
558 targetDialog = refreshDialog;
560 targetDialog = findDialog;
562 // Open the dialog in a new thread.
563 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
567 if (isFoundResource()) {
568 setFoundResource(false);
572 PlatformUI.getWorkbench().getDisplay().asyncExec(new Thread() {
575 targetDialog.open(); // This method returns once the
576 // cancel button is pressed.
578 // Interrupt the sleep thread.
579 if (null != sleepThreadHandle
580 && sleepThreadHandle.isAlive()) {
581 sleepThreadHandle.interrupt();
584 // Set the status of find.
585 setFoundResource(false);
589 // Thread for find time-out.
590 sleepThreadHandle = new Thread() {
595 Thread.sleep(Constants.FIND_RESOURCES_TIMEOUT * 1000);
596 } catch (InterruptedException e) {
600 child = new Thread() {
603 if (null != targetDialog) {
604 targetDialog.close();
606 // Check if any new resources are
608 // Is no new resources, then display a
610 if (!isFoundResource()) {
616 "No servers are available as of now.\n"
617 + "Please check the servers' status and press"
618 + "'Refresh' button to restart the search.");
620 // Resetting the status to false for
622 setFoundResource(false);
628 PlatformUI.getWorkbench().getDisplay().syncExec(child);
631 sleepThreadHandle.start();
636 private void addManagerListeners() {
637 resourceManager.addFindresourceUIListener(findListener);
641 public void dispose() {
642 // Unregister the listener
643 if (null != findListener) {
644 resourceManager.removeFindresourceUIListener(findListener);
645 resourceManager.resourceSelectionChanged(null);
651 public void setFocus() {
652 // TODO Auto-generated method stub
656 public synchronized void setFoundResource(boolean value) {
657 foundResource = value;
660 public synchronized boolean isFoundResource() {
661 return foundResource;
665 class TreeContentProvider implements ITreeContentProvider {
668 public void dispose() {
672 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
677 public Object[] getChildren(Object parent) {
682 public Object[] getElements(Object parent) {
683 List<RemoteResource> resourceList = Activator.getDefault()
684 .getResourceManager().getResourceList();
685 return resourceList.toArray();
689 public Object getParent(Object child) {
694 public boolean hasChildren(Object parent) {
700 class FavTreeContentProvider implements ITreeContentProvider {
703 public void dispose() {
707 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
712 public Object[] getChildren(Object parent) {
717 public Object[] getElements(Object parent) {
718 List<RemoteResource> resourceList = Activator.getDefault()
719 .getResourceManager().getFavResourceList();
720 return resourceList.toArray();
724 public Object getParent(Object child) {
729 public boolean hasChildren(Object parent) {
735 class TreeLabelProvider extends LabelProvider {
737 public String getText(Object element) {
738 RemoteResource resource = (RemoteResource) element;
739 return resource.getResourceURI();
742 // TODO: Commenting temporarily until resource icons are finalized.
744 public Image getImage(Object element) {
745 RemoteResource resource = (RemoteResource) element;
746 ResourceManager resourceManager = Activator.getDefault()
747 .getResourceManager();
748 return resourceManager.getImage(resource.getResourceURI());