KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > views > ConfigurationPreview


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.views;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.StringWriter JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Iterator JavaDoc;
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 JavaDoc parent, Object JavaDoc[] children) {
49     }
50     public void objectsRemoved(Object JavaDoc parent, Object JavaDoc[] children) {
51     }
52     public void objectChanged(Object JavaDoc object, String JavaDoc 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 JavaDoc url = (String JavaDoc) 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 JavaDoc indexArg = (String JavaDoc) e.getHref();
79                 try {
80                     int index = Integer.parseInt(indexArg);
81                     if (tasks != null)
82                         tasks[index].run();
83                 } catch (NumberFormatException JavaDoc ex) {
84                 }
85             }
86         };
87         //taskAction.setStatusLineManager(view.getConfigurationWindow().getStatusLineManager());
88
desc = toolkit.createFormText(form.getBody(), true);
89         desc.setHyperlinkSettings(toolkit.getHyperlinkGroup());
90         desc.addHyperlinkListener(urlAction);
91         desc.setText("", false, false); //$NON-NLS-1$
92
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); //$NON-NLS-1$
98
//factory.setHyperlinkUnderlineMode(HyperlinkSettings.UNDERLINE_ROLLOVER);
99
td = new TableWrapData(TableWrapData.FILL_GRAB);
100         taskList.setLayoutData(td);
101         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "org.eclipse.update.ui.SiteForm"); //$NON-NLS-1$
102
}
103     public void setSelection(IStructuredSelection selection) {
104         Object JavaDoc object = selection.getFirstElement();
105         tasks = view.getPreviewTasks(object);
106         String JavaDoc title = getObjectLabel(selection.size()>1?selection:object);
107         form.setText(title);
108         String JavaDoc description = getObjectDescription(selection.size()>1?selection:object);
109         boolean tags = description.startsWith("<form>"); //$NON-NLS-1$
110
desc.setText(description, tags, !tags);
111         String JavaDoc taskText = getTasksText();
112         taskList.setText(taskText, true, false);
113         form.reflow(true);
114     }
115     private String JavaDoc getObjectLabel(Object JavaDoc object) {
116         if (object == null)
117             return ""; //$NON-NLS-1$
118
TreeViewer viewer = view.getTreeViewer();
119         LabelProvider provider = (LabelProvider) viewer.getLabelProvider();
120         if (object instanceof IStructuredSelection) {
121             StringWriter JavaDoc sw = new StringWriter JavaDoc();
122             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
123             int counter=1;
124             for (Iterator JavaDoc iter = ((IStructuredSelection)object).iterator();
125                 iter.hasNext();) {
126                 pw.println(counter+". "+provider.getText(iter.next())); //$NON-NLS-1$
127
counter++;
128             }
129             return sw.toString();
130         }
131         return provider.getText(object);
132     }
133     private String JavaDoc getObjectDescription(Object JavaDoc 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 ""; //$NON-NLS-1$
144
}
145     private String JavaDoc getFeatureDescription(IFeatureAdapter adapter) {
146         try {
147             IFeature feature = adapter.getFeature(null);
148             IURLEntry entry = feature.getDescription();
149             if (entry != null) {
150                 String JavaDoc text = entry.getAnnotation();
151                 if (text != null) {
152                     URL JavaDoc url = entry.getURL();
153                     if (url == null)
154                         return text;
155                     else {
156                         String JavaDoc link = " <a HREF=\"" + url //$NON-NLS-1$
157
+ "\">"+UpdateUIMessages.MoreInfoGenerator_moreInfo+"</a>"; //$NON-NLS-1$//$NON-NLS-2$
158
String JavaDoc fullText = "<form><p>" + text + link //$NON-NLS-1$
159
+ "</p></form>"; //$NON-NLS-1$
160
return fullText;
161                     }
162                 }
163             }
164         } catch (CoreException e) {
165         }
166         return ""; //$NON-NLS-1$
167
}
168     private String JavaDoc getTasksText() {
169         if (tasks == null || tasks.length == 0)
170             return "<form/>"; //$NON-NLS-1$
171
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/>"; //$NON-NLS-1$
180
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
181         buf.append("<form><p><b>"); //$NON-NLS-1$
182
buf.append(UpdateUIMessages.ConfigurationPreviewForm_AvailableTasks);
183         buf.append("</b></p>"); //$NON-NLS-1$
184
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 + "\">" //$NON-NLS-1$ //$NON-NLS-2$
189
+ task.getName() + "</a></li>"); //$NON-NLS-1$
190
buf.append("<li style=\"text\" indent=\"10\" vspace=\"false\">" //$NON-NLS-1$
191
+ task.getDescription() + "</li>"); //$NON-NLS-1$
192
}
193         buf.append("</form>"); //$NON-NLS-1$
194
return buf.toString();
195     }
196 }
197
Popular Tags