sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / popup / ComponentPopupBuilderImpl.java
bloba7cac6f3b1e9e73052d2bfb859e4314e04404769
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.Processor;
27 import com.intellij.util.ui.EmptyIcon;
28 import com.intellij.ui.InplaceButton;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionListener;
35 import java.util.*;
36 import java.util.List;
38 /**
39 * User: anna
40 * Date: 15-Mar-2006
42 public class ComponentPopupBuilderImpl implements ComponentPopupBuilder {
43 private String myTitle = "";
44 private boolean myResizable;
45 private boolean myMovable;
46 private final JComponent myComponent;
47 private final JComponent myPrefferedFocusedComponent;
48 private boolean myRequestFocus;
49 private boolean myForceHeavyweight;
50 private String myDimensionServiceKey = null;
51 private Computable<Boolean> myCallback = null;
52 private Project myProject;
53 private boolean myCancelOnClickOutside = true;
54 private final Set<JBPopupListener> myListeners = new LinkedHashSet<JBPopupListener>();
55 private boolean myUseDimSevriceForXYLocation;
57 private IconButton myCancelButton;
58 private MouseChecker myCancelOnMouseOutCallback;
59 private boolean myCancelOnWindow;
60 private ActiveIcon myTitleIcon = new ActiveIcon(new EmptyIcon(0));
61 private boolean myCancelKeyEnabled = true;
62 private boolean myLocateByContent = false;
63 private boolean myPlacewithinScreen = true;
64 private Processor<JBPopup> myPinCallback = null;
65 private Dimension myMinSize;
66 private MaskProvider myMaskProvider;
67 private float myAlpha;
68 private ArrayList<Object> myUserData;
70 private boolean myInStack = true;
71 private boolean myModalContext = true;
72 private Component[] myFocusOwners = new Component[0];
74 private String myAd;
75 private boolean myFocusable = true;
76 private boolean myHeaderAlwaysFocusable;
77 private InplaceButton myCommandButton;
78 private List<Pair<ActionListener, KeyStroke>> myKeyboardActions = Collections.emptyList();
79 private Component mySettingsButtons;
81 public ComponentPopupBuilderImpl(final JComponent component,
82 final JComponent prefferedFocusedComponent) {
83 myComponent = component;
84 myPrefferedFocusedComponent = prefferedFocusedComponent;
87 @NotNull
88 public ComponentPopupBuilder setTitle(String title) {
89 myTitle = title;
90 return this;
93 @NotNull
94 public ComponentPopupBuilder setResizable(final boolean resizable) {
95 myResizable = resizable;
96 return this;
99 @NotNull
100 public ComponentPopupBuilder setMovable(final boolean movable) {
101 myMovable = movable;
102 return this;
105 @NotNull
106 public ComponentPopupBuilder setCancelOnClickOutside(final boolean cancel) {
107 myCancelOnClickOutside = cancel;
108 return this;
111 @NotNull
112 public ComponentPopupBuilder setCancelOnMouseOutCallback(final MouseChecker shouldCancel) {
113 myCancelOnMouseOutCallback = shouldCancel;
114 return this;
117 @NotNull
118 public ComponentPopupBuilder addListener(final JBPopupListener listener) {
119 myListeners.add(listener);
120 return this;
123 @NotNull
124 public ComponentPopupBuilder setRequestFocus(final boolean requestFocus) {
125 myRequestFocus = requestFocus;
126 return this;
129 @NotNull
130 public ComponentPopupBuilder setFocusable(final boolean focusable) {
131 myFocusable = focusable;
132 return this;
135 @NotNull
136 public ComponentPopupBuilder setForceHeavyweight(final boolean forceHeavyweight) {
137 myForceHeavyweight = forceHeavyweight;
138 return this;
141 @NotNull
142 public ComponentPopupBuilder setDimensionServiceKey(final Project project, final String dimensionServiceKey, final boolean useForXYLocation) {
143 myDimensionServiceKey = dimensionServiceKey;
144 myUseDimSevriceForXYLocation = useForXYLocation;
145 myProject = project;
146 return this;
149 @NotNull
150 public ComponentPopupBuilder setCancelCallback(final Computable<Boolean> shouldProceed) {
151 myCallback = shouldProceed;
152 return this;
155 @NotNull
156 public ComponentPopupBuilder setCancelButton(@NotNull final IconButton cancelButton) {
157 myCancelButton = cancelButton;
158 return this;
160 @NotNull
161 public ComponentPopupBuilder setCommandButton(@NotNull InplaceButton button) {
162 myCommandButton = button;
163 return this;
166 @NotNull
167 public ComponentPopupBuilder setCouldPin(@Nullable final Processor<JBPopup> callback) {
168 myPinCallback = callback;
169 return this;
172 @NotNull
173 public ComponentPopupBuilder setKeyboardActions(@NotNull List<Pair<ActionListener, KeyStroke>> keyboardActions) {
174 myKeyboardActions = keyboardActions;
175 return this;
178 @NotNull
179 public ComponentPopupBuilder setSettingButtons(@NotNull Component button) {
180 mySettingsButtons = button;
181 return this;
184 @NotNull
185 public ComponentPopupBuilder setCancelOnOtherWindowOpen(final boolean cancelOnWindow) {
186 myCancelOnWindow = cancelOnWindow;
187 return this;
190 @NotNull
191 public ComponentPopupBuilder setProject(Project project) {
192 myProject = project;
193 return this;
196 @NotNull
197 public JBPopup createPopup() {
198 final AbstractPopup popup = new AbstractPopup().init(myProject, myComponent, myPrefferedFocusedComponent, myRequestFocus, myFocusable, myForceHeavyweight,
199 myMovable, myDimensionServiceKey, myResizable, myTitle,
200 myCallback, myCancelOnClickOutside, myListeners, myUseDimSevriceForXYLocation, myCommandButton,
201 myCancelButton,
202 myCancelOnMouseOutCallback, myCancelOnWindow, myTitleIcon, myCancelKeyEnabled, myLocateByContent,
203 myPlacewithinScreen, myMinSize, myAlpha, myMaskProvider, myInStack, myModalContext, myFocusOwners, myAd,
204 myHeaderAlwaysFocusable, myKeyboardActions, mySettingsButtons, myPinCallback);
205 if (myUserData != null) {
206 popup.setUserData(myUserData);
208 //default disposable parent
209 Disposer.register(ApplicationManager.getApplication(), popup);
210 return popup;
213 @NotNull
214 public ComponentPopupBuilder setRequestFocusCondition(Project project, Condition<Project> condition) {
215 myRequestFocus = condition.value(project);
216 return this;
219 @NotNull
220 public ComponentPopupBuilder setTitleIcon(@NotNull final ActiveIcon icon) {
221 myTitleIcon = icon;
222 return this;
225 @NotNull
226 public ComponentPopupBuilder setCancelKeyEnabled(final boolean enabled) {
227 myCancelKeyEnabled = enabled;
228 return this;
231 @NotNull
232 public ComponentPopupBuilder setLocateByContent(final boolean byContent) {
233 myLocateByContent = byContent;
234 return this;
237 @NotNull
238 public ComponentPopupBuilder setLocateWithinScreenBounds(final boolean within) {
239 myPlacewithinScreen = within;
240 return this;
243 @NotNull
244 public ComponentPopupBuilder setMinSize(final Dimension minSize) {
245 myMinSize = minSize;
246 return this;
249 @NotNull
250 public ComponentPopupBuilder setMaskProvider(MaskProvider maskProvider) {
251 myMaskProvider = maskProvider;
252 return this;
255 @NotNull
256 public ComponentPopupBuilder setAlpha(final float alpha) {
257 myAlpha = alpha;
258 return this;
261 @NotNull
262 public ComponentPopupBuilder setBelongsToGlobalPopupStack(final boolean isInStack) {
263 myInStack = isInStack;
264 return this;
267 @NotNull
268 public ComponentPopupBuilder addUserData(final Object object) {
269 if (myUserData == null) {
270 myUserData = new ArrayList<Object>();
272 myUserData.add(object);
273 return this;
276 @NotNull
277 public ComponentPopupBuilder setModalContext(final boolean modal) {
278 myModalContext = modal;
279 return this;
282 @NotNull
283 public ComponentPopupBuilder setFocusOwners(@NotNull final Component[] focusOwners) {
284 myFocusOwners = focusOwners;
285 return this;
288 @NotNull
289 public ComponentPopupBuilder setAdText(@Nullable final String text) {
290 myAd = text;
291 return this;