KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > typehierarchy > 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.typehierarchy;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.dialogs.StatusDialog;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.window.Window;
29
30 import org.eclipse.ui.PlatformUI;
31
32 import org.eclipse.jdt.core.IJavaElement;
33
34 import org.eclipse.jdt.ui.JavaElementLabelProvider;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
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.IListAdapter;
41 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
43
44 public class HistoryListAction extends Action {
45     
46     private class HistoryListDialog extends StatusDialog {
47         
48         private ListDialogField fHistoryList;
49         private IStatus fHistoryStatus;
50         private IJavaElement fResult;
51         
52         private HistoryListDialog(Shell shell, IJavaElement[] elements) {
53             super(shell);
54             setTitle(TypeHierarchyMessages.HistoryListDialog_title);
55             
56             String JavaDoc[] buttonLabels= new String JavaDoc[] {
57                 TypeHierarchyMessages.HistoryListDialog_remove_button,
58             };
59                     
60             IListAdapter adapter= new IListAdapter() {
61                 public void customButtonPressed(ListDialogField field, int index) {
62                     doCustomButtonPressed();
63                 }
64                 public void selectionChanged(ListDialogField field) {
65                     doSelectionChanged();
66                 }
67                 
68                 public void doubleClicked(ListDialogField field) {
69                     doDoubleClicked();
70                 }
71             };
72         
73             JavaElementLabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_QUALIFIED | JavaElementLabelProvider.SHOW_ROOT);
74             
75             fHistoryList= new ListDialogField(adapter, buttonLabels, labelProvider);
76             fHistoryList.setLabelText(TypeHierarchyMessages.HistoryListDialog_label);
77             fHistoryList.setElements(Arrays.asList(elements));
78             
79             ISelection sel;
80             if (elements.length > 0) {
81                 sel= new StructuredSelection(elements[0]);
82             } else {
83                 sel= new StructuredSelection();
84             }
85             
86             fHistoryList.selectElements(sel);
87         }
88
89             
90         /*
91          * @see Dialog#createDialogArea(Composite)
92          */

93         protected Control createDialogArea(Composite parent) {
94             initializeDialogUnits(parent);
95             
96             Composite composite= (Composite) super.createDialogArea(parent);
97             
98             Composite inner= new Composite(composite, SWT.NONE);
99             inner.setFont(parent.getFont());
100             
101             inner.setLayoutData(new GridData(GridData.FILL_BOTH));
102
103             LayoutUtil.doDefaultLayout(inner, new DialogField[] { fHistoryList }, true, 0, 0);
104             LayoutUtil.setHeightHint(fHistoryList.getListControl(null), convertHeightInCharsToPixels(12));
105             LayoutUtil.setHorizontalGrabbing(fHistoryList.getListControl(null));
106
107             applyDialogFont(composite);
108             return composite;
109         }
110
111         /**
112          * Method doCustomButtonPressed.
113          */

114         private void doCustomButtonPressed() {
115             fHistoryList.removeElements(fHistoryList.getSelectedElements());
116         }
117         
118         private void doDoubleClicked() {
119             if (fHistoryStatus.isOK()) {
120                 okPressed();
121             }
122         }
123         
124         
125         private void doSelectionChanged() {
126             StatusInfo status= new StatusInfo();
127             List JavaDoc selected= fHistoryList.getSelectedElements();
128             if (selected.size() != 1) {
129                 status.setError(""); //$NON-NLS-1$
130
fResult= null;
131             } else {
132                 fResult= (IJavaElement) selected.get(0);
133             }
134             fHistoryList.enableButton(0, fHistoryList.getSize() > selected.size() && selected.size() != 0);
135             fHistoryStatus= status;
136             updateStatus(status);
137         }
138                 
139         public IJavaElement getResult() {
140             return fResult;
141         }
142         
143         public IJavaElement[] getRemaining() {
144             List JavaDoc elems= fHistoryList.getElements();
145             return (IJavaElement[]) elems.toArray(new IJavaElement[elems.size()]);
146         }
147         
148         /*
149          * @see org.eclipse.jface.window.Window#configureShell(Shell)
150          */

151         protected void configureShell(Shell newShell) {
152             super.configureShell(newShell);
153             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.HISTORY_LIST_DIALOG);
154         }
155
156         /* (non-Javadoc)
157          * @see org.eclipse.jface.window.Window#create()
158          */

159         public void create() {
160             setShellStyle(getShellStyle() | SWT.RESIZE);
161             super.create();
162         }
163
164     }
165     
166     private TypeHierarchyViewPart fView;
167     
168     public HistoryListAction(TypeHierarchyViewPart view) {
169         fView= view;
170         setText(TypeHierarchyMessages.HistoryListAction_label);
171         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.HISTORY_LIST_ACTION);
172     }
173         
174     /*
175      * @see IAction#run()
176      */

177     public void run() {
178         IJavaElement[] historyEntries= fView.getHistoryEntries();
179         HistoryListDialog dialog= new HistoryListDialog(JavaPlugin.getActiveWorkbenchShell(), historyEntries);
180         if (dialog.open() == Window.OK) {
181             fView.setHistoryEntries(dialog.getRemaining());
182             fView.setInputElement(dialog.getResult());
183         }
184     }
185
186 }
187
188
Popular Tags