a65c331bf3c0f071e71ad7bedcba92364daac3fe
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / popup / ComponentPopupBuilderImpl.java
bloba65c331bf3c0f071e71ad7bedcba92364daac3fe
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.
17 package com.intellij.ui.popup;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.popup.*;
22 import com.intellij.openapi.util.Computable;
23 import com.intellij.openapi.util.Condition;
24 import com.intellij.openapi.util.Disposer;
25 import com.intellij.openapi.util.Pair;
26 import com.intellij.util.ui.EmptyIcon;
27 import com.intellij.ui.InplaceButton;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.event.ActionListener;
34 import java.util.*;
35 import java.util.List;
37 /**
38 * User: anna
39 * Date: 15-Mar-2006
41 public class ComponentPopupBuilderImpl implements ComponentPopupBuilder {
42 private String myTitle = "";
43 private boolean myResizable;
44 private boolean myMovable;
45 private final JComponent myComponent;
46 private final JComponent myPrefferedFocusedComponent;
47 private boolean myRequestFocus;
48 private boolean myForceHeavyweight;
49 private String myDimensionServiceKey = null;
50 private Computable<Boolean> myCallback = null;
51 private Project myProject;
52 private boolean myCancelOnClickOutside = true;
53 private final Set<JBPopupListener> myListeners = new LinkedHashSet<JBPopupListener>();
54 private boolean myUseDimSevriceForXYLocation;
56 private IconButton myCancelButton;
57 private MouseChecker myCancelOnMouseOutCallback;
58 private boolean myCancelOnWindow;
59 private ActiveIcon myTitleIcon = new ActiveIcon(new EmptyIcon(0));
60 private boolean myCancelKeyEnabled = true;
61 private boolean myLocateByContent = false;
62 private boolean myPlacewithinScreen = true;
63 private Dimension myMinSize;
64 private MaskProvider myMaskProvider;
65 private float myAlpha;
66 private ArrayList<Object> myUserData;
68 private boolean myInStack = true;
69 private boolean myModalContext = true;
70 private Component[] myFocusOwners = new Component[0];
72 private String myAd;
73 private boolean myFocusable = true;
74 private boolean myHeaderAlwaysFocusable;
75 private InplaceButton myCommandButton;
76 private List<Pair<ActionListener, KeyStroke>> myKeyboardActions = Collections.emptyList();
77 private Component mySettingsButtons;
79 public ComponentPopupBuilderImpl(final JComponent component,
80 final JComponent prefferedFocusedComponent) {
81 myComponent = component;
82 myPrefferedFocusedComponent = prefferedFocusedComponent;
85 @NotNull
86 public ComponentPopupBuilder setTitle(String title) {
87 myTitle = title;
88 return this;
91 @NotNull
92 public ComponentPopupBuilder setResizable(final boolean resizable) {
93 myResizable = resizable;
94 return this;
97 @NotNull
98 public ComponentPopupBuilder setMovable(final boolean movable) {
99 myMovable = movable;
100 return this;
103 @NotNull
104 public ComponentPopupBuilder setCancelOnClickOutside(final boolean cancel) {
105 myCancelOnClickOutside = cancel;
106 return this;
109 @NotNull
110 public ComponentPopupBuilder setCancelOnMouseOutCallback(final MouseChecker shouldCancel) {
111 myCancelOnMouseOutCallback = shouldCancel;
112 return this;
115 @NotNull
116 public ComponentPopupBuilder addListener(final JBPopupListener listener) {
117 myListeners.add(listener);
118 return this;
121 @NotNull
122 public ComponentPopupBuilder setRequestFocus(final boolean requestFocus) {
123 myRequestFocus = requestFocus;
124 return this;
127 @NotNull
128 public ComponentPopupBuilder setFocusable(final boolean focusable) {
129 myFocusable = focusable;
130 return this;
133 @NotNull
134 public ComponentPopupBuilder setForceHeavyweight(final boolean forceHeavyweight) {
135 myForceHeavyweight = forceHeavyweight;
136 return this;
139 @NotNull
140 public ComponentPopupBuilder setDimensionServiceKey(final Project project, final String dimensionServiceKey, final boolean useForXYLocation) {
141 myDimensionServiceKey = dimensionServiceKey;
142 myUseDimSevriceForXYLocation = useForXYLocation;
143 myProject = project;
144 return this;
147 @NotNull
148 public ComponentPopupBuilder setCancelCallback(final Computable<Boolean> shouldProceed) {
149 myCallback = shouldProceed;
150 return this;
153 @NotNull
154 public ComponentPopupBuilder setCancelButton(@NotNull final IconButton cancelButton) {
155 myCancelButton = cancelButton;
156 return this;
158 @NotNull
159 public ComponentPopupBuilder setCommandButton(@NotNull InplaceButton button) {
160 myCommandButton = button;
161 return this;
164 @NotNull
165 public ComponentPopupBuilder setKeyboardActions(@NotNull List<Pair<ActionListener, KeyStroke>> keyboardActions) {
166 myKeyboardActions = keyboardActions;
167 return this;
170 @NotNull
171 public ComponentPopupBuilder setSettingButtons(@NotNull Component button) {
172 mySettingsButtons = button;
173 return this;
176 @NotNull
177 public ComponentPopupBuilder setCancelOnOtherWindowOpen(final boolean cancelOnWindow) {
178 myCancelOnWindow = cancelOnWindow;
179 return this;
182 @NotNull
183 public ComponentPopupBuilder setProject(Project project) {
184 myProject = project;
185 return this;
188 @NotNull
189 public JBPopup createPopup() {
190 final AbstractPopup popup = new AbstractPopup().init(myProject, myComponent, myPrefferedFocusedComponent, myRequestFocus, myFocusable, myForceHeavyweight,
191 myMovable, myDimensionServiceKey, myResizable, myTitle,
192 myCallback, myCancelOnClickOutside, myListeners, myUseDimSevriceForXYLocation, myCommandButton,
193 myCancelButton,
194 myCancelOnMouseOutCallback, myCancelOnWindow, myTitleIcon, myCancelKeyEnabled, myLocateByContent,
195 myPlacewithinScreen, myMinSize, myAlpha, myMaskProvider, myInStack, myModalContext, myFocusOwners, myAd,
196 myHeaderAlwaysFocusable, myKeyboardActions, mySettingsButtons);
197 if (myUserData != null) {
198 popup.setUserData(myUserData);
200 //default disposable parent
201 Disposer.register(ApplicationManager.getApplication(), popup);
202 return popup;
205 @NotNull
206 public ComponentPopupBuilder setRequestFocusCondition(Project project, Condition<Project> condition) {
207 myRequestFocus = condition.value(project);
208 return this;
211 @NotNull
212 public ComponentPopupBuilder setTitleIcon(@NotNull final ActiveIcon icon) {
213 myTitleIcon = icon;
214 return this;
217 @NotNull
218 public ComponentPopupBuilder setCancelKeyEnabled(final boolean enabled) {
219 myCancelKeyEnabled = enabled;
220 return this;
223 @NotNull
224 public ComponentPopupBuilder setLocateByContent(final boolean byContent) {
225 myLocateByContent = byContent;
226 return this;
229 @NotNull
230 public ComponentPopupBuilder setLocateWithinScreenBounds(final boolean within) {
231 myPlacewithinScreen = within;
232 return this;
235 @NotNull
236 public ComponentPopupBuilder setMinSize(final Dimension minSize) {
237 myMinSize = minSize;
238 return this;
241 @NotNull
242 public ComponentPopupBuilder setMaskProvider(MaskProvider maskProvider) {
243 myMaskProvider = maskProvider;
244 return this;
247 @NotNull
248 public ComponentPopupBuilder setAlpha(final float alpha) {
249 myAlpha = alpha;
250 return this;
253 @NotNull
254 public ComponentPopupBuilder setBelongsToGlobalPopupStack(final boolean isInStack) {
255 myInStack = isInStack;
256 return this;
259 @NotNull
260 public ComponentPopupBuilder addUserData(final Object object) {
261 if (myUserData == null) {
262 myUserData = new ArrayList<Object>();
264 myUserData.add(object);
265 return this;
268 @NotNull
269 public ComponentPopupBuilder setModalContext(final boolean modal) {
270 myModalContext = modal;
271 return this;
274 @NotNull
275 public ComponentPopupBuilder setFocusOwners(@NotNull final Component[] focusOwners) {
276 myFocusOwners = focusOwners;
277 return this;
280 @NotNull
281 public ComponentPopupBuilder setAdText(@Nullable final String text) {
282 myAd = text;
283 return this;