KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > importexport > breakpoints > WizardExportBreakpointsPage


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.debug.internal.ui.importexport.breakpoints;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import com.ibm.icu.text.MessageFormat;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.model.IBreakpoint;
23 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
24 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
25 import org.eclipse.debug.internal.ui.SWTFactory;
26 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsViewer;
27 import org.eclipse.debug.ui.DebugUITools;
28 import org.eclipse.debug.ui.actions.ExportBreakpointsOperation;
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.dialogs.IDialogSettings;
31 import org.eclipse.jface.dialogs.IMessageProvider;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.CheckStateChangedEvent;
34 import org.eclipse.jface.viewers.ICheckStateListener;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.wizard.WizardPage;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.graphics.Font;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Event;
45 import org.eclipse.swt.widgets.FileDialog;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Listener;
49 import org.eclipse.swt.widgets.Text;
50 import org.eclipse.swt.widgets.Widget;
51 import org.eclipse.ui.PlatformUI;
52
53 /**
54  * <p>
55  * This class provides an internal implementation of a WizardPage, which is used
56  * in the Export Breakpoints wizard.
57  * </p>
58  * <p>
59  * The implementation presents the breakpoints to the user as they are shown in
60  * their current breakpoint view.
61  * </p>
62  * <p>
63  * Possible extensions would include:
64  * <ul>
65  * <li> Able to change the views as in the breakpoints view itself
66  * <li> Able to reorder groups from within the wizard - easier in the viewer itself though
67  * </ul>
68  * </p>
69  * This class is used by <code>WizardExportBreakpoints</code>
70  *
71  * @since 3.2
72  */

