129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.InformationControlReplacer;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19 import dwt.graphics.Point;
|
|
20 import dwt.graphics.Rectangle;
|
|
21 import dwt.widgets.Shell;
|
|
22 import dwtx.jface.text.AbstractInformationControlManager;
|
|
23 import dwtx.jface.text.AbstractReusableInformationControlCreator;
|
|
24 import dwtx.jface.text.DefaultInformationControl;
|
|
25 import dwtx.jface.text.IInformationControl;
|
|
26 import dwtx.jface.text.IInformationControlCreator;
|
|
27 import dwtx.jface.text.IInformationControlExtension2;
|
|
28 import dwtx.jface.text.IInformationControlExtension3;
|
|
29 import dwtx.jface.util.Geometry;
|
|
30
|
|
31
|
|
32 /**
|
|
33 * An information control replacer can replace an
|
|
34 * {@link AbstractInformationControlManager}'s control.
|
|
35 *
|
|
36 * @see AbstractInformationControlManager#setInformationControlReplacer(InformationControlReplacer)
|
|
37 * @since 3.4
|
|
38 */
|
|
39 public class InformationControlReplacer : AbstractInformationControlManager {
|
|
40
|
|
41 /**
|
|
42 * Minimal width in pixels.
|
|
43 */
|
|
44 private static final int MIN_WIDTH= 80;
|
|
45 /**
|
|
46 * Minimal height in pixels.
|
|
47 */
|
|
48 private static final int MIN_HEIGHT= 50;
|
|
49
|
|
50 /**
|
|
51 * Default control creator.
|
|
52 */
|
|
53 protected static class DefaultInformationControlCreator : AbstractReusableInformationControlCreator {
|
|
54 public IInformationControl doCreateInformationControl(Shell shell) {
|
|
55 return new DefaultInformationControl(shell, true);
|
|
56 }
|
|
57 }
|
|
58
|
|
59 private bool fIsReplacing;
|
|
60 private Object fReplacableInformation;
|
|
61 private bool fDelayedInformationSet;
|
|
62 private Rectangle fReplaceableArea;
|
|
63 private Rectangle fContentBounds;
|
|
64
|
|
65
|
|
66 /**
|
|
67 * Creates a new information control replacer.
|
|
68 *
|
|
69 * @param creator the default information control creator
|
|
70 */
|
|
71 public InformationControlReplacer(IInformationControlCreator creator) {
|
|
72 super(creator);
|
|
73 takesFocusWhenVisible(false);
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * Replace the information control.
|
|
78 *
|
|
79 * @param informationPresenterControlCreator the information presenter control creator
|
|
80 * @param contentBounds the bounds of the content area of the information control
|
|
81 * @param information the information to show
|
|
82 * @param subjectArea the subject area
|
|
83 * @param takeFocus <code>true</code> iff the replacing information control should take focus
|
|
84 */
|
|
85 public void replaceInformationControl(IInformationControlCreator informationPresenterControlCreator, Rectangle contentBounds, Object information, final Rectangle subjectArea, bool takeFocus) {
|
|
86
|
|
87 try {
|
|
88 fIsReplacing= true;
|
|
89 if (! fDelayedInformationSet)
|
|
90 fReplacableInformation= information;
|
|
91 else
|
|
92 takeFocus= true; // delayed input has been set, so the original info control must have been focused
|
|
93 fContentBounds= contentBounds;
|
|
94 fReplaceableArea= subjectArea;
|
|
95
|
|
96 setCustomInformationControlCreator(informationPresenterControlCreator);
|
|
97
|
|
98 takesFocusWhenVisible(takeFocus);
|
|
99
|
|
100 showInformation();
|
|
101 } finally {
|
|
102 fIsReplacing= false;
|
|
103 fReplacableInformation= null;
|
|
104 fDelayedInformationSet= false;
|
|
105 fReplaceableArea= null;
|
|
106 setCustomInformationControlCreator(null);
|
|
107 }
|
|
108 }
|
|
109
|
|
110 /*
|
|
111 * @see dwtx.jface.text.AbstractInformationControlManager#computeInformation()
|
|
112 */
|
|
113 protected void computeInformation() {
|
|
114 if (fIsReplacing && fReplacableInformation !is null) {
|
|
115 setInformation(fReplacableInformation, fReplaceableArea);
|
|
116 return;
|
|
117 }
|
|
118
|
|
119 if (DEBUG)
|
|
120 System.out.println("InformationControlReplacer: no active replaceable"); //$NON-NLS-1$
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Opens the information control with the given information and the specified
|
|
125 * subject area. It also activates the information control closer.
|
|
126 *
|
|
127 * @param subjectArea the information area
|
|
128 * @param information the information
|
|
129 */
|
|
130 public void showInformationControl(Rectangle subjectArea, Object information) {
|
|
131 IInformationControl informationControl= getInformationControl();
|
|
132
|
|
133 Rectangle controlBounds= fContentBounds;
|
|
134 if (informationControl instanceof IInformationControlExtension3) {
|
|
135 IInformationControlExtension3 iControl3= (IInformationControlExtension3) informationControl;
|
|
136 Rectangle trim= iControl3.computeTrim();
|
|
137 controlBounds= Geometry.add(controlBounds, trim);
|
|
138
|
|
139 /*
|
|
140 * Ensure minimal size. Interacting with a tiny information control
|
|
141 * (resizing, selecting text) would be a pain.
|
|
142 */
|
|
143 controlBounds.width= Math.max(controlBounds.width, MIN_WIDTH);
|
|
144 controlBounds.height= Math.max(controlBounds.height, MIN_HEIGHT);
|
|
145
|
|
146 getInternalAccessor().cropToClosestMonitor(controlBounds);
|
|
147 }
|
|
148
|
|
149 Point location= Geometry.getLocation(controlBounds);
|
|
150 Point size= Geometry.getSize(controlBounds);
|
|
151
|
|
152 // Caveat: some IInformationControls fail unless setSizeConstraints(..) is called with concrete values
|
|
153 informationControl.setSizeConstraints(size.x, size.y);
|
|
154
|
|
155 if (informationControl instanceof IInformationControlExtension2)
|
|
156 ((IInformationControlExtension2) informationControl).setInput(information);
|
|
157 else
|
|
158 informationControl.setInformation(information.toString());
|
|
159
|
|
160 informationControl.setLocation(location);
|
|
161 informationControl.setSize(size.x, size.y);
|
|
162
|
|
163 showInformationControl(subjectArea);
|
|
164 }
|
|
165
|
|
166 /*
|
|
167 * @see dwtx.jface.text.AbstractInformationControlManager#hideInformationControl()
|
|
168 */
|
|
169 public void hideInformationControl() {
|
|
170 super.hideInformationControl();
|
|
171 }
|
|
172
|
|
173 /**
|
|
174 * @param input the delayed input, or <code>null</code> to request cancellation
|
|
175 */
|
|
176 public void setDelayedInput(Object input) {
|
|
177 fReplacableInformation= input;
|
|
178 if (! isReplacing()) {
|
|
179 fDelayedInformationSet= true;
|
|
180 } else if (getCurrentInformationControl2() instanceof IInformationControlExtension2) {
|
|
181 ((IInformationControlExtension2) getCurrentInformationControl2()).setInput(input);
|
|
182 } else if (getCurrentInformationControl2() !is null) {
|
|
183 getCurrentInformationControl2().setInformation(input.toString());
|
|
184 }
|
|
185 }
|
|
186
|
|
187 /**
|
|
188 * Tells whether the replacer is currently replacing another information control.
|
|
189 *
|
|
190 * @return <code>true</code> while code from {@link #replaceInformationControl(IInformationControlCreator, Rectangle, Object, Rectangle, bool)} is run
|
|
191 */
|
|
192 public bool isReplacing() {
|
|
193 return fIsReplacing;
|
|
194 }
|
|
195
|
|
196 /**
|
|
197 * @return the current information control, or <code>null</code> if none available
|
|
198 */
|
|
199 public IInformationControl getCurrentInformationControl2() {
|
|
200 return getInternalAccessor().getCurrentInformationControl();
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * The number of pixels to blow up the keep-up zone.
|
|
205 *
|
|
206 * @return the margin in pixels
|
|
207 */
|
|
208 public int getKeepUpMargin() {
|
|
209 return 15;
|
|
210 }
|
|
211 }
|