1 | /**
|
---|
2 | * InteractiveWidget.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items.widgets;
|
---|
20 |
|
---|
21 | import java.io.PrintWriter;
|
---|
22 | import java.io.StringWriter;
|
---|
23 | import java.lang.reflect.Constructor;
|
---|
24 | import java.util.ArrayList;
|
---|
25 | import java.util.Collection;
|
---|
26 | import java.util.Collections;
|
---|
27 | import java.util.LinkedList;
|
---|
28 | import java.util.List;
|
---|
29 |
|
---|
30 | import org.apache.commons.cli.CommandLine;
|
---|
31 | import org.apache.commons.cli.CommandLineParser;
|
---|
32 | import org.apache.commons.cli.GnuParser;
|
---|
33 | import org.apache.commons.cli.HelpFormatter;
|
---|
34 | import org.apache.commons.cli.Options;
|
---|
35 | import org.apache.commons.cli.ParseException;
|
---|
36 | import org.expeditee.Util;
|
---|
37 | import org.expeditee.actions.Actions;
|
---|
38 | import org.expeditee.core.Anchoring;
|
---|
39 | import org.expeditee.core.Clip;
|
---|
40 | import org.expeditee.core.Colour;
|
---|
41 | import org.expeditee.core.Dimension;
|
---|
42 | import org.expeditee.core.EcosystemSpecific;
|
---|
43 | import org.expeditee.core.Fill;
|
---|
44 | import org.expeditee.core.Point;
|
---|
45 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
46 | import org.expeditee.gio.EcosystemManager;
|
---|
47 | import org.expeditee.gio.EcosystemManager.Ecosystem;
|
---|
48 | import org.expeditee.gio.GraphicsManager;
|
---|
49 | import org.expeditee.gui.DisplayController;
|
---|
50 | import org.expeditee.gui.Frame;
|
---|
51 | import org.expeditee.gui.FrameIO;
|
---|
52 | import org.expeditee.gui.FreeItems;
|
---|
53 | import org.expeditee.items.Item;
|
---|
54 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
55 | import org.expeditee.items.ItemUtils;
|
---|
56 | import org.expeditee.items.Text;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The bridge between swing space and Expeditee space
|
---|
60 | *
|
---|
61 | * @author Brook
|
---|
62 | *
|
---|
63 | */
|
---|
64 | public abstract class Widget implements EcosystemSpecific
|
---|
65 | {
|
---|
66 |
|
---|
67 | /** The colour to fill the widget area with when not rendering the widget itself (i.e. when picked up). */
|
---|
68 | protected final static Colour FREESPACE_BACKCOLOR = Colour.FromRGB255(100, 100, 100);
|
---|
69 |
|
---|
70 | /** The minimum border thickness for widgets. */
|
---|
71 | public final static float DEFAULT_MINIMUM_BORDER_THICKNESS = 1.0f;
|
---|
72 |
|
---|
73 | // @formatter:off
|
---|
74 | /* GUIDE:
|
---|
75 | * d1--l1---d2
|
---|
76 | * | |
|
---|
77 | * l4 | X | l2
|
---|
78 | * | |
|
---|
79 | * d4---l3--d3
|
---|
80 | */
|
---|
81 | // @formatter:on
|
---|
82 |
|
---|
83 | /** The top-left corner of the widget. */
|
---|
84 | private WidgetCorner _d1;
|
---|
85 | /** The top-right corner of the widget. */
|
---|
86 | private WidgetCorner _d2;
|
---|
87 | /** The bottom-right corner of the widget. */
|
---|
88 | private WidgetCorner _d3;
|
---|
89 | ///** The bottom-left corner of the widget. */
|
---|
90 | private WidgetCorner _d4;
|
---|
91 |
|
---|
92 | /** The Anchoring for the top-left corner of the widget */
|
---|
93 | private Anchoring _d1Anchoring = new Anchoring();
|
---|
94 | /** The Anchoring for the bottom-right corner of the widget */
|
---|
95 | private Anchoring _d3Anchoring = new Anchoring();
|
---|
96 |
|
---|
97 | /** The top edge of the widget. */
|
---|
98 | private WidgetEdge _l1;
|
---|
99 | /** The right edge of the widget. */
|
---|
100 | private WidgetEdge _l2;
|
---|
101 | /** The bottom edge of the widget. */
|
---|
102 | private WidgetEdge _l3;
|
---|
103 | /** The left edge of the widget. */
|
---|
104 | private WidgetEdge _l4;
|
---|
105 |
|
---|
106 | /** Used for quickly returning item list. */
|
---|
107 | private List<Item> _expediteeItems;
|
---|
108 |
|
---|
109 | // Widget size restrictions
|
---|
110 | /** Minimum width of the widget. */
|
---|
111 | private int _minWidth = 50;
|
---|
112 | /** Minimum height of the widget. */
|
---|
113 | private int _minHeight = 50;
|
---|
114 | /** Maximum width of the widget. */
|
---|
115 | private int _maxWidth = 300;
|
---|
116 | /** Maximum height of the widget. */
|
---|
117 | private int _maxHeight = 300;
|
---|
118 |
|
---|
119 | /** The Expeditee item that is used for saving widget state. */
|
---|
120 | protected Text _textRepresentation;
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Creates a InteractiveWidget from a text item.
|
---|
124 | *
|
---|
125 | * @param source
|
---|
126 | * Must not be null, first line of text used - which the format
|
---|
127 | * must be as follows: "@iw: <<widget_class_name>> [<<width>>] [<<height>>] [: [<<arg1>>] [<<arg2>>]
|
---|
128 | * [...]]".
|
---|
129 | *
|
---|
130 | * e.g: "@iw: org.expeditee.items.SampleWidget1 100 20 : 2" creates a
|
---|
131 | * SampleWidget1 with width = 100 and height = 20 with 1 argument = "2"
|
---|
132 | *
|
---|
133 | * @return An InteractiveWidget instance. Never null.
|
---|
134 | *
|
---|
135 | * @throws NullPointerException
|
---|
136 | * if source is null
|
---|
137 | *
|
---|
138 | * @throws IllegalArgumentException
|
---|
139 | * if source's text is in the incorrect format or if source's
|
---|
140 | * parent is null
|
---|
141 | *
|
---|
142 | * @throws InteractiveWidgetNotAvailableException
|
---|
143 | * If the given widget class name in the source text doesn't
|
---|
144 | * exist or not an InteractiveWidget or the widget does not
|
---|
145 | * supply a valid constructor for creation.
|
---|
146 | *
|
---|
147 | * @throws InteractiveWidgetInitialisationFailedException
|
---|
148 | * The sub-constructor failed - depends on the type of widget
|
---|
149 | * being instantainiated.
|
---|
150 | *
|
---|
151 | * class names beginning with $, the $ will be replaced with
|
---|
152 | * "org.expeditee.items."
|
---|
153 | */
|
---|
154 | public static Widget createWidget(final Text source) throws InteractiveWidgetNotAvailableException,
|
---|
155 | InteractiveWidgetInitialisationFailedException
|
---|
156 | {
|
---|
157 |
|
---|
158 | if (source == null) {
|
---|
159 | throw new NullPointerException("source");
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (source.getParent() == null) {
|
---|
163 | throw new IllegalArgumentException("source's parent is null, InteractiveWidget's must be created from Text items with non-null parents");
|
---|
164 | }
|
---|
165 |
|
---|
166 | final String TAG = ItemUtils.GetTag(ItemUtils.TAG_IWIDGET);
|
---|
167 |
|
---|
168 | String text = source.getText();
|
---|
169 | if (text == null) {
|
---|
170 | throw new IllegalArgumentException("source does not have any text");
|
---|
171 | }
|
---|
172 |
|
---|
173 | text = text.trim();
|
---|
174 |
|
---|
175 | // Check starts with the widget tag and separator
|
---|
176 | if (!text.startsWith(TAG + ":")) {
|
---|
177 | throw new IllegalArgumentException("Source text must begin with \"" + TAG + ":\"");
|
---|
178 | }
|
---|
179 |
|
---|
180 | // skip over the '@iw:' preamble
|
---|
181 | text = text.substring(TAG.length() + 1).trim();
|
---|
182 |
|
---|
183 | int index = 0;
|
---|
184 | if (text.length() > 0) {
|
---|
185 | // Having removed @iw:, then next ':' is used for signifying start of arguments
|
---|
186 | index = text.indexOf(':');
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (index == 0) {
|
---|
190 | throw new IllegalArgumentException("Source text must begin with \"" + TAG + "\"");
|
---|
191 | }
|
---|
192 |
|
---|
193 | //
|
---|
194 | // Step 1:
|
---|
195 | // For an X-rayable text item in the form:
|
---|
196 | // @iw: <class name> [options] width height : <rest...>
|
---|
197 | //
|
---|
198 | // the 'core' part of the X-rayable text item
|
---|
199 | // i.e, the text between the first ':' and the second ':'
|
---|
200 | //
|
---|
201 | //
|
---|
202 |
|
---|
203 | final String tokens_str = (index == -1) ? text : text.substring(0, index);
|
---|
204 |
|
---|
205 | String[] tokens = Text.parseArgsApache(tokens_str);
|
---|
206 |
|
---|
207 | // make anything starting with a '-' lowercase
|
---|
208 | for (int i=0; i<tokens.length; i++) {
|
---|
209 | if (tokens[i].startsWith("-")) {
|
---|
210 | tokens[i] = tokens[i].toLowerCase();
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | // create the command line parser
|
---|
215 | final CommandLineParser parser = new GnuParser();
|
---|
216 |
|
---|
217 | // create the Options
|
---|
218 | final Options options = new Options();
|
---|
219 | options.addOption( "al", "anchorleft", true, "Anchor the vertical left-hand edge of the interactive widget to the value provided " );
|
---|
220 | options.addOption( "ar", "anchorright", true, "Anchor the vertical right-hand edge of the interactive widget to the value provided " );
|
---|
221 | options.addOption( "at", "anchortop", true, "Anchor the vertical top edge of the interactive widget to the value provided " );
|
---|
222 | options.addOption( "ab", "anchorbottom", true, "Anchor the vertical bottom edge of the interactive widget to the value provided " );
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * Alternative way to do the above, but with more control over how the values are set up and can be used
|
---|
226 | *
|
---|
227 | @SuppressWarnings("static-access")
|
---|
228 | Option anchor_left_opt = OptionBuilder.withLongOpt( "anchorleft" )
|
---|
229 | .withDescription( "use <x-pos> as left anchor for the vertical lefthand edge of the interactive widget" )
|
---|
230 | .hasArg()
|
---|
231 | .withArgName("<x-pos>")
|
---|
232 | .create("al");
|
---|
233 |
|
---|
234 | @SuppressWarnings("static-access")
|
---|
235 | Option anchor_right_opt = OptionBuilder.withLongOpt( "anchorright" )
|
---|
236 | .withDescription( "use <x-pos> as right anchor for the vertical righthand edge of the interactive widget" )
|
---|
237 | .hasArg()
|
---|
238 | .withArgName("<x-pos>")
|
---|
239 | .create("ar");
|
---|
240 |
|
---|
241 |
|
---|
242 | @SuppressWarnings("static-access")
|
---|
243 | Option anchor_top_opt = OptionBuilder.withLongOpt( "anchortop" )
|
---|
244 | .withDescription( "use <y-pos> as top anchor for the horizontal top end of the interactive widget" )
|
---|
245 | .hasArg()
|
---|
246 | .withArgName("<x-pos>")
|
---|
247 | .create("at");
|
---|
248 |
|
---|
249 | @SuppressWarnings("static-access")
|
---|
250 | Option anchor_bottom_opt = OptionBuilder.withLongOpt( "anchorbottom" )
|
---|
251 | .withDescription( "use <y-pos> as bottom anchor for the horizontal bottom edge of the interactive widget" )
|
---|
252 | .hasArg()
|
---|
253 | .withArgName("<x-pos>")
|
---|
254 | .create("ab");
|
---|
255 |
|
---|
256 | options.addOption(anchor_left_opt);
|
---|
257 | options.addOption(anchor_right_opt);
|
---|
258 | options.addOption(anchor_top_opt);
|
---|
259 | options.addOption(anchor_bottom_opt);
|
---|
260 | */
|
---|
261 |
|
---|
262 | CommandLine core_line;
|
---|
263 | try {
|
---|
264 | // parse the command line arguments
|
---|
265 | core_line = parser.parse( options, tokens );
|
---|
266 |
|
---|
267 | // Update tokens to be version with the options removed
|
---|
268 | tokens = core_line.getArgs();
|
---|
269 |
|
---|
270 | }
|
---|
271 | catch( final ParseException exp ) {
|
---|
272 | System.err.println( "Unexpected exception:" + exp.getMessage() );
|
---|
273 | core_line = null;
|
---|
274 |
|
---|
275 | }
|
---|
276 |
|
---|
277 | final HelpFormatter formatter = new HelpFormatter();
|
---|
278 | final StringWriter usage_str_writer = new StringWriter();
|
---|
279 | final PrintWriter printWriter = new PrintWriter(usage_str_writer);
|
---|
280 |
|
---|
281 | String widget_name = tokens[0];
|
---|
282 | final String widget_prefix = "org.expeditee.items.widgets";
|
---|
283 | if (widget_name.startsWith(widget_prefix)) {
|
---|
284 | widget_name = widget_name.substring(widget_prefix.length());
|
---|
285 | }
|
---|
286 |
|
---|
287 | formatter.printHelp(printWriter, 80, TAG + ":" + widget_name + " [options] width height", null, options, 4, 0, null);
|
---|
288 | //System.out.println(usage_str_writer.toString());
|
---|
289 |
|
---|
290 | float width = -1, height = -1;
|
---|
291 |
|
---|
292 | if (tokens.length < 1) {
|
---|
293 | throw new IllegalArgumentException("Missing widget class name in source text");
|
---|
294 | }
|
---|
295 |
|
---|
296 | try {
|
---|
297 |
|
---|
298 | if (tokens.length >= 2) { // parse optional width
|
---|
299 | width = Integer.parseInt(tokens[1]);
|
---|
300 | width = (width <= 0) ? width = -1 : width;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (tokens.length >= 3) { // parse optional height
|
---|
304 | height = Integer.parseInt(tokens[2]);
|
---|
305 | height = (height <= 0) ? height = -1 : height;
|
---|
306 | }
|
---|
307 |
|
---|
308 | } catch (final NumberFormatException nfe) {
|
---|
309 | throw new IllegalArgumentException(
|
---|
310 | "Bad width or height given in source text", nfe);
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (tokens.length > 3) {
|
---|
314 | throw new IllegalArgumentException(
|
---|
315 | "to many arguments given before \":\" in source text");
|
---|
316 | }
|
---|
317 |
|
---|
318 | String classname = tokens[0];
|
---|
319 | if (classname.charAt(0) == '$'){
|
---|
320 | classname = classname.substring(1);
|
---|
321 | }
|
---|
322 |
|
---|
323 | // Attempt to locate the class using reflection
|
---|
324 | final Class<?> iwclass = findIWidgetClass(classname);
|
---|
325 |
|
---|
326 | if (iwclass == null) {
|
---|
327 | throw new InteractiveWidgetNotAvailableException(classname
|
---|
328 | + " does not exist or is not an InteractiveWidget");
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | //
|
---|
333 | // Step 2:
|
---|
334 | // For an X-rayable text item in the form:
|
---|
335 | // @iw: <class name> [options] width height : <rest...>
|
---|
336 | //
|
---|
337 | // Parse the <rest ...> part
|
---|
338 |
|
---|
339 | // Extract out the parameters - if any
|
---|
340 | String[] args = null;
|
---|
341 | if (index > 0) { // index of the first ":"
|
---|
342 | if (text.length()>(index+1)) {
|
---|
343 | final String args_str = text.substring(index + 1);
|
---|
344 |
|
---|
345 | args = Text.parseArgsApache(args_str);
|
---|
346 |
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | Widget inst = null;
|
---|
351 | try {
|
---|
352 | // Instantiate the widget - passing the params
|
---|
353 | final Class parameterTypes[] = new Class[] { Text.class, String[].class };
|
---|
354 | final Constructor ct = iwclass.getConstructor(parameterTypes);
|
---|
355 |
|
---|
356 | final Object arglist[] = new Object[] { source, args };
|
---|
357 |
|
---|
358 | inst = (Widget) ct.newInstance(arglist);
|
---|
359 | } catch (final Exception e) {
|
---|
360 | throw new InteractiveWidgetNotAvailableException(
|
---|
361 | "Failed to create instance via reflection: " + e.toString(),
|
---|
362 | e);
|
---|
363 | }
|
---|
364 |
|
---|
365 | // Check that the widget is suitable for the current platform
|
---|
366 | final Ecosystem systemType = EcosystemManager.getType();
|
---|
367 | if (!inst.isSupportedOnEcosystem(systemType)) {
|
---|
368 | // Try create a JavaFXSwingWidget if creating a Swing widget in JavaFX
|
---|
369 | if (systemType == Ecosystem.JavaFX && inst.isSupportedOnEcosystem(Ecosystem.Swing)) {
|
---|
370 | inst = new JavaFXSwingWidget((SwingWidget) inst);
|
---|
371 | } else {
|
---|
372 | throw new InteractiveWidgetNotAvailableException(classname + " is not supported while using a " + systemType + " system.");
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | // Step 3:
|
---|
377 | // Set up the size and position of the widget
|
---|
378 |
|
---|
379 | // Use default dimensions if not provided (or provided as negative
|
---|
380 | // values)
|
---|
381 | if (width <= 0) {
|
---|
382 | width = inst.getWidth();
|
---|
383 | }
|
---|
384 | if (height <= 0) {
|
---|
385 | height = inst.getHeight();
|
---|
386 | }
|
---|
387 |
|
---|
388 | inst.setSize(width, height);
|
---|
389 |
|
---|
390 | // Apply any anchor values supplied in the core part of the @iw item
|
---|
391 | Integer anchor_left = null;
|
---|
392 | Integer anchor_right = null;
|
---|
393 | Integer anchor_top = null;
|
---|
394 | Integer anchor_bottom = null;
|
---|
395 |
|
---|
396 | if(core_line.hasOption( "anchorleft" ) ) {
|
---|
397 | final String al_str = core_line.getOptionValue( "anchorleft" );
|
---|
398 |
|
---|
399 | anchor_left = (int) Float.parseFloat(al_str);
|
---|
400 | }
|
---|
401 |
|
---|
402 | if(core_line.hasOption( "anchorright" ) ) {
|
---|
403 | final String ar_str = core_line.getOptionValue( "anchorright" );
|
---|
404 |
|
---|
405 | anchor_right = (int) Float.parseFloat(ar_str);
|
---|
406 | }
|
---|
407 |
|
---|
408 | if(core_line.hasOption( "anchortop" ) ) {
|
---|
409 | final String at_str = core_line.getOptionValue( "anchortop" );
|
---|
410 |
|
---|
411 | anchor_top = (int) Float.parseFloat(at_str);
|
---|
412 | }
|
---|
413 |
|
---|
414 | if(core_line.hasOption( "anchorbottom" ) ) {
|
---|
415 | final String ab_str = core_line.getOptionValue( "anchorbottom" );
|
---|
416 |
|
---|
417 | anchor_bottom = (int) Float.parseFloat(ab_str);
|
---|
418 | }
|
---|
419 |
|
---|
420 | inst.setAnchorCorners(anchor_left,anchor_right,anchor_top,anchor_bottom);
|
---|
421 |
|
---|
422 | return inst;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Locates the class from the classname of an InteractiveWidget class
|
---|
427 | *
|
---|
428 | * @param classname
|
---|
429 | * The name of the class to search
|
---|
430 | * @return Null if doesn't exist or not an InteractiveWidget
|
---|
431 | */
|
---|
432 | private static Class findIWidgetClass(String classname)
|
---|
433 | {
|
---|
434 | if(classname == null) {
|
---|
435 | return null;
|
---|
436 | }
|
---|
437 | // try just the classname
|
---|
438 | try {
|
---|
439 | final Class c = Class.forName(classname); // attempt to find the class
|
---|
440 |
|
---|
441 | // If one is found, ensure that it is a descendant of an
|
---|
442 | // InteractiveWidget
|
---|
443 | for (Class superclass = c.getSuperclass(); superclass != null
|
---|
444 | && superclass != Item.class; superclass = superclass
|
---|
445 | .getSuperclass()) {
|
---|
446 | if (superclass == Widget.class) {
|
---|
447 | return c;
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | } catch (final ClassNotFoundException e) {
|
---|
452 | }
|
---|
453 | // see if the class is a widget with invalid capitalisation, or missing the widget package prefix
|
---|
454 | if(classname.startsWith(Actions.WIDGET_PACKAGE)) {
|
---|
455 | classname = classname.substring(Actions.WIDGET_PACKAGE.length());
|
---|
456 | }
|
---|
457 | try {
|
---|
458 | final Class c = Class.forName(Actions.getClassName(classname)); // attempt to find the class
|
---|
459 |
|
---|
460 | // If one is found, ensure that it is a descendant of an
|
---|
461 | // InteractiveWidget
|
---|
462 | for (Class superclass = c.getSuperclass(); superclass != null
|
---|
463 | && superclass != Item.class; superclass = superclass
|
---|
464 | .getSuperclass()) {
|
---|
465 | if (superclass == Widget.class) {
|
---|
466 | return c;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | } catch (final ClassNotFoundException e) {
|
---|
471 | }
|
---|
472 |
|
---|
473 | // Doesn't exist or not an InteractiveWidget
|
---|
474 | return null;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Arguments represent the widgets <i>current state</i> state. They are
|
---|
479 | * used for saving, loading, creating and cloning Special formatting is done
|
---|
480 | * for you.
|
---|
481 | *
|
---|
482 | * @see #getData()
|
---|
483 | *
|
---|
484 | * @return Can be null for no params.
|
---|
485 | */
|
---|
486 | protected abstract String[] getArgs();
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Data represents the widgets <i>current state</i> state. For any state
|
---|
490 | * information you do not wish to be defined as arguments (e.g. metadata),
|
---|
491 | * you can set as the widgets source items data.
|
---|
492 | *
|
---|
493 | * The default implementation returns null. Override to make use of.
|
---|
494 | *
|
---|
495 | * @see #getArgs()
|
---|
496 | *
|
---|
497 | * @return Null for for data. Otherwise the data that represent this widgets
|
---|
498 | * <i>current state</i>
|
---|
499 | */
|
---|
500 | protected List<String> getData()
|
---|
501 | {
|
---|
502 | return null;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * Constructor
|
---|
507 | *
|
---|
508 | * @param source
|
---|
509 | * Must not be null. Neither must it's parent
|
---|
510 | *
|
---|
511 | * @param component
|
---|
512 | * Must not be null.
|
---|
513 | *
|
---|
514 | * @param minWidth
|
---|
515 | * The min width restriction for the widget. If negative, then
|
---|
516 | * there is no restriction.
|
---|
517 | *
|
---|
518 | * @param maxWidth
|
---|
519 | * The max width restriction for the widget. If negative, then
|
---|
520 | * there is no restriction.
|
---|
521 | *
|
---|
522 | * @param minHeight
|
---|
523 | * The min height restriction for the widget. If negative, then
|
---|
524 | * there is no restriction.
|
---|
525 | *
|
---|
526 | * @param maxHeight
|
---|
527 | * The max height restriction for the widget. If negative, then
|
---|
528 | * there is no restriction.
|
---|
529 | *
|
---|
530 | * @throws NullPointerException
|
---|
531 | * If source, component if null.
|
---|
532 | *
|
---|
533 | * @throws IllegalArgumentException
|
---|
534 | * If source's parent is null. If maxWidth smaller than minWidth
|
---|
535 | * and maxWidth larger or equal to zero or if maxHeight smaller
|
---|
536 | * than minHeight && maxHeight is larger or equal to zero
|
---|
537 | *
|
---|
538 | */
|
---|
539 | protected Widget(final Text source, final int minWidth, final int maxWidth, final int minHeight, final int maxHeight)
|
---|
540 | {
|
---|
541 | if (source == null) {
|
---|
542 | throw new NullPointerException("source");
|
---|
543 | }
|
---|
544 | if (source.getParent() == null) {
|
---|
545 | throw new IllegalArgumentException("source's parent is null, InteractiveWidget's must be created from Text items with non-null parents");
|
---|
546 | }
|
---|
547 |
|
---|
548 | //addThisAsContainerListenerToContent();
|
---|
549 | //addKeyListenerToWidget();
|
---|
550 |
|
---|
551 | _textRepresentation = source;
|
---|
552 |
|
---|
553 | setSizeRestrictions(minWidth, maxWidth, minHeight, maxHeight); // throws IllegalArgumentException's
|
---|
554 |
|
---|
555 | final int x = source.getX();
|
---|
556 | final int y = source.getY();
|
---|
557 | final int width = (_minWidth < 0) ? 10 : _minWidth;
|
---|
558 | final int height = (_minHeight < 0) ? 10 : _minHeight;
|
---|
559 |
|
---|
560 | final Frame idAllocator = _textRepresentation.getParent();
|
---|
561 |
|
---|
562 | // create WidgetCorners
|
---|
563 | _d1 = new WidgetCorner(x, y, idAllocator.getNextItemID(), this);
|
---|
564 | _d2 = new WidgetCorner(x + width, y, idAllocator.getNextItemID(), this);
|
---|
565 | _d3 = new WidgetCorner(x + width, y + height, idAllocator.getNextItemID(), this);
|
---|
566 | _d4 = new WidgetCorner(x, y + height, idAllocator.getNextItemID(), this);
|
---|
567 |
|
---|
568 | // create WidgetEdges
|
---|
569 | _l1 = new WidgetEdge(_d1, _d2, idAllocator.getNextItemID(), this);
|
---|
570 | _l2 = new WidgetEdge(_d2, _d3, idAllocator.getNextItemID(), this);
|
---|
571 | _l3 = new WidgetEdge(_d3, _d4, idAllocator.getNextItemID(), this);
|
---|
572 | _l4 = new WidgetEdge(_d4, _d1, idAllocator.getNextItemID(), this);
|
---|
573 |
|
---|
574 | final Collection<Item> enclist = new ArrayList<Item>(4);
|
---|
575 | enclist.add(_d1);
|
---|
576 | enclist.add(_d2);
|
---|
577 | enclist.add(_d3);
|
---|
578 | enclist.add(_d4);
|
---|
579 | _d1.setEnclosedList(enclist);
|
---|
580 | _d2.setEnclosedList(enclist);
|
---|
581 | _d3.setEnclosedList(enclist);
|
---|
582 | _d4.setEnclosedList(enclist);
|
---|
583 |
|
---|
584 | _expediteeItems = new ArrayList<Item>(8); // Note: order important
|
---|
585 | _expediteeItems.add(_d1);
|
---|
586 | _expediteeItems.add(_d2);
|
---|
587 | _expediteeItems.add(_d3);
|
---|
588 | _expediteeItems.add(_d4);
|
---|
589 | _expediteeItems.add(_l1);
|
---|
590 | _expediteeItems.add(_l2);
|
---|
591 | _expediteeItems.add(_l3);
|
---|
592 | _expediteeItems.add(_l4);
|
---|
593 |
|
---|
594 | setWidgetEdgeColor(source.getBorderColor());
|
---|
595 | setWidgetEdgeThickness(source.getThickness());
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Sets the restrictions - checks values.
|
---|
600 | *
|
---|
601 | * @param minWidth
|
---|
602 | * The min width restriction for the widget. If negative, then
|
---|
603 | * there is no restriction.
|
---|
604 | *
|
---|
605 | * @param maxWidth
|
---|
606 | * The max width restriction for the widget. If negative, then
|
---|
607 | * there is no restriction.
|
---|
608 | *
|
---|
609 | * @param minHeight
|
---|
610 | * The min height restriction for the widget. If negative, then
|
---|
611 | * there is no restriction.
|
---|
612 | *
|
---|
613 | * @param maxHeight
|
---|
614 | * The max height restriction for the widget. If negative, then
|
---|
615 | * there is no restriction.
|
---|
616 | *
|
---|
617 | * @throws IllegalArgumentException
|
---|
618 | * If maxWidth smaller than minWidth and maxWidth larger or
|
---|
619 | * equal to zero or if maxHeight smaller than minHeight &&
|
---|
620 | * maxHeight is larger or equal to zero
|
---|
621 | *
|
---|
622 | */
|
---|
623 | private void setSizeRestrictions(final int minWidth, final int maxWidth, final int minHeight, final int maxHeight)
|
---|
624 | {
|
---|
625 | _minWidth = minWidth;
|
---|
626 |
|
---|
627 | if (maxWidth < _minWidth && maxWidth >= 0) {
|
---|
628 | throw new IllegalArgumentException("maxWidth smaller than the min Width");
|
---|
629 | }
|
---|
630 | _maxWidth = maxWidth;
|
---|
631 |
|
---|
632 | _minHeight = minHeight;
|
---|
633 | if (maxHeight < _minHeight && maxHeight >= 0) {
|
---|
634 | throw new IllegalArgumentException("maxHeight smaller than the min Height");
|
---|
635 | }
|
---|
636 | _maxHeight = maxHeight;
|
---|
637 | }
|
---|
638 |
|
---|
639 | public int getMinWidth()
|
---|
640 | {
|
---|
641 | return _minWidth;
|
---|
642 | }
|
---|
643 |
|
---|
644 | public int getMinHeight()
|
---|
645 | {
|
---|
646 | return _minHeight;
|
---|
647 | }
|
---|
648 |
|
---|
649 | public int getMaxWidth()
|
---|
650 | {
|
---|
651 | return _maxWidth;
|
---|
652 | }
|
---|
653 |
|
---|
654 | public int getMaxHeight()
|
---|
655 | {
|
---|
656 | return _maxHeight;
|
---|
657 | }
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * This can be overridden for creating custom copies. The default
|
---|
661 | * implementation creates a new widget based on the current state of the
|
---|
662 | * widget (via getArgs).
|
---|
663 | *
|
---|
664 | * @see InteractiveWidget#getArgs().
|
---|
665 | *
|
---|
666 | * @return A copy of this widget.
|
---|
667 | *
|
---|
668 | */
|
---|
669 | public Widget copy() throws InteractiveWidgetNotAvailableException, InteractiveWidgetInitialisationFailedException
|
---|
670 | {
|
---|
671 | final Text t = _textRepresentation.copy();
|
---|
672 | final String clonedAnnotation = getAnnotationString();
|
---|
673 | t.setText(clonedAnnotation);
|
---|
674 | t.setData(getData());
|
---|
675 | return Widget.createWidget(t);
|
---|
676 | }
|
---|
677 |
|
---|
678 | /**
|
---|
679 | * Notifies widget of delete
|
---|
680 | */
|
---|
681 | public void onDelete()
|
---|
682 | {
|
---|
683 | // Allocate new ID's
|
---|
684 | Frame parent = getParentFrame();
|
---|
685 |
|
---|
686 | if (parent == null) {
|
---|
687 | parent = DisplayController.getCurrentFrame();
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (parent != null) {
|
---|
691 | for (final Item i : _expediteeItems) {
|
---|
692 | i.setID(parent.getNextItemID());
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | invalidateLink();
|
---|
697 | }
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * Note updates the source text with current state info
|
---|
701 | *
|
---|
702 | * @return The Text item that this widget was created from.
|
---|
703 | */
|
---|
704 | public Text getSource()
|
---|
705 | {
|
---|
706 | // Build the annotation string such that it represents this widgets
|
---|
707 | // current state
|
---|
708 | final String newAnnotation = getAnnotationString();
|
---|
709 |
|
---|
710 | // Set the new text
|
---|
711 | _textRepresentation.setText(newAnnotation);
|
---|
712 |
|
---|
713 | // Set the data
|
---|
714 | _textRepresentation.setData(getData());
|
---|
715 |
|
---|
716 | return _textRepresentation;
|
---|
717 | }
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * @return The current representation for this widget. The representation
|
---|
721 | * stores link information, data etc... It is used for saving and
|
---|
722 | * loading of the widget. Never null.
|
---|
723 | *
|
---|
724 | */
|
---|
725 | protected Item getCurrentRepresentation()
|
---|
726 | {
|
---|
727 | return _textRepresentation;
|
---|
728 | }
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * @return The Expeditee annotation string.
|
---|
732 | */
|
---|
733 | protected String getAnnotationString() {
|
---|
734 |
|
---|
735 | // Create tag and append classname
|
---|
736 | final StringBuilder sb = new StringBuilder(ItemUtils.GetTag(ItemUtils.TAG_IWIDGET));
|
---|
737 | sb.append(':');
|
---|
738 | sb.append(' ');
|
---|
739 | sb.append(getClass().getName());
|
---|
740 |
|
---|
741 | if (_d1Anchoring.getLeftAnchor() != null) {
|
---|
742 | sb.append(" --anchorLeft " + Math.round(_d1Anchoring.getLeftAnchor()));
|
---|
743 | }
|
---|
744 | if (_d3Anchoring.getRightAnchor() != null) {
|
---|
745 | sb.append(" --anchorRight " + Math.round(_d3Anchoring.getRightAnchor()));
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (_d1Anchoring.getTopAnchor() != null) {
|
---|
749 | sb.append(" --anchorTop " + Math.round(_d1Anchoring.getTopAnchor()));
|
---|
750 | }
|
---|
751 |
|
---|
752 | if (_d3Anchoring.getBottomAnchor() != null) {
|
---|
753 | sb.append(" --anchorBottom " + Math.round(_d3Anchoring.getBottomAnchor()));
|
---|
754 | }
|
---|
755 |
|
---|
756 | // Append size information if needed (not an attribute of text items)
|
---|
757 | if (!isFixedSize()) {
|
---|
758 | sb.append(' ');
|
---|
759 | sb.append(getWidth());
|
---|
760 | sb.append(' ');
|
---|
761 | sb.append(getHeight());
|
---|
762 | }
|
---|
763 |
|
---|
764 | // Append arguments if any
|
---|
765 | final String stateArgs = Util.formatArgs(getArgs());
|
---|
766 | if (stateArgs != null) {
|
---|
767 | sb.append(':');
|
---|
768 | sb.append(stateArgs);
|
---|
769 | }
|
---|
770 |
|
---|
771 | return sb.toString();
|
---|
772 | }
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Sets both the new size as well as the new min/max widget/height
|
---|
776 | * restrictions.
|
---|
777 | *
|
---|
778 | * @param minWidth
|
---|
779 | * The min width restriction for the widget. If negative, then
|
---|
780 | * there is no restriction.
|
---|
781 | *
|
---|
782 | * @param maxWidth
|
---|
783 | * The max width restriction for the widget. If negative, then
|
---|
784 | * there is no restriction.
|
---|
785 | *
|
---|
786 | * @param minHeight
|
---|
787 | * The min height restriction for the widget. If negative, then
|
---|
788 | * there is no restriction.
|
---|
789 | *
|
---|
790 | * @param maxHeight
|
---|
791 | * The max height restriction for the widget. If negative, then
|
---|
792 | * there is no restriction.
|
---|
793 | *
|
---|
794 | * @param newWidth
|
---|
795 | * Clamped to new restrictions.
|
---|
796 | *
|
---|
797 | * @param newHeight
|
---|
798 | * Clamped to new restrictions.
|
---|
799 | *
|
---|
800 | * @throws IllegalArgumentException
|
---|
801 | * If maxWidth smaller than minWidth and maxWidth larger or
|
---|
802 | * equal to zero or if maxHeight smaller than minHeight &&
|
---|
803 | * maxHeight is larger or equal to zero
|
---|
804 | *
|
---|
805 | * @see #setSize(float, float)
|
---|
806 | *
|
---|
807 | */
|
---|
808 | public void setSize(final int minWidth, final int maxWidth, final int minHeight, final int maxHeight, final float newWidth, final float newHeight)
|
---|
809 | {
|
---|
810 | setSizeRestrictions(minWidth, maxWidth, minHeight, maxHeight);
|
---|
811 | setSize(newWidth, newHeight);
|
---|
812 | }
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Clamped to current min/max width/height.
|
---|
816 | *
|
---|
817 | * @param width
|
---|
818 | * Clamped to current restrictions.
|
---|
819 | * @param height
|
---|
820 | * Clamped to current restrictions.
|
---|
821 | *
|
---|
822 | * @see #setSize(int, int, int, int, float, float)
|
---|
823 | */
|
---|
824 | public void setSize(float width, float height)
|
---|
825 | {
|
---|
826 | // If 'width' and 'height' exceed the min/max values for width/height
|
---|
827 | // => clamp to the relevant min/max value
|
---|
828 | if (width < _minWidth && _minWidth >= 0) {
|
---|
829 | width = _minWidth;
|
---|
830 | } else if (width > _maxWidth && _maxWidth >= 0) {
|
---|
831 | width = _maxWidth;
|
---|
832 | }
|
---|
833 |
|
---|
834 | if (height < _minHeight && _minHeight >= 0) {
|
---|
835 | height = _minHeight;
|
---|
836 | } else if (height > _maxHeight && _maxHeight >= 0) {
|
---|
837 | height = _maxHeight;
|
---|
838 | }
|
---|
839 |
|
---|
840 | // Remember current isFloating() values
|
---|
841 | final boolean vfloating[] = new boolean[] { _d1.isFloating(), _d2.isFloating(), _d3.isFloating(), _d4.isFloating() };
|
---|
842 |
|
---|
843 | _d1.setFloating(true);
|
---|
844 | _d2.setFloating(true);
|
---|
845 | _d3.setFloating(true);
|
---|
846 | _d4.setFloating(true);
|
---|
847 |
|
---|
848 | final float xr = _d1.getX() + width;
|
---|
849 | final float yb = _d1.getY() + height;
|
---|
850 |
|
---|
851 | _d2.setX(xr);
|
---|
852 | _d3.setX(xr);
|
---|
853 | _d3.setY(yb);
|
---|
854 | _d4.setY(yb);
|
---|
855 |
|
---|
856 | // Restore isFloating() values
|
---|
857 | _d1.setFloating(vfloating[0]);
|
---|
858 | _d2.setFloating(vfloating[1]);
|
---|
859 | _d3.setFloating(vfloating[2]);
|
---|
860 | _d4.setFloating(vfloating[3]);
|
---|
861 |
|
---|
862 | onSizeChanged();
|
---|
863 | }
|
---|
864 |
|
---|
865 | public void setAnchorCorners(final Integer left, final Integer right, final Integer top, final Integer bottom)
|
---|
866 | {
|
---|
867 | setAnchorLeft(left);
|
---|
868 | setAnchorRight(right);
|
---|
869 | setAnchorTop(top);
|
---|
870 | setAnchorBottom(bottom);
|
---|
871 | }
|
---|
872 |
|
---|
873 |
|
---|
874 | public void setPosition(final int x, final int y)
|
---|
875 | {
|
---|
876 | if (x == getX() && y == getY()) {
|
---|
877 | return;
|
---|
878 | }
|
---|
879 |
|
---|
880 | // Remember current isFloating() values
|
---|
881 | final boolean vfloating[] = new boolean[] { _d1.isFloating(), _d2.isFloating(), _d3.isFloating(), _d4.isFloating() };
|
---|
882 |
|
---|
883 | final int width = getWidth();
|
---|
884 | final int height = getHeight();
|
---|
885 |
|
---|
886 | invalidateLink();
|
---|
887 |
|
---|
888 | _d1.setFloating(true);
|
---|
889 | _d2.setFloating(true);
|
---|
890 | _d3.setFloating(true);
|
---|
891 | _d4.setFloating(true);
|
---|
892 |
|
---|
893 | _d1.setPosition(x, y);
|
---|
894 | _d2.setPosition(x + width, y);
|
---|
895 | _d3.setPosition(x + width, y + height);
|
---|
896 | _d4.setPosition(x, y + height);
|
---|
897 |
|
---|
898 | // Restore isFloating() values
|
---|
899 | _d1.setFloating(vfloating[0]);
|
---|
900 | _d2.setFloating(vfloating[1]);
|
---|
901 | _d3.setFloating(vfloating[2]);
|
---|
902 | _d4.setFloating(vfloating[3]);
|
---|
903 |
|
---|
904 | invalidateLink();
|
---|
905 |
|
---|
906 | onMoved();
|
---|
907 |
|
---|
908 | }
|
---|
909 |
|
---|
910 | private boolean _settingPositionFlag = false; // used for recursion
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Updates position of given WidgetCorner to the given (x,y),
|
---|
914 | * and updates related values (connected corners, width and height)
|
---|
915 | *
|
---|
916 | * @param src
|
---|
917 | * @param x
|
---|
918 | * @param y
|
---|
919 | * @return False if need to call super.setPosition
|
---|
920 | */
|
---|
921 | public boolean setPositions(final WidgetCorner src, final float x, final float y)
|
---|
922 | {
|
---|
923 | if (_settingPositionFlag) {
|
---|
924 | return false;
|
---|
925 | }
|
---|
926 |
|
---|
927 | _settingPositionFlag = true;
|
---|
928 |
|
---|
929 | invalidateLink();
|
---|
930 |
|
---|
931 | // Check to see if the widget is fully being picked up
|
---|
932 | final boolean isAllPickedUp = (_d1.isFloating() && _d2.isFloating() && _d3.isFloating() && _d4.isFloating());
|
---|
933 |
|
---|
934 | // If so, then this will be called one by one ..
|
---|
935 | if (isAllPickedUp) {
|
---|
936 | src.setPosition(x, y);
|
---|
937 | } else {
|
---|
938 |
|
---|
939 | float newX = x;
|
---|
940 |
|
---|
941 | // Reference:
|
---|
942 | // D1 D2
|
---|
943 | // D3 D4
|
---|
944 |
|
---|
945 | //
|
---|
946 | // GUIDE:
|
---|
947 | // d1--l1---d2
|
---|
948 | // | |
|
---|
949 | // l4 | X | l2
|
---|
950 | // | |
|
---|
951 | // d4---l3--d3
|
---|
952 | //
|
---|
953 |
|
---|
954 | // X Positions
|
---|
955 | if (src == _d1 || src == _d4) {
|
---|
956 |
|
---|
957 | // Check min X constraint
|
---|
958 | if (_minWidth >= 0) {
|
---|
959 | if ((_d2.getX() - x) < _minWidth) {
|
---|
960 | newX = _d2.getX() - _minWidth;
|
---|
961 | }
|
---|
962 | }
|
---|
963 | // Check max X constraint
|
---|
964 | if (_maxWidth >= 0) {
|
---|
965 | if ((_d2.getX() - x) > _maxWidth) {
|
---|
966 | newX = _d2.getX() - _maxWidth;
|
---|
967 | }
|
---|
968 | }
|
---|
969 |
|
---|
970 | if (!(src == _d4 && _d1.isFloating() && _d4.isFloating())) {
|
---|
971 | _d1.setX(newX);
|
---|
972 | }
|
---|
973 | if (!(src == _d1 && _d4.isFloating() && _d1.isFloating())) {
|
---|
974 | _d4.setX(newX);
|
---|
975 | }
|
---|
976 |
|
---|
977 | } else if (src == _d2 || src == _d3) {
|
---|
978 |
|
---|
979 | // Check min X constraint
|
---|
980 | if (_minWidth >= 0) {
|
---|
981 | if ((x - _d1.getX()) < _minWidth) {
|
---|
982 | newX = _d1.getX() + _minWidth;
|
---|
983 | }
|
---|
984 | }
|
---|
985 | // Check max X constraint
|
---|
986 | if (_maxWidth >= 0) {
|
---|
987 | if ((x - _d1.getX()) > _maxWidth) {
|
---|
988 | newX = _d1.getX() + _maxWidth;
|
---|
989 | }
|
---|
990 | }
|
---|
991 |
|
---|
992 | if (!(src == _d3 && _d2.isFloating() && _d3.isFloating())) {
|
---|
993 | _d2.setX(newX);
|
---|
994 | }
|
---|
995 | if (!(src == _d2 && _d3.isFloating() && _d2.isFloating())) {
|
---|
996 | _d3.setX(newX);
|
---|
997 | }
|
---|
998 | }
|
---|
999 |
|
---|
1000 | float newY = y;
|
---|
1001 |
|
---|
1002 | // Y Positions
|
---|
1003 | if (src == _d1 || src == _d2) {
|
---|
1004 |
|
---|
1005 | // Check min Y constraint
|
---|
1006 | if (_minHeight >= 0) {
|
---|
1007 | if ((_d4.getY() - y) < _minHeight) {
|
---|
1008 | newY = _d4.getY() - _minHeight;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 | // Check max Y constraint
|
---|
1012 | if (_maxHeight >= 0) {
|
---|
1013 | if ((_d4.getY() - y) > _maxHeight) {
|
---|
1014 | newY = _d4.getY() - _maxHeight;
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | if (!(src == _d2 && _d1.isFloating() && _d2.isFloating())) {
|
---|
1019 | _d1.setY(newY);
|
---|
1020 | }
|
---|
1021 | if (!(src == _d1 && _d2.isFloating() && _d1.isFloating())) {
|
---|
1022 | _d2.setY(newY);
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | } else if (src == _d3 || src == _d4) {
|
---|
1026 |
|
---|
1027 | // Check min Y constraint
|
---|
1028 | if (_minHeight >= 0) {
|
---|
1029 | if ((y - _d1.getY()) < _minHeight) {
|
---|
1030 | newY = _d1.getY() + _minHeight;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | // Check max Y constraint
|
---|
1034 | if (_maxHeight >= 0) {
|
---|
1035 | if ((y - _d1.getY()) > _maxHeight) {
|
---|
1036 | newY = _d1.getY() + _maxHeight;
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | if (!(src == _d4 && _d3.isFloating() && _d4.isFloating())) {
|
---|
1041 | _d3.setY(newY);
|
---|
1042 | }
|
---|
1043 | if (!(src == _d3 && _d4.isFloating() && _d3.isFloating())) {
|
---|
1044 | _d4.setY(newY);
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | // Update source text position so position is remembered from loading
|
---|
1050 | final float newTextX = getX();
|
---|
1051 | final float newTextY = getY();
|
---|
1052 | if (_textRepresentation.getX() != newTextX || _textRepresentation.getY() != newTextY) {
|
---|
1053 | _textRepresentation.setPosition(newTextX, newTextY);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | _settingPositionFlag = false;
|
---|
1057 |
|
---|
1058 | invalidateLink();
|
---|
1059 |
|
---|
1060 | onMoved();
|
---|
1061 | onSizeChanged();
|
---|
1062 |
|
---|
1063 | return true;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | public int getX()
|
---|
1067 | {
|
---|
1068 | return Math.min(_d1.getX(), _d2.getX());
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | public int getY()
|
---|
1072 | {
|
---|
1073 | return Math.min(_d1.getY(), _d4.getY());
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | public int getWidth()
|
---|
1077 | {
|
---|
1078 | return Math.abs(_d2.getX() - _d1.getX());
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | public int getHeight()
|
---|
1082 | {
|
---|
1083 | return Math.abs(_d4.getY() - _d1.getY());
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | public Point getPosition()
|
---|
1087 | {
|
---|
1088 | return new Point(getX(), getY());
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | public Dimension getSize()
|
---|
1092 | {
|
---|
1093 | return new Dimension(getWidth(), getHeight());
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /**
|
---|
1097 | * The order of the items in the list is as specified: _d1 _d2 _d3 _d4 _l1
|
---|
1098 | * _l2 _l3 _l4
|
---|
1099 | *
|
---|
1100 | * @return All of the Expeditee items that form the boundaries of this
|
---|
1101 | * widget in an unmodifiable list
|
---|
1102 | */
|
---|
1103 | public List<Item> getItems()
|
---|
1104 | {
|
---|
1105 | return Collections.unmodifiableList(_expediteeItems);
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | public final void onParentStateChanged(final ItemParentStateChangedEvent e)
|
---|
1109 | {
|
---|
1110 | switch (e.getEventType()) {
|
---|
1111 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
|
---|
1112 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
|
---|
1113 | case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
|
---|
1114 | EcosystemManager.removeInteractiveWidget(this);
|
---|
1115 | break;
|
---|
1116 |
|
---|
1117 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
|
---|
1118 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
|
---|
1119 | if (org.expeditee.gui.Browser.DEBUG) {
|
---|
1120 | System.err.println("Widget::onParentStateChanged: Added but not yet shown widget.");
|
---|
1121 | }
|
---|
1122 | break;
|
---|
1123 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
|
---|
1124 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
|
---|
1125 | if (org.expeditee.gui.Browser.DEBUG) {
|
---|
1126 | System.err.println("Widget::onParentStateChanged: Shown widget.");
|
---|
1127 | }
|
---|
1128 | EcosystemManager.addInteractiveWidget(this);
|
---|
1129 | addWidgetContent(e);
|
---|
1130 | break;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | DisplayController.invalidateItem(_d1, getContentBounds());
|
---|
1134 |
|
---|
1135 | // Forward filtered event to upper classes...
|
---|
1136 | onParentStateChanged(e.getEventType());
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /**
|
---|
1140 | * Subclassing Widgets overwrite to add their specific content to the Frame.
|
---|
1141 | * For example, a widget being Java Swing must add their Swing component to Expeditee's content pane.
|
---|
1142 | * @param e Can be used to identify if it is appropriate to draw the widget.
|
---|
1143 | */
|
---|
1144 | abstract protected void addWidgetContent(final ItemParentStateChangedEvent e);
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Subclassing Widgets overwrite to specify their own functionality as to how key events
|
---|
1148 | * are to be forwarded to Expeditee.
|
---|
1149 | */
|
---|
1150 | abstract protected void addKeyListenerToWidget();
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Subclassing widgets overwrite to specify how content will respond to being added and removed.
|
---|
1154 | */
|
---|
1155 | abstract protected void addThisAsContainerListenerToContent();
|
---|
1156 |
|
---|
1157 | /**
|
---|
1158 | * Subclassing widgets overwrite to provide the size and position of their content.
|
---|
1159 | * For example, a widget using Java Swing might return _swingComponent.getBounds().
|
---|
1160 | * @return The bounds of the content that is being drawn.
|
---|
1161 | */
|
---|
1162 | abstract public AxisAlignedBoxBounds getContentBounds();
|
---|
1163 |
|
---|
1164 | /**
|
---|
1165 | * Due to absolute positioning...
|
---|
1166 | * @param parent
|
---|
1167 | */
|
---|
1168 | abstract protected void layout();
|
---|
1169 |
|
---|
1170 | /**
|
---|
1171 | * Subclassing widgets overwrite to respond to changes in widget bounds.
|
---|
1172 | */
|
---|
1173 | abstract protected void onBoundsChanged();
|
---|
1174 |
|
---|
1175 | /**
|
---|
1176 | * Override to make use of. Internally this is reported once by all corners,
|
---|
1177 | * but is filtered out so that this method is invoked once per event.
|
---|
1178 | *
|
---|
1179 | * @param eventType
|
---|
1180 | * The {@link ItemParentStateChangedEvent#getEventType()} that
|
---|
1181 | * occured.
|
---|
1182 | *
|
---|
1183 | */
|
---|
1184 | protected void onParentStateChanged(final int eventType)
|
---|
1185 | {
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | /**
|
---|
1189 | * @return True if at least one corner is floating
|
---|
1190 | */
|
---|
1191 | public boolean isFloating()
|
---|
1192 | {
|
---|
1193 | return _d1.isFloating() || _d2.isFloating() || _d3.isFloating() || _d4.isFloating();
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | public boolean areCornersFullyAnchored()
|
---|
1197 | {
|
---|
1198 | return _d1.getParent() != null && _d2.getParent() != null && _d3.getParent() != null && _d4.getParent() != null;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | /**
|
---|
1202 | *
|
---|
1203 | * @return The current bounds for this widget. Never null.
|
---|
1204 | */
|
---|
1205 | public AxisAlignedBoxBounds getBounds()
|
---|
1206 | {
|
---|
1207 | return getContentBounds();
|
---|
1208 | //return new AxisAlignedBoxBounds(getX(), getY(), getWidth(), getHeight());
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | protected void paintLink()
|
---|
1212 | {
|
---|
1213 | // If this widget is linked .. then draw the link icon
|
---|
1214 | if (_textRepresentation.getLink() != null || _textRepresentation.hasAction()) {
|
---|
1215 | _textRepresentation.paintLinkGraphic(getLinkX(), getLinkY());
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | private int getLinkX()
|
---|
1221 | {
|
---|
1222 | return getX() - Item.LEFT_MARGIN;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | private int getLinkY()
|
---|
1226 | {
|
---|
1227 | return getY() + (getHeight() / 2);
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | /** Invoked whenever the widget is to be repainted in free space. */
|
---|
1231 | protected void paintInFreeSpace()
|
---|
1232 | {
|
---|
1233 | final GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
1234 | g.drawRectangle(new Point(getX(), getY()), new Dimension(getWidth(), getHeight()), 0.0, new Fill(FREESPACE_BACKCOLOR), null, null, null);
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | public final void paint()
|
---|
1238 | {
|
---|
1239 | paintWidget();
|
---|
1240 | paintLink();
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /** Should be overridden by native widgets to draw themselves. */
|
---|
1244 | protected abstract void paintWidget();
|
---|
1245 |
|
---|
1246 | /**
|
---|
1247 | * Called from the widgets corners: Whenever one of the corners are invoked
|
---|
1248 | * for a refill of the enclosed area.
|
---|
1249 | *
|
---|
1250 | * If the widget is floating (e.g. currently picked up / rubberbanding) then
|
---|
1251 | * a shaded area is drawn instead of the actual widget so the manipulation
|
---|
1252 | * of the widget is as smooth as possible.
|
---|
1253 | *
|
---|
1254 | * @param g
|
---|
1255 | * @param notifier
|
---|
1256 | */
|
---|
1257 | void paintFill()
|
---|
1258 | {
|
---|
1259 | //if (_swingComponent.getParent() == null) {
|
---|
1260 | // Note that frames with @f may want to paint the widgets so do not
|
---|
1261 | // paint over the widget interface in these cases: must only
|
---|
1262 | // paint if an object is floating
|
---|
1263 | if (isFloating()) {
|
---|
1264 | paintInFreeSpace();
|
---|
1265 | paintLink();
|
---|
1266 | }
|
---|
1267 | //}
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | /**
|
---|
1271 | * @return True if this widget cannot be resized in either directions
|
---|
1272 | */
|
---|
1273 | public boolean isFixedSize()
|
---|
1274 | {
|
---|
1275 | return this._minHeight == this._maxHeight &&
|
---|
1276 | this._minWidth == this._maxWidth &&
|
---|
1277 | this._minHeight >= 0 &&
|
---|
1278 | this._minWidth >= 0;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | /**
|
---|
1282 | * Removes this widget from the parent frame or free space.
|
---|
1283 | *
|
---|
1284 | * @return True if removed from a parent frame. Thus a parent changed event
|
---|
1285 | * will be invoked.
|
---|
1286 | *
|
---|
1287 | * False if removed purely from free space.
|
---|
1288 | */
|
---|
1289 | protected boolean removeSelf()
|
---|
1290 | {
|
---|
1291 | final Frame parent = getParentFrame();
|
---|
1292 |
|
---|
1293 | if (parent != null) {
|
---|
1294 | parent.removeAllItems(_expediteeItems);
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | FreeItems.getInstance().removeAll(_expediteeItems);
|
---|
1298 |
|
---|
1299 | return (parent != null);
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | /**
|
---|
1303 | * @return The parent frame. Null if has none. Note: Based on corners
|
---|
1304 | * parents.
|
---|
1305 | */
|
---|
1306 | public Frame getParentFrame()
|
---|
1307 | {
|
---|
1308 | Frame parent = null;
|
---|
1309 | if (_d1.getParent() != null) {
|
---|
1310 | parent = _d1.getParent();
|
---|
1311 | } else if (_d2.getParent() != null) {
|
---|
1312 | parent = _d2.getParent();
|
---|
1313 | } else if (_d3.getParent() != null) {
|
---|
1314 | parent = _d3.getParent();
|
---|
1315 | } else if (_d4.getParent() != null) {
|
---|
1316 | parent = _d4.getParent();
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | return parent;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | protected void invalidateSelf()
|
---|
1323 | {
|
---|
1324 | final AxisAlignedBoxBounds dirty = new AxisAlignedBoxBounds(getX(), getY(), getWidth(), getHeight());
|
---|
1325 | DisplayController.invalidateArea(dirty);
|
---|
1326 | invalidateLink();
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /**
|
---|
1330 | * Invalidates the link for this widget - if it has one.
|
---|
1331 | */
|
---|
1332 | protected void invalidateLink()
|
---|
1333 | {
|
---|
1334 | if (_textRepresentation.getLink() != null || _textRepresentation.hasAction()) {
|
---|
1335 | final AxisAlignedBoxBounds linkArea = _textRepresentation.getLinkDrawArea(getLinkX(), getLinkY());
|
---|
1336 | DisplayController.invalidateArea(linkArea);
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /**
|
---|
1341 | * @see ItemUtils#isVisible(Item)
|
---|
1342 | *
|
---|
1343 | * @return True if this widget is visible from the current frame. Considers
|
---|
1344 | * overlays and vectors.
|
---|
1345 | *
|
---|
1346 | */
|
---|
1347 | public boolean isVisible()
|
---|
1348 | {
|
---|
1349 | return ItemUtils.isVisible(_d1);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /**
|
---|
1353 | * Invoked whenever the widget have moved. Can override.
|
---|
1354 | *
|
---|
1355 | */
|
---|
1356 | protected abstract void onMoved();
|
---|
1357 |
|
---|
1358 | /**
|
---|
1359 | * Invoked whenever the widget have moved. Can override.
|
---|
1360 | *
|
---|
1361 | */
|
---|
1362 | protected abstract void onSizeChanged();
|
---|
1363 |
|
---|
1364 | /**
|
---|
1365 | * Override to have a custom min border thickness for your widget.
|
---|
1366 | *
|
---|
1367 | * @see #DEFAULT_MINIMUM_BORDER_THICKNESS
|
---|
1368 | *
|
---|
1369 | * @return The minimum border thickness. Should be larger or equal to zero.
|
---|
1370 | *
|
---|
1371 | */
|
---|
1372 | public float getMinimumBorderThickness()
|
---|
1373 | {
|
---|
1374 | return DEFAULT_MINIMUM_BORDER_THICKNESS;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | /**
|
---|
1378 | * Looks fors a dataline in the current representation of the widget.
|
---|
1379 | *
|
---|
1380 | * @see #getCurrentRepresentation
|
---|
1381 | * @see #getStrippedDataInt(String, int)
|
---|
1382 | * @see #getStrippedDataLong(String, long)
|
---|
1383 | *
|
---|
1384 | * @param tag
|
---|
1385 | * The prefix of a dataline that will be matched. Must be larger
|
---|
1386 | * than zero and not null.
|
---|
1387 | *
|
---|
1388 | * @return The <i>first</i> dataline that matched the prefix - without the
|
---|
1389 | * prefix. Null if their was no data that matched the given prefix.
|
---|
1390 | *
|
---|
1391 | * @throws IllegalArgumentException
|
---|
1392 | * If tag is null.
|
---|
1393 | *
|
---|
1394 | * @throws NullPointerException
|
---|
1395 | * If tag is empty.
|
---|
1396 | */
|
---|
1397 | protected String getStrippedDataString(final String tag)
|
---|
1398 | {
|
---|
1399 | if (tag == null) {
|
---|
1400 | throw new NullPointerException("tag");
|
---|
1401 | }
|
---|
1402 | if (tag.length() == 0) {
|
---|
1403 | throw new IllegalArgumentException("tag is empty");
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 | if (getCurrentRepresentation().getData() != null) {
|
---|
1407 | for (final String str : getCurrentRepresentation().getData()) {
|
---|
1408 | if (str != null && str.startsWith(tag) && str.length() > tag.length()) {
|
---|
1409 | return str.substring(tag.length());
|
---|
1410 | }
|
---|
1411 | }
|
---|
1412 | }
|
---|
1413 | return null;
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /**
|
---|
1417 | * Looks fors a dataline in the current representation of the widget.
|
---|
1418 | *
|
---|
1419 | * @see #getCurrentRepresentation
|
---|
1420 | * @see #getStrippedDataString(String)
|
---|
1421 | * @see #getStrippedDataLong(String, long)
|
---|
1422 | *
|
---|
1423 | * @param tag
|
---|
1424 | * The prefix of a dataline that will be matched. Must be larger
|
---|
1425 | * the zero and not null.
|
---|
1426 | *
|
---|
1427 | * @param defaultValue
|
---|
1428 | * The default value if the tag does not exist or contains
|
---|
1429 | * invalid data.
|
---|
1430 | *
|
---|
1431 | * @return The <i>first</i> dataline that matched the prefix - parsed as an
|
---|
1432 | * int (after the prefix). defaultValue if their was no data that
|
---|
1433 | * matched the given prefix or the data was invalid.
|
---|
1434 | *
|
---|
1435 | * @throws IllegalArgumentException
|
---|
1436 | * If tag is null.
|
---|
1437 | *
|
---|
1438 | * @throws NullPointerException
|
---|
1439 | * If tag is empty.
|
---|
1440 | *
|
---|
1441 | */
|
---|
1442 | protected Integer getStrippedDataInt(final String tag, final Integer defaultValue)
|
---|
1443 | {
|
---|
1444 | String strippedStr = getStrippedDataString(tag);
|
---|
1445 |
|
---|
1446 | if (strippedStr != null) {
|
---|
1447 | strippedStr = strippedStr.trim();
|
---|
1448 | if (strippedStr.length() > 0) {
|
---|
1449 | try {
|
---|
1450 | return Integer.parseInt(strippedStr);
|
---|
1451 | } catch (final NumberFormatException e) { /* Consume */
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | return defaultValue;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 |
|
---|
1460 |
|
---|
1461 | /**
|
---|
1462 | * Looks for a dataline in the current representation of the widget.
|
---|
1463 | *
|
---|
1464 | * @see #getCurrentRepresentation
|
---|
1465 | * @see #getStrippedDataString(String)
|
---|
1466 | * @see #getStrippedDataInt(String, int)
|
---|
1467 | *
|
---|
1468 | * @param tag
|
---|
1469 | * The prefix of a dataline that will be matched. Must be larger
|
---|
1470 | * the zero and not null.
|
---|
1471 | *
|
---|
1472 | * @param defaultValue
|
---|
1473 | * The default value if the tag does not exist or contains
|
---|
1474 | * invalid data.
|
---|
1475 | *
|
---|
1476 | * @return The <i>first</i> dataline that matched the prefix - parsed as a
|
---|
1477 | * long (after the prefix). defaultValue if their was no data that
|
---|
1478 | * matched the given prefix or the data was invalid.
|
---|
1479 | *
|
---|
1480 | * @throws IllegalArgumentException
|
---|
1481 | * If tag is null.
|
---|
1482 | *
|
---|
1483 | * @throws NullPointerException
|
---|
1484 | * If tag is empty.
|
---|
1485 | *
|
---|
1486 | */
|
---|
1487 | protected Long getStrippedDataLong(final String tag, final Long defaultValue)
|
---|
1488 | {
|
---|
1489 | String strippedStr = getStrippedDataString(tag);
|
---|
1490 |
|
---|
1491 | if (strippedStr != null) {
|
---|
1492 | strippedStr = strippedStr.trim();
|
---|
1493 | if (strippedStr.length() > 0) {
|
---|
1494 | try {
|
---|
1495 | return Long.parseLong(strippedStr);
|
---|
1496 | } catch (final NumberFormatException e) { /* Consume */
|
---|
1497 | }
|
---|
1498 | }
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | return defaultValue;
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | /**
|
---|
1505 | * Looks for a data-line in the current representation of the widget.
|
---|
1506 | *
|
---|
1507 | * @see #getCurrentRepresentation
|
---|
1508 | * @see #getStrippedDataString(String)
|
---|
1509 | * @see #getStrippedDataLong(String, long)
|
---|
1510 | *
|
---|
1511 | * @param tag
|
---|
1512 | * The prefix of a data-line that will be matched. Must be larger
|
---|
1513 | * the zero and not null.
|
---|
1514 | *
|
---|
1515 | * @param defaultValue
|
---|
1516 | * The default value if the tag does not exist or contains
|
---|
1517 | * invalid data.
|
---|
1518 | *
|
---|
1519 | * @return The <i>first</i> data-line that matched the prefix - if
|
---|
1520 | * case insensitive match is 'true' then return true, otherwise
|
---|
1521 | * false. defaultValue if their was no data that
|
---|
1522 | * matched the given prefix or the data was invalid.
|
---|
1523 | *
|
---|
1524 | * @throws IllegalArgumentException
|
---|
1525 | * If tag is null.
|
---|
1526 | *
|
---|
1527 | * @throws NullPointerException
|
---|
1528 | * If tag is empty.
|
---|
1529 | *
|
---|
1530 | */
|
---|
1531 | protected Boolean getStrippedDataBool(final String tag, final Boolean defaultValue) {
|
---|
1532 |
|
---|
1533 | String strippedStr = getStrippedDataString(tag);
|
---|
1534 |
|
---|
1535 | if (strippedStr != null) {
|
---|
1536 | strippedStr = strippedStr.trim();
|
---|
1537 | if (strippedStr.length() > 0) {
|
---|
1538 | return strippedStr.equalsIgnoreCase("true") ? true : false;
|
---|
1539 | }
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | return defaultValue;
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 |
|
---|
1546 | /**
|
---|
1547 | * All data is removed that is prefixed with the given tag.
|
---|
1548 | *
|
---|
1549 | * @param tag
|
---|
1550 | * The prefix of the data lines to remove. Must be larger the
|
---|
1551 | * zero and not null.
|
---|
1552 | *
|
---|
1553 | * @throws IllegalArgumentException
|
---|
1554 | * If tag is null.
|
---|
1555 | *
|
---|
1556 | * @throws NullPointerException
|
---|
1557 | * If tag is empty.
|
---|
1558 | *
|
---|
1559 | */
|
---|
1560 | protected void removeData(final String tag)
|
---|
1561 | {
|
---|
1562 | updateData(tag, null);
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | // TODO: Ambiguous name. Refactor. cts16
|
---|
1566 | protected void addDataIfCaseInsensitiveNotExists(String tag)
|
---|
1567 | {
|
---|
1568 | if (tag == null) {
|
---|
1569 | throw new NullPointerException("tag");
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | List<String> data = getCurrentRepresentation().getData();
|
---|
1573 |
|
---|
1574 | if (data == null) {
|
---|
1575 | data = new LinkedList<String>();
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 | for (final String s : data) {
|
---|
1579 | if (s != null && s.equalsIgnoreCase(tag)) {
|
---|
1580 | return;
|
---|
1581 | }
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | data.add(tag);
|
---|
1585 | getCurrentRepresentation().setData(data);
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 |
|
---|
1589 | /**
|
---|
1590 | * Updates the data with a given tag. All data is removed that is prefixed
|
---|
1591 | * with the given tag. Then a new line is added (if not null).
|
---|
1592 | *
|
---|
1593 | * Note that passing newData with null is the equivelant of removing tag
|
---|
1594 | * lines.
|
---|
1595 | *
|
---|
1596 | * @param tag
|
---|
1597 | * The prefix of the data lines to remove. Must be larger the
|
---|
1598 | * zero and not null.
|
---|
1599 | *
|
---|
1600 | * @param newData
|
---|
1601 | * The new line to add. Can be null - for not adding anything.
|
---|
1602 | *
|
---|
1603 | * @throws IllegalArgumentException
|
---|
1604 | * If tag is null.
|
---|
1605 | *
|
---|
1606 | * @throws NullPointerException
|
---|
1607 | * If tag is empty.
|
---|
1608 | *
|
---|
1609 | * @see #removeData(String)
|
---|
1610 | *
|
---|
1611 | */
|
---|
1612 | protected void updateData(final String tag, final String newData)
|
---|
1613 | {
|
---|
1614 | if (tag == null) {
|
---|
1615 | throw new NullPointerException("tag");
|
---|
1616 | } else if (tag.length() == 0) {
|
---|
1617 | throw new IllegalArgumentException("tag is empty");
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | // Get current data
|
---|
1621 | List<String> data = getCurrentRepresentation().getData();
|
---|
1622 |
|
---|
1623 | if (data != null) {
|
---|
1624 | for (int i = 0; i < data.size(); i++) {
|
---|
1625 | final String str = data.get(i);
|
---|
1626 | if (str != null && str.startsWith(tag)) {
|
---|
1627 | data.remove(i);
|
---|
1628 | }
|
---|
1629 | }
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | if (newData != null) {
|
---|
1633 | if (data != null) {
|
---|
1634 | data.add(newData);
|
---|
1635 | } else {
|
---|
1636 | data = new LinkedList<String>();
|
---|
1637 | data.add(newData);
|
---|
1638 | getCurrentRepresentation().setData(data);
|
---|
1639 |
|
---|
1640 | }
|
---|
1641 | }
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | public boolean containsData(final String str) {
|
---|
1645 | assert(str != null);
|
---|
1646 | if (getCurrentRepresentation().getData() != null) {
|
---|
1647 | return getCurrentRepresentation().getData().contains(str);
|
---|
1648 | }
|
---|
1649 | return false;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | public boolean containsDataTrimmedIgnoreCase(final String str) {
|
---|
1653 | assert(str != null);
|
---|
1654 | if (getCurrentRepresentation().getData() != null) {
|
---|
1655 | for (final String data : getCurrentRepresentation().getData()) {
|
---|
1656 | if (data != null && data.trim().equalsIgnoreCase(str)) {
|
---|
1657 | return true;
|
---|
1658 | }
|
---|
1659 | }
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | return false;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | /**
|
---|
1666 | * Sets the link for this widget.
|
---|
1667 | *
|
---|
1668 | * @param link
|
---|
1669 | * The new frame link. Can be null (for no link)
|
---|
1670 | *
|
---|
1671 | * @param linker
|
---|
1672 | * The text item creating the link. Null if not created from
|
---|
1673 | * a text item.
|
---|
1674 | */
|
---|
1675 | public void setLink(final String link, final Text linker)
|
---|
1676 | {
|
---|
1677 | // Make sure the link is redrawn when a link is added
|
---|
1678 | if (link == null) {
|
---|
1679 | invalidateLink();
|
---|
1680 | }
|
---|
1681 | getSource().setLink(link);
|
---|
1682 | if (link != null) {
|
---|
1683 | invalidateLink();
|
---|
1684 | }
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | public void setBackgroundColor(final Colour c)
|
---|
1688 | {
|
---|
1689 | getSource().setBackgroundColor(c);
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | /**
|
---|
1693 | * @return The link for this widget. Null if none.
|
---|
1694 | */
|
---|
1695 | public String getLink()
|
---|
1696 | {
|
---|
1697 | return _textRepresentation.getLink();
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | /**
|
---|
1701 | * <b>Note:</b> That if the widget has no parent (e.g. the widget is a
|
---|
1702 | * free-item) then the absolute link returned will be for the frameset of
|
---|
1703 | * the current frame.
|
---|
1704 | *
|
---|
1705 | * @return The absolute link for this item. Null if there is no link, or if
|
---|
1706 | * there is no parent for this widget and the current frame is
|
---|
1707 | * unavailable.
|
---|
1708 | *
|
---|
1709 | */
|
---|
1710 | public String getAbsoluteLink()
|
---|
1711 | {
|
---|
1712 | // Note: cannot return the source absolute link since it does not have
|
---|
1713 | // a parent ... thus must manually format link
|
---|
1714 |
|
---|
1715 | final String link = getLink();
|
---|
1716 |
|
---|
1717 | if (link == null || link.length() == 0) {
|
---|
1718 | return null;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | if (FrameIO.isPositiveInteger(link)) { // relative - convert to
|
---|
1722 | // absolute
|
---|
1723 |
|
---|
1724 | // Get the frameset of this item
|
---|
1725 | Frame parent = getParentFrame();
|
---|
1726 | if (parent == null) {
|
---|
1727 | parent = DisplayController.getCurrentFrame();
|
---|
1728 | }
|
---|
1729 | if (parent == null) {
|
---|
1730 | return null;
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | final String framesetName = parent.getFramesetName();
|
---|
1734 |
|
---|
1735 | if (framesetName == null) {
|
---|
1736 | return null;
|
---|
1737 | }
|
---|
1738 |
|
---|
1739 | return framesetName + link;
|
---|
1740 |
|
---|
1741 | } else if (FrameIO.isValidFrameName(link)) { // already absolute
|
---|
1742 | return link;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | return null;
|
---|
1746 | }
|
---|
1747 |
|
---|
1748 | /**
|
---|
1749 | * Sets the border color for the widget.
|
---|
1750 | * That is, for the source (so it is remembered) and also for all the
|
---|
1751 | * corners/edges.
|
---|
1752 | *
|
---|
1753 | * @param c
|
---|
1754 | * The color to set.
|
---|
1755 | */
|
---|
1756 | public void setWidgetEdgeColor(final Colour c)
|
---|
1757 | {
|
---|
1758 | // Indirectly invokes setSourceBorderColor accordingly
|
---|
1759 | for (final Item i : _expediteeItems) {
|
---|
1760 | i.setColor(c);
|
---|
1761 | }
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | /**
|
---|
1765 | * Sets the thickness of the widget edge.
|
---|
1766 | *
|
---|
1767 | * @see Item#setThickness(float)
|
---|
1768 | *
|
---|
1769 | * @param thickness
|
---|
1770 | * The new thickness to set.
|
---|
1771 | */
|
---|
1772 | public void setWidgetEdgeThickness(final float thickness) {
|
---|
1773 | _l1.setThickness(thickness, true);
|
---|
1774 | //for (Item i : _expediteeItems) i.setThickness(thickness);
|
---|
1775 | // Above indirectly invokes setSourceThickness accordingly
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | /**
|
---|
1779 | * Override to dis-allow widget thickness manipulation from the user.
|
---|
1780 | * @return
|
---|
1781 | */
|
---|
1782 | public boolean isWidgetEdgeThicknessAdjustable() {
|
---|
1783 | return true;
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | // TODO: Maybe rename setSource* .. to update* ... These should actually be friendly!
|
---|
1787 | public void setSourceColor(final Colour c) {
|
---|
1788 | _textRepresentation.setColor(c);
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | public void setSourceBorderColor(final Colour c) {
|
---|
1792 | _textRepresentation.setBorderColor(c);
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | public void setSourceFillColor(final Colour c) {
|
---|
1796 | _textRepresentation.setFillColor(c);
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | public void setSourceThickness(final float newThickness, final boolean setConnected) {
|
---|
1800 | _textRepresentation.setThickness(newThickness, setConnected);
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | public void setSourceData(final List<String> data) {
|
---|
1804 | _textRepresentation.setData(data);
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | protected Point getOrigin() {
|
---|
1808 | return _d1.getPosition(); // NOTE FROM BROOK: This flips around ... the origin can be any point
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | protected Item getFirstCorner() {
|
---|
1812 | return _d1;
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | public void setAnchorLeft(final Integer anchor)
|
---|
1816 | {
|
---|
1817 | _d1Anchoring.setLeftAnchor(anchor);
|
---|
1818 | // Anchor left-edge corners (dots) as well
|
---|
1819 | _d1.setAnchorCornerX(anchor,null);
|
---|
1820 | _d4.setAnchorCornerX(anchor,null);
|
---|
1821 |
|
---|
1822 | if (anchor != null) {
|
---|
1823 | setPositions(_d1, anchor, _d1.getY());
|
---|
1824 | invalidateSelf();
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | // Move X-rayable item as well
|
---|
1828 | getCurrentRepresentation().setAnchorLeft(anchor);
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | public void setAnchorRight(final Integer anchor) {
|
---|
1832 | _d3Anchoring.setRightAnchor(anchor);
|
---|
1833 | // Anchor right-edge corners (dots) as well
|
---|
1834 | _d2.setAnchorCornerX(null,anchor); // right
|
---|
1835 | _d3.setAnchorCornerX(null,anchor); // right
|
---|
1836 |
|
---|
1837 | if (anchor != null) {
|
---|
1838 | setPositions(_d2, DisplayController.getFramePaintAreaWidth() - anchor, _d2.getY());
|
---|
1839 | invalidateSelf();
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 | if (_d1Anchoring.getLeftAnchor() == null) {
|
---|
1843 | // Prefer having the X-rayable item at anchorLeft position (if defined) over moving to anchorRight
|
---|
1844 | getCurrentRepresentation().setAnchorRight(anchor);
|
---|
1845 | }
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | public void setAnchorTop(final Integer anchor) {
|
---|
1849 | _d1Anchoring.setTopAnchor(anchor);
|
---|
1850 | // Anchor top-edge corners (dots) as well
|
---|
1851 | _d1.setAnchorCornerY(anchor,null);
|
---|
1852 | _d2.setAnchorCornerY(anchor,null);
|
---|
1853 |
|
---|
1854 | if (anchor != null) {
|
---|
1855 | setPositions(_d2, _d2.getX(), anchor);
|
---|
1856 | invalidateSelf();
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | // Move X-rayable item as well
|
---|
1860 | getCurrentRepresentation().setAnchorTop(anchor);
|
---|
1861 | }
|
---|
1862 |
|
---|
1863 | public void setAnchorBottom(final Integer anchor) {
|
---|
1864 | _d3Anchoring.setBottomAnchor(anchor);
|
---|
1865 | // Anchor bottom-edge corners (dots) as well
|
---|
1866 | _d3.setAnchorCornerY(null,anchor);
|
---|
1867 | _d4.setAnchorCornerY(null,anchor);
|
---|
1868 |
|
---|
1869 | if (anchor != null) {
|
---|
1870 | setPositions(_d3, _d3.getX(), DisplayController.getFramePaintAreaHeight() - anchor);
|
---|
1871 | invalidateSelf();
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | if (_d1Anchoring.getTopAnchor() == null) {
|
---|
1875 | // Prefer having the X-rayable item at anchorTop position (if defined) over moving to anchorBottom
|
---|
1876 | getCurrentRepresentation().setAnchorBottom(anchor);
|
---|
1877 | }
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 |
|
---|
1881 | public boolean isAnchored() {
|
---|
1882 | return (isAnchoredX()) || (isAnchoredY());
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | public boolean isAnchoredX() {
|
---|
1886 | return _d1Anchoring.isXAnchored() || _d3Anchoring.isXAnchored();
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | public boolean isAnchoredY() {
|
---|
1890 | return _d1Anchoring.isYAnchored() || _d3Anchoring.isYAnchored();
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | /**
|
---|
1894 | * Call from expeditee for representing the name of the item.
|
---|
1895 | * Override to return custom name.
|
---|
1896 | *
|
---|
1897 | * Note: Used for the new frame title when creating links for widgets.
|
---|
1898 | *
|
---|
1899 | * @return
|
---|
1900 | * The name representing this widget
|
---|
1901 | */
|
---|
1902 | public String getName() {
|
---|
1903 | return this.toString();
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | /**
|
---|
1907 | * Event called when the widget is left clicked while there are items attached to the FreeItems buffer.
|
---|
1908 | * Used to enable expeditee like text-widget interaction for left mouse clicks.
|
---|
1909 | * @return true if event was handled (no pass through), otherwise false.
|
---|
1910 | */
|
---|
1911 | public boolean ItemsLeftClickDropped() {
|
---|
1912 | return false;
|
---|
1913 | }
|
---|
1914 |
|
---|
1915 | /**
|
---|
1916 | * Event called when the widget is middle clicked while there are items attached to the FreeItems buffer.
|
---|
1917 | * Used to enable expeditee like text-widget interaction for middle mouse clicks.
|
---|
1918 | * @return true if event was handled (no pass through), otherwise false.
|
---|
1919 | */
|
---|
1920 | public boolean ItemsMiddleClickDropped() {
|
---|
1921 | return false;
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | /**
|
---|
1925 | * Event called when the widget is left clicked while there are items attached to the FreeItems buffer.
|
---|
1926 | * Used to enable expeditee like text-widget interaction for right mouse clicks
|
---|
1927 | * @return true if event was handled (no pass through), otherwise false.
|
---|
1928 | */
|
---|
1929 | public boolean ItemsRightClickDropped() {
|
---|
1930 | return false;
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | /** Gets the clip area for this widget. */
|
---|
1934 | public Clip getClip()
|
---|
1935 | {
|
---|
1936 | final Clip clip = new Clip(getContentBounds());
|
---|
1937 | return clip;
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | public void onResized() {
|
---|
1941 | invalidateSelf();
|
---|
1942 | onBoundsChanged();
|
---|
1943 | layout();
|
---|
1944 | }
|
---|
1945 | }
|
---|