1 11 package org.eclipse.update.internal.ui.views; 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 14 import java.net.URL ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.jface.viewers.*; 19 import org.eclipse.swt.widgets.*; 20 import org.eclipse.ui.*; 21 import org.eclipse.ui.forms.events.*; 22 import org.eclipse.ui.forms.widgets.*; 23 import org.eclipse.update.configuration.ILocalSite; 24 import org.eclipse.update.core.*; 25 import org.eclipse.update.internal.ui.*; 26 import org.eclipse.update.internal.ui.model.*; 27 import org.eclipse.update.operations.*; 28 29 public class ConfigurationPreview implements IUpdateModelChangedListener { 30 private ScrolledForm form; 31 private ConfigurationView view; 32 private FormToolkit toolkit; 33 private FormText desc; 34 private FormText taskList; 35 private IPreviewTask[] tasks; 36 37 public ConfigurationPreview(ConfigurationView view) { 38 this.view = view; 39 UpdateModel model = UpdateUI.getDefault().getUpdateModel(); 40 model.addUpdateModelChangedListener(this); 41 } 42 public void dispose() { 43 UpdateModel model = UpdateUI.getDefault().getUpdateModel(); 44 model.removeUpdateModelChangedListener(this); 45 toolkit.dispose(); 46 } 47 48 public void objectsAdded(Object parent, Object [] children) { 49 } 50 public void objectsRemoved(Object parent, Object [] children) { 51 } 52 public void objectChanged(Object object, String property) { 53 } 54 55 public Control getControl() { 56 return form; 57 } 58 59 public void createControl(Composite parent) { 60 toolkit = new FormToolkit(parent.getDisplay()); 61 form = toolkit.createScrolledForm(parent); 62 TableWrapLayout layout = new TableWrapLayout(); 63 form.getBody().setLayout(layout); 64 layout.leftMargin = 10; 65 layout.rightMargin = 5; 66 layout.topMargin = 10; 67 layout.horizontalSpacing = 0; 68 layout.verticalSpacing = 20; 69 IHyperlinkListener urlAction = new HyperlinkAdapter() { 70 public void linkActivated(HyperlinkEvent e) { 71 String url = (String ) e.getHref(); 72 if (url != null) 73 UpdateUI.showURL(url); 74 } 75 }; 76 IHyperlinkListener taskAction = new HyperlinkAdapter() { 77 public void linkActivated(HyperlinkEvent e) { 78 String indexArg = (String ) e.getHref(); 79 try { 80 int index = Integer.parseInt(indexArg); 81 if (tasks != null) 82 tasks[index].run(); 83 } catch (NumberFormatException ex) { 84 } 85 } 86 }; 87 desc = toolkit.createFormText(form.getBody(), true); 89 desc.setHyperlinkSettings(toolkit.getHyperlinkGroup()); 90 desc.addHyperlinkListener(urlAction); 91 desc.setText("", false, false); TableWrapData td = new TableWrapData(TableWrapData.FILL_GRAB); 93 desc.setLayoutData(td); 94 taskList = toolkit.createFormText(form.getBody(), false); 95 taskList.setHyperlinkSettings(toolkit.getHyperlinkGroup()); 96 taskList.addHyperlinkListener(taskAction); 97 taskList.setText("", false, false); td = new TableWrapData(TableWrapData.FILL_GRAB); 100 taskList.setLayoutData(td); 101 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "org.eclipse.update.ui.SiteForm"); } 103 public void setSelection(IStructuredSelection selection) { 104 Object object = selection.getFirstElement(); 105 tasks = view.getPreviewTasks(object); 106 String title = getObjectLabel(selection.size()>1?selection:object); 107 form.setText(title); 108 String description = getObjectDescription(selection.size()>1?selection:object); 109 boolean tags = description.startsWith("<form>"); desc.setText(description, tags, !tags); 111 String taskText = getTasksText(); 112 taskList.setText(taskText, true, false); 113 form.reflow(true); 114 } 115 private String getObjectLabel(Object object) { 116 if (object == null) 117 return ""; TreeViewer viewer = view.getTreeViewer(); 119 LabelProvider provider = (LabelProvider) viewer.getLabelProvider(); 120 if (object instanceof IStructuredSelection) { 121 StringWriter sw = new StringWriter (); 122 PrintWriter pw = new PrintWriter (sw); 123 int counter=1; 124 for (Iterator iter = ((IStructuredSelection)object).iterator(); 125 iter.hasNext();) { 126 pw.println(counter+". "+provider.getText(iter.next())); counter++; 128 } 129 return sw.toString(); 130 } 131 return provider.getText(object); 132 } 133 private String getObjectDescription(Object object) { 134 if (object instanceof IFeatureAdapter) { 135 return getFeatureDescription((IFeatureAdapter) object); 136 } 137 if (object instanceof IConfiguredSiteAdapter) { 138 return UpdateUIMessages.ConfigurationPreviewForm_install; 139 } 140 if (object instanceof ILocalSite) { 141 return UpdateUIMessages.ConfigurationPreviewForm_configDescription; 142 } 143 return ""; } 145 private String getFeatureDescription(IFeatureAdapter adapter) { 146 try { 147 IFeature feature = adapter.getFeature(null); 148 IURLEntry entry = feature.getDescription(); 149 if (entry != null) { 150 String text = entry.getAnnotation(); 151 if (text != null) { 152 URL url = entry.getURL(); 153 if (url == null) 154 return text; 155 else { 156 String link = " <a HREF=\"" + url + "\">"+UpdateUIMessages.MoreInfoGenerator_moreInfo+"</a>"; String fullText = "<form><p>" + text + link + "</p></form>"; return fullText; 161 } 162 } 163 } 164 } catch (CoreException e) { 165 } 166 return ""; } 168 private String getTasksText() { 169 if (tasks == null || tasks.length == 0) 170 return "<form/>"; boolean hasEnabledTasks = false; 172 for (int i = 0; i < tasks.length; i++) { 173 if (tasks[i].isEnabled()) { 174 hasEnabledTasks = true; 175 break; 176 } 177 } 178 if (!hasEnabledTasks) 179 return "<form/>"; StringBuffer buf = new StringBuffer (); 181 buf.append("<form><p><b>"); buf.append(UpdateUIMessages.ConfigurationPreviewForm_AvailableTasks); 183 buf.append("</b></p>"); for (int i = 0; i < tasks.length; i++) { 185 IPreviewTask task = tasks[i]; 186 if (task.isEnabled() == false) 187 continue; 188 buf.append("<li style=\"text\" indent=\"0\"><a HREF=\"" + i + "\">" + task.getName() + "</a></li>"); buf.append("<li style=\"text\" indent=\"10\" vspace=\"false\">" + task.getDescription() + "</li>"); } 193 buf.append("</form>"); return buf.toString(); 195 } 196 } 197 | Popular Tags |