KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > HistoryListAction


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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.viewsupport;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19
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.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.StatusDialog;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.jdt.internal.corext.util.Messages;
36
37 import org.eclipse.jdt.internal.ui.JavaUIMessages;
38 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
40 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
41 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.Separator;
45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
46
47 /*package*/ class HistoryListAction extends Action {
48     
49     private class HistoryListDialog extends StatusDialog {
50         private static final int MAX_MAX_ENTRIES= 100;
51         private ListDialogField fHistoryList;
52         private StringDialogField fMaxEntriesField;
53         private int fMaxEntries;
54         
55         private Object JavaDoc fResult;
56         
57         private HistoryListDialog() {
58             super(fHistory.getShell());
59             setTitle(fHistory.getHistoryListDialogTitle());
60             
61             createHistoryList();
62             createMaxEntriesField();
63             setHelpAvailable(false);
64         }
65         
66         private void createHistoryList() {
67             IListAdapter adapter= new IListAdapter() {
68                 public void customButtonPressed(ListDialogField field, int index) {
69                     doCustomButtonPressed(index);
70                 }
71                 public void selectionChanged(ListDialogField field) {
72                     doSelectionChanged();
73                 }
74                 
75                 public void doubleClicked(ListDialogField field) {
76                     doDoubleClicked();
77                 }
78             };
79             String JavaDoc[] buttonLabels= new String JavaDoc[] { JavaUIMessages.HistoryListAction_remove, JavaUIMessages.HistoryListAction_remove_all };
80             LabelProvider labelProvider= new TestRunLabelProvider();
81             fHistoryList= new ListDialogField(adapter, buttonLabels, labelProvider);
82             fHistoryList.setLabelText(fHistory.getHistoryListDialogMessage());
83             
84             List JavaDoc historyEntries= fHistory.getHistoryEntries();
85             fHistoryList.setElements(historyEntries);
86             
87             Object JavaDoc currentEntry= fHistory.getCurrentEntry();
88             ISelection sel;
89             if (currentEntry != null) {
90                 sel= new StructuredSelection(currentEntry);
91             } else {
92                 sel= new StructuredSelection();
93             }
94             fHistoryList.selectElements(sel);
95         }
96
97         private void createMaxEntriesField() {
98             fMaxEntriesField= new StringDialogField();
99             fMaxEntriesField.setLabelText(fHistory.getMaxEntriesMessage());
100             fMaxEntriesField.setDialogFieldListener(new IDialogFieldListener() {
101                 public void dialogFieldChanged(DialogField field) {
102                     String JavaDoc maxString= fMaxEntriesField.getText();
103                     boolean valid;
104                     try {
105                         fMaxEntries= Integer.parseInt(maxString);
106                         valid= fMaxEntries > 0 && fMaxEntries < MAX_MAX_ENTRIES;
107                     } catch (NumberFormatException JavaDoc e) {
108                         valid= false;
109                     }
110                     if (valid)
111                         updateStatus(StatusInfo.OK_STATUS);
112                     else
113                         updateStatus(new StatusInfo(IStatus.ERROR, Messages.format(JavaUIMessages.HistoryListAction_max_entries_constraint, Integer.toString(MAX_MAX_ENTRIES))));
114                 }
115             });
116             fMaxEntriesField.setText(Integer.toString(fHistory.getMaxEntries()));
117         }
118
119         /*
120          * @see Dialog#createDialogArea(Composite)
121          */

122         protected Control createDialogArea(Composite parent) {
123             initializeDialogUnits(parent);
124             
125             Composite composite= (Composite) super.createDialogArea(parent);
126             
127             Composite inner= new Composite(composite, SWT.NONE);
128             inner.setLayoutData(new GridData(GridData.FILL_BOTH));
129             inner.setFont(composite.getFont());
130
131             LayoutUtil.doDefaultLayout(inner, new DialogField[] { fHistoryList, new Separator() }, true);
132             LayoutUtil.setHeightHint(fHistoryList.getListControl(null), convertHeightInCharsToPixels(12));
133             LayoutUtil.setHorizontalGrabbing(fHistoryList.getListControl(null));
134             
135             Composite additionalControls= new Composite(inner, SWT.NONE);
136             additionalControls.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
137             LayoutUtil.doDefaultLayout(additionalControls, new DialogField[] { fMaxEntriesField }, false);
138             LayoutUtil.setHorizontalGrabbing(fMaxEntriesField.getTextControl(null));
139             
140             applyDialogFont(composite);
141             return composite;
142         }
143
144         private void doCustomButtonPressed(int index) {
145             switch (index) {
146                 case 0: // remove
147
fHistoryList.removeElements(fHistoryList.getSelectedElements());
148                     fHistoryList.selectFirstElement();
149                     break;
150
151                 case 1: // remove all
152
fHistoryList.removeAllElements();
153                     
154                 default:
155                     break;
156             }
157         }
158         
159         private void doDoubleClicked() {
160             okPressed();
161         }
162         
163         private void doSelectionChanged() {
164             List JavaDoc selected= fHistoryList.getSelectedElements();
165             if (selected.size() >= 1) {
166                 fResult= selected.get(0);
167             } else {
168                 fResult= null;
169             }
170             fHistoryList.enableButton(0, selected.size() != 0);
171         }
172                 
173         public Object JavaDoc getResult() {
174             return fResult;
175         }
176         
177         public List JavaDoc getRemaining() {
178             return fHistoryList.getElements();
179         }
180         
181         public int getMaxEntries() {
182             return fMaxEntries;
183         }
184         
185         /*
186          * @see org.eclipse.jface.dialogs.StatusDialog#create()
187          */

188         public void create() {
189             setShellStyle(getShellStyle() | SWT.RESIZE);
190             super.create();
191         }
192
193     }
194     
195     private final class TestRunLabelProvider extends LabelProvider {
196         private final HashMap JavaDoc fImages= new HashMap JavaDoc();
197
198         public String JavaDoc getText(Object JavaDoc element) {
199             return fHistory.getText(element);
200         }
201
202         public Image getImage(Object JavaDoc element) {
203             ImageDescriptor imageDescriptor= fHistory.getImageDescriptor(element);
204             return getCachedImage(imageDescriptor);
205         }
206
207         private Image getCachedImage(ImageDescriptor imageDescriptor) {
208             Object JavaDoc cached= fImages.get(imageDescriptor);
209             if (cached != null)
210                 return (Image) cached;
211             Image image= imageDescriptor.createImage(fHistory.getShell().getDisplay());
212             fImages.put(imageDescriptor, image);
213             return image;
214         }
215
216         public void dispose() {
217             for (Iterator JavaDoc iter= fImages.values().iterator(); iter.hasNext();) {
218                 Image image= (Image) iter.next();
219                 image.dispose();
220             }
221             fImages.clear();
222         }
223     }
224     
225     private ViewHistory fHistory;
226     
227     public HistoryListAction(ViewHistory history) {
228         super(null, IAction.AS_RADIO_BUTTON);
229         fHistory= history;
230         fHistory.configureHistoryListAction(this);
231     }
232         
233     /*
234      * @see IAction#run()
235      */

236     public void run() {
237         HistoryListDialog dialog= new HistoryListDialog();
238         if (dialog.open() == Window.OK) {
239             fHistory.setHistoryEntries(dialog.getRemaining(), dialog.getResult());
240             fHistory.setMaxEntries(dialog.getMaxEntries());
241         }
242     }
243
244 }
245
246
Popular Tags