KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > AbstractWorkingSetWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.workingsets;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Text;
23
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.wizard.WizardPage;
27
28 import org.eclipse.ui.IWorkingSet;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.dialogs.IWorkingSetPage;
31
32 /**
33  * @since 3.1
34  */

35 public abstract class AbstractWorkingSetWizardPage extends WizardPage implements IWorkingSetPage {
36
37     private boolean fIsFirstValidation;
38     private Text fWorkingSetName;
39     private IWorkingSet fWorkingSet;
40
41     /**
42      * Default constructor.
43      */

44     public AbstractWorkingSetWizardPage(String JavaDoc pageid, String JavaDoc title, ImageDescriptor image) {
45         super(pageid, title, image);
46     }
47
48     /*
49      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
50      */

51     public void createControl(Composite parent) {
52         initializeDialogUnits(parent);
53         
54         Composite composite= new Composite(parent, SWT.NONE);
55         composite.setLayout(new GridLayout());
56         composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
57         setControl(composite);
58
59         Label label= new Label(composite, SWT.WRAP);
60         label.setText(WorkingSetMessages.AbstractWorkingSetPage_workingSet_name);
61         GridData gd= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
62         label.setLayoutData(gd);
63
64         fWorkingSetName= new Text(composite, SWT.SINGLE | SWT.BORDER);
65         fWorkingSetName.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
66         fWorkingSetName.addModifyListener(
67             new ModifyListener() {
68                 public void modifyText(ModifyEvent e) {
69                     validateInput();
70                 }
71             }
72         );
73         fWorkingSetName.setFocus();
74         
75         Dialog.applyDialogFont(composite);
76     }
77
78     /*
79      * Implements method from IWorkingSetPage
80      */

81     public IWorkingSet getSelection() {
82         return fWorkingSet;
83     }
84
85     /*
86      * Implements method from IWorkingSetPage
87      */

88     public void setSelection(IWorkingSet workingSet) {
89         Assert.isNotNull(workingSet, "Working set must not be null"); //$NON-NLS-1$
90
fWorkingSet= workingSet;
91         if (getContainer() != null && getShell() != null && fWorkingSetName != null) {
92             fWorkingSetName.setText(fWorkingSet.getName());
93             validateInput();
94         }
95     }
96
97     /*
98      * Implements method from IWorkingSetPage
99      */

100     public void finish() {
101         String JavaDoc workingSetName= fWorkingSetName.getText();
102         if (fWorkingSet == null) {
103             fWorkingSet= createWorkingSet(workingSetName);
104         }
105         fWorkingSet.setName(workingSetName);
106     }
107     
108     protected abstract IWorkingSet createWorkingSet(String JavaDoc workingSetName);
109
110     private void validateInput() {
111         String JavaDoc errorMessage= null;
112         String JavaDoc newText= fWorkingSetName.getText();
113
114         if (newText.equals(newText.trim()) == false)
115             errorMessage = WorkingSetMessages.AbstractWorkingSetPage_warning_nameWhitespace;
116         if (newText.equals("")) { //$NON-NLS-1$
117
if (fIsFirstValidation) {
118                 setPageComplete(false);
119                 fIsFirstValidation= false;
120                 return;
121             } else {
122                 errorMessage= WorkingSetMessages.AbstractWorkingSetPage_warning_nameMustNotBeEmpty;
123             }
124         }
125
126         fIsFirstValidation= false;
127
128         if (errorMessage == null && (fWorkingSet == null || newText.equals(fWorkingSet.getName()) == false)) {
129             IWorkingSet[] workingSets= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
130             for (int i= 0; i < workingSets.length; i++) {
131                 if (newText.equals(workingSets[i].getName())) {
132                     errorMessage= WorkingSetMessages.AbstractWorkingSetPage_warning_workingSetExists;
133                 }
134             }
135         }
136         setErrorMessage(errorMessage);
137         setPageComplete(errorMessage == null);
138     }
139 }
140
Popular Tags