1 11 12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.DisposeEvent; 23 import org.eclipse.swt.events.DisposeListener; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Shell; 29 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.SelectionChangedEvent; 33 import org.eclipse.jface.viewers.StructuredSelection; 34 35 import org.eclipse.ui.forms.events.HyperlinkAdapter; 36 import org.eclipse.ui.forms.events.HyperlinkEvent; 37 import org.eclipse.ui.forms.widgets.FormText; 38 import org.eclipse.ui.forms.widgets.FormToolkit; 39 import org.eclipse.ui.forms.widgets.TableWrapData; 40 import org.eclipse.ui.forms.widgets.TableWrapLayout; 41 42 import org.eclipse.jdt.core.IPackageFragmentRoot; 43 import org.eclipse.jdt.core.JavaModelException; 44 45 import org.eclipse.jdt.internal.corext.util.Messages; 46 47 import org.eclipse.jdt.internal.ui.JavaPlugin; 48 import org.eclipse.jdt.internal.ui.preferences.ScrolledPageContent; 49 import org.eclipse.jdt.internal.ui.util.PixelConverter; 50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 51 52 61 public final class HintTextGroup implements ISelectionChangedListener { 62 63 private Composite fTopComposite; 64 private DialogPackageExplorerActionGroup fActionGroup; 65 private List fNewFolders; 66 private HashMap fImageMap; 67 68 public HintTextGroup() { 69 fNewFolders= new ArrayList (); 70 fImageMap= new HashMap (); 71 } 72 73 public Composite createControl(Composite parent) { 74 fTopComposite= new Composite(parent, SWT.NONE); 75 fTopComposite.setFont(parent.getFont()); 76 77 GridData gridData= new GridData(GridData.FILL_BOTH); 78 PixelConverter converter= new PixelConverter(parent); 79 gridData.heightHint= converter.convertHeightInCharsToPixels(12); 80 gridData.widthHint= converter.convertWidthInCharsToPixels(25); 81 GridLayout gridLayout= new GridLayout(); 82 gridLayout.marginWidth= 0; gridLayout.marginHeight= 0; fTopComposite.setLayout(gridLayout); 85 fTopComposite.setLayoutData(gridData); 86 fTopComposite.setData(null); 87 fTopComposite.addDisposeListener(new DisposeListener() { 88 public void widgetDisposed(DisposeEvent e) { 89 Collection collection= fImageMap.values(); 90 Iterator iterator= collection.iterator(); 91 while(iterator.hasNext()) { 92 Image image= (Image)iterator.next(); 93 image.dispose(); 94 } 95 } 96 }); 97 return fTopComposite; 98 } 99 100 private Shell getShell() { 101 return JavaPlugin.getActiveWorkbenchShell(); 102 } 103 104 118 public void setActionGroup(DialogPackageExplorerActionGroup actionGroup) { 119 fActionGroup= actionGroup; 120 } 121 122 131 private FormText createFormText(Composite parent, String text) { 132 FormToolkit toolkit= new FormToolkit(getShell().getDisplay()); 133 try { 134 FormText formText= toolkit.createFormText(parent, true); 135 formText.setFont(parent.getFont()); 136 try { 137 formText.setText(text, true, false); 138 } catch (IllegalArgumentException e) { 139 formText.setText(e.getMessage(), false, false); 140 JavaPlugin.log(e); 141 } 142 formText.marginHeight= 2; 143 formText.marginWidth= 0; 144 formText.setBackground(null); 145 formText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 146 return formText; 147 } finally { 148 toolkit.dispose(); 149 } 150 } 151 152 159 private void createLabel(Composite parent, String text, final BuildpathModifierAction action) { 160 FormText formText= createFormText(parent, text); 161 Image image= (Image)fImageMap.get(action.getId()); 162 if (image == null) { 163 image= action.getImageDescriptor().createImage(); 164 fImageMap.put(action.getId(), image); 165 } 166 formText.setImage("defaultImage", image); formText.addHyperlinkListener(new HyperlinkAdapter() { 168 169 public void linkActivated(HyperlinkEvent e) { 170 action.run(); 171 } 172 173 }); 174 } 175 176 193 void handleFolderCreation(List result) { 194 if (result.size() == 1) { 195 try { 196 fNewFolders.add(((IPackageFragmentRoot)result.get(0)).getCorrespondingResource()); 197 } catch (JavaModelException e) { 198 JavaPlugin.log(e); 199 } 200 } 201 } 202 203 public List getCreatedResources() { 204 return fNewFolders; 205 } 206 207 public void resetCreatedResources() { 208 fNewFolders.clear(); 209 } 210 211 214 public void selectionChanged(SelectionChangedEvent event) { 215 if (event.getSelection() instanceof StructuredSelection) { 216 handlePostSelectionChange((StructuredSelection)event.getSelection()); 217 } else { 218 handlePostSelectionChange(StructuredSelection.EMPTY); 219 } 220 } 221 222 private void handlePostSelectionChange(StructuredSelection selection) { 223 224 BuildpathModifierAction[] actions= fActionGroup.getHintTextGroupActions(); 225 String [] descriptions= new String [actions.length]; 226 for (int i= 0; i < actions.length; i++) { 227 descriptions[i]= actions[i].getDetailedDescription(); 228 } 229 230 Composite childComposite= (Composite)fTopComposite.getData(); 232 233 if (childComposite != null && childComposite.getParent() != null) 235 childComposite.getParent().dispose(); 236 237 PixelConverter converter= new PixelConverter(fTopComposite); 238 239 ScrolledPageContent spc= new ScrolledPageContent(fTopComposite, SWT.V_SCROLL); 241 spc.getVerticalBar().setIncrement(5); 242 243 GridData gridData= new GridData(GridData.FILL_BOTH); 244 245 gridData.heightHint= converter.convertHeightInCharsToPixels(12); 246 gridData.widthHint= converter.convertWidthInCharsToPixels(25); 247 spc.setLayoutData(gridData); 248 249 childComposite= spc.getBody(); 250 TableWrapLayout tableWrapLayout= new TableWrapLayout(); 251 tableWrapLayout.leftMargin= 0; 252 tableWrapLayout.rightMargin= 0; 253 childComposite.setLayout(tableWrapLayout); 254 gridData= new GridData(GridData.FILL_BOTH); 255 gridData.heightHint= converter.convertHeightInCharsToPixels(12); 256 gridData.widthHint= converter.convertWidthInCharsToPixels(25); 257 childComposite.setLayoutData(gridData); 258 259 fTopComposite.setData(childComposite); 260 261 if (noContextHelpAvailable(actions)) { 262 String noAction= noAction(selection); 263 createFormText(childComposite, Messages.format(NewWizardMessages.HintTextGroup_NoAction, noAction)); 264 fTopComposite.layout(true); 265 return; 266 } 267 268 for (int i= 0; i < actions.length; i++) { 269 createLabel(childComposite, descriptions[i], actions[i]); 270 } 271 272 fTopComposite.layout(true); 273 } 274 275 private String noAction(ISelection selection) { 276 if (selection instanceof StructuredSelection) { 277 return noAction(((StructuredSelection)selection).toList()); 278 } else { 279 return noAction(Collections.EMPTY_LIST); 280 } 281 } 282 283 private String noAction(List selectedElements) { 284 if (selectedElements.size() == 0) 285 return NewWizardMessages.PackageExplorerActionGroup_NoAction_NullSelection; 286 287 if (selectedElements.size() == 1) 288 return NewWizardMessages.PackageExplorerActionGroup_NoAction_NoReason; 289 290 return NewWizardMessages.PackageExplorerActionGroup_NoAction_MultiSelection; 291 } 292 293 309 private boolean noContextHelpAvailable(BuildpathModifierAction[] actions) { 310 if (actions.length == 0) 311 return true; 312 if (actions.length == 1) { 313 int id= Integer.parseInt(actions[0].getId()); 314 if (id == BuildpathModifierAction.CREATE_LINK) 315 return true; 316 } 317 if (actions.length == 2) { 318 int idLink= Integer.parseInt(actions[0].getId()); 319 int idReset= Integer.parseInt(actions[1].getId()); 320 if (idReset == BuildpathModifierAction.RESET_ALL && 321 idLink == BuildpathModifierAction.CREATE_LINK) 322 return true; 323 } 324 return false; 325 } 326 } 327 | Popular Tags |