c75065efaf01066e65b5115c007b13a813328904
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / DeleteResourceWizard.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view.dialogs;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.net.URL;
21 import java.util.Date;
22 import java.util.Set;
23
24 import oic.simulator.serviceprovider.Activator;
25 import oic.simulator.serviceprovider.manager.ResourceManager;
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.model.SingleResource;
28
29 import org.eclipse.core.runtime.FileLocator;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.jface.operation.IRunnableWithProgress;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.jface.wizard.Wizard;
36 import org.oic.simulator.ILogger.Level;
37 import org.oic.simulator.SimulatorException;
38
39 /**
40  * This class creates a UI wizard for delete resource operation.
41  */
42 public class DeleteResourceWizard extends Wizard {
43
44     private DeleteResourcePage page;
45
46     private String             status;
47
48     public DeleteResourceWizard() {
49         setWindowTitle("Delete resources");
50         IPath path = new Path("/icons/oic_logo_64x64.png");
51         URL find = FileLocator.find(Activator.getDefault().getBundle(), path,
52                 null);
53         setDefaultPageImageDescriptor(ImageDescriptor.createFromURL(find));
54
55         setNeedsProgressMonitor(true);
56     }
57
58     @Override
59     public void addPages() {
60         page = new DeleteResourcePage();
61         addPage(page);
62     }
63
64     @Override
65     public boolean performFinish() {
66         if (null == page) {
67             return false;
68         }
69         try {
70             getContainer().run(true, false, new IRunnableWithProgress() {
71
72                 @Override
73                 public void run(IProgressMonitor monitor)
74                         throws InvocationTargetException, InterruptedException {
75                     ResourceManager resourceManager = Activator.getDefault()
76                             .getResourceManager();
77                     try {
78                         monitor.beginTask("Resource Deletion", 2);
79                         monitor.worked(1);
80                         Set<SingleResource> singleResources = page
81                                 .getSelectedSingleResourcesList();
82                         if (null != singleResources
83                                 && singleResources.size() > 0) {
84                             Activator.getDefault().getResourceManager()
85                                     .removeSingleResources(singleResources);
86
87                             Resource res = resourceManager
88                                     .getCurrentResourceInSelection();
89                             if (null != res && res instanceof SingleResource) {
90                                 if (singleResources
91                                         .contains((SingleResource) res)) {
92                                     resourceManager
93                                             .resourceSelectionChanged(null);
94                                 }
95                             }
96                         }
97                         monitor.worked(1);
98                         status = "Resources deleted.";
99                     } catch (SimulatorException e) {
100                         status = "Failed to delete some of the resources. Please try again.";
101                     } finally {
102                         monitor.done();
103                     }
104                 }
105             });
106         } catch (InvocationTargetException e) {
107             Activator.getDefault().getLogManager()
108                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
109         } catch (InterruptedException e) {
110             Activator.getDefault().getLogManager()
111                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
112         }
113         return true;
114     }
115
116     public String getStatus() {
117         return status;
118     }
119 }