KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > PatchTargetPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.compare.internal.patch;
12
13 import org.eclipse.compare.internal.ICompareContextIds;
14 import org.eclipse.compare.internal.Utilities;
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.jface.wizard.IWizardPage;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.*;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.model.WorkbenchContentProvider;
29 import org.eclipse.ui.model.WorkbenchLabelProvider;
30 import org.eclipse.ui.views.navigator.ResourceComparator;
31
32 import com.ibm.icu.text.MessageFormat;
33
34 /***
35  * This page only shows up if the user is trying to apply
36  * a non-workspace rooted patch.
37  */

38 public class PatchTargetPage extends WizardPage {
39
40     private boolean fShowError = false;
41
42     // SWT widgets
43
private TreeViewer fPatchTargets;
44     private Button useWorkspaceAsTarget;
45     private Button selectTarget;
46
47     protected WorkspacePatcher fPatcher;
48
49     protected final static String JavaDoc PATCHTARGETPAGE_NAME = "PatchTargetPage"; //$NON-NLS-1$
50

51     PatchTargetPage(WorkspacePatcher patcher) {
52         super(PATCHTARGETPAGE_NAME, PatchMessages.PatchTargetPage_title, null);
53         setMessage(PatchMessages.PatchTargetPage_message);
54         fPatcher = patcher;
55     }
56
57     /*
58      * Get a path from the supplied text widget.
59      * @return org.eclipse.core.runtime.IPath
60      */

61     protected IPath getPathFromText(Text textField) {
62         return (new Path(textField.getText())).makeAbsolute();
63     }
64
65     public void createControl(Composite parent) {
66
67         Composite composite = new Composite(parent, SWT.NULL);
68         composite.setLayout(new GridLayout());
69         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
70         setControl(composite);
71         
72         useWorkspaceAsTarget = createRadioButton(composite, PatchMessages.PatchTargetPage_0, 1);
73         selectTarget = createRadioButton(composite, PatchMessages.InputPatchPage_SelectInput, 1);
74         
75         buildInputGroup(composite);
76
77         updateWidgetEnablements();
78
79         Dialog.applyDialogFont(composite);
80         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICompareContextIds.PATCH_INPUT_WIZARD_PAGE);
81         
82         useWorkspaceAsTarget.addListener(SWT.Selection, new Listener() {
83             public void handleEvent(Event event) {
84                 fShowError = true;
85                 if (useWorkspaceAsTarget.getSelection()) {
86                     fPatchTargets.getTree().setEnabled(false);
87                     fPatcher.setTarget(ResourcesPlugin.getWorkspace().getRoot());
88                 } else {
89                     fPatchTargets.getTree().setEnabled(true);
90                     fPatcher.setTarget(Utilities.getFirstResource(fPatchTargets.getSelection()));
91                 }
92                 updateWidgetEnablements();
93             }
94         });
95     }
96
97     private Button createRadioButton(Composite parent, String JavaDoc label, int span) {
98         Button button = new Button(parent, SWT.RADIO);
99         button.setText(label);
100         GridData data = new GridData();
101         data.horizontalSpan = span;
102         button.setLayoutData(data);
103         return button;
104     }
105     
106     /* (non-JavaDoc)
107      * Method declared in IWizardPage.
108      */

109     public IWizardPage getNextPage() {
110
111         // if selected target is file ensure that patch file
112
// contains only a patch for a single file
113
if (fPatcher.getTarget() instanceof IFile && fPatcher.getDiffs().length > 1) {
114             InputPatchPage inputPage = (InputPatchPage) getWizard().getPage(InputPatchPage.INPUTPATCHPAGE_NAME);
115             String JavaDoc source = ""; //$NON-NLS-1$
116
switch (inputPage.getInputMethod()) {
117                 case InputPatchPage.CLIPBOARD :
118                     source = PatchMessages.InputPatchPage_Clipboard_title;
119                     break;
120
121                 case InputPatchPage.FILE :
122                     source = PatchMessages.InputPatchPage_PatchFile_title;
123                     break;
124
125                 case InputPatchPage.WORKSPACE :
126                     source = PatchMessages.InputPatchPage_WorkspacePatch_title;
127                     break;
128             }
129             String JavaDoc format = PatchMessages.InputPatchPage_SingleFileError_format;
130             String JavaDoc message = MessageFormat.format(format, new String JavaDoc[] {source});
131             MessageDialog.openInformation(null, PatchMessages.InputPatchPage_PatchErrorDialog_title, message);
132             return this;
133         }
134
135         return super.getNextPage();
136     }
137
138     /* (non-JavaDoc)
139      * Method declared in IWizardPage.
140      */

141     public boolean canFlipToNextPage() {
142         // we can't call getNextPage to determine if flipping is allowed since computing
143
// the next page is quite expensive. So we say yes if the page is complete.
144
return isPageComplete();
145     }
146
147     private void buildInputGroup(Composite parent) {
148         Tree tree = new Tree(parent, SWT.BORDER);
149         GridData gd = new GridData(GridData.FILL_BOTH);
150         gd.heightHint = 200;
151         tree.setLayoutData(gd);
152
153         fPatchTargets = new TreeViewer(tree);
154         fPatchTargets.setLabelProvider(new WorkbenchLabelProvider());
155         fPatchTargets.setContentProvider(new WorkbenchContentProvider());
156         fPatchTargets.setComparator(new ResourceComparator(ResourceComparator.NAME));
157         fPatchTargets.setInput(ResourcesPlugin.getWorkspace().getRoot());
158         
159         IResource target = fPatcher.getTarget();
160         if (target != null && !(target instanceof IWorkspaceRoot)) {
161             fPatchTargets.expandToLevel(target, 0);
162             fPatchTargets.setSelection(new StructuredSelection(target));
163         }
164
165         // register listeners
166
fPatchTargets.addSelectionChangedListener(new ISelectionChangedListener() {
167             public void selectionChanged(SelectionChangedEvent event) {
168                 fShowError = true;
169                 fPatcher.setTarget(Utilities.getFirstResource(event.getSelection()));
170                 updateWidgetEnablements();
171             }
172         });
173         
174         fPatchTargets.addDoubleClickListener(new IDoubleClickListener() {
175             public void doubleClick(DoubleClickEvent event) {
176                 ((PatchWizard)getWizard()).showPage(getNextPage());
177             }
178         });
179     }
180
181     /**
182      * Updates the enable state of this page's controls.
183      */

184     private void updateWidgetEnablements() {
185         String JavaDoc error = null;
186
187         if (fPatcher.getTarget() == null) {
188             useWorkspaceAsTarget.setSelection(false);
189             selectTarget.setSelection(true);
190             error = PatchMessages.InputPatchPage_NothingSelected_message;
191             setPageComplete(false);
192             if (fShowError)
193                 setErrorMessage(error);
194             return;
195         }
196         setErrorMessage(null);
197         useWorkspaceAsTarget.setSelection(fPatcher.getTarget() instanceof IWorkspaceRoot);
198         selectTarget.setSelection(!useWorkspaceAsTarget.getSelection());
199         setPageComplete(true);
200     }
201
202     /**
203      * The Finish button was pressed. Try to do the required work now and answer
204      * a boolean indicating success. If false is returned then the wizard will
205      * not close.
206      *
207      * @return boolean
208      */

209     public boolean finish() {
210         return true;
211     }
212
213 }
214
Popular Tags