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
;
34 import java
.awt
.event
.ActionListener
;
36 import java
.util
.List
;
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];
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
;
88 public ComponentPopupBuilder
setTitle(String title
) {
94 public ComponentPopupBuilder
setResizable(final boolean resizable
) {
95 myResizable
= resizable
;
100 public ComponentPopupBuilder
setMovable(final boolean movable
) {
106 public ComponentPopupBuilder
setCancelOnClickOutside(final boolean cancel
) {
107 myCancelOnClickOutside
= cancel
;
112 public ComponentPopupBuilder
setCancelOnMouseOutCallback(final MouseChecker shouldCancel
) {
113 myCancelOnMouseOutCallback
= shouldCancel
;
118 public ComponentPopupBuilder
addListener(final JBPopupListener listener
) {
119 myListeners
.add(listener
);
124 public ComponentPopupBuilder
setRequestFocus(final boolean requestFocus
) {
125 myRequestFocus
= requestFocus
;
130 public ComponentPopupBuilder
setFocusable(final boolean focusable
) {
131 myFocusable
= focusable
;
136 public ComponentPopupBuilder
setForceHeavyweight(final boolean forceHeavyweight
) {
137 myForceHeavyweight
= forceHeavyweight
;
142 public ComponentPopupBuilder
setDimensionServiceKey(final Project project
, final String dimensionServiceKey
, final boolean useForXYLocation
) {
143 myDimensionServiceKey
= dimensionServiceKey
;
144 myUseDimSevriceForXYLocation
= useForXYLocation
;
150 public ComponentPopupBuilder
setCancelCallback(final Computable
<Boolean
> shouldProceed
) {
151 myCallback
= shouldProceed
;
156 public ComponentPopupBuilder
setCancelButton(@NotNull final IconButton cancelButton
) {
157 myCancelButton
= cancelButton
;
161 public ComponentPopupBuilder
setCommandButton(@NotNull InplaceButton button
) {
162 myCommandButton
= button
;
167 public ComponentPopupBuilder
setCouldPin(@Nullable final Processor
<JBPopup
> callback
) {
168 myPinCallback
= callback
;
173 public ComponentPopupBuilder
setKeyboardActions(@NotNull List
<Pair
<ActionListener
, KeyStroke
>> keyboardActions
) {
174 myKeyboardActions
= keyboardActions
;
179 public ComponentPopupBuilder
setSettingButtons(@NotNull Component button
) {
180 mySettingsButtons
= button
;
185 public ComponentPopupBuilder
setCancelOnOtherWindowOpen(final boolean cancelOnWindow
) {
186 myCancelOnWindow
= cancelOnWindow
;
191 public ComponentPopupBuilder
setProject(Project project
) {
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
,
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
);
214 public ComponentPopupBuilder
setRequestFocusCondition(Project project
, Condition
<Project
> condition
) {
215 myRequestFocus
= condition
.value(project
);
220 public ComponentPopupBuilder
setTitleIcon(@NotNull final ActiveIcon icon
) {
226 public ComponentPopupBuilder
setCancelKeyEnabled(final boolean enabled
) {
227 myCancelKeyEnabled
= enabled
;
232 public ComponentPopupBuilder
setLocateByContent(final boolean byContent
) {
233 myLocateByContent
= byContent
;
238 public ComponentPopupBuilder
setLocateWithinScreenBounds(final boolean within
) {
239 myPlacewithinScreen
= within
;
244 public ComponentPopupBuilder
setMinSize(final Dimension minSize
) {
250 public ComponentPopupBuilder
setMaskProvider(MaskProvider maskProvider
) {
251 myMaskProvider
= maskProvider
;
256 public ComponentPopupBuilder
setAlpha(final float alpha
) {
262 public ComponentPopupBuilder
setBelongsToGlobalPopupStack(final boolean isInStack
) {
263 myInStack
= isInStack
;
268 public ComponentPopupBuilder
addUserData(final Object object
) {
269 if (myUserData
== null) {
270 myUserData
= new ArrayList
<Object
>();
272 myUserData
.add(object
);
277 public ComponentPopupBuilder
setModalContext(final boolean modal
) {
278 myModalContext
= modal
;
283 public ComponentPopupBuilder
setFocusOwners(@NotNull final Component
[] focusOwners
) {
284 myFocusOwners
= focusOwners
;
289 public ComponentPopupBuilder
setAdText(@Nullable final String text
) {