73 public class WizardExportBreakpointsPage extends WizardPage implements Listener {
74
75     // widgets
76
private Button fOverwriteExistingFilesCheckbox = null;
77     private Text fDestinationNameField = null;
78     private Button fDestinationBrowseButton = null;
79     private IPath fPath = null;
80     private EmbeddedBreakpointsViewer fTView = null;
81     private IStructuredSelection fSelection = null;
82     private Button fSelectAll = null;
83     private Button fDeselectAll = null;
84
85     //state constants
86
private static final String JavaDoc OVERWRITE_ALL_STATE = "overwrite"; //$NON-NLS-1$
87
private static final String JavaDoc DESTINATION_FILE_NAME = "filename"; //$NON-NLS-1$
88

89     /**
90      * This is the default constructor. It accepts the name for the tab as a
91      * parameter and an existing selection
92      *
93      * @param pageName the name of the page
94      */

95     public WizardExportBreakpointsPage(String JavaDoc pageName, IStructuredSelection selection) {
96         super(pageName, ImportExportMessages.WizardExportBreakpoints_0, null);
97         fSelection = selection;
98     }
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
102      */

103     public void handleEvent(Event event) {
104         Widget source = event.widget;
105         if (source == fDestinationBrowseButton) {
106             handleDestinationBrowseButtonPressed();
107         }
108         else if (source == fDestinationNameField) {
109             handlePathTextModifiedEvent();
110         }
111         else if(source == fSelectAll) {
112             handleSelectAllPressed();
113         }
114         else if(source == fDeselectAll) {
115             handleDeselectAllPressed();
116         }
117     }
118     
119     /**
120      * Handles the select all button pressed
121      *
122      */

123     private void handleSelectAllPressed() {
124         BreakpointsViewer viewer = fTView.getViewer();
125         viewer.getTree().selectAll();
126         viewer.setCheckedElements(((IStructuredSelection)viewer.getSelection()).toArray());
127         viewer.setGrayedElements(new Object JavaDoc[] {});
128         viewer.getTree().deselectAll();
129         setPageComplete(detectPageComplete());
130     }
131     
132     /**
133      * Handles the deselect all button pressed
134      *
135      */

136     private void handleDeselectAllPressed() {
137         BreakpointsViewer viewer = fTView.getViewer();
138         viewer.setCheckedElements(new Object JavaDoc[] {});
139         viewer.setGrayedElements(new Object JavaDoc[] {});
140         setPageComplete(detectPageComplete());
141     }
142     
143     /**
144      * This method handles the modified event fomr the path combobox.
145      */

146     protected void handlePathTextModifiedEvent() {
147         setPageComplete(detectPageComplete());
148     }
149
150     /**
151      * Open the SaveAsDialog so the user can save the listing of selected breakpoints
152      */

153     protected void handleDestinationBrowseButtonPressed() {
154         FileDialog dialog = new FileDialog(getContainer().getShell(), SWT.SAVE);
155         dialog.setFilterExtensions(new String JavaDoc[]{"*."+IImportExportConstants.EXTENSION}); //$NON-NLS-1$
156
dialog.setText(ImportExportMessages.WizardExportBreakpoints_0);
157         String JavaDoc file = dialog.open();
158         if(file != null) {
159             fPath = new Path(file);
160             if (fPath != null) {
161                 setErrorMessage(null);
162                 if(fPath.getFileExtension() == null) {
163                     fPath = fPath.addFileExtension(IImportExportConstants.EXTENSION);
164                 }
165                 else if(!fPath.getFileExtension().equals(IImportExportConstants.EXTENSION)) {
166                     fPath = fPath.addFileExtension(IImportExportConstants.EXTENSION);
167                 }
168                 fDestinationNameField.setText(fPath.toString());
169             }
170         }
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
175      */

176     public void createControl(Composite parent) {
177         initializeDialogUnits(parent);
178         Composite composite = new Composite(parent, SWT.NULL);
179         composite.setLayout(new GridLayout());
180         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
181
182         fTView = new EmbeddedBreakpointsViewer(composite, DebugPlugin.getDefault().getBreakpointManager(), fSelection);
183         fTView.getViewer().addCheckStateListener(new ICheckStateListener() {
184             public void checkStateChanged(CheckStateChangedEvent event) {
185                 setPageComplete(detectPageComplete());
186             }
187         });
188         fTView.getViewer().setSelection(fSelection);
189         //ensure we can see the beginning check-boxes etc. (bug 180971)
190
//this will not work in Windows Vista as there is no way to over-ride the default viewer item showing policy
191
//by setting the horizontal bar selection index. I.e. the following line of code is ignored in Vista
192
fTView.getViewer().getTree().getHorizontalBar().setSelection(0);
193         createButtonsGroup(composite);
194         createDestinationGroup(composite);
195         createOptionsGroup(composite);
196         setControl(composite);
197         setPageComplete(detectPageComplete());
198         restoreWidgetState();
199         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.EXPORT_BREAKPOINTS_WIZARD_PAGE);
200         
201         Dialog.applyDialogFont(parent);
202     }
203     
204     /* (non-Javadoc)
205      * @see org.eclipse.jface.dialogs.IDialogPage#getImage()
206      */

207     public Image getImage() {
208         return DebugUITools.getImage(IInternalDebugUIConstants.IMG_WIZBAN_EXPORT_BREAKPOINTS);
209     }
210
211     /**
212      * Creates the buttons for selecting all or none of the elements.
213      *
214      * @param parent the parent control
215      */

216     private void createButtonsGroup(Composite parent) {
217         Composite composite = new Composite(parent, SWT.NONE);
218         composite.setFont(parent.getFont());
219         GridLayout layout = new GridLayout();
220         layout.numColumns = 3;
221         layout.makeColumnsEqualWidth = true;
222         composite.setLayout(layout);
223         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
224         fSelectAll = SWTFactory.createPushButton(composite, ImportExportMessages.WizardBreakpointsPage_1, null);
225         fSelectAll.addListener(SWT.Selection, this);
226         fDeselectAll = SWTFactory.createPushButton(composite, ImportExportMessages.WizardBreakpointsPage_2, null);
227         fDeselectAll.addListener(SWT.Selection, this);
228     }
229     
230     /**
231      * This method is used to determine if the page can be "finished".
232      *
233      * To be determined "finishable" there must be a save path and there must be
234      * a selection in the tree.
235      *
236      * @return if the prerequesites of the wizard are met to allow the wizard to complete.
237      */

238     private boolean detectPageComplete() {
239         boolean emptyFile = fDestinationNameField.getText().trim().equals(""); //$NON-NLS-1$
240
if (emptyFile) {
241             setMessage(ImportExportMessages.WizardExportBreakpointsPage_0, IMessageProvider.NONE);
242             return false;
243         }
244         int size = fTView.getCheckedElements().size();
245         if (size == 0) {
246             setMessage(ImportExportMessages.WizardExportBreakpointsPage_1, IMessageProvider.ERROR);
247             return false;
248         }
249         setMessage(ImportExportMessages.WizardBreakpointsPage_4);
250         return true;
251     }
252
253     /**
254      * Create the Options specification widgets.
255      *
256      * @param parent the parent to add this
257      */

258     protected void createOptionsGroup(Composite parent) {
259         Font font = parent.getFont();
260         // Options group
261
Group OptionsGroup = new Group(parent, SWT.NONE);
262         GridLayout layout = new GridLayout();
263         OptionsGroup.setLayout(layout);
264         OptionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
265         OptionsGroup.setText(ImportExportMessages.WizardBreakpointsPage_5);
266         OptionsGroup.setFont(parent.getFont());
267         fOverwriteExistingFilesCheckbox = new Button(OptionsGroup, SWT.CHECK | SWT.LEFT);
268         fOverwriteExistingFilesCheckbox.setText(ImportExportMessages.WizardBreakpointsPage_6);
269         fOverwriteExistingFilesCheckbox.setFont(font);
270     }
271
272     /**
273      * Create the export destination specification widgets
274      *
275      * @param parent org.eclipse.swt.widgets.Composite
276      */

277     protected void createDestinationGroup(Composite parent) {
278         Font font = parent.getFont();
279         // destination specification group
280
Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
281         GridLayout layout = new GridLayout();
282         layout.numColumns = 3;
283         destinationSelectionGroup.setLayout(layout);
284         destinationSelectionGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
285         destinationSelectionGroup.setFont(font);
286         Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE);
287         destinationLabel.setText(ImportExportMessages.WizardBreakpointsPage_7);
288         destinationLabel.setFont(font);
289         fDestinationNameField = new Text(destinationSelectionGroup, SWT.BORDER);
290         fDestinationNameField.addListener(SWT.Modify, this);
291         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
292         fDestinationNameField.setLayoutData(data);
293         fDestinationNameField.setFont(font);
294         fDestinationBrowseButton = new Button(destinationSelectionGroup, SWT.PUSH);
295         fDestinationBrowseButton.setText(ImportExportMessages.WizardBreakpointsPage_8);
296         fDestinationBrowseButton.addListener(SWT.Selection, this);
297         fDestinationBrowseButton.setFont(font);
298         setButtonLayoutData(fDestinationBrowseButton);
299     }
300
301     /**
302      * Save the state of the widgets select, for successive invocations of the wizard
303      */

