|
1 /* |
|
2 * Copyright (c) 2009 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 |
|
18 package com.nokia.carbide.cpp.internal.pi.button.ui; |
|
19 |
|
20 import java.io.File; |
|
21 import java.util.ArrayList; |
|
22 |
|
23 import org.eclipse.jface.wizard.WizardPage; |
|
24 import org.eclipse.swt.SWT; |
|
25 import org.eclipse.swt.events.ModifyEvent; |
|
26 import org.eclipse.swt.events.ModifyListener; |
|
27 import org.eclipse.swt.events.SelectionAdapter; |
|
28 import org.eclipse.swt.events.SelectionEvent; |
|
29 import org.eclipse.swt.layout.GridData; |
|
30 import org.eclipse.swt.layout.GridLayout; |
|
31 import org.eclipse.swt.widgets.Button; |
|
32 import org.eclipse.swt.widgets.Composite; |
|
33 import org.eclipse.swt.widgets.FileDialog; |
|
34 import org.eclipse.swt.widgets.Label; |
|
35 import org.eclipse.swt.widgets.Text; |
|
36 import org.eclipse.ui.PlatformUI; |
|
37 |
|
38 import com.nokia.carbide.cpp.pi.button.BupEventMapManager; |
|
39 import com.nokia.carbide.cpp.pi.button.ComNokiaCarbidePiButtonHelpIDs; |
|
40 import com.nokia.carbide.cpp.pi.button.IBupEventMapProfile; |
|
41 |
|
42 |
|
43 public class ImportBupMapGetXmlTask extends WizardPage { |
|
44 private final static String XML_EXTENSION= "xml"; //$NON-NLS-1$ |
|
45 private boolean visableBefore = false; |
|
46 String importXmlPathString = null; |
|
47 |
|
48 ArrayList<IBupEventMapProfile> overlapList = new ArrayList<IBupEventMapProfile>(); |
|
49 |
|
50 protected ImportBupMapGetXmlTask() { |
|
51 super(Messages.getString("ImportBupMapGetXmlTask.selectProfile")); //$NON-NLS-1$ |
|
52 setTitle(Messages.getString("ImportBupMapGetXmlTask.selectProfile")); //$NON-NLS-1$ |
|
53 setDescription(Messages.getString("ImportBupMapGetXmlTask.importProfileExplained")); //$NON-NLS-1$ |
|
54 } |
|
55 |
|
56 // controls |
|
57 Composite container; |
|
58 private Text inputXmlFileText; |
|
59 |
|
60 /* (non-Javadoc) |
|
61 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) |
|
62 */ |
|
63 public void createControl(Composite parent) { |
|
64 container = new Composite(parent, SWT.NULL); |
|
65 GridLayout layout = new GridLayout(); |
|
66 container.setLayout(layout); |
|
67 layout.numColumns = 1; |
|
68 layout.verticalSpacing = 9; |
|
69 |
|
70 Label labelSampleFile = new Label(container, SWT.NONE); |
|
71 labelSampleFile.setText(Messages.getString("ImportBupMapGetXmlTask.importThisProfile")); //$NON-NLS-1$ |
|
72 |
|
73 // subcomposite with textbox/button |
|
74 Composite subContainer = new Composite(container, SWT.NULL); |
|
75 GridLayout subLayout = new GridLayout(); |
|
76 subContainer.setLayout(subLayout); |
|
77 subContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
|
78 subLayout.numColumns = 2; |
|
79 subLayout.verticalSpacing = 9; |
|
80 |
|
81 inputXmlFileText = new Text(subContainer, SWT.BORDER | SWT.SINGLE); |
|
82 inputXmlFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
|
83 inputXmlFileText.addModifyListener(new ModifyListener(){ |
|
84 public void modifyText(ModifyEvent arg0) { |
|
85 validatePage(); |
|
86 } |
|
87 }); |
|
88 |
|
89 Button button = new Button(subContainer, SWT.PUSH); |
|
90 button.setText(Messages.getString("ImportBupMapGetXmlTask.keyPressProfile")); //$NON-NLS-1$ |
|
91 button.addSelectionListener(new SelectionAdapter() { |
|
92 public void widgetSelected(SelectionEvent e) { |
|
93 handleBrowse(); |
|
94 } |
|
95 }); |
|
96 |
|
97 // top tap get focus |
|
98 inputXmlFileText.setFocus(); |
|
99 |
|
100 setControl(container); |
|
101 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ComNokiaCarbidePiButtonHelpIDs.PI_KEY_MAP_IMPORT_WIZARD_XML); |
|
102 } |
|
103 |
|
104 public void validatePage() { |
|
105 if (inputXmlFileText.getText() == null || inputXmlFileText.getText().length() < 1) { |
|
106 updateStatus(null); |
|
107 setPageComplete(false); |
|
108 return; // blank should be OK, consistent with Eclipse guideline |
|
109 } |
|
110 |
|
111 int dotLoc = inputXmlFileText.getText().lastIndexOf('.'); |
|
112 if (dotLoc != -1) { |
|
113 String ext = inputXmlFileText.getText().substring(dotLoc + 1); |
|
114 if (ext.equalsIgnoreCase(XML_EXTENSION) == false) { |
|
115 updateStatus(Messages.getString("ImportBupMapGetXmlTask.mustEndWithXML")); //$NON-NLS-1$ |
|
116 return; |
|
117 } |
|
118 } |
|
119 |
|
120 if (!(new File(inputXmlFileText.getText()).exists())) { |
|
121 updateStatus(Messages.getString("ImportBupMapGetXmlTask.fileDoesNotExist")); //$NON-NLS-1$ |
|
122 return; |
|
123 } |
|
124 if (!(new File(inputXmlFileText.getText()).isFile())) { |
|
125 updateStatus(Messages.getString("ImportBupMapGetXmlTask.enterProfileFileName")); //$NON-NLS-1$ |
|
126 return; |
|
127 } |
|
128 |
|
129 // this file looks good, let's see if it got overlap |
|
130 java.io.File file = new java.io.File(inputXmlFileText.getText()); |
|
131 overlapList = BupEventMapManager.getInstance().getOverLapWithWorkspace(file.toURI()); |
|
132 importXmlPathString = inputXmlFileText.getText(); |
|
133 ((ImportBupMapWizard)getWizard()).setAllowFinish(overlapList.size() == 0); |
|
134 |
|
135 setPageComplete(true); |
|
136 updateStatus(null); |
|
137 } |
|
138 |
|
139 private void updateStatus(String message) { |
|
140 // need to enable finish, setPageComplete eventually check global states |
|
141 setErrorMessage(message); |
|
142 setPageComplete(message == null); |
|
143 } |
|
144 |
|
145 private void handleBrowse() { |
|
146 FileDialog dialog = new FileDialog(getShell()); |
|
147 String[] datExtensions = {"*.xml", //$NON-NLS-1$ |
|
148 "*.*"}; //$NON-NLS-1$ |
|
149 String[] datNames = {Messages.getString("ImportBupMapGetXmlTask.profileFiles"), //$NON-NLS-1$ |
|
150 Messages.getString("ImportBupMapGetXmlTask.allFiles")}; //$NON-NLS-1$ |
|
151 dialog.setFilterExtensions(datExtensions); |
|
152 dialog.setFilterNames(datNames); |
|
153 // Try guiding user to path user is trying to fill |
|
154 File clueFile = new File(inputXmlFileText.getText()); |
|
155 String cluePath = null; |
|
156 if (clueFile.isDirectory()) { |
|
157 cluePath = clueFile.getAbsolutePath(); |
|
158 } else { |
|
159 cluePath = clueFile.getParent(); |
|
160 } |
|
161 if (cluePath != null) { |
|
162 dialog.setFilterPath(cluePath); |
|
163 } |
|
164 String filePath = dialog.open(); |
|
165 |
|
166 if (filePath != null) { |
|
167 if (filePath.length() > 0) { |
|
168 inputXmlFileText.setText(filePath); |
|
169 } |
|
170 } |
|
171 } |
|
172 |
|
173 public void setVisible(boolean visable) { |
|
174 super.setVisible(visable); |
|
175 |
|
176 if (visable) { |
|
177 // block the text if it's wrong on startup, so user know what's wrong |
|
178 if (visableBefore == false && getErrorMessage() != null) { |
|
179 inputXmlFileText.selectAll(); |
|
180 } |
|
181 if (new java.io.File(inputXmlFileText.getText()).exists()) { |
|
182 if (overlapList.size() > 0) { |
|
183 ((ImportBupMapWizard)getWizard()).setAllowFinish(false); |
|
184 } |
|
185 ((ImportBupMapWizard)getWizard()).setAllowFinish(true); |
|
186 } else { |
|
187 ((ImportBupMapWizard)getWizard()).setAllowFinish(false); |
|
188 } |
|
189 } else { |
|
190 ((ImportBupMapWizard)getWizard()).setAllowFinish(true); |
|
191 } |
|
192 visableBefore = visable; |
|
193 } |
|
194 |
|
195 /** |
|
196 * @return |
|
197 */ |
|
198 public String[] getOverLapList() { |
|
199 ArrayList<String> resultList = new ArrayList<String>(); |
|
200 |
|
201 for (IBupEventMapProfile overlapProfile : overlapList) { |
|
202 resultList.add(overlapProfile.getProfileId()); |
|
203 } |
|
204 |
|
205 return resultList.toArray(new String[resultList.size()]); |
|
206 } |
|
207 |
|
208 /** |
|
209 * @return |
|
210 */ |
|
211 public String getxImportXml() { |
|
212 return importXmlPathString; |
|
213 } |
|
214 } |