KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > wizards > UpDownList


1 /*
2  * Created on 18-Oct-2004
3  */

4 package org.hibernate.eclipse.console.wizards;
5
6 import java.util.Iterator JavaDoc;
7
8 import org.eclipse.jface.viewers.IStructuredSelection;
9 import org.eclipse.jface.viewers.TableViewer;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Group;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableItem;
24
25 /**
26  * @author max
27  *
28  */

29 public abstract class UpDownList {
30
31     private SelectionListener buttonListener= new SelectionAdapter() {
32         public void widgetSelected(SelectionEvent e) {
33             handleButtonPressed((Button) e.widget);
34         }
35     };
36     
37     private Button[] addButton;
38     private Button removeButton;
39     private Button upButton;
40     private Button downButton;
41     private TableViewer tableView;
42     private Shell shell;
43
44     private final Composite parent;
45     private final String JavaDoc title;
46     
47     public UpDownList(Composite parent, Shell shell2, String JavaDoc title) {
48         this.parent = parent;
49         this.shell = shell2;
50         this.title = title;
51         build();
52     }
53
54     void build() {
55         
56         Font font = parent.getFont();
57         
58         Group topLevel = new Group(parent, SWT.NONE);
59         topLevel.setText(title);
60         GridData gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
61         gd.verticalSpan = 3;
62         gd.horizontalSpan = 3;
63         
64         topLevel.setLayoutData(gd);
65         GridLayout layout = new GridLayout();
66         
67         layout.marginHeight = 0;
68         layout.marginWidth = 0;
69         layout.numColumns = 3;
70         topLevel.setLayout(layout);
71         
72         // table of builders and tools
73
tableView = new TableViewer(topLevel, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
74         //TODO: viewer.setLabelProvider(labelProvider);
75

76         Table builderTable= tableView.getTable();
77         builderTable.setLayoutData(new GridData(GridData.FILL_BOTH));
78         builderTable.setFont(font);
79         builderTable.addSelectionListener(new SelectionAdapter() {
80             public void widgetSelected(SelectionEvent e) {
81                 handleTableSelectionChanged();
82             }
83         });
84         
85         //button area
86
Composite buttonArea = new Composite(topLevel, SWT.NONE);
87         layout = new GridLayout();
88         layout.marginHeight = 0;
89         layout.marginWidth = 0;
90         buttonArea.setLayout(layout);
91         buttonArea.setFont(font);
92         buttonArea.setLayoutData(new GridData(GridData.FILL_VERTICAL));
93         
94         String JavaDoc[] addButtonLabels = getAddButtonLabels();
95         addButton = new Button[addButtonLabels.length];
96         for (int i = 0; i < addButtonLabels.length; i++) {
97             String JavaDoc label = addButtonLabels[i];
98             addButton[i] = createButton(buttonArea, label); //$NON-NLS-1$
99
addButton[i].setEnabled(true);
100         }
101         removeButton = createButton(buttonArea, "Remove"); //$NON-NLS-1$
102
new Label(buttonArea, SWT.LEFT);
103         upButton = createButton(buttonArea, "Up"); //$NON-NLS-1$
104
downButton = createButton(buttonArea, "Down");
105         
106         
107         
108         //populate widget contents
109
//addBuildersToTable();
110

111     }
112
113     protected String JavaDoc[] getAddButtonLabels() {
114         return new String JavaDoc[] { "Add..." };
115     }
116
117     /**
118      * One of the buttons has been pressed, act accordingly.
119      */

120     private void handleButtonPressed(Button button) {
121         if (button == removeButton) {
122             handleRemoveButtonPressed(tableView);
123         } else if (button == upButton) {
124             moveSelectionUp(tableView);
125         } else if (button == downButton) {
126             moveSelectionDown(tableView);
127         } else {
128             for (int i = 0; i < addButton.length; i++) {
129                 Button but = addButton[i];
130                 if(button == but) {
131                     handleAddButtonPressed(i);
132                 }
133             }
134         }
135         handleTableSelectionChanged();
136         tableView.getTable().setFocus();
137         
138     }
139     
140     private void moveSelectionDown(TableViewer viewer) {
141         Table table = viewer.getTable();
142         int indices[]= table.getSelectionIndices();
143         if (indices.length < 1) {
144             return;
145         }
146         int newSelection[]= new int[indices.length];
147         int max= table.getItemCount() - 1;
148         for (int i = indices.length - 1; i >= 0; i--) {
149             int index= indices[i];
150             if (index < max) {
151                 move (viewer, table.getItem(index), index + 1);
152                 newSelection[i]= index + 1;
153             }
154         }
155         table.setSelection(newSelection);
156     }
157
158     private void moveSelectionUp(TableViewer viewer) {
159         Table table = viewer.getTable();
160         int indices[]= table.getSelectionIndices();
161         int newSelection[]= new int[indices.length];
162         for (int i = 0; i < indices.length; i++) {
163             int index= indices[i];
164             if (index > 0) {
165                 move (viewer, table.getItem(index), index - 1);
166                 newSelection[i]= index - 1;
167             }
168         }
169         table.setSelection(newSelection);
170     }
171
172     /**
173      * Moves an entry in the builder table to the given index.
174      */

175     private void move(TableViewer viewer, TableItem item, int index) {
176         Object JavaDoc data = item.getData();
177         item.dispose();
178         viewer.insert(data, index);
179     }
180     
181     private void handleRemoveButtonPressed(TableViewer viewer) {
182         IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
183         if (selection != null) {
184             int numSelected= selection.size();
185             
186             Iterator JavaDoc iterator= selection.iterator();
187             while (iterator.hasNext()) {
188                 Object JavaDoc item= iterator.next();
189                 viewer.remove(item);
190             }
191             listChanged();
192         }
193     }
194     
195     private void handleAddButtonPressed(int i) {
196         Object JavaDoc[] o = handleAdd(i);
197         if(o!=null) {
198             add(o,true);
199         }
200     }
201
202     public void add(Object JavaDoc[] o, boolean notify) {
203         tableView.add(o);
204         if (notify) listChanged();
205     }
206
207     abstract protected void listChanged();
208
209     abstract protected Object JavaDoc[] handleAdd(int i);
210
211     protected Shell getShell() {
212         return shell;
213     }
214
215     /**
216      * The user has selected a different item in table.
217      * Update button enablement.
218      */

219     private void handleTableSelectionChanged() {
220         for (int i = 0; i < addButton.length; i++) {
221             addButton[i].setEnabled(true);
222         }
223         Table builderTable= tableView.getTable();
224         TableItem[] items = builderTable.getSelection();
225         boolean validSelection= items != null && items.length > 0;
226         boolean enableEdit= validSelection;
227         boolean enableRemove= validSelection;
228         boolean enableUp= validSelection;
229         boolean enableDown= validSelection;
230         if (validSelection) {
231             int indices[]= builderTable.getSelectionIndices();
232             int max = builderTable.getItemCount();
233             enableUp= indices[0] != 0;
234             enableDown= indices[indices.length - 1] < max - 1;
235             enableRemove = true;
236         }
237         removeButton.setEnabled(enableRemove);
238         upButton.setEnabled(enableUp);
239         downButton.setEnabled(enableDown);
240     }
241
242     /**
243      * Creates and returns a button with the given label, id, and enablement.
244      */

245     private Button createButton(Composite parent, String JavaDoc label) {
246         Button button = new Button(parent, SWT.PUSH);
247         GridData data = new GridData();
248         //data.grabExcessHorizontalSpace = true;
249
//data.grabExcessVerticalSpace = true;
250
data.horizontalAlignment = GridData.FILL;
251         //data.verticalAlignment = GridData.FILL;
252

253         button.setLayoutData(data);
254         button.setFont(parent.getFont());
255         button.setText(label);
256         button.setEnabled(false);
257         button.addSelectionListener(buttonListener);
258         return button;
259     }
260
261     public Table getTable() {
262         return tableView.getTable();
263     }
264
265
266 }
267
Popular Tags