304     private void saveWidgetState() {
305         IDialogSettings settings = getDialogSettings();
306         if(settings != null) {
307             settings.put(OVERWRITE_ALL_STATE, fOverwriteExistingFilesCheckbox.getSelection());
308             settings.put(DESTINATION_FILE_NAME, fDestinationNameField.getText().trim());
309         }
310     }
311     
312     /**
313      * Restores the state of the wizard from previous invocations
314      */

315     private void restoreWidgetState() {
316         IDialogSettings settings = getDialogSettings();
317         if(settings != null) {
318             fOverwriteExistingFilesCheckbox.setSelection(Boolean.valueOf(settings.get(OVERWRITE_ALL_STATE)).booleanValue());
319             String JavaDoc filename = settings.get(DESTINATION_FILE_NAME);
320             if (filename != null) {
321                 fDestinationNameField.setText(filename);
322             }
323         }
324     }
325     
326     /**
327      * The Finish button is clicked on the main wizard
328      * dialog to export the breakpoints, we write them out with all persistnat
329      * information to a simple XML file via the use of XMLMemento.
330      *
331      * @return if the save operation was successful or not
332      */

333     public boolean finish() {
334         try {
335             //name typed in without using selection box
336
if(fPath == null) {
337                 fPath = new Path(fDestinationNameField.getText().trim());
338                 if(fPath.getFileExtension() == null) {
339                     fPath = fPath.addFileExtension(IImportExportConstants.EXTENSION);
340                 }
341                 else if(!fPath.getFileExtension().equals(IImportExportConstants.EXTENSION)) {
342                     fPath = fPath.addFileExtension(IImportExportConstants.EXTENSION);
343                 }
344             }
345             saveWidgetState();
346             if(fPath.toFile().exists() && !fOverwriteExistingFilesCheckbox.getSelection()) {
347                 if (!MessageDialog.openQuestion(null, ImportExportMessages.WizardBreakpointsPage_12, MessageFormat.format(ImportExportMessages.ImportExportOperations_0, new String JavaDoc[] {fPath.toPortableString()}))) {
348                     return false;
349                 }
350             }
351             // collect breakpoints
352
Object JavaDoc[] elements = fTView.getCheckedElements().toArray();
353             List JavaDoc breakpoints = new ArrayList JavaDoc();
354             for (int i = 0; i < elements.length; i++) {
355                 Object JavaDoc object = elements[i];
356                 if (object instanceof IBreakpoint) {
357                     breakpoints.add(object);
358                 }
359             }
360             getContainer().run(true, true, new ExportBreakpointsOperation((IBreakpoint[]) breakpoints.toArray(new IBreakpoint[breakpoints.size()]), fPath.toOSString()));
361         }
362         catch (InterruptedException JavaDoc e) {
363             DebugPlugin.log(e);
364             return false;
365         }
366         catch (InvocationTargetException JavaDoc e) {
367             DebugPlugin.log(e);
368             return false;
369         }
370         return true;
371     }
372 }
373
Popular Tags