upload tizen2.0 alpha installmanager source
[sdk/installer/install-manager.git] / InstallManager_java / src / org / tizen / installmanager / ui / page / InstallableListPage.java
1 /*
2  *  InstallManager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * Wooyoung Cho <wooyoung1.cho@samsung.com>
8  * Shihyun Kim <shihyun.kim@samsung.com>
9  * Taeyoung Son <taeyoung2.son@samsung.com>
10  * Yongsung kim <yongsung1.kim@samsung.com>
11  * 
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  * Contributors:
25  * - S-Core Co., Ltd
26  *
27  */
28
29 package org.tizen.installmanager.ui.page;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.tizen.installmanager.core.IMFatalException;
41 import org.tizen.installmanager.core.InstallManager;
42 import org.tizen.installmanager.core.SDKExtensionInfo;
43 import org.tizen.installmanager.lib.ErrorController;
44 import org.tizen.installmanager.lib.IMError;
45 import org.tizen.installmanager.lib.Log;
46 import org.tizen.installmanager.ui.InstallManagerWindow;
47 import org.tizen.installmanager.ui.model.Item;
48 import org.tizen.installmanager.pkg.lib.PackageManager;
49 import org.tizen.installmanager.pkg.model.Package;
50
51 /**
52  * This class displays installable package list on page.
53  * 
54  * @author Yongsung Kim <yongsung1.kim@samsung.com>
55  */
56 public class InstallableListPage extends PackageListPage {
57
58         static final String RSC_PATH_IMAGE = RESOURCE_PATH
59                         + "/install_manager_graphicmotif_002.png";
60         static final String STRING_TITLE = "Install Type";
61
62         static final String TYPICAL_BUTTON_NAME = "Typical";
63         static final String MINIMAL_BUTTON_NAME = "Minimal";
64         static final String CUSTOM_BUTTON_NAME = "Custom";
65
66         private Composite buttonComposite;
67
68         // Install Type button
69         private Button typicalButton;
70         private Button minimalButton;
71         private Button customButton;
72
73         // Install Type Explain Label
74         private Label typicalLabel;
75         private Label minimalLabel;
76
77         /**
78          * @param parent
79          * @param style
80          */
81         public InstallableListPage(Composite parent, int style) {
82                 super(parent, style, Type.INSTALL);
83                 setLeftImageFromResource(RSC_PATH_IMAGE);
84                 setTitleBounds(160, 3, 468, 20);
85                 disableSubtitleBounds();
86                 setTitle(STRING_TITLE);
87
88                 buttonComposite = new Composite(this, SWT.NONE);
89
90                 buttonComposite.setSize(468, 60);
91                 buttonComposite.setLocation(160, 27);
92                 buttonComposite
93                                 .setBackground(InstallManagerWindow.getBackgroundColor());
94
95                 setButtons();
96         }
97
98         /**
99          * Set Install Type radio buttons.
100          */
101         private void setButtons() {
102                 setTypicalButton();
103                 setMinimalButton();
104                 setCustomButton();
105         }
106
107         /**
108          * Set Typical Type radio button.
109          */
110         private void setTypicalButton() {
111                 if (typicalButton == null) {
112                         typicalButton = new Button(buttonComposite, SWT.RADIO);
113                 }
114
115                 typicalButton.setSelection(true);
116
117                 typicalButton.addSelectionListener(new SelectionAdapter() {
118                         @Override
119                         public void widgetSelected(SelectionEvent e) {
120                                 setCheckedByType(TYPICAL_BUTTON_NAME.toUpperCase());
121                                 checkboxTreeViewer.refresh();
122                                 checkboxTreeViewer.getTree().setEnabled(false);
123
124                                 lblRequiredSize.setText("Total space required : "
125                                                 + convertToVolumeSize(getCheckedPackageSize()));
126                                 
127                                 setNextBtnEnabledAboutCheckedPackageCount();
128                         }
129                 });
130
131                 typicalButton.setBounds(0, 0, 80, 15);
132                 typicalButton.setText(TYPICAL_BUTTON_NAME + ":");
133                 typicalButton.setBackground(InstallManagerWindow.getBackgroundColor());
134
135                 typicalLabel = new Label(buttonComposite, SWT.NONE);
136                 typicalLabel.setBounds(81, 0, 410, 15);
137         }
138
139         /**
140          * Set Minimal Type radio button.
141          */
142         private void setMinimalButton() {
143                 if (minimalButton == null) {
144                         minimalButton = new Button(buttonComposite, SWT.RADIO);
145                 }
146
147                 minimalButton.addSelectionListener(new SelectionAdapter() {
148                         @Override
149                         public void widgetSelected(SelectionEvent e) {
150                                 setCheckedByType(MINIMAL_BUTTON_NAME.toUpperCase());
151                                 checkboxTreeViewer.refresh();
152                                 checkboxTreeViewer.getTree().setEnabled(false);
153
154                                 lblRequiredSize.setText("Total space required : "
155                                                 + convertToVolumeSize(getCheckedPackageSize()));
156                                 
157                                 setNextBtnEnabledAboutCheckedPackageCount();
158                         }
159                 });
160
161                 minimalButton.setBounds(0, 20, 80, 15);
162                 minimalButton.setText(MINIMAL_BUTTON_NAME + ":");
163                 minimalButton.setBackground(InstallManagerWindow.getBackgroundColor());
164
165                 minimalLabel = new Label(buttonComposite, SWT.NONE);
166                 minimalLabel.setBounds(81, 20, 410, 15);
167         }
168
169         /**
170          * Set Custom Type radio button.
171          */
172         private void setCustomButton() {
173                 if (customButton == null) {
174                         customButton = new Button(buttonComposite, SWT.RADIO);
175                 }
176
177                 customButton.setBounds(0, 40, 80, 15);
178                 customButton.setText(CUSTOM_BUTTON_NAME);
179                 customButton.setBackground(InstallManagerWindow.getBackgroundColor());
180
181                 customButton.addSelectionListener(new SelectionAdapter() {
182                         @Override
183                         public void widgetSelected(SelectionEvent e) {
184                                 checkboxTreeViewer.getTree().setEnabled(true);
185                         }
186                 });
187         }
188
189         /**
190          * Check tree item by each install type.
191          */
192         private void setCheckedByType(String installType) {
193                 if (installType == null || installType.isEmpty()) {
194                         Log.err("Install type is empty. check install type or package server is old.");
195                 }
196                 Package pkg = InstallManager.getInstance().getPackageManager()
197                                 .getPackageByName(installType);
198
199                 if (pkg == null) {
200                         Log.err(installType + " do not exist in package list.");
201                         return;
202                 }
203
204                 Collection<Item> checkedItems = new ArrayList<Item>();
205
206                 for (String pkgName : pkg.getDependentPackageNames()) {
207                         Item item = findItem(pkgName, items);
208
209                         if (item != null) {
210                                 checkedItems.add(item);
211                         }
212                 }
213
214                 setCheckedItem(checkedItems);
215         }
216
217         private void setCheckedItem(Collection<Item> itemList) {
218                 checkboxTreeViewer.setCheckedElements(itemList.toArray());
219
220                 // refresh tree item. All of items are unchecked.
221                 setAllChecked(false);
222
223                 // check selected item.
224                 for (Item item : itemList) {
225                         setCheckbox(item, true);
226                 }
227         }
228
229         /**
230          * Display installable package list from view controller.
231          */
232         public boolean loadPage(ViewController controller) {
233                 this.viewController = controller;
234                 getItems().clear();
235
236                 // back to next on component selection page case.
237                 // in this case, page status should be typical selection case.
238                 if (customButton.getSelection()) {
239                         customButton.setSelection(false);
240                         typicalButton.setSelection(true);
241                 } else if (minimalButton.getSelection()) {
242                         minimalButton.setSelection(false);
243                         typicalButton.setSelection(true);
244                 }
245
246                 try {
247                         loadPackagesToTreeViewer(viewController.getRootMetaPackageListOfRepository());
248
249                         SDKExtensionInfo sdkExtensionInfo = SDKExtensionInfo.getInstance();
250                         addExtensionPackagesToExtra(sdkExtensionInfo.getExtensionPackages());
251                 } catch (IMFatalException fatalException) {
252                         Log.err(ErrorController.getErrorMessage());
253                         IMError.fatalError(ErrorController.getErrorMessage());
254                 }
255
256                 checkboxTreeViewer.refresh();
257
258                 if (getItems().size() <= 0) {
259                         return false;
260                 }
261
262                 setCheckedByType(TYPICAL_BUTTON_NAME.toUpperCase());
263                 checkboxTreeViewer.getTree().setEnabled(false);
264
265                 checkboxTreeViewer.refresh();
266
267                 setNextBtnEnabledAboutCheckedPackageCount();
268                 lblRequiredSize.setText("Total space required : "
269                                 + convertToVolumeSize(getCheckedPackageSize()));
270                 
271                 initDescriptionToInstallType();
272
273                 return true;
274         }
275         
276         private void initDescriptionToInstallType() {
277                 PackageManager pm = PackageManager.getInstance();
278                 
279                 //set typical description
280                 String typicalExplain = "";
281                 Package typicalPackage = pm.getPackageByName(TYPICAL_BUTTON_NAME.toUpperCase());
282                 if (typicalPackage != null) {
283                         typicalExplain = typicalPackage.getDescription();
284                 }
285                 
286                 typicalLabel.setText(typicalExplain);
287                 typicalLabel.setBackground(InstallManagerWindow.getBackgroundColor());
288                 
289                 //set minimal description               
290                 String minimalExplain = "";
291                 Package minimalPackage = pm.getPackageByName(MINIMAL_BUTTON_NAME.toUpperCase());
292                 if (minimalPackage != null) {
293                         minimalExplain = minimalPackage.getDescription();
294                 }
295                 
296                 minimalLabel.setText(minimalExplain);
297                 minimalLabel.setBackground(InstallManagerWindow.getBackgroundColor());
298         }
299
300         /**
301          * Find item which should be checked in component tree.
302          * 
303          * @param name
304          *            the name of item which should be checked.
305          * @param itemList
306          * @return item which should be checked in component tree.
307          */
308         private Item findItem(String name, Collection<Item> itemList) {
309                 Item retItem = null;
310                 for (Item item : itemList) {
311                         if (name.equalsIgnoreCase(item.getPackageName())) {
312                                 return item;
313                         } else {
314                                 Collection<Item> childItemList = item.getChildren();
315
316                                 if (childItemList.size() > 0) {
317                                         retItem = findItem(name, childItemList);
318
319                                         if (retItem != null) {
320                                                 break;
321                                         }
322                                 }
323                         }
324                 }
325                 return retItem;
326         }
327 }
328
329
330