1 11 package org.eclipse.compare.internal.patch; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.*; 26 import org.eclipse.ui.model.BaseWorkbenchContentProvider; 27 import org.eclipse.ui.model.WorkbenchLabelProvider; 28 import org.eclipse.ui.views.navigator.ResourceComparator; 29 30 class RetargetPatchElementDialog extends Dialog { 31 32 private static class RetargetPatchContentProvider extends BaseWorkbenchContentProvider { 33 private final PatchDiffNode node; 34 public RetargetPatchContentProvider(PatchDiffNode node) { 35 this.node = node; 36 } 37 public Object [] getChildren(Object element) { 38 if (element instanceof IWorkspaceRoot) { 39 IProject[] allProjects= ((IWorkspaceRoot) element).getProjects(); 41 ArrayList accessibleProjects= new ArrayList (); 42 for (int i= 0; i<allProjects.length; i++) { 43 if (allProjects[i].isOpen()) { 44 accessibleProjects.add(allProjects[i]); 45 } 46 } 47 return accessibleProjects.toArray(); 48 } 49 if (element instanceof IProject && node instanceof PatchProjectDiffNode) { 51 return new Object [0]; 52 } 53 return super.getChildren(element); 54 } 55 } 56 57 private final PatchDiffNode fSelectedNode; 58 private final WorkspacePatcher fPatcher; 59 private TreeViewer fViewer; 60 private IResource fSelectedResource; 61 62 public RetargetPatchElementDialog(Shell shell, WorkspacePatcher patcher, PatchDiffNode node) { 63 super(shell); 64 Assert.isNotNull(patcher); 65 Assert.isNotNull(node); 66 setShellStyle(getShellStyle() | SWT.RESIZE); 67 this.fPatcher = patcher; 68 fSelectedNode= node; 69 } 70 71 protected Control createButtonBar(Composite parent) { 72 Control control = super.createButtonBar(parent); 73 Button okButton = this.getButton(IDialogConstants.OK_ID); 74 okButton.setEnabled(false); 75 return control; 76 } 77 78 protected Control createDialogArea(Composite parent) { 79 Composite composite= (Composite) super.createDialogArea(parent); 80 81 initializeDialogUnits(parent); 82 83 getShell().setText(PatchMessages.PreviewPatchPage_RetargetPatch); 84 85 GridLayout layout= new GridLayout(); 86 layout.numColumns= 1; 87 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 88 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 89 composite.setLayout(layout); 90 final GridData data= new GridData(SWT.FILL, SWT.FILL, true, true); 91 composite.setLayoutData(data); 92 93 Label label= new Label(composite, SWT.LEFT|SWT.WRAP); 95 label.setText(getTreeLabel()); 96 final GridData data2= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 97 label.setLayoutData(data2); 98 99 fViewer= new TreeViewer(composite, SWT.BORDER); 100 GridData gd= new GridData(SWT.FILL, SWT.FILL, true, true); 101 gd.widthHint= 0; 102 gd.heightHint= 0; 103 fViewer.getTree().setLayoutData(gd); 104 fViewer.setContentProvider(new RetargetPatchContentProvider(fSelectedNode)); 105 fViewer.setLabelProvider(new WorkbenchLabelProvider()); 106 fViewer.setComparator(new ResourceComparator(ResourceComparator.NAME)); 107 fViewer.setInput(getViewerInput()); 108 IResource resource = getInitialSelection(); 109 if (resource != null) { 110 fViewer.setSelection(new StructuredSelection(resource)); 111 fViewer.expandToLevel(resource, 0); 112 } 113 setupListeners(); 114 115 Dialog.applyDialogFont(composite); 116 return parent; 117 } 118 119 private IResource getViewerInput() { 120 if (fPatcher.isWorkspacePatch()) 121 return ResourcesPlugin.getWorkspace().getRoot(); 122 return fPatcher.getTarget(); 123 } 124 125 private IResource getInitialSelection() { 126 if (fSelectedNode instanceof PatchFileDiffNode) { 127 PatchFileDiffNode node = (PatchFileDiffNode) fSelectedNode; 128 return fPatcher.getTargetFile(node.getDiffResult().getDiff()); 129 } else if (fSelectedNode instanceof HunkDiffNode) { 130 HunkDiffNode node = (HunkDiffNode) fSelectedNode; 131 return fPatcher.getTargetFile(node.getHunkResult().getDiffResult().getDiff()); 132 } else if (fSelectedNode instanceof PatchProjectDiffNode) { 133 PatchProjectDiffNode node = (PatchProjectDiffNode) fSelectedNode; 134 DiffProject diffProject = node.getDiffProject(); 135 return diffProject.getProject(); 136 } 137 return null; 138 } 139 140 private String getTreeLabel() { 141 if (fSelectedNode instanceof PatchProjectDiffNode) { 142 PatchProjectDiffNode node = (PatchProjectDiffNode) fSelectedNode; 143 DiffProject project = node.getDiffProject(); 144 return NLS.bind(PatchMessages.PreviewPatchPage_SelectProject, project.getName()); 145 } else if (fSelectedNode instanceof PatchFileDiffNode) { 146 PatchFileDiffNode node = (PatchFileDiffNode) fSelectedNode; 147 FileDiff diff = node.getDiffResult().getDiff(); 149 return NLS.bind(PatchMessages.RetargetPatchElementDialog_0, fPatcher.getPath(diff)); 150 } else if (fSelectedNode instanceof HunkDiffNode) { 151 HunkDiffNode node = (HunkDiffNode) fSelectedNode; 152 Hunk hunk = node.getHunkResult().getHunk(); 153 return NLS.bind(PatchMessages.RetargetPatchElementDialog_1, fPatcher.getPath(hunk.getParent())); 154 } 155 return ""; } 157 158 protected void okPressed() { 159 if (fSelectedResource != null){ 160 if (fSelectedNode instanceof PatchProjectDiffNode && fSelectedResource instanceof IProject) { 161 PatchProjectDiffNode node = (PatchProjectDiffNode) fSelectedNode; 162 DiffProject project = node.getDiffProject(); 163 fPatcher.retargetProject(project, (IProject)fSelectedResource); 164 } else if (fSelectedNode instanceof PatchFileDiffNode && fSelectedResource instanceof IFile) { 165 PatchFileDiffNode node = (PatchFileDiffNode) fSelectedNode; 166 FileDiff diff = node.getDiffResult().getDiff(); 168 fPatcher.retargetDiff(diff, (IFile)fSelectedResource); 169 } else if (fSelectedNode instanceof HunkDiffNode && fSelectedResource instanceof IFile) { 170 HunkDiffNode node = (HunkDiffNode) fSelectedNode; 171 fPatcher.retargetHunk(node.getHunkResult().getHunk(), (IFile)fSelectedResource); 172 } 173 } 174 super.okPressed(); 175 } 176 177 void setupListeners() { 178 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 179 public void selectionChanged(SelectionChangedEvent event) { 180 IStructuredSelection s= (IStructuredSelection) event.getSelection(); 181 Object obj= s.getFirstElement(); 182 if (obj instanceof IResource){ 183 fSelectedResource = (IResource) obj; 184 if (fSelectedNode instanceof PatchProjectDiffNode) { 185 if (fSelectedResource instanceof IProject){ 186 Button okButton = getButton(IDialogConstants.OK_ID); 187 okButton.setEnabled(true); 188 } 189 } else if (fSelectedNode instanceof PatchFileDiffNode 190 || fSelectedNode instanceof HunkDiffNode) { 191 if (fSelectedResource instanceof IFile){ 192 Button okButton = getButton(IDialogConstants.OK_ID); 193 okButton.setEnabled(true); 194 } 195 } 196 } 197 } 198 }); 199 200 fViewer.addDoubleClickListener(new IDoubleClickListener() { 201 public void doubleClick(DoubleClickEvent event) { 202 ISelection s= event.getSelection(); 203 if (s instanceof IStructuredSelection) { 204 Object item= ((IStructuredSelection) s).getFirstElement(); 205 if (fViewer.getExpandedState(item)) 206 fViewer.collapseToLevel(item, 1); 207 else 208 fViewer.expandToLevel(item, 1); 209 } 210 } 211 }); 212 213 } 214 215 protected Point getInitialSize() { 216 final Point size= super.getInitialSize(); 217 size.x= convertWidthInCharsToPixels(75); 218 size.y+= convertHeightInCharsToPixels(20); 219 return size; 220 } 221 } | Popular Tags |