1810
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.internal.discovery.ui.wizard;
|
|
18 |
|
|
19 |
|
|
20 |
import java.io.File;
|
1818
|
21 |
import java.io.FileInputStream;
|
|
22 |
import java.io.IOException;
|
1826
|
23 |
import java.net.URI;
|
1818
|
24 |
import java.text.MessageFormat;
|
1826
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.Collection;
|
1810
|
27 |
|
|
28 |
import org.eclipse.core.runtime.IPath;
|
1818
|
29 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
30 |
import org.eclipse.core.runtime.IStatus;
|
1810
|
31 |
import org.eclipse.core.runtime.Path;
|
1818
|
32 |
import org.eclipse.core.runtime.Status;
|
1810
|
33 |
import org.eclipse.jface.layout.GridDataFactory;
|
|
34 |
import org.eclipse.jface.layout.GridLayoutFactory;
|
|
35 |
import org.eclipse.swt.SWT;
|
1818
|
36 |
import org.eclipse.swt.events.ModifyEvent;
|
|
37 |
import org.eclipse.swt.events.ModifyListener;
|
1810
|
38 |
import org.eclipse.swt.events.SelectionAdapter;
|
|
39 |
import org.eclipse.swt.events.SelectionEvent;
|
|
40 |
import org.eclipse.swt.widgets.Button;
|
|
41 |
import org.eclipse.swt.widgets.Composite;
|
|
42 |
import org.eclipse.swt.widgets.FileDialog;
|
1818
|
43 |
import org.eclipse.ui.progress.UIJob;
|
1810
|
44 |
|
1818
|
45 |
import com.nokia.carbide.discovery.ui.Activator;
|
1824
|
46 |
import com.nokia.carbide.discovery.ui.Messages;
|
1810
|
47 |
import com.nokia.cpp.internal.api.utils.ui.BrowseDialogUtils;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Page in import wizard
|
|
51 |
*/
|
|
52 |
class ImportPage extends AbstractImportExportPage {
|
|
53 |
|
|
54 |
// the following two arrays need to correspond
|
|
55 |
static final String[] FILTER_EXTS = {
|
|
56 |
"*.xml", //$NON-NLS-1$
|
|
57 |
"*.*" //$NON-NLS-1$
|
|
58 |
};
|
|
59 |
static final String[] FILTER_EXT_NAMES = {
|
1824
|
60 |
Messages.ImportPage_XMLFileFilterName,
|
|
61 |
Messages.ImportPage_AllFilesFilterName
|
1810
|
62 |
};
|
1818
|
63 |
|
|
64 |
private String currentPath;
|
|
65 |
private ImportExportData readData;
|
1824
|
66 |
private boolean wantsOriginalVersions;
|
1810
|
67 |
|
|
68 |
protected ImportPage() {
|
|
69 |
super("ImportPage"); //$NON-NLS-1$
|
1824
|
70 |
setTitle(Messages.ImportPage_Title);
|
|
71 |
setDescription(Messages.ImportPage_Description);
|
1810
|
72 |
}
|
|
73 |
|
|
74 |
public void createControl(Composite parent) {
|
|
75 |
super.createControl(parent);
|
|
76 |
Composite composite = (Composite) getControl();
|
|
77 |
|
1824
|
78 |
createBrowseGroup(composite, Messages.ImportPage_BrowseGroupLabel);
|
|
79 |
createViewerGroup(composite, Messages.ImportPage_ViewerGroupLabel);
|
|
80 |
createVersionPrefGroup(composite);
|
1818
|
81 |
|
|
82 |
setPageComplete(validatePage());
|
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
|
86 |
protected void createBrowseGroup(Composite parent, String labelText) {
|
|
87 |
super.createBrowseGroup(parent, labelText);
|
|
88 |
pathText.addModifyListener(new ModifyListener() {
|
|
89 |
@Override
|
|
90 |
public void modifyText(ModifyEvent e) {
|
|
91 |
handlePathChanged();
|
|
92 |
}
|
|
93 |
});
|
|
94 |
|
|
95 |
browseButton.addSelectionListener(new SelectionAdapter() {
|
1810
|
96 |
@Override
|
|
97 |
public void widgetSelected(SelectionEvent e) {
|
|
98 |
FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
|
1824
|
99 |
fileDialog.setText(Messages.ImportPage_FileDialogText);
|
1810
|
100 |
fileDialog.setFilterExtensions(FILTER_EXTS);
|
|
101 |
fileDialog.setFilterNames(FILTER_EXT_NAMES);
|
|
102 |
BrowseDialogUtils.initializeFrom(fileDialog, pathText);
|
|
103 |
String pathstr = fileDialog.open();
|
|
104 |
if (pathstr != null) {
|
|
105 |
pathText.setText(pathstr);
|
|
106 |
}
|
|
107 |
}
|
1818
|
108 |
});
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
protected void createViewerGroup(Composite parent, String labelText) {
|
|
113 |
super.createViewerGroup(parent, labelText);
|
|
114 |
updateViewer();
|
1810
|
115 |
}
|
|
116 |
|
1818
|
117 |
private void startGetInputJob(final String path) {
|
1824
|
118 |
UIJob j = new UIJob(Messages.ImportPage_ReadFileJobName) {
|
1818
|
119 |
@Override
|
|
120 |
public IStatus runInUIThread(IProgressMonitor monitor) {
|
|
121 |
try {
|
|
122 |
FileInputStream is = new FileInputStream(path);
|
|
123 |
readData = Streamer.readFromXML(is);
|
|
124 |
viewer.setInput(readData.getFeatureInfos());
|
|
125 |
updateViewer();
|
|
126 |
monitor.done();
|
|
127 |
} catch (IOException e) {
|
|
128 |
// may have bad/incomplete path, so don't log this
|
|
129 |
} catch (Exception e) {
|
1824
|
130 |
Activator.logError(MessageFormat.format(Messages.ImportPage_ReadFileError, path), e);
|
1818
|
131 |
}
|
|
132 |
return Status.OK_STATUS;
|
|
133 |
}
|
|
134 |
};
|
|
135 |
j.schedule();
|
|
136 |
}
|
|
137 |
|
1824
|
138 |
protected void createVersionPrefGroup(Composite parent) {
|
1810
|
139 |
Composite composite = new Composite(parent, SWT.NONE);
|
|
140 |
GridLayoutFactory.fillDefaults().applyTo(composite);
|
|
141 |
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(composite);
|
|
142 |
|
|
143 |
final Button originalVersionCheck = new Button(composite, SWT.CHECK);
|
1824
|
144 |
originalVersionCheck.setText(Messages.ImportPage_OriginalVersionCheckLabel);
|
1810
|
145 |
originalVersionCheck.addSelectionListener(new SelectionAdapter() {
|
|
146 |
@Override
|
|
147 |
public void widgetSelected(SelectionEvent e) {
|
1818
|
148 |
wantsOriginalVersions = originalVersionCheck.getSelection();
|
1810
|
149 |
}
|
|
150 |
});
|
|
151 |
}
|
1818
|
152 |
|
1810
|
153 |
protected boolean validatePage() {
|
|
154 |
setErrorMessage(null);
|
|
155 |
IPath path = new Path(pathText.getText());
|
1818
|
156 |
if (isFilePath(path.toOSString())) {
|
|
157 |
if (readData == null || readData.getFeatureInfos().isEmpty()) {
|
1824
|
158 |
setErrorMessage(Messages.ImportPage_NoValidFeaturesInFileError);
|
1810
|
159 |
return false;
|
|
160 |
}
|
1818
|
161 |
if (readData == null || readData.getURIs().isEmpty()) {
|
1824
|
162 |
setErrorMessage(Messages.ImportPage_NoValidReposInFileError);
|
1818
|
163 |
return false;
|
|
164 |
}
|
1810
|
165 |
}
|
|
166 |
else {
|
1824
|
167 |
setErrorMessage(Messages.ImportPage_NoValidFileError);
|
1810
|
168 |
return false;
|
|
169 |
}
|
|
170 |
|
|
171 |
if (viewer.getCheckedElements().length == 0) {
|
1824
|
172 |
setErrorMessage(Messages.ImportPage_NoFeaturesSelectedError);
|
1810
|
173 |
return false;
|
|
174 |
}
|
|
175 |
|
|
176 |
return true;
|
|
177 |
}
|
|
178 |
|
1818
|
179 |
private boolean isFilePath(String path) {
|
|
180 |
File file = new File(path);
|
|
181 |
return file.exists() && !file.isDirectory();
|
1810
|
182 |
}
|
|
183 |
|
1818
|
184 |
private void handlePathChanged() {
|
|
185 |
String path = pathText.getText();
|
|
186 |
if (!path.equals(currentPath) && isFilePath(path)) {
|
|
187 |
startGetInputJob(currentPath = path);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
1826
|
191 |
public Collection<FeatureInfo> getFeatureInfos() {
|
|
192 |
Collection<FeatureInfo> infos = new ArrayList<FeatureInfo>();
|
|
193 |
Object[] objs = viewer.getCheckedElements();
|
|
194 |
for (Object o : objs) {
|
|
195 |
infos.add((FeatureInfo) o);
|
|
196 |
}
|
|
197 |
return infos;
|
|
198 |
}
|
|
199 |
|
|
200 |
public Collection<URI> getURIs() {
|
|
201 |
return readData.getURIs();
|
1818
|
202 |
}
|
1824
|
203 |
|
|
204 |
public boolean getWantsOriginalVersions() {
|
|
205 |
return wantsOriginalVersions;
|
|
206 |
}
|
1810
|
207 |
}
|