KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > ReorgUserInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.ui.refactoring.reorg;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.ResourcesPlugin;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.TreeViewer;
30
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
33
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37
38 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestinationValidator;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.ui.JavaElementComparator;
42 import org.eclipse.jdt.ui.JavaElementLabelProvider;
43 import org.eclipse.jdt.ui.JavaElementLabels;
44
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46
47
48 abstract class ReorgUserInputPage extends UserInputWizardPage{
49     private static final long LABEL_FLAGS= JavaElementLabels.ALL_DEFAULT
50             | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.F_PRE_TYPE_SIGNATURE;
51     private TreeViewer fViewer;
52     public ReorgUserInputPage(String JavaDoc pageName) {
53         super(pageName);
54     }
55     /* (non-Javadoc)
56      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
57      */

58     public void createControl(Composite parent) {
59         initializeDialogUnits(parent);
60         Composite result= new Composite(parent, SWT.NONE);
61         setControl(result);
62         result.setLayout(new GridLayout());
63         
64         Object JavaDoc initialSelection= getInitiallySelectedElement();
65         verifyDestination(initialSelection, true);
66
67         addLabel(result);
68         
69         fViewer= createViewer(result);
70         fViewer.setSelection(new StructuredSelection(initialSelection), true);
71         fViewer.addSelectionChangedListener(new ISelectionChangedListener(){
72             public void selectionChanged(SelectionChangedEvent event) {
73                 ReorgUserInputPage.this.viewerSelectionChanged(event);
74             }
75         });
76         Dialog.applyDialogFont(result);
77     }
78     
79     protected Control addLabel(Composite parent) {
80         Label label= new Label(parent, SWT.WRAP);
81         String JavaDoc text;
82         int resources= getResources().length;
83         int javaElements= getJavaElements().length;
84
85         if (resources == 0 && javaElements == 1) {
86             text= Messages.format(
87                     ReorgMessages.ReorgUserInputPage_choose_destination_single,
88                     JavaElementLabels.getElementLabel(getJavaElements()[0], LABEL_FLAGS));
89         } else if (resources == 1 && javaElements == 0) {
90             text= Messages.format(
91                     ReorgMessages.ReorgUserInputPage_choose_destination_single,
92                     getResources()[0].getName());
93         } else {
94             text= Messages.format(
95                     ReorgMessages.ReorgUserInputPage_choose_destination_multi,
96                     String.valueOf(resources + javaElements));
97         }
98
99         label.setText(text);
100         GridData data= new GridData(SWT.FILL, SWT.END, true, false);
101         data.widthHint= convertWidthInCharsToPixels(50);
102         label.setLayoutData(data);
103         return label;
104     }
105     
106     private void viewerSelectionChanged(SelectionChangedEvent event) {
107         ISelection selection= event.getSelection();
108         if (!(selection instanceof IStructuredSelection))
109             return;
110         IStructuredSelection ss= (IStructuredSelection)selection;
111         verifyDestination(ss.getFirstElement(), false);
112     }
113     
114     protected abstract Object JavaDoc getInitiallySelectedElement();
115     
116     /** Set and verify destination */
117     protected abstract RefactoringStatus verifyDestination(Object JavaDoc selected) throws JavaModelException;
118     
119     protected abstract IResource[] getResources();
120     protected abstract IJavaElement[] getJavaElements();
121
122     protected abstract IReorgDestinationValidator getDestinationValidator();
123     
124     private final void verifyDestination(Object JavaDoc selected, boolean initialVerification) {
125         try {
126             RefactoringStatus status= verifyDestination(selected);
127             if (initialVerification)
128                 setPageComplete(status.isOK());
129             else
130                 setPageComplete(status);
131         } catch (JavaModelException e) {
132             JavaPlugin.log(e);
133             setPageComplete(false);
134         }
135     }
136
137     private TreeViewer createViewer(Composite parent) {
138         TreeViewer treeViewer= new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
139         GridData gd= new GridData(GridData.FILL_BOTH);
140         gd.widthHint= convertWidthInCharsToPixels(40);
141         gd.heightHint= convertHeightInCharsToPixels(15);
142         treeViewer.getTree().setLayoutData(gd);
143         treeViewer.setLabelProvider(new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_SMALL_ICONS));
144         treeViewer.setContentProvider(new DestinationContentProvider(getDestinationValidator()));
145         treeViewer.setComparator(new JavaElementComparator());
146         treeViewer.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
147         return treeViewer;
148     }
149     
150     protected TreeViewer getTreeViewer() {
151         return fViewer;
152     }
153 }
154
Popular Tags