]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/viewsupport/HistoryListAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / viewsupport / HistoryListAction.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
12package org.eclipse.jdt.internal.ui.viewsupport;
13
14import java.util.HashMap;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.swt.SWT;
19import org.eclipse.swt.graphics.Image;
20import org.eclipse.swt.layout.GridData;
21import org.eclipse.swt.widgets.Composite;
22import org.eclipse.swt.widgets.Control;
23
24import org.eclipse.core.runtime.IStatus;
25
26import org.eclipse.jface.action.Action;
27import org.eclipse.jface.action.IAction;
28import org.eclipse.jface.dialogs.StatusDialog;
29import org.eclipse.jface.resource.ImageDescriptor;
30import org.eclipse.jface.viewers.ISelection;
31import org.eclipse.jface.viewers.LabelProvider;
32import org.eclipse.jface.viewers.StructuredSelection;
33import org.eclipse.jface.window.Window;
34
35import org.eclipse.jdt.internal.corext.util.Messages;
36
37import org.eclipse.jdt.internal.ui.JavaUIMessages;
38import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
39import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
40import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
41import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
42import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
43import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
44import org.eclipse.jdt.internal.ui.wizards.dialogfields.Separator;
45import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
46
47/*package*/ class HistoryListAction<E> extends Action {
48
49 private class HistoryListDialog extends StatusDialog {
50 private static final int MAX_MAX_ENTRIES= 100;
51 private ListDialogField<E> fHistoryList;
52 private StringDialogField fMaxEntriesField;
53 private int fMaxEntries;
54
55 private E fResult;
56
57 private HistoryListDialog() {
58 super(fHistory.getShell());
59 setTitle(fHistory.getHistoryListDialogTitle());
60
61 createHistoryList();
62 createMaxEntriesField();
63 setHelpAvailable(false);
64 }
65
66 /*
67 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
68 * @since 3.4
69 */
70 @Override
71 protected boolean isResizable() {
72 return true;
73 }
74
75 private void createHistoryList() {
76 IListAdapter<E> adapter= new IListAdapter<E>() {
77 public void customButtonPressed(ListDialogField<E> field, int index) {
78 doCustomButtonPressed(index);
79 }
80 public void selectionChanged(ListDialogField<E> field) {
81 doSelectionChanged();
82 }
83
84 public void doubleClicked(ListDialogField<E> field) {
85 doDoubleClicked();
86 }
87 };
88 String[] buttonLabels= new String[] { JavaUIMessages.HistoryListAction_remove, JavaUIMessages.HistoryListAction_remove_all };
89 LabelProvider labelProvider= new TestRunLabelProvider();
90 fHistoryList= new ListDialogField<E>(adapter, buttonLabels, labelProvider);
91 fHistoryList.setLabelText(fHistory.getHistoryListDialogMessage());
92
93 List<E> historyEntries= fHistory.getHistoryEntries();
94 fHistoryList.setElements(historyEntries);
95
96 Object currentEntry= fHistory.getCurrentEntry();
97 ISelection sel;
98 if (currentEntry != null) {
99 sel= new StructuredSelection(currentEntry);
100 } else {
101 sel= new StructuredSelection();
102 }
103 fHistoryList.selectElements(sel);
104 }
105
106 private void createMaxEntriesField() {
107 fMaxEntriesField= new StringDialogField();
108 fMaxEntriesField.setLabelText(fHistory.getMaxEntriesMessage());
109 fMaxEntriesField.setDialogFieldListener(new IDialogFieldListener() {
110 public void dialogFieldChanged(DialogField field) {
111 String maxString= fMaxEntriesField.getText();
112 boolean valid;
113 try {
114 fMaxEntries= Integer.parseInt(maxString);
115 valid= fMaxEntries > 0 && fMaxEntries < MAX_MAX_ENTRIES;
116 } catch (NumberFormatException e) {
117 valid= false;
118 }
119 if (valid)
120 updateStatus(StatusInfo.OK_STATUS);
121 else
122 updateStatus(new StatusInfo(IStatus.ERROR, Messages.format(JavaUIMessages.HistoryListAction_max_entries_constraint, Integer.toString(MAX_MAX_ENTRIES))));
123 }
124 });
125 fMaxEntriesField.setText(Integer.toString(fHistory.getMaxEntries()));
126 }
127
128 /*
129 * @see Dialog#createDialogArea(Composite)
130 */
131 @Override
132 protected Control createDialogArea(Composite parent) {
133 initializeDialogUnits(parent);
134
135 Composite composite= (Composite) super.createDialogArea(parent);
136
137 Composite inner= new Composite(composite, SWT.NONE);
138 inner.setLayoutData(new GridData(GridData.FILL_BOTH));
139 inner.setFont(composite.getFont());
140
141 LayoutUtil.doDefaultLayout(inner, new DialogField[] { fHistoryList, new Separator() }, true);
142 LayoutUtil.setHeightHint(fHistoryList.getListControl(null), convertHeightInCharsToPixels(12));
143 LayoutUtil.setHorizontalGrabbing(fHistoryList.getListControl(null));
144
145 Composite additionalControls= new Composite(inner, SWT.NONE);
146 additionalControls.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
147 LayoutUtil.doDefaultLayout(additionalControls, new DialogField[] { fMaxEntriesField }, false);
148 LayoutUtil.setHorizontalGrabbing(fMaxEntriesField.getTextControl(null));
149
150 applyDialogFont(composite);
151 return composite;
152 }
153
154 private void doCustomButtonPressed(int index) {
155 switch (index) {
156 case 0: // remove
157 fHistoryList.removeElements(fHistoryList.getSelectedElements());
158 fHistoryList.selectFirstElement();
159 break;
160
161 case 1: // remove all
162 fHistoryList.removeAllElements();
163 break;
164 default:
165 break;
166 }
167 }
168
169 private void doDoubleClicked() {
170 okPressed();
171 }
172
173 private void doSelectionChanged() {
174 List<E> selected= fHistoryList.getSelectedElements();
175 if (selected.size() >= 1) {
176 fResult= selected.get(0);
177 } else {
178 fResult= null;
179 }
180 fHistoryList.enableButton(0, selected.size() != 0);
181 }
182
183 public E getResult() {
184 return fResult;
185 }
186
187 public List<E> getRemaining() {
188 return fHistoryList.getElements();
189 }
190
191 public int getMaxEntries() {
192 return fMaxEntries;
193 }
194
195 }
196
197 private final class TestRunLabelProvider extends LabelProvider {
198 private final HashMap<ImageDescriptor, Image> fImages= new HashMap<ImageDescriptor, Image>();
199
200 @SuppressWarnings("unchecked")
201 @Override
202 public String getText(Object element) {
203 return fHistory.getText((E) element);
204 }
205
206 @Override
207 public Image getImage(Object element) {
208 ImageDescriptor imageDescriptor= fHistory.getImageDescriptor(element);
209 return getCachedImage(imageDescriptor);
210 }
211
212 private Image getCachedImage(ImageDescriptor imageDescriptor) {
213 Object cached= fImages.get(imageDescriptor);
214 if (cached != null)
215 return (Image) cached;
216 Image image= imageDescriptor.createImage(fHistory.getShell().getDisplay());
217 fImages.put(imageDescriptor, image);
218 return image;
219 }
220
221 @Override
222 public void dispose() {
223 for (Iterator<Image> iter= fImages.values().iterator(); iter.hasNext();) {
224 Image image= iter.next();
225 image.dispose();
226 }
227 fImages.clear();
228 }
229 }
230
231 private ViewHistory<E> fHistory;
232
233 public HistoryListAction(ViewHistory<E> history) {
234 super(null, IAction.AS_RADIO_BUTTON);
235 fHistory= history;
236 fHistory.configureHistoryListAction(this);
237 }
238
239 /*
240 * @see IAction#run()
241 */
242 @Override
243 public void run() {
244 HistoryListDialog dialog= new HistoryListDialog();
245 if (dialog.open() == Window.OK) {
246 fHistory.setHistoryEntries(dialog.getRemaining(), dialog.getResult());
247 fHistory.setMaxEntries(dialog.getMaxEntries());
248 }
249 }
250
251}
252