|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 package com.symbian.smt.gui.smtwidgets; |
|
17 |
|
18 import java.io.IOException; |
|
19 import java.io.InputStream; |
|
20 import java.net.URL; |
|
21 import java.util.ArrayList; |
|
22 |
|
23 import org.eclipse.core.runtime.FileLocator; |
|
24 import org.eclipse.core.runtime.Path; |
|
25 import org.eclipse.core.runtime.Platform; |
|
26 import org.eclipse.jface.dialogs.Dialog; |
|
27 import org.eclipse.swt.SWT; |
|
28 import org.eclipse.swt.events.PaintEvent; |
|
29 import org.eclipse.swt.events.PaintListener; |
|
30 import org.eclipse.swt.events.SelectionAdapter; |
|
31 import org.eclipse.swt.events.SelectionEvent; |
|
32 import org.eclipse.swt.graphics.Image; |
|
33 import org.eclipse.swt.layout.FillLayout; |
|
34 import org.eclipse.swt.layout.GridData; |
|
35 import org.eclipse.swt.layout.GridLayout; |
|
36 import org.eclipse.swt.layout.RowData; |
|
37 import org.eclipse.swt.layout.RowLayout; |
|
38 import org.eclipse.swt.widgets.Button; |
|
39 import org.eclipse.swt.widgets.Composite; |
|
40 import org.eclipse.swt.widgets.Display; |
|
41 import org.eclipse.swt.widgets.List; |
|
42 import org.eclipse.swt.widgets.Shell; |
|
43 import org.eclipse.ui.PlatformUI; |
|
44 import org.osgi.framework.Bundle; |
|
45 |
|
46 import com.symbian.smt.gui.Helper; |
|
47 import com.symbian.smt.gui.Logger; |
|
48 import com.symbian.smt.gui.smtwidgets.ValidModelEvent.Type; |
|
49 |
|
50 public class SystemDefinitionFilesWidget extends Composite implements |
|
51 ValidModelObservable { |
|
52 private List systemDefinitionFilesList = null; |
|
53 private ArrayList<ValidModelDefinedListener> listeners = new ArrayList<ValidModelDefinedListener>(); |
|
54 |
|
55 /** |
|
56 * Creates a SystemDefinitionFilesWidget composite object |
|
57 * |
|
58 * @return void |
|
59 */ |
|
60 public SystemDefinitionFilesWidget(final Composite parent, int style) { |
|
61 super(parent, style); |
|
62 |
|
63 this.setLayout(new FillLayout()); |
|
64 this.setRedraw(true); |
|
65 |
|
66 final Composite compositeMainGridLayout = new Composite(this, SWT.NONE); |
|
67 final GridLayout gridLayout = new GridLayout(); |
|
68 gridLayout.numColumns = 2; |
|
69 compositeMainGridLayout.setLayout(gridLayout); |
|
70 |
|
71 systemDefinitionFilesList = new List(compositeMainGridLayout, |
|
72 SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); |
|
73 systemDefinitionFilesList.setBackground(Display.getCurrent() |
|
74 .getSystemColor(SWT.COLOR_WHITE)); |
|
75 final GridData gd_list = new GridData(SWT.FILL, SWT.FILL, true, true); |
|
76 gd_list.widthHint = 271; |
|
77 systemDefinitionFilesList.setLayoutData(gd_list); |
|
78 |
|
79 systemDefinitionFilesList.addPaintListener(new PaintListener() { |
|
80 public void paintControl(PaintEvent e) { |
|
81 CheckRequiredInformationPresent(); |
|
82 } |
|
83 }); |
|
84 |
|
85 final Composite compositeUpDownButtons = new Composite( |
|
86 compositeMainGridLayout, SWT.NONE); |
|
87 final GridData gd_compositeUpDownButtons = new GridData(); |
|
88 compositeUpDownButtons.setLayoutData(gd_compositeUpDownButtons); |
|
89 compositeUpDownButtons.setLayout(new RowLayout(SWT.VERTICAL)); |
|
90 |
|
91 final Button upButton = new Button(compositeUpDownButtons, SWT.FLAT); |
|
92 final RowData rd_upButton = new RowData(); |
|
93 rd_upButton.height = 26; |
|
94 rd_upButton.width = 26; |
|
95 upButton.setLayoutData(rd_upButton); |
|
96 upButton.setImage(getUpArrowImage()); |
|
97 upButton.addSelectionListener(new SelectionAdapter() { |
|
98 public void widgetSelected(final SelectionEvent e) { |
|
99 // Reorder the system definition files in the list by swapping |
|
100 // the item |
|
101 // selected with the item above |
|
102 int index = systemDefinitionFilesList.getSelectionIndex(); |
|
103 |
|
104 if (index != -1 && index - 1 >= 0) { |
|
105 String item = systemDefinitionFilesList.getItem(index); |
|
106 String itemAbove = systemDefinitionFilesList |
|
107 .getItem(index - 1); |
|
108 |
|
109 systemDefinitionFilesList.setItem(index - 1, item); |
|
110 systemDefinitionFilesList.setItem(index, itemAbove); |
|
111 |
|
112 systemDefinitionFilesList.setSelection(index - 1); |
|
113 |
|
114 } |
|
115 systemDefinitionFilesList.setFocus(); |
|
116 } |
|
117 }); |
|
118 |
|
119 final Button downButton = new Button(compositeUpDownButtons, SWT.FLAT); |
|
120 final RowData rd_downButton = new RowData(); |
|
121 rd_downButton.height = 26; |
|
122 rd_downButton.width = 26; |
|
123 downButton.setLayoutData(rd_downButton); |
|
124 downButton.setImage(getDownArrowImage()); |
|
125 downButton.addSelectionListener(new SelectionAdapter() { |
|
126 public void widgetSelected(final SelectionEvent e) { |
|
127 // Reorder the system definition files in the list by swapping |
|
128 // the item |
|
129 // selected with the item below |
|
130 int index = systemDefinitionFilesList.getSelectionIndex(); |
|
131 |
|
132 if (index != -1 |
|
133 && index + 1 < systemDefinitionFilesList.getItemCount()) { // index |
|
134 // is |
|
135 // 0 |
|
136 // relative, |
|
137 // getItemCount |
|
138 // is |
|
139 // not |
|
140 String item = systemDefinitionFilesList.getItem(index); |
|
141 String itemBelow = systemDefinitionFilesList |
|
142 .getItem(index + 1); |
|
143 |
|
144 systemDefinitionFilesList.setItem(index + 1, item); |
|
145 systemDefinitionFilesList.setItem(index, itemBelow); |
|
146 |
|
147 systemDefinitionFilesList.setSelection(index + 1); |
|
148 } |
|
149 systemDefinitionFilesList.setFocus(); |
|
150 } |
|
151 }); |
|
152 |
|
153 final Composite compositeAddRemoveButtons = new Composite( |
|
154 compositeMainGridLayout, SWT.NONE); |
|
155 final GridData gd_compositeAddRemoveButtons = new GridData(SWT.CENTER, |
|
156 SWT.CENTER, false, false, 2, 1); |
|
157 compositeAddRemoveButtons.setLayoutData(gd_compositeAddRemoveButtons); |
|
158 final RowLayout rowLayout = new RowLayout(); |
|
159 rowLayout.spacing = 30; |
|
160 compositeAddRemoveButtons.setLayout(rowLayout); |
|
161 |
|
162 final Button addSystemDefinitionButton = new Button( |
|
163 compositeAddRemoveButtons, SWT.NONE); |
|
164 |
|
165 addSystemDefinitionButton.setText("Add System Definition File"); |
|
166 addSystemDefinitionButton.addSelectionListener(new SelectionAdapter() { |
|
167 public void widgetSelected(SelectionEvent e) { |
|
168 java.util.List<String> currentItems = Helper |
|
169 .toListOfStrings(systemDefinitionFilesList.getItems()); |
|
170 SystemDefinitionFileSelectionValidator validator = new SystemDefinitionFileSelectionValidator( |
|
171 currentItems); |
|
172 Shell shell = PlatformUI.getWorkbench() |
|
173 .getActiveWorkbenchWindow().getShell(); |
|
174 String dialogTitle = "New System Definition File"; |
|
175 String dialogMessage = "Enter the path or URL to the system definition file"; |
|
176 String initialPath = ""; |
|
177 String[] filterNames = { "*.xml" }; |
|
178 XmlFileSelectionDialog dialog = new XmlFileSelectionDialog( |
|
179 shell, dialogTitle, dialogMessage, initialPath, |
|
180 filterNames, validator); |
|
181 |
|
182 dialog.open(); |
|
183 |
|
184 if (dialog.getReturnCode() == Dialog.CANCEL) { |
|
185 return; |
|
186 } |
|
187 |
|
188 String filename = dialog.getValue(); |
|
189 |
|
190 if (filename != null && (filename.length() != 0)) { |
|
191 systemDefinitionFilesList.add(filename); |
|
192 } |
|
193 |
|
194 systemDefinitionFilesList.setFocus(); |
|
195 } |
|
196 }); |
|
197 |
|
198 final Button removeSystemDefinitionButton = new Button( |
|
199 compositeAddRemoveButtons, SWT.NONE); |
|
200 removeSystemDefinitionButton.setText("Remove System Definition File"); |
|
201 removeSystemDefinitionButton |
|
202 .addSelectionListener(new SelectionAdapter() { |
|
203 public void widgetSelected(SelectionEvent e) { |
|
204 int listIndex = systemDefinitionFilesList |
|
205 .getSelectionIndex(); |
|
206 |
|
207 if (listIndex != -1) { |
|
208 // Remove the system definition file from the list |
|
209 systemDefinitionFilesList.remove(listIndex); |
|
210 } |
|
211 systemDefinitionFilesList.setFocus(); |
|
212 } |
|
213 }); |
|
214 } |
|
215 |
|
216 /** |
|
217 * Registers a listener object, which will be notified if the conditions for |
|
218 * proceeding have been met. |
|
219 * |
|
220 * @param listener |
|
221 * A ValidModelDefinedListener object |
|
222 * @return void |
|
223 * @see com.symbian.smt.gui.smtwidgets.ValidModelObservable#addModelListener(ValidModelDefinedListener) |
|
224 */ |
|
225 public void addModelListener(ValidModelDefinedListener listener) { |
|
226 listeners.add(listener); |
|
227 } |
|
228 |
|
229 // Check that at least one system definition file has been specified. Any |
|
230 // registered |
|
231 // model listeners are notified with a ValidModelEvent. |
|
232 private void CheckRequiredInformationPresent() { |
|
233 if (listeners.size() > 0) { |
|
234 Boolean modelDefined = true; |
|
235 String message = ""; |
|
236 Type type = Type.SUCCESS; |
|
237 |
|
238 if (systemDefinitionFilesList.getItems().length == 0) { |
|
239 modelDefined = false; |
|
240 message = "You must specify at least 1 system definition xml file to proceed."; |
|
241 type = Type.ERROR; |
|
242 } |
|
243 |
|
244 ValidModelEvent event = new ValidModelEvent(modelDefined, message, |
|
245 type); |
|
246 |
|
247 for (ValidModelDefinedListener listener : listeners) { |
|
248 listener.validModelDefined(event); |
|
249 } |
|
250 } |
|
251 } |
|
252 |
|
253 private Image getDownArrowImage() { |
|
254 return new Image(getDisplay(), |
|
255 getImageAsStream("icons/Arrow_down_icons_24px.png")); |
|
256 } |
|
257 |
|
258 private InputStream getImageAsStream(String imageLocation) { |
|
259 Bundle bundle = Platform.getBundle("com.symbian.smt.gui"); |
|
260 Path path = new Path(imageLocation); |
|
261 URL imageURL = FileLocator.find(bundle, path, null); |
|
262 |
|
263 try { |
|
264 return imageURL.openStream(); |
|
265 } catch (IOException e) { |
|
266 Logger.log(e.getMessage(), e); |
|
267 } |
|
268 |
|
269 return null; |
|
270 } |
|
271 |
|
272 /** |
|
273 * Returns a list of the system definition files |
|
274 * |
|
275 * @return String[] |
|
276 */ |
|
277 public String[] getSystemDefinitions() { |
|
278 String[] sysDefs = systemDefinitionFilesList.getItems(); |
|
279 return sysDefs; |
|
280 } |
|
281 |
|
282 private Image getUpArrowImage() { |
|
283 return new Image(getDisplay(), |
|
284 getImageAsStream("icons/Arrow_Up_icons_24px.png")); |
|
285 } |
|
286 |
|
287 /** |
|
288 * @see com.symbian.smt.gui.smtwidgets.ValidModelObservable#removeModelListener(ValidModelDefinedListener) |
|
289 */ |
|
290 public void removeModelListener(ValidModelDefinedListener listener) { |
|
291 synchronized (listeners) { |
|
292 listeners.remove(listener); |
|
293 } |
|
294 } |
|
295 |
|
296 /** |
|
297 * Sets the system definition files |
|
298 * |
|
299 * @param sysDefs |
|
300 * A list containing system definition files |
|
301 * @return void |
|
302 */ |
|
303 public void setSystemDefinitions(String[] sysDefs) { |
|
304 systemDefinitionFilesList.setItems(sysDefs); |
|
305 } |
|
306 } |