1 /**
2  * Created on Jul 16, 2009
3  *
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  */
20 
21 package com.aelitis.azureus.ui.swt.devices.add;
22 
23 import java.util.Arrays;
24 import java.util.Comparator;
25 
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.RowLayout;
30 import org.eclipse.swt.widgets.*;
31 
32 import org.gudy.azureus2.core3.util.Debug;
33 import org.gudy.azureus2.ui.swt.Utils;
34 import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
35 
36 import com.aelitis.azureus.core.devices.DeviceTemplate;
37 import com.aelitis.azureus.core.devices.DeviceManager.DeviceManufacturer;
38 import com.aelitis.azureus.ui.swt.UIFunctionsManagerSWT;
39 import com.aelitis.azureus.ui.swt.devices.TranscodeChooser;
40 import com.aelitis.azureus.ui.swt.skin.*;
41 import com.aelitis.azureus.ui.swt.views.skin.SkinnedDialog;
42 import com.aelitis.azureus.ui.swt.views.skin.SkinnedDialog.SkinnedDialogClosedListener;
43 
44 /**
45  * @author TuxPaper
46  * @created Jul 16, 2009
47  *
48  */
49 public class DeviceTemplateChooser
50 {
51 	private DeviceTemplateClosedListener listener;
52 	private SkinnedDialog skinnedDialog;
53 	private DeviceTemplate selectedDeviceTemplate;
54 	private DeviceManufacturer mf;
55 	private SWTSkinObjectContainer soList;
56 
57 	/**
58 	 *
59 	 */
DeviceTemplateChooser(DeviceManufacturer mf)60 	public DeviceTemplateChooser(DeviceManufacturer mf) {
61 		this.mf = mf;
62 	}
63 
open(DeviceTemplateClosedListener l)64 	public void open(DeviceTemplateClosedListener l) {
65 		this.listener = l;
66 		skinnedDialog = new SkinnedDialog("skin3_dlg_deviceadd_mfchooser",
67 				"shell", SWT.TITLE | SWT.BORDER);
68 
69 		skinnedDialog.addCloseListener(new SkinnedDialogClosedListener() {
70 			public void skinDialogClosed(SkinnedDialog dialog) {
71 				if (listener != null) {
72 					listener.deviceTemplateChooserClosed(selectedDeviceTemplate);
73 				}
74 			}
75 		});
76 
77 		SWTSkin skin = skinnedDialog.getSkin();
78 		SWTSkinObject so= skin.getSkinObject("list");
79 		if (so instanceof SWTSkinObjectContainer) {
80 			soList = (SWTSkinObjectContainer) so;
81 
82 			createDeviceTemplateList2(soList);
83 		}
84 
85 		skinnedDialog.open();
86 	}
87 
createDeviceTemplateList2(SWTSkinObjectContainer soList)88 	private void createDeviceTemplateList2(SWTSkinObjectContainer soList) {
89 		DeviceTemplate[] devices = mf.getDeviceTemplates();
90 
91 		if (devices.length == 0) {
92 			noDevices();
93 			return;
94 		}
95 
96 		Arrays.sort(devices, new Comparator<DeviceTemplate>() {
97 			public int compare(DeviceTemplate o1, DeviceTemplate o2) {
98 				return o1.getName().compareToIgnoreCase(o2.getName());
99 			}
100 		});
101 
102 		Composite parent = soList.getComposite();
103 		if (parent.getChildren().length > 0) {
104 			Utils.disposeComposite(parent, false);
105 		}
106 
107 		SWTSkin skin = skinnedDialog.getSkin();
108 
109 		SWTSkinObjectText soInfoTitle = (SWTSkinObjectText) skin.getSkinObject("info-title");
110 		SWTSkinObjectText soInfoText = (SWTSkinObjectText) skin.getSkinObject("info-text");
111 
112 		RowLayout layout = new RowLayout(SWT.HORIZONTAL);
113 		layout.spacing = 0;
114 		layout.marginLeft = layout.marginRight = 0;
115 		layout.wrap = true;
116 		layout.justify = true;
117 		layout.fill = true;
118 		parent.setLayout(layout);
119 
120 
121 		Listener clickListener = new Listener() {
122 			boolean down = false;
123 
124 			public void handleEvent(Event event) {
125 				if (event.type == SWT.MouseDown) {
126 					down = true;
127 				} else if (event.type == SWT.MouseUp && down) {
128 					Widget widget = (event.widget instanceof Label)
129 							? ((Label) event.widget).getParent() : event.widget;
130 					selectedDeviceTemplate = (DeviceTemplate) widget.getData("obj");
131 					if (selectedDeviceTemplate == null) {
132 						Debug.out("selectedDeviceTemplate is null!");
133 					}
134 					skinnedDialog.close();
135 					down = false;
136 				}
137 			}
138 		};
139 
140 		GridData gridData;
141 		for (DeviceTemplate deviceTemplate : devices) {
142 			if (deviceTemplate.isAuto()) {
143 				continue;
144 			}
145 			String iconURL = null; // deviceTemplate.getIconURL();
146 			TranscodeChooser.addImageBox(parent, clickListener, null, deviceTemplate,
147 					iconURL, deviceTemplate.getName());
148 		}
149 
150 		SWTSkinObjectText soTitle = (SWTSkinObjectText) skin.getSkinObject("title");
151 		if (soTitle != null) {
152 			soTitle.setTextID("devices.choose.device.title");
153 		}
154 
155 		SWTSkinObjectText soSubTitle = (SWTSkinObjectText) skin.getSkinObject("subtitle");
156 		if (soSubTitle != null) {
157 			soSubTitle.setTextID("label.clickone");
158 		}
159 
160 		Shell shell = skinnedDialog.getShell();
161 		Point computeSize = shell.computeSize(shell.getSize().x, SWT.DEFAULT, true);
162 		shell.setSize(computeSize);
163 		Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
164 		Utils.centerWindowRelativeTo(shell, mainShell);
165 	}
166 
167 	/**
168 	 *
169 	 *
170 	 * @since 4.1.0.5
171 	 */
noDevices()172 	private void noDevices() {
173 		new MessageBoxShell(
174 				SWT.OK,
175 				"No Devices Found",
176 				"We couldn't find any devices.  Maybe you didn't install the Vuze Transcoder Plugin?").open(null);
177 		skinnedDialog.close();
178 	}
179 
180 	public static interface DeviceTemplateClosedListener {
deviceTemplateChooserClosed(DeviceTemplate deviceTemplate)181 		public void deviceTemplateChooserClosed(DeviceTemplate deviceTemplate);
182 	}
183 }
184