1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.cip.runtime.wizards; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | |
9 | import org.eclipse.core.runtime.CoreException; |
10 | import org.eclipse.jface.viewers.ILabelProvider; |
11 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
12 | import org.eclipse.jface.viewers.IStructuredSelection; |
13 | import org.eclipse.jface.viewers.ITreeContentProvider; |
14 | import org.eclipse.jface.viewers.LabelProvider; |
15 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
16 | import org.eclipse.jface.viewers.TreeViewer; |
17 | import org.eclipse.jface.viewers.Viewer; |
18 | import org.eclipse.jface.wizard.IWizardPage; |
19 | import org.eclipse.jface.wizard.WizardPage; |
20 | import org.eclipse.swt.SWT; |
21 | import org.eclipse.swt.graphics.Image; |
22 | import org.eclipse.swt.layout.GridData; |
23 | import org.eclipse.swt.layout.GridLayout; |
24 | import org.eclipse.swt.widgets.Composite; |
25 | import org.eclipse.ui.dialogs.FilteredTree; |
26 | import org.eclipse.ui.dialogs.PatternFilter; |
27 | |
28 | import de.uka.ipd.sdq.cip.completions.RegisteredCompletion; |
29 | import de.uka.ipd.sdq.cip.ConstantsContainer; |
30 | import de.uka.ipd.sdq.cip.runtime.RunConfigImages; |
31 | import de.uka.ipd.sdq.cip.runtime.wizards.CompletionConfigNode.FeatureConfig; |
32 | import de.uka.ipd.sdq.cip.runtime.wizards.CompletionConfigNode.UserDefined; |
33 | import de.uka.ipd.sdq.cip.runtime.wizards.CompletionConfigNode.UserDefinedID; |
34 | |
35 | /** |
36 | * @author thomass |
37 | * |
38 | */ |
39 | public class CompletionSelectionPage extends WizardPage/* implements SelectionListener*/{ |
40 | |
41 | |
42 | private FilteredTree completionTree; |
43 | private TreeViewer completionTreeViewer; |
44 | private List<CompletionConfigNode> completionConfigNodes; |
45 | |
46 | protected CompletionSelectionPage(String pageName) { |
47 | super(pageName); |
48 | completionConfigNodes = new ArrayList<CompletionConfigNode>(); |
49 | try { |
50 | completionConfigNodes.add(CompletionConfigNode.createWorkspaceNode()); |
51 | } catch (CoreException e) { |
52 | e.printStackTrace(); |
53 | } |
54 | completionConfigNodes.add(CompletionConfigNode.createRegisteredNode()); |
55 | completionConfigNodes.add(CompletionConfigNode.createUserDefinedNode()); |
56 | } |
57 | |
58 | /* (non-Javadoc) |
59 | * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) |
60 | */ |
61 | @Override |
62 | public void createControl(Composite parent) { |
63 | Composite mainComponent = new Composite(parent, SWT.NONE); |
64 | |
65 | GridLayout mainlayout = new GridLayout(); |
66 | mainComponent.setLayout(mainlayout); |
67 | |
68 | completionTree = new FilteredTree(mainComponent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, new PatternFilter(), true); |
69 | GridData gd = new GridData(); |
70 | gd.grabExcessHorizontalSpace = true; |
71 | gd.grabExcessVerticalSpace = true; |
72 | gd.verticalAlignment = SWT.FILL; |
73 | gd.horizontalAlignment = SWT.FILL; |
74 | completionTree.setLayoutData(gd); |
75 | completionTreeViewer = completionTree.getViewer(); |
76 | |
77 | completionTreeViewer.setContentProvider(new CompletionTypeTreeContentProvider()); |
78 | completionTreeViewer.setLabelProvider(new CompletionTypeTreeLabelProvider()); |
79 | completionTreeViewer.addSelectionChangedListener(new CompletionTypeSelectionListener()); |
80 | completionTreeViewer.setInput(completionConfigNodes); |
81 | setControl(mainComponent); |
82 | |
83 | } |
84 | |
85 | @Override |
86 | public IWizardPage getNextPage() { |
87 | return super.getNextPage(); |
88 | } |
89 | |
90 | @Override |
91 | public boolean canFlipToNextPage() { |
92 | IStructuredSelection selection = (IStructuredSelection) completionTreeViewer.getSelection(); |
93 | Object element = selection.getFirstElement(); |
94 | |
95 | if(element == null) |
96 | return false; |
97 | |
98 | if(element instanceof FeatureConfig) |
99 | return false; |
100 | |
101 | if(element instanceof RegisteredCompletion) { |
102 | RegisteredCompletion completion = (RegisteredCompletion) element; |
103 | if(!completion.containsCategory(ConstantsContainer.PLAIN_COMPLETION)) { |
104 | return true; |
105 | } |
106 | } |
107 | |
108 | if(element instanceof UserDefined) { |
109 | return true; |
110 | } |
111 | |
112 | return false; |
113 | } |
114 | |
115 | public boolean canFinish() { |
116 | IStructuredSelection selection = (IStructuredSelection) completionTreeViewer.getSelection(); |
117 | Object element = selection.getFirstElement(); |
118 | |
119 | if(element instanceof FeatureConfig) |
120 | return true; |
121 | |
122 | if(element instanceof RegisteredCompletion) { |
123 | RegisteredCompletion completion = (RegisteredCompletion) element; |
124 | if(completion.containsCategory(ConstantsContainer.PLAIN_COMPLETION)) { |
125 | return true; |
126 | } |
127 | } |
128 | |
129 | return false; |
130 | } |
131 | |
132 | Object getSelection() { |
133 | IStructuredSelection selection = (IStructuredSelection) completionTreeViewer.getSelection(); |
134 | Object element = selection.getFirstElement(); |
135 | |
136 | return element; |
137 | } |
138 | |
139 | private class CompletionTypeSelectionListener implements ISelectionChangedListener { |
140 | |
141 | @Override |
142 | public void selectionChanged(SelectionChangedEvent event) { |
143 | getWizard().getContainer().updateButtons(); |
144 | } |
145 | |
146 | } |
147 | |
148 | private class CompletionTypeTreeContentProvider implements ITreeContentProvider{ |
149 | |
150 | @Override |
151 | public Object[] getChildren(Object parentElement) { |
152 | if(parentElement instanceof CompletionConfigNode) |
153 | return ((CompletionConfigNode)parentElement).getChildren(); |
154 | return null; |
155 | } |
156 | |
157 | @Override |
158 | public Object getParent(Object element) { |
159 | return null; |
160 | } |
161 | |
162 | @Override |
163 | public boolean hasChildren(Object element) { |
164 | if(element instanceof CompletionConfigNode) |
165 | return true; |
166 | return false; |
167 | } |
168 | |
169 | @Override |
170 | public Object[] getElements(Object inputElement) { |
171 | if(inputElement instanceof List<?>) |
172 | return ((List<?>) inputElement).toArray(); |
173 | return null; |
174 | } |
175 | |
176 | @Override |
177 | public void dispose() { |
178 | } |
179 | |
180 | @Override |
181 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
182 | } |
183 | } |
184 | |
185 | /** |
186 | * @author thomass |
187 | * |
188 | */ |
189 | public class CompletionTypeTreeLabelProvider extends LabelProvider implements ILabelProvider{ |
190 | |
191 | /* (non-Javadoc) |
192 | * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object) |
193 | */ |
194 | @Override |
195 | public Image getImage(Object element) { |
196 | if(element instanceof CompletionConfigNode) |
197 | return RunConfigImages.getFolderIcon(); |
198 | if(element instanceof RegisteredCompletion) { |
199 | if(((RegisteredCompletion)element).containsCategory(ConstantsContainer.MARK_COMPLETION)) |
200 | return RunConfigImages.getAnnotationIcon(); |
201 | } |
202 | if(element instanceof UserDefined) { |
203 | if(((UserDefined)element).getID() == UserDefinedID.ANNOTATED) |
204 | return RunConfigImages.getAnnotationNewIcon(); |
205 | } |
206 | return null; |
207 | } |
208 | |
209 | /* (non-Javadoc) |
210 | * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object) |
211 | */ |
212 | @Override |
213 | public String getText(Object element) { |
214 | if(element instanceof CompletionConfigNode) |
215 | return ((CompletionConfigNode) element).getCaption(); |
216 | else if(element instanceof FeatureConfig) |
217 | return ((FeatureConfig) element).getName(); |
218 | else if(element instanceof RegisteredCompletion) |
219 | return ((RegisteredCompletion) element).getName(); |
220 | else if(element instanceof UserDefined) |
221 | return ((UserDefined) element).getCaption(); |
222 | return null; |
223 | } |
224 | |
225 | |
226 | } |
227 | |
228 | } |