KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > TargetOrderDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.launchConfigurations;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
19 import org.eclipse.ant.internal.ui.model.AntModelContentProvider;
20 import org.eclipse.ant.internal.ui.model.AntModelLabelProvider;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.TableViewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.ui.PlatformUI;
38
39 /**
40  * Dialog to specify target execution order
41  */

42 public class TargetOrderDialog extends Dialog implements ISelectionChangedListener {
43     
44     private Button fUp;
45     private Button fDown;
46     private TableViewer fViewer;
47     private Object JavaDoc[] fTargets;
48
49     /**
50      * Constructs the dialog.
51      *
52      * @param parentShell
53      */

54     public TargetOrderDialog(Shell parentShell, Object JavaDoc[] targets) {
55         super(parentShell);
56         fTargets = targets;
57     }
58
59     /* (non-Javadoc)
60      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
61      */

62     protected Control createDialogArea(Composite parent) {
63         getShell().setText(AntLaunchConfigurationMessages.TargetOrderDialog_Order_Targets_1);
64         
65         Composite comp = (Composite)super.createDialogArea(parent);
66         ((GridLayout)comp.getLayout()).numColumns= 2;
67         Label label = new Label(comp, SWT.NONE);
68         label.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Specify_target_execution_order__2);
69         label.setFont(comp.getFont());
70         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
71         gd.horizontalSpan = 2;
72         label.setLayoutData(gd);
73         
74         createTargetList(comp);
75         
76         createButtons(comp);
77         
78         updateButtons();
79         
80         return comp;
81     }
82
83     /**
84      * Create button area & buttons
85      *
86      * @param comp
87      */

88     private void createButtons(Composite parent) {
89         Composite comp = new Composite(parent, SWT.NONE);
90         GridLayout layout = new GridLayout();
91         layout.marginHeight= 0;
92         layout.marginWidth= 0;
93         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
94         gd.verticalAlignment = GridData.BEGINNING;
95         comp.setLayout(layout);
96         comp.setLayoutData(gd);
97         
98         fUp = new Button(comp, SWT.PUSH);
99         fUp.setFont(parent.getFont());
100         fUp.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Up_3);
101         setButtonLayoutData(fUp);
102         fUp.addSelectionListener(new SelectionAdapter() {
103             public void widgetSelected(SelectionEvent e) {
104                 handleUpPressed();
105             }
106         });
107         
108         fDown = new Button(comp, SWT.PUSH);
109         fDown.setFont(parent.getFont());
110         fDown.setText(AntLaunchConfigurationMessages.TargetOrderDialog__Down_4);
111         setButtonLayoutData(fDown);
112         fDown.addSelectionListener(new SelectionAdapter() {
113             public void widgetSelected(SelectionEvent e) {
114                 handleDownPressed();
115             }
116         });
117     }
118
119     private void handleDownPressed() {
120         List JavaDoc targets = getOrderedSelection();
121         if (targets.isEmpty()) {
122             return;
123         }
124         List JavaDoc list= new ArrayList JavaDoc(Arrays.asList(fTargets));
125         int bottom = list.size() - 1;
126         int index = 0;
127         for (int i = targets.size() - 1; i >= 0; i--) {
128             Object JavaDoc target = targets.get(i);
129             index = list.indexOf(target);
130             if (index < bottom) {
131                 bottom = index + 1;
132                 Object JavaDoc temp = list.get(bottom);
133                 list.set(bottom, target);
134                 list.set(index, temp);
135             }
136             bottom = index;
137         }
138         setEntries(list);
139     }
140
141     private void handleUpPressed() {
142         List JavaDoc targets = getOrderedSelection();
143         if (targets.isEmpty()) {
144             return;
145         }
146         int top = 0;
147         int index = 0;
148         List JavaDoc list= new ArrayList JavaDoc(Arrays.asList(fTargets));
149         Iterator JavaDoc entries = targets.iterator();
150         while (entries.hasNext()) {
151             Object JavaDoc target = entries.next();
152             index = list.indexOf(target);
153             if (index > top) {
154                 top = index - 1;
155                 Object JavaDoc temp = list.get(top);
156                 list.set(top, target);
157                 list.set(index, temp);
158             }
159             top = index;
160         }
161         setEntries(list);
162     }
163     
164     /**
165      * Updates the entries to the entries in the given list
166      */

167     private void setEntries(List JavaDoc list) {
168         fTargets= list.toArray();
169         fViewer.setInput(fTargets);
170         // update all selection listeners
171
fViewer.setSelection(fViewer.getSelection());
172     }
173     
174     /**
175      * Returns the selected items in the list, in the order they are
176      * displayed (not in the order they were selected).
177      *
178      * @return targets for an action
179      */

180     private List JavaDoc getOrderedSelection() {
181         List JavaDoc targets = new ArrayList JavaDoc();
182         List JavaDoc selection = ((IStructuredSelection)fViewer.getSelection()).toList();
183         Object JavaDoc[] entries = fTargets;
184         for (int i = 0; i < entries.length; i++) {
185             Object JavaDoc target = entries[i];
186             if (selection.contains(target)) {
187                 targets.add(target);
188             }
189         }
190         return targets;
191     }
192
193     /**
194      * Creates a list viewer for the targets
195      *
196      * @param comp
197      */

198     private void createTargetList(Composite comp) {
199         fViewer = new TableViewer(comp, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
200         fViewer.setLabelProvider(new AntModelLabelProvider());
201     
202         fViewer.setContentProvider(new AntModelContentProvider());
203         fViewer.setInput(fTargets);
204         fViewer.addSelectionChangedListener(this);
205         Table table = fViewer.getTable();
206         GridData gd = new GridData(GridData.FILL_BOTH);
207         gd.heightHint = 200;
208         gd.widthHint = 250;
209         table.setLayoutData(gd);
210         table.setFont(comp.getFont());
211     }
212     
213     /**
214      * Returns the ordered targets
215      */

216     public Object JavaDoc[] getTargets() {
217         return fTargets;
218     }
219     
220     /**
221      * Update button enablement
222      *
223      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
224      */

225     public void selectionChanged(SelectionChangedEvent event) {
226         updateButtons();
227     }
228     
229     private void updateButtons() {
230         int[] selections = fViewer.getTable().getSelectionIndices();
231         int last = fTargets.length - 1;
232         boolean up = true && selections.length > 0;
233         boolean down = true && selections.length > 0;
234         for (int i = 0; i < selections.length; i++) {
235             if (selections[i] == 0) {
236                 up = false;
237             }
238             if (selections[i] == last) {
239                 down = false;
240             }
241         }
242         fUp.setEnabled(up);
243         fDown.setEnabled(down);
244     }
245     
246     /* (non-Javadoc)
247      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
248      */

249     protected void configureShell(Shell shell) {
250         super.configureShell(shell);
251         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.TARGET_ORDER_DIALOG);
252     }
253 }
254
Popular Tags