use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Dialog method showPopupDialog.
/**
* A popup dialog is shown with the context of a component and its selection, it is disposed seamlessly if the back button is pressed
* or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
* dialog has the PopupDialog style by default.
*
* @param rect the screen rectangle to which the popup should point
* @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
*/
public Command showPopupDialog(Rectangle rect) {
if (getDialogUIID().equals("Dialog")) {
setDialogUIID("PopupDialog");
if (getTitleComponent().getUIID().equals("DialogTitle")) {
getTitleComponent().setUIID("PopupDialogTitle");
}
getContentPane().setUIID("PopupContentPane");
}
disposeOnRotation = true;
disposeWhenPointerOutOfBounds = true;
Command backCommand = null;
if (getBackCommand() == null) {
backCommand = new Command("Back");
setBackCommand(backCommand);
}
Component contentPane = super.getContentPane();
Label title = super.getTitleComponent();
int menuHeight = calcMenuHeight();
UIManager manager = getUIManager();
// preferred size logic of the dialog won't work with large title borders
if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
boolean b = getTitle().length() > 0;
getTitleArea().setVisible(b);
getTitleComponent().setVisible(b);
if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
getTitleComponent().setPreferredSize(new Dimension(0, 0));
getTitleComponent().getStyle().setBorder(null);
getTitleArea().setPreferredSize(new Dimension(0, 0));
if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
int titlePT = getTitleComponent().getStyle().getPaddingTop();
byte[] pu = getContentPane().getStyle().getPaddingUnit();
if (pu == null) {
pu = new byte[4];
}
pu[0] = Style.UNIT_TYPE_PIXELS;
getContentPane().getStyle().setPaddingUnit(pu);
int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
}
}
}
// allows a text area to recalculate its preferred size if embedded within a dialog
revalidate();
Style contentPaneStyle = getDialogStyle();
boolean restoreArrow = false;
if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
Border border = contentPaneStyle.getBorder();
if (border != null) {
border.setImageBorderSpecialTile(t, b, l, r, rect);
restoreArrow = true;
}
}
int prefHeight = contentPane.getPreferredH();
int prefWidth = contentPane.getPreferredW();
if (contentPaneStyle.getBorder() != null) {
prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
}
prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
int availableWidth = Display.getInstance().getDisplayWidth();
int width = Math.min(availableWidth, prefWidth);
int x = 0;
int y = 0;
Command result;
boolean showPortrait;
if (popupDirectionBiasPortrait != null) {
showPortrait = popupDirectionBiasPortrait.booleanValue();
} else {
showPortrait = Display.getInstance().isPortrait();
}
// if we don't have enough space then disregard device orientation
if (showPortrait) {
if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
showPortrait = false;
}
} else {
if (availableHeight / 2 > availableWidth - rect.getWidth()) {
showPortrait = true;
}
}
if (showPortrait) {
if (width < availableWidth) {
int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
// if the ideal position is less than 0 just use 0
if (idealX > 0) {
// if the idealX is too far to the right just align to the right
if (idealX + width > availableWidth) {
x = availableWidth - width;
} else {
x = idealX;
}
}
}
if (rect.getY() < availableHeight / 2) {
// popup downwards
y = rect.getY() + rect.getSize().getHeight();
int height = Math.min(prefHeight, availableHeight - y);
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
} else {
// popup upwards
int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
y = rect.getY() - height;
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
}
} else {
int height = Math.min(prefHeight, availableHeight);
if (height < availableHeight) {
int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
// if the ideal position is less than 0 just use 0
if (idealY > 0) {
// if the idealY is too far up just align to the top
if (idealY + height > availableHeight) {
y = availableHeight - height;
} else {
y = idealY;
}
}
}
if (prefWidth > rect.getX()) {
// popup right
x = rect.getX() + rect.getSize().getWidth();
if (x + prefWidth > availableWidth) {
x = availableWidth - prefWidth;
}
width = Math.min(prefWidth, availableWidth - x);
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
} else {
// popup left
width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
x = rect.getX() - width;
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
}
}
if (restoreArrow) {
contentPaneStyle.getBorder().clearImageBorderSpecialTile();
}
if (result == backCommand) {
return null;
}
return result;
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Display method setCurrent.
/**
* Displays the given Form on the screen.
*
* @param newForm the Form to Display
*/
void setCurrent(final Form newForm, boolean reverse) {
if (edt == null) {
throw new IllegalStateException("Initialize must be invoked before setCurrent!");
}
Form current = impl.getCurrentForm();
if (autoFoldVKBOnFormSwitch && !(newForm instanceof Dialog)) {
setShowVirtualKeyboard(false);
}
if (current == newForm) {
current.revalidate();
current.repaint();
current.onShowCompletedImpl();
return;
}
if (impl.isEditingText()) {
switch(showDuringEdit) {
case SHOW_DURING_EDIT_ALLOW_DISCARD:
break;
case SHOW_DURING_EDIT_ALLOW_SAVE:
impl.saveTextEditingState();
break;
case SHOW_DURING_EDIT_EXCEPTION:
throw new IllegalStateException("Show during edit");
case SHOW_DURING_EDIT_IGNORE:
return;
case SHOW_DURING_EDIT_SET_AS_NEXT:
impl.setCurrentForm(newForm);
return;
}
}
if (!isEdt()) {
callSerially(new RunnableWrapper(newForm, null, reverse));
return;
}
if (current != null) {
if (current.isInitialized()) {
current.deinitializeImpl();
} else {
Form fg = getCurrentUpcoming();
if (fg != current) {
if (fg.isInitialized()) {
fg.deinitializeImpl();
}
}
}
}
if (!newForm.isInitialized()) {
newForm.initComponentImpl();
}
if (newForm.getWidth() != getDisplayWidth() || newForm.getHeight() != getDisplayHeight()) {
newForm.setSize(new Dimension(getDisplayWidth(), getDisplayHeight()));
newForm.setShouldCalcPreferredSize(true);
newForm.layoutContainer();
} else {
// if shouldLayout is true
newForm.layoutContainer();
}
boolean transitionExists = false;
if (animationQueue != null && animationQueue.size() > 0) {
Object o = animationQueue.get(animationQueue.size() - 1);
if (o instanceof Transition) {
current = (Form) ((Transition) o).getDestination();
impl.setCurrentForm(current);
}
}
if (current != null) {
// to the correct parent!
if (current instanceof Dialog && ((Dialog) current).isMenu()) {
Transition t = current.getTransitionOutAnimator();
if (t != null) {
// go back to the parent form first
if (((Dialog) current).getPreviousForm() != null) {
initTransition(t.copy(false), current, ((Dialog) current).getPreviousForm());
}
}
current = ((Dialog) current).getPreviousForm();
impl.setCurrentForm(current);
}
// prevent the transition from occurring from a form into itself
if (newForm != current) {
if ((current != null && current.getTransitionOutAnimator() != null) || newForm.getTransitionInAnimator() != null) {
if (animationQueue == null) {
animationQueue = new ArrayList<Animation>();
}
// transitions which are a bit sensitive
if (current != null && (!(newForm instanceof Dialog))) {
Transition t = current.getTransitionOutAnimator();
if (current != null && t != null) {
transitionExists = initTransition(t.copy(reverse), current, newForm);
}
}
if (current != null && !(current instanceof Dialog)) {
Transition t = newForm.getTransitionInAnimator();
if (t != null) {
transitionExists = initTransition(t.copy(reverse), current, newForm);
}
}
}
}
}
synchronized (lock) {
lock.notify();
}
if (!transitionExists) {
if (animationQueue == null || animationQueue.size() == 0) {
setCurrentForm(newForm);
} else {
// we need to add an empty transition to "serialize" this
// screen change...
Transition t = CommonTransitions.createEmpty();
initTransition(t, current, newForm);
}
}
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Image method scaledImpl.
/**
* Returns a scaled version of this image image using the given width and height,
* this is a fast algorithm that preserves translucent information.
* The method accepts -1 to preserve aspect ratio in the given axis.
*
* @param width width for the scaling
* @param height height of the scaled image
* @return new image instance scaled to the given height and width
*/
Image scaledImpl(int width, int height) {
if (width == -1) {
return scaledHeight(height);
}
if (height == -1) {
return scaledWidth(width);
}
Dimension d = new Dimension(width, height);
Image i = getCachedImage(d);
if (i != null) {
return i;
}
if (svgData != null) {
try {
i = createSVG(svgBaseURL, animated, svgData);
} catch (IOException ex) {
i = new Image(this.image);
}
} else {
i = new Image(this.image);
}
i.scaleCache = scaleCache;
i.scale(width, height);
i.transform = this.transform;
i.animated = animated;
i.svgBaseURL = svgBaseURL;
i.svgData = svgData;
cacheImage(new Dimension(width, height), i);
return i;
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class List method calculateElementSize.
/**
* Calculates the size of an element based on a forumla or on rendering prototype
*/
private Dimension calculateElementSize(boolean selected, boolean addMargin) {
if (renderingPrototype != null) {
Component unselected = renderer.getListCellRendererComponent(this, renderingPrototype, 0, selected);
if (addMargin) {
return unselected.getPreferredSizeWithMargin();
} else {
return unselected.getPreferredSize();
}
}
int width = 0;
int height = 0;
int elements = Math.min(listSizeCalculationSampleCount, model.getSize());
int marginY = 0;
int marginX = 0;
for (int iter = 0; iter < elements; iter++) {
Component cmp = renderer.getListCellRendererComponent(this, model.getItemAt(iter), iter, selected);
if (cmp instanceof Container) {
cmp.setShouldCalcPreferredSize(true);
}
Dimension d = cmp.getPreferredSize();
width = Math.max(width, d.getWidth());
height = Math.max(height, d.getHeight());
if (iter == 0) {
Style s = cmp.getStyle();
marginY = s.getVerticalMargins();
marginX = s.getHorizontalMargins();
}
}
return new Dimension(width + marginX, height + marginY);
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class List method calcPreferredSize.
/**
* {@inheritDoc}
*/
protected Dimension calcPreferredSize() {
if (shouldShowHint()) {
Label l = getHintLabelImpl();
if (l != null) {
Dimension d1 = getUIManager().getLookAndFeel().getListPreferredSize(this);
Dimension d2 = l.getPreferredSize();
return new Dimension(d1.getWidth() + d2.getWidth(), d1.getHeight() + d2.getHeight());
}
}
Dimension d = getUIManager().getLookAndFeel().getListPreferredSize(this);
if (spinnerOverlay != null) {
if (spinnerOverlay.getBorder() != null) {
d.setWidth(Math.max(spinnerOverlay.getBorder().getMinimumWidth(), d.getWidth()));
d.setHeight(Math.max(spinnerOverlay.getBorder().getMinimumHeight(), d.getHeight()));
}
}
return d;
}
Aggregations