KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > 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.ltk.internal.ui.refactoring;
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  * @since 3.2
30  */

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

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

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

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

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

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

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

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

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