KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > ExportProjectSetLocationPage


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.wizards;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.dialogs.*;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.*;
29 import org.eclipse.team.core.TeamException;
30 import org.eclipse.team.internal.ui.TeamUIMessages;
31 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
32 import org.eclipse.ui.model.WorkbenchLabelProvider;
33
34 public class ExportProjectSetLocationPage extends TeamWizardPage {
35
36     Combo fileCombo;
37     protected IFile workspaceFile;
38     protected String JavaDoc file = ""; //$NON-NLS-1$
39
Button browseButton;
40     
41     private boolean saveToFileSystem;
42     private Button fileRadio;
43     private Button workspaceRadio;
44     
45     protected Text workspaceText;
46     
47     public ExportProjectSetLocationPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor titleImage) {
48         super(pageName, title, titleImage);
49         setDescription(TeamUIMessages.ExportProjectSetMainPage_description);
50     }
51
52     public void createControl(Composite parent) {
53         Composite composite = createComposite(parent, 1);
54         initializeDialogUnits(composite);
55         
56         Group locationGroup = new Group(composite, SWT.None);
57         GridLayout layout = new GridLayout();
58         locationGroup.setLayout(layout);
59         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
60         locationGroup.setLayoutData(data);
61         locationGroup.setText(TeamUIMessages.ExportProjectSetMainPage_Project_Set_File_Name__3);
62
63         createExportToFile(locationGroup);
64
65         createExportToWorkspace(locationGroup);
66         
67         saveToFileSystem = true;
68         
69         setControl(composite);
70         updateEnablement();
71         Dialog.applyDialogFont(parent);
72     }
73     
74     private void createExportToFile(Composite composite) {
75         fileRadio = new Button(composite, SWT.RADIO);
76         fileRadio.setText(TeamUIMessages.ExportProjectSetMainPage_FileButton);
77         fileRadio.addListener(SWT.Selection, new Listener() {
78             public void handleEvent(Event event) {
79                 saveToFileSystem = true;
80                 updateEnablement();
81             }
82         });
83
84         Composite inner = new Composite(composite, SWT.NONE);
85         GridLayout layout = new GridLayout();
86         layout.numColumns = 2;
87         layout.marginWidth = 0;
88         inner.setLayout(layout);
89         GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
90         inner.setLayoutData(data);
91
92         fileCombo = createDropDownCombo(inner);
93         file = PsfFilenameStore.getSuggestedDefault();
94         fileCombo.setItems(PsfFilenameStore.getHistory());
95         fileCombo.setText(file);
96         fileCombo.addListener(SWT.Modify, new Listener() {
97             public void handleEvent(Event event) {
98                 file = fileCombo.getText();
99                 updateEnablement();
100             }
101         });
102
103         browseButton = new Button(inner, SWT.PUSH);
104         browseButton.setText(TeamUIMessages.ExportProjectSetMainPage_Browse_4);
105         data = new GridData();
106         data.horizontalAlignment = GridData.FILL;
107         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
108         data.widthHint = Math.max(widthHint, browseButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
109         browseButton.setLayoutData(data);
110         browseButton.addListener(SWT.Selection, new Listener() {
111             public void handleEvent(Event event) {
112                 if (!isSaveToFileSystem())
113                     saveToFileSystem = true;
114
115                 FileDialog d = new FileDialog(getShell(), SWT.SAVE);
116                 d.setFilterExtensions(new String JavaDoc[] {"*.psf"}); //$NON-NLS-1$
117
d.setFilterNames(new String JavaDoc[] {TeamUIMessages.ExportProjectSetMainPage_Project_Set_Files_3});
118                 d.setFileName(TeamUIMessages.ExportProjectSetMainPage_default);
119                 String JavaDoc fileName = getFileName();
120                 if (fileName != null) {
121                     int separator = fileName.lastIndexOf(System.getProperty("file.separator").charAt(0)); //$NON-NLS-1$
122
if (separator != -1) {
123                         fileName = fileName.substring(0, separator);
124                     }
125                 }
126                 d.setFilterPath(fileName);
127                 String JavaDoc f = d.open();
128                 if (f != null) {
129                     fileCombo.setText(f);
130                     file = f;
131                 }
132             }
133         });
134     }
135
136     private void createExportToWorkspace(Composite composite) {
137         workspaceRadio = new Button(composite, SWT.RADIO);
138         workspaceRadio.setText(TeamUIMessages.ExportProjectSetMainPage_WorkspaceButton);
139         workspaceRadio.addListener(SWT.Selection, new Listener() {
140             public void handleEvent(Event event) {
141                 saveToFileSystem = false;
142                 updateEnablement();
143             }
144         });
145
146         final Composite nameGroup = new Composite(composite, SWT.NONE);
147         GridLayout layout = new GridLayout();
148         layout.numColumns = 2;
149         layout.marginWidth = 0;
150         nameGroup.setLayout(layout);
151         final GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
152         nameGroup.setLayoutData(data);
153
154         workspaceText = createTextField(nameGroup);
155         workspaceText.setEditable(false);
156         workspaceText.addListener(SWT.Modify, new Listener() {
157             public void handleEvent(Event event) {
158                 file = workspaceFile.getLocation().toString();
159                 updateEnablement();
160             }
161         });
162         Button wsBrowseButton = new Button(nameGroup, SWT.PUSH);
163         GridData gd = new GridData();
164         gd.horizontalAlignment = GridData.FILL;
165         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
166         gd.widthHint = Math.max(widthHint, wsBrowseButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
167         wsBrowseButton.setLayoutData(gd);
168         wsBrowseButton.setText(TeamUIMessages.ExportProjectSetMainPage_Browse);
169         wsBrowseButton.addListener(SWT.Selection, new Listener() {
170             public void handleEvent(Event event) {
171                 if (isSaveToFileSystem())
172                     saveToFileSystem = false;
173
174                 WorkspaceDialog d = new WorkspaceDialog(getShell());
175                 d.open();
176             }
177         });
178     }
179     
180     private void updateEnablement() {
181         boolean complete;
182         //update radio buttons
183
fileRadio.setSelection(saveToFileSystem);
184         workspaceRadio.setSelection(!saveToFileSystem);
185
186         if (file.length() == 0) {
187             setMessage(null);
188             complete = false;
189         } else {
190             File JavaDoc f = new File JavaDoc(file);
191             if (f.isDirectory()) {
192                 setMessage(TeamUIMessages.ExportProjectSetMainPage_You_have_specified_a_folder_5, ERROR);
193                 complete = false;
194             } else {
195                 if (!isSaveToFileSystem() && workspaceFile == null)
196                     complete = false;
197                 else
198                     complete = true;
199             }
200         }
201         if (complete) {
202             setMessage(null);
203         }
204         setPageComplete(complete);
205     }
206     
207     public void setVisible(boolean visible) {
208         super.setVisible(visible);
209         if (visible) {
210             fileCombo.setFocus();
211         }
212     }
213
214     public boolean isSaveToFileSystem() {
215         return saveToFileSystem;
216     }
217
218     public void refreshWorkspaceFile(IProgressMonitor monitor) throws CoreException {
219         if (workspaceFile != null)
220             workspaceFile.refreshLocal(IResource.DEPTH_ONE, monitor);
221     }
222     
223     public String JavaDoc getFileName() {
224         return file;
225     }
226
227     public void setFileName(String JavaDoc file) {
228         if (file != null) {
229             this.file = file;
230         }
231     }
232     
233     class WorkspaceDialog extends TitleAreaDialog {
234
235         protected TreeViewer wsTreeViewer;
236         protected Text wsFilenameText;
237         protected IContainer wsContainer;
238         protected Image dlgTitleImage;
239
240         public WorkspaceDialog(Shell shell) {
241             super(shell);
242         }
243
244         protected Control createContents(Composite parent) {
245             Control control = super.createContents(parent);
246             setTitle(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogTitle);
247             setMessage(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogTitleMessage);
248
249             return control;
250         }
251
252         protected Control createDialogArea(Composite parent) {
253             Composite composite = (Composite) super.createDialogArea(parent);
254
255             GridLayout layout = new GridLayout();
256             layout.numColumns = 1;
257             composite.setLayout(layout);
258             final GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
259             composite.setLayoutData(data);
260
261             getShell().setText(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogMessage);
262
263             wsTreeViewer = new TreeViewer(composite, SWT.BORDER);
264             final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
265             gd.widthHint = 550;
266             gd.heightHint = 250;
267             wsTreeViewer.getTree().setLayoutData(gd);
268
269             wsTreeViewer.setContentProvider(new LocationPageContentProvider());
270             wsTreeViewer.setLabelProvider(new WorkbenchLabelProvider());
271             wsTreeViewer.setInput(ResourcesPlugin.getWorkspace());
272
273             final Composite group = new Composite(composite, SWT.NONE);
274             layout = new GridLayout(2, false);
275             layout.marginWidth = 0;
276             group.setLayout(layout);
277             group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
278
279             final Label label = new Label(group, SWT.NONE);
280             label.setLayoutData(new GridData());
281             label.setText(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogFilename);
282
283             wsFilenameText = new Text(group, SWT.BORDER);
284             wsFilenameText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
285             wsFilenameText.setText("projectSet.psf"); //$NON-NLS-1$
286

287             setupListeners();
288
289             return parent;
290         }
291
292         protected void okPressed() {
293             //make sure that a filename has been typed in
294

295             String JavaDoc patchName = wsFilenameText.getText();
296
297             if (patchName.equals("")) { //$NON-NLS-1$
298
setErrorMessage(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogErrorNoFilename);
299                 return;
300             }
301
302             //make sure that the filename does not contain more than one segment
303
if (!(ResourcesPlugin.getWorkspace().validateName(patchName, IResource.FILE)).isOK()) {
304                 wsFilenameText.setText(""); //$NON-NLS-1$
305
setErrorMessage(TeamUIMessages.ExportProjectSetMainPage_WorkspaceDialogErrorFilenameSegments);
306                 return;
307             }
308
309             //Make sure that a container has been selected
310
if (wsContainer == null) {
311                 getSelectedContainer();
312             }
313             //Assert.isNotNull(wsContainer);
314

315             workspaceFile = wsContainer.getFile(new Path(wsFilenameText.getText()));
316             if (workspaceFile != null) {
317                 workspaceText.setText(workspaceFile.getFullPath().toString());
318             }
319             //this.page.validatePage();
320
//workspaceText.setText(wsFilenameText.getText());
321
super.okPressed();
322         }
323
324         private void getSelectedContainer() {
325             Object JavaDoc obj = ((IStructuredSelection) wsTreeViewer.getSelection()).getFirstElement();
326
327             if (obj instanceof IContainer)
328                 wsContainer = (IContainer) obj;
329             else if (obj instanceof IFile) {
330                 wsContainer = ((IFile) obj).getParent();
331             }
332         }
333
334         protected void cancelPressed() {
335             //this.page.validatePage();
336
getSelectedContainer();
337             super.cancelPressed();
338         }
339
340         public boolean close() {
341             /* if (dlgTitleImage != null)
342              dlgTitleImage.dispose();*/

343             return super.close();
344         }
345
346         void setupListeners() {
347             wsTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
348                 public void selectionChanged(SelectionChangedEvent event) {
349                     IStructuredSelection s = (IStructuredSelection) event.getSelection();
350                     Object JavaDoc obj = s.getFirstElement();
351                     if (obj != null) {
352
353                     }
354                     if (obj instanceof IContainer)
355                         wsContainer = (IContainer) obj;
356                     else if (obj instanceof IFile) {
357                         IFile tempFile = (IFile) obj;
358                         wsContainer = tempFile.getParent();
359                         wsFilenameText.setText(tempFile.getName());
360                     }
361                 }
362             });
363
364             wsTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
365                 public void doubleClick(DoubleClickEvent event) {
366                     ISelection s = event.getSelection();
367                     if (s instanceof IStructuredSelection) {
368                         Object JavaDoc item = ((IStructuredSelection) s).getFirstElement();
369                         if (wsTreeViewer.getExpandedState(item))
370                             wsTreeViewer.collapseToLevel(item, 1);
371                         else
372                             wsTreeViewer.expandToLevel(item, 1);
373                     }
374                 }
375             });
376
377             wsFilenameText.addModifyListener(new ModifyListener() {
378                 public void modifyText(ModifyEvent e) {
379                     setErrorMessage(null);
380                 }
381             });
382         }
383     }
384
385     
386     class LocationPageContentProvider extends BaseWorkbenchContentProvider {
387         //Never show closed projects
388
boolean showClosedProjects = false;
389
390         public Object JavaDoc[] getChildren(Object JavaDoc element) {
391             if (element instanceof IWorkspace) {
392                 // check if closed projects should be shown
393
IProject[] allProjects = ((IWorkspace) element).getRoot().getProjects();
394                 if (showClosedProjects)
395                     return allProjects;
396
397                 ArrayList JavaDoc accessibleProjects = new ArrayList JavaDoc();
398                 for (int i = 0; i < allProjects.length; i++) {
399                     if (allProjects[i].isOpen()) {
400                         accessibleProjects.add(allProjects[i]);
401                     }
402                 }
403                 return accessibleProjects.toArray();
404             }
405
406             return super.getChildren(element);
407         }
408     }
409
410
411     public void validateEditWorkspaceFile(Shell shell) throws TeamException {
412         if (workspaceFile == null || ! workspaceFile.exists() || !workspaceFile.isReadOnly())
413             return;
414         IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {workspaceFile}, shell);
415         if (!status.isOK()) {
416             throw new TeamException(status);
417         }
418     }
419 }
420
Popular Tags