Imported Upstream version 1.0.1
[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.net.URL;
20
21 import oic.simulator.serviceprovider.Activator;
22 import oic.simulator.serviceprovider.resource.DeleteCategory;
23
24 import org.eclipse.core.runtime.FileLocator;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.PlatformUI;
32
33 /**
34  * This class creates a UI wizard for delete resource operation.
35  */
36 public class DeleteResourceWizard extends Wizard {
37
38     private DeleteResourcePage page;
39
40     public DeleteResourceWizard() {
41         setWindowTitle("Delete resources");
42         IPath path = new Path("/icons/oic_logo_64x64.png");
43         URL find = FileLocator.find(Activator.getDefault().getBundle(), path,
44                 null);
45         setDefaultPageImageDescriptor(ImageDescriptor.createFromURL(find));
46     }
47
48     @Override
49     public void addPages() {
50         page = new DeleteResourcePage();
51         addPage(page);
52     }
53
54     @Override
55     public boolean performFinish() {
56         if (null == page) {
57             return false;
58         }
59         // Check the existence of the resource if the user has entered the uri
60         if (page.getDeleteCategory() == DeleteCategory.BY_URI) {
61             // Check whether the uri is in full form or short form
62             // If it is in short form, expand it to its full form.
63             String uri = page.getDeleteCandidate();
64             boolean dispName = Activator.getDefault().getResourceManager()
65                     .isDisplayName(uri);
66             if (dispName) {
67                 uri = Activator.getDefault().getResourceManager()
68                         .getCompleteUriFromDisplayName(uri);
69             }
70             boolean exist = Activator.getDefault().getResourceManager()
71                     .isResourceExist(uri);
72             if (!exist) {
73                 Shell activeShell = PlatformUI.getWorkbench().getDisplay()
74                         .getActiveShell();
75                 MessageDialog dialog = new MessageDialog(activeShell,
76                         "Resource Not Found", null,
77                         "No resource exist with the given URI.",
78                         MessageDialog.INFORMATION, new String[] { "OK" }, 0);
79                 dialog.open();
80                 page.setFocusToTextBox();
81                 return false;
82             }
83         }
84         return true;
85     }
86
87     public DeleteCategory getDeleteCategory() {
88         if (null == page) {
89             return DeleteCategory.NONE;
90         }
91         return page.getDeleteCategory();
92     }
93
94     public String getDeleteCandidate() {
95         if (null == page) {
96             return null;
97         }
98         return page.getDeleteCandidate();
99     }
100 }