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
;
33 import java
.awt
.event
.ActionListener
;
35 import java
.util
.List
;
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];
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
;
86 public ComponentPopupBuilder
setTitle(String title
) {
92 public ComponentPopupBuilder
setResizable(final boolean resizable
) {
93 myResizable
= resizable
;
98 public ComponentPopupBuilder
setMovable(final boolean movable
) {
104 public ComponentPopupBuilder
setCancelOnClickOutside(final boolean cancel
) {
105 myCancelOnClickOutside
= cancel
;
110 public ComponentPopupBuilder
setCancelOnMouseOutCallback(final MouseChecker shouldCancel
) {
111 myCancelOnMouseOutCallback
= shouldCancel
;
116 public ComponentPopupBuilder
addListener(final JBPopupListener listener
) {
117 myListeners
.add(listener
);
122 public ComponentPopupBuilder
setRequestFocus(final boolean requestFocus
) {
123 myRequestFocus
= requestFocus
;
128 public ComponentPopupBuilder
setFocusable(final boolean focusable
) {
129 myFocusable
= focusable
;
134 public ComponentPopupBuilder
setForceHeavyweight(final boolean forceHeavyweight
) {
135 myForceHeavyweight
= forceHeavyweight
;
140 public ComponentPopupBuilder
setDimensionServiceKey(final Project project
, final String dimensionServiceKey
, final boolean useForXYLocation
) {
141 myDimensionServiceKey
= dimensionServiceKey
;
142 myUseDimSevriceForXYLocation
= useForXYLocation
;
148 public ComponentPopupBuilder
setCancelCallback(final Computable
<Boolean
> shouldProceed
) {
149 myCallback
= shouldProceed
;
154 public ComponentPopupBuilder
setCancelButton(@NotNull final IconButton cancelButton
) {
155 myCancelButton
= cancelButton
;
159 public ComponentPopupBuilder
setCommandButton(@NotNull InplaceButton button
) {
160 myCommandButton
= button
;
165 public ComponentPopupBuilder
setKeyboardActions(@NotNull List
<Pair
<ActionListener
, KeyStroke
>> keyboardActions
) {
166 myKeyboardActions
= keyboardActions
;
171 public ComponentPopupBuilder
setSettingButtons(@NotNull Component button
) {
172 mySettingsButtons
= button
;
177 public ComponentPopupBuilder
setCancelOnOtherWindowOpen(final boolean cancelOnWindow
) {
178 myCancelOnWindow
= cancelOnWindow
;
183 public ComponentPopupBuilder
setProject(Project project
) {
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
,
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
);
206 public ComponentPopupBuilder
setRequestFocusCondition(Project project
, Condition
<Project
> condition
) {
207 myRequestFocus
= condition
.value(project
);
212 public ComponentPopupBuilder
setTitleIcon(@NotNull final ActiveIcon icon
) {
218 public ComponentPopupBuilder
setCancelKeyEnabled(final boolean enabled
) {
219 myCancelKeyEnabled
= enabled
;
224 public ComponentPopupBuilder
setLocateByContent(final boolean byContent
) {
225 myLocateByContent
= byContent
;
230 public ComponentPopupBuilder
setLocateWithinScreenBounds(final boolean within
) {
231 myPlacewithinScreen
= within
;
236 public ComponentPopupBuilder
setMinSize(final Dimension minSize
) {
242 public ComponentPopupBuilder
setMaskProvider(MaskProvider maskProvider
) {
243 myMaskProvider
= maskProvider
;
248 public ComponentPopupBuilder
setAlpha(final float alpha
) {
254 public ComponentPopupBuilder
setBelongsToGlobalPopupStack(final boolean isInStack
) {
255 myInStack
= isInStack
;
260 public ComponentPopupBuilder
addUserData(final Object object
) {
261 if (myUserData
== null) {
262 myUserData
= new ArrayList
<Object
>();
264 myUserData
.add(object
);
269 public ComponentPopupBuilder
setModalContext(final boolean modal
) {
270 myModalContext
= modal
;
275 public ComponentPopupBuilder
setFocusOwners(@NotNull final Component
[] focusOwners
) {
276 myFocusOwners
= focusOwners
;
281 public ComponentPopupBuilder
setAdText(@Nullable final String text
) {