update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / ui / NameSuggestionsManager.java
blobdd3673b0e1cf1ae4fea3550e95790a5be87ead2a
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.refactoring.ui;
18 import com.intellij.psi.PsiType;
19 import com.intellij.psi.codeStyle.SuggestedNameInfo;
20 import com.intellij.util.containers.HashMap;
22 import javax.swing.*;
23 import java.awt.event.ItemEvent;
24 import java.awt.event.ItemListener;
26 /**
27 * @author dsl
29 public class NameSuggestionsManager {
30 private final TypeSelector myTypeSelector;
31 private final NameSuggestionsField myNameField;
32 private final NameSuggestionsGenerator myGenerator;
34 private final HashMap<PsiType, SuggestedNameInfo> myTypesToSuggestions = new HashMap<PsiType, SuggestedNameInfo>();
36 public NameSuggestionsManager(TypeSelector typeSelector, NameSuggestionsField nameField, NameSuggestionsGenerator generator) {
37 myTypeSelector = typeSelector;
38 myNameField = nameField;
39 myGenerator = generator;
41 myTypeSelector.addItemListener(new ItemListener() {
42 public void itemStateChanged(ItemEvent e) {
43 if (e.getStateChange() == ItemEvent.SELECTED) {
44 updateSuggestions(myTypeSelector.getSelectedType());
47 });
48 updateSuggestions(myTypeSelector.getSelectedType());
52 public void nameSelected() {
53 SuggestedNameInfo nameInfo = myTypesToSuggestions.get(myTypeSelector.getSelectedType());
55 if (nameInfo != null) {
56 nameInfo.nameChoosen(myNameField.getEnteredName());
60 private void updateSuggestions(PsiType selectedType) {
61 SuggestedNameInfo nameInfo = myTypesToSuggestions.get(selectedType);
62 if (nameInfo == null) {
63 nameInfo = myGenerator.getSuggestedNameInfo(selectedType);
64 myTypesToSuggestions.put(selectedType, nameInfo);
66 myNameField.setSuggestions(nameInfo.names);
69 public void setLabelsFor(JLabel typeSelectorLabel, JLabel nameLabel) {
70 if(myTypeSelector.getFocusableComponent() != null) {
71 typeSelectorLabel.setLabelFor(myTypeSelector.getFocusableComponent());
74 if(myNameField.getFocusableComponent() != null) {
75 nameLabel.setLabelFor(myNameField.getFocusableComponent());