KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarimport > RefactoringLocationControl


1 /*******************************************************************************
2  * Copyright (c) 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.jarimport;
12
13 import java.util.LinkedList JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.wizard.IWizard;
25
26 /**
27  * Control which maintains a selectable location field with persisted history.
28  *
29  * TODO: replace with type in org.eclipse.ltk.ui.refactoring
30  *
31  * @since 3.2
32  */

33 public class RefactoringLocationControl extends Composite {
34
35     /** The combo */
36     protected final Combo fCombo;
37
38     /** The dialog settings key */
39     protected final String JavaDoc fKey;
40
41     /** The wizard */
42     protected final IWizard fWizard;
43
44     /** The maximum size of the history private static final int */
45     protected final int MAX_HISTORY_SIZE= 5;
46
47     /**
48      * Creates a new refactoring location control.
49      *
50      * @param wizard
51      * the wizard
52      * @param parent
53      * the parent control
54      * @param key
55      * the dialog settings key
56      */

57     public RefactoringLocationControl(final IWizard wizard, final Composite parent, final String JavaDoc key) {
58         super(parent, SWT.NONE);
59         final GridLayout gridLayout= new GridLayout(1, true);
60         gridLayout.horizontalSpacing= 0;
61         gridLayout.marginWidth= 0;
62         setLayout(gridLayout);
63         fCombo= new Combo(this, SWT.SINGLE | SWT.BORDER);
64         fCombo.setLayoutData(createGridData(GridData.FILL_BOTH, 1, 0));
65         Assert.isNotNull(wizard);
66         Assert.isTrue(key != null && !"".equals(key)); //$NON-NLS-1$
67
fWizard= wizard;
68         fKey= key;
69     }
70
71     /**
72      * Creates a new grid data.
73      *
74      * @param flag
75      * the flags to use
76      * @param hspan
77      * the horizontal span
78      * @param indent
79      * the indent
80      * @return the grid data
81      */

82     protected GridData createGridData(final int flag, final int hspan, final int indent) {
83         final GridData data= new GridData(flag);
84         data.horizontalIndent= indent;
85         data.horizontalSpan= hspan;
86         return data;
87     }
88
89     /**
90      * Returns the combo control.
91      *
92      * @return the combo control
93      */

94     public Combo getControl() {
95         return fCombo;
96     }
97
98     /**
99      * Returns the text of this control.
100      *
101      * @return the text
102      */

103     public String JavaDoc getText() {
104         return fCombo.getText();
105     }
106
107     /**
108      * Restores the history of this control.
109      */

110     public void loadHistory() {
111         final IDialogSettings settings= fWizard.getDialogSettings();
112         if (settings != null) {
113             String JavaDoc[] locations= settings.getArray(fKey);
114             if (locations == null || locations.length == 0)
115                 return;
116             for (int index= 0; index < locations.length; index++)
117                 fCombo.add(locations[index]);
118             fCombo.select(0);
119         }
120     }
121
122     /**
123      * Saves the history of this control.
124      */

125     public void saveHistory() {
126         final IDialogSettings settings= fWizard.getDialogSettings();
127         if (settings != null) {
128             final LinkedList JavaDoc locations= new LinkedList JavaDoc();
129             final String JavaDoc[] items= fCombo.getItems();
130             for (int index= 0; index < items.length; index++)
131                 locations.add(items[index]);
132             final String JavaDoc text= fCombo.getText().trim();
133             if (!"".equals(text)) { //$NON-NLS-1$
134
locations.remove(text);
135                 locations.addFirst(text);
136             }
137             final int size= locations.size();
138             for (int index= 0; index < size - MAX_HISTORY_SIZE; index++)
139                 locations.removeLast();
140             settings.put(fKey, (String JavaDoc[]) locations.toArray(new String JavaDoc[locations.size()]));
141         }
142     }
143
144     /**
145      * {@inheritDoc}
146      */

147     public void setEnabled(final boolean enabled) {
148         super.setEnabled(enabled);
149         fCombo.setEnabled(enabled);
150     }
151
152     /**
153      * Sets the text of this control.
154      *
155      * @param text
156      * the text to set
157      */

158     public void setText(final String JavaDoc text) {
159         fCombo.setText(text);
160     }
161 }
Popular Tags