16 */ |
16 */ |
17 package com.nokia.carbide.internal.discovery.ui.wizard; |
17 package com.nokia.carbide.internal.discovery.ui.wizard; |
18 |
18 |
19 |
19 |
20 import java.io.File; |
20 import java.io.File; |
|
21 import java.io.FileInputStream; |
|
22 import java.io.IOException; |
|
23 import java.text.MessageFormat; |
21 |
24 |
22 import org.eclipse.core.runtime.IPath; |
25 import org.eclipse.core.runtime.IPath; |
|
26 import org.eclipse.core.runtime.IProgressMonitor; |
|
27 import org.eclipse.core.runtime.IStatus; |
23 import org.eclipse.core.runtime.Path; |
28 import org.eclipse.core.runtime.Path; |
|
29 import org.eclipse.core.runtime.Status; |
24 import org.eclipse.jface.layout.GridDataFactory; |
30 import org.eclipse.jface.layout.GridDataFactory; |
25 import org.eclipse.jface.layout.GridLayoutFactory; |
31 import org.eclipse.jface.layout.GridLayoutFactory; |
26 import org.eclipse.jface.viewers.CheckStateChangedEvent; |
32 import org.eclipse.jface.viewers.CheckStateChangedEvent; |
27 import org.eclipse.jface.viewers.ICheckStateListener; |
33 import org.eclipse.jface.viewers.ICheckStateListener; |
28 import org.eclipse.swt.SWT; |
34 import org.eclipse.swt.SWT; |
|
35 import org.eclipse.swt.events.ModifyEvent; |
|
36 import org.eclipse.swt.events.ModifyListener; |
29 import org.eclipse.swt.events.SelectionAdapter; |
37 import org.eclipse.swt.events.SelectionAdapter; |
30 import org.eclipse.swt.events.SelectionEvent; |
38 import org.eclipse.swt.events.SelectionEvent; |
31 import org.eclipse.swt.widgets.Button; |
39 import org.eclipse.swt.widgets.Button; |
32 import org.eclipse.swt.widgets.Composite; |
40 import org.eclipse.swt.widgets.Composite; |
33 import org.eclipse.swt.widgets.FileDialog; |
41 import org.eclipse.swt.widgets.FileDialog; |
|
42 import org.eclipse.ui.progress.UIJob; |
34 |
43 |
|
44 import com.nokia.carbide.discovery.ui.Activator; |
35 import com.nokia.cpp.internal.api.utils.ui.BrowseDialogUtils; |
45 import com.nokia.cpp.internal.api.utils.ui.BrowseDialogUtils; |
36 |
46 |
37 /** |
47 /** |
38 * Page in import wizard |
48 * Page in import wizard |
39 */ |
49 */ |
40 class ImportPage extends AbstractImportExportPage { |
50 class ImportPage extends AbstractImportExportPage { |
41 |
|
42 private boolean importOriginalVersion; |
|
43 |
51 |
44 // the following two arrays need to correspond |
52 // the following two arrays need to correspond |
45 static final String[] FILTER_EXTS = { |
53 static final String[] FILTER_EXTS = { |
46 "*.xml", //$NON-NLS-1$ |
54 "*.xml", //$NON-NLS-1$ |
47 "*.*" //$NON-NLS-1$ |
55 "*.*" //$NON-NLS-1$ |
48 }; |
56 }; |
49 static final String[] FILTER_EXT_NAMES = { |
57 static final String[] FILTER_EXT_NAMES = { |
50 "XML Files", |
58 "XML Files", |
51 "All Files" |
59 "All Files" |
52 }; |
60 }; |
|
61 |
|
62 private String currentPath; |
|
63 private ImportExportData readData; |
|
64 protected boolean wantsOriginalVersions; |
53 |
65 |
54 |
|
55 protected ImportPage() { |
66 protected ImportPage() { |
56 super("ImportPage"); //$NON-NLS-1$ |
67 super("ImportPage"); //$NON-NLS-1$ |
57 setTitle("Import Feature Configuration and Install"); |
68 setTitle("Import Feature Configuration and Install"); |
58 setDescription("Import a feature configurations from a file and install the features"); |
69 setDescription("Import a feature configurations from a file and install the features"); |
59 } |
70 } |
60 |
71 |
61 public void createControl(Composite parent) { |
72 public void createControl(Composite parent) { |
62 super.createControl(parent); |
73 super.createControl(parent); |
63 Composite composite = (Composite) getControl(); |
74 Composite composite = (Composite) getControl(); |
64 |
75 |
65 createBrowseGroup(composite, "Feature configuration file:"); |
76 createBrowseGroup(composite, "Import file:"); |
66 browseButton.addSelectionListener(new SelectionAdapter() { |
77 createViewerGroup(composite, "Import Features:"); |
|
78 |
|
79 setPageComplete(validatePage()); |
|
80 } |
|
81 |
|
82 @Override |
|
83 protected void createBrowseGroup(Composite parent, String labelText) { |
|
84 super.createBrowseGroup(parent, labelText); |
|
85 pathText.addModifyListener(new ModifyListener() { |
|
86 @Override |
|
87 public void modifyText(ModifyEvent e) { |
|
88 handlePathChanged(); |
|
89 } |
|
90 }); |
|
91 |
|
92 browseButton.addSelectionListener(new SelectionAdapter() { |
67 @Override |
93 @Override |
68 public void widgetSelected(SelectionEvent e) { |
94 public void widgetSelected(SelectionEvent e) { |
69 FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); |
95 FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); |
70 fileDialog.setText("Select an Exported Feature Configuration File"); |
96 fileDialog.setText("Select an Exported Feature Configuration File"); |
71 fileDialog.setFilterExtensions(FILTER_EXTS); |
97 fileDialog.setFilterExtensions(FILTER_EXTS); |
72 fileDialog.setFilterNames(FILTER_EXT_NAMES); |
98 fileDialog.setFilterNames(FILTER_EXT_NAMES); |
73 BrowseDialogUtils.initializeFrom(fileDialog, pathText); |
99 BrowseDialogUtils.initializeFrom(fileDialog, pathText); |
74 String pathstr = fileDialog.open(); |
100 String pathstr = fileDialog.open(); |
75 if (pathstr != null) { |
101 if (pathstr != null) { |
76 pathText.setText(pathstr); |
102 pathText.setText(pathstr); |
|
103 // handlePathChanged(); |
77 } |
104 } |
78 } |
105 } |
79 }); |
106 }); |
80 createViewerGroup(composite, "Import Features:"); |
107 } |
|
108 |
|
109 @Override |
|
110 protected void createViewerGroup(Composite parent, String labelText) { |
|
111 super.createViewerGroup(parent, labelText); |
|
112 |
81 viewer.addCheckStateListener(new ICheckStateListener() { |
113 viewer.addCheckStateListener(new ICheckStateListener() { |
82 public void checkStateChanged(CheckStateChangedEvent event) { |
114 public void checkStateChanged(CheckStateChangedEvent event) { |
83 setPageComplete(validatePage()); |
115 setPageComplete(validatePage()); |
84 } |
116 } |
85 }); |
117 }); |
86 |
118 updateViewer(); |
87 setPageComplete(validatePage()); |
|
88 } |
119 } |
89 |
120 |
|
121 private void startGetInputJob(final String path) { |
|
122 UIJob j = new UIJob("Reading Feature Configuration File") { |
|
123 @Override |
|
124 public IStatus runInUIThread(IProgressMonitor monitor) { |
|
125 try { |
|
126 FileInputStream is = new FileInputStream(path); |
|
127 readData = Streamer.readFromXML(is); |
|
128 viewer.setInput(readData.getFeatureInfos()); |
|
129 updateViewer(); |
|
130 monitor.done(); |
|
131 } catch (IOException e) { |
|
132 // may have bad/incomplete path, so don't log this |
|
133 } catch (Exception e) { |
|
134 Activator.logError(MessageFormat.format("Could not read data from file: {0}", path), e); |
|
135 } |
|
136 return Status.OK_STATUS; |
|
137 } |
|
138 }; |
|
139 j.schedule(); |
|
140 } |
|
141 |
90 protected void createVersionRadioGroup(Composite parent) { |
142 protected void createVersionRadioGroup(Composite parent) { |
91 Composite composite = new Composite(parent, SWT.NONE); |
143 Composite composite = new Composite(parent, SWT.NONE); |
92 GridLayoutFactory.fillDefaults().applyTo(composite); |
144 GridLayoutFactory.fillDefaults().applyTo(composite); |
93 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(composite); |
145 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(composite); |
94 |
146 |
95 final Button originalVersionCheck = new Button(composite, SWT.CHECK); |
147 final Button originalVersionCheck = new Button(composite, SWT.CHECK); |
96 originalVersionCheck.setText("Attempt import original feature versions"); |
148 originalVersionCheck.setText("Attempt import original feature versions"); |
97 originalVersionCheck.addSelectionListener(new SelectionAdapter() { |
149 originalVersionCheck.addSelectionListener(new SelectionAdapter() { |
98 @Override |
150 @Override |
99 public void widgetSelected(SelectionEvent e) { |
151 public void widgetSelected(SelectionEvent e) { |
100 importOriginalVersion = originalVersionCheck.getSelection(); |
152 wantsOriginalVersions = originalVersionCheck.getSelection(); |
101 } |
153 } |
102 }); |
154 }); |
103 } |
155 } |
104 |
156 |
105 protected boolean validatePage() { |
157 protected boolean validatePage() { |
106 setErrorMessage(null); |
158 setErrorMessage(null); |
107 IPath path = new Path(pathText.getText()); |
159 IPath path = new Path(pathText.getText()); |
108 File file = path.toFile(); |
160 if (isFilePath(path.toOSString())) { |
109 if (file.exists()) { |
161 if (readData == null || readData.getFeatureInfos().isEmpty()) { |
110 FeatureInfo[] featureInfos = getFeaturesFromFile(file); |
162 setErrorMessage("No valid features found in configurations file"); |
111 if (featureInfos.length == 0) { |
|
112 setErrorMessage("The file has no valid features"); |
|
113 return false; |
163 return false; |
114 } |
164 } |
115 viewer.setInput(featureInfos); |
165 if (readData == null || readData.getURIs().isEmpty()) { |
116 updateViewer(); |
166 setErrorMessage("No valid repositories found in configurations file"); |
|
167 return false; |
|
168 } |
117 } |
169 } |
118 else { |
170 else { |
119 setErrorMessage("A valid exported feature configuration file must be selected"); |
171 setErrorMessage("A valid exported feature configuration file must be selected"); |
120 return false; |
172 return false; |
121 } |
173 } |