use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class SideMenuBar method createMenu.
private Form createMenu(final String placement) {
final Form m = new Form() {
private boolean pressedInRightPanel;
private boolean manualMotionLock;
protected boolean shouldSendPointerReleaseToOtherForm() {
return true;
}
void actionCommandImpl(Command cmd, ActionEvent ev) {
if (cmd instanceof SideMenuBar.CommandWrapper) {
cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
ev = new ActionEvent(cmd, ActionEvent.Type.Command);
}
final Command c = cmd;
final ActionEvent e = ev;
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (Display.getInstance().getCurrent() != parent) {
try {
Thread.sleep(40);
} catch (Exception ex) {
}
}
}
});
Display.getInstance().callSerially(new Runnable() {
public void run() {
parent.actionCommandImpl(c, e);
}
});
}
});
}
protected void sizeChanged(int w, int h) {
Style formStyle = getStyle();
int width = w - (formStyle.getHorizontalMargins());
parent.sizeChangedInternal(w, h);
// close the menu
if (getWidth() != width) {
closeMenu();
}
super.sizeChanged(w, h);
}
public void pointerPressed(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerPressed(x, y);
if (rightPanel.contains(x, y)) {
pressedInRightPanel = true;
}
}
public void pointerDragged(int[] x, int[] y) {
if (manualMotionLock) {
return;
}
if (!transitionRunning && pressedInRightPanel) {
dragActivated = true;
pressedInRightPanel = false;
}
if (dragActivated) {
setMenuGlassPane(menu, placement);
draggedX = x[0];
repaint();
return;
}
super.pointerDragged(x, y);
}
public void pointerReleased(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerReleased(x, y);
boolean isRTLValue = isRTL();
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
isRTLValue = !isRTLValue;
}
int displayWidth = Display.getInstance().getDisplayWidth();
if (isRTLValue) {
if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
} else {
if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
}
if (dragActivated || rightPanel.contains(x, y)) {
setMenuGlassPane(menu, placement);
draggedX = x;
int start = x;
int end = 0;
if (isRTLValue) {
end = getWidth();
}
final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
motion.start();
manualMotionLock = true;
sideSwipePotential = false;
rightSideSwipePotential = false;
topSwipePotential = false;
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
}
return true;
}
public void paint(Graphics g) {
repaint();
if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
parent.setTransitionInAnimator(CommonTransitions.createEmpty());
parent.show();
deregisterAnimated(this);
Display.getInstance().callSerially(new Runnable() {
public void run() {
clean();
}
});
}
}
});
}
}
public void keyReleased(int keyCode) {
if (keyCode == leftSK) {
if (transitionRunning) {
return;
}
closeMenu();
return;
}
super.keyReleased(keyCode);
}
};
m.setScrollable(false);
m.removeComponentFromForm(m.getTitleArea());
m.putClientProperty("Menu", "true");
m.setTransitionInAnimator(CommonTransitions.createEmpty());
m.setTransitionOutAnimator(CommonTransitions.createEmpty());
m.setBackCommand(new Command("") {
public void actionPerformed(ActionEvent evt) {
if (transitionRunning) {
return;
}
closeMenu();
}
});
m.setLayout(new BorderLayout());
if (Display.getInstance().areMutableImagesFast()) {
rightPanel = new Container(new BorderLayout());
} else {
rightPanel = new Container(new BorderLayout()) {
public void paintBackground(Graphics g) {
}
public void paintBackgrounds(Graphics g) {
}
public void paint(Graphics g) {
Component c = (Component) rightPanel.getClientProperty("$parent");
// not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
if (c != null) {
boolean b = c.isVisible();
c.setVisible(true);
int x = getAbsoluteX();
g.translate(x, 0);
Container.sidemenuBarTranslation = x;
c.paintComponent(g, true);
Container.sidemenuBarTranslation = 0;
g.translate(-x, 0);
c.setVisible(b);
}
}
};
}
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
int v = 0;
if (Display.getInstance().isPortrait()) {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getHeight() * 2 / 3;
} else {
v = m.getHeight() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
if (v < 0) {
v = openButton.getHeight();
} else {
v = m.getHeight() / 100 * v;
}
}
} else {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 4 / 10;
} else {
v = m.getHeight() / 100 * v;
}
}
}
rightPanel.setPreferredH(v);
} else {
if (Display.getInstance().isPortrait()) {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getWidth() * 2 / 3;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
if (v < 0) {
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
if (isRTL()) {
v = openButton.getWidth();
} else {
v = rightSideButton.getWidth();
}
} else {
v = openButton.getWidth();
}
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
} else {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 4 / 10;
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
}
}
if (sidePanel != null) {
sidePanel.removeAll();
sidePanel = null;
}
sidePanel = createSideNavigationComponent(getCommands(), placement);
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
m.addComponent(BorderLayout.WEST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
} else {
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
m.addComponent(BorderLayout.NORTH, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
Button button = new Button(" ");
button.setUIID("Container");
button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
m.addComponent(BorderLayout.SOUTH, button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
closeMenu();
}
});
} else {
m.addComponent(BorderLayout.EAST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
}
}
m.putClientProperty("cn1$sideMenuParent", this);
return m;
}
use of com.codename1.ui.events.ActionListener in project codenameone-google-maps by codenameone.
the class MapContainer method addMarker.
/**
* Adds a marker to the map with the given attributes
* @param opts The marker options.
* @return marker reference object that should be used when removing the marker
*/
public MapObject addMarker(MarkerOptions opts) {
// public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
EncodedImage icon = opts.getIcon();
Coord location = opts.getLocation();
String text = opts.getText();
String longText = opts.getLongText();
ActionListener onClick = opts.getOnClick();
if (internalNative != null) {
byte[] iconData = null;
if (icon != null) {
iconData = icon.getImageData();
internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
}
long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
MapObject o = new MapObject();
o.mapKey = key;
o.callback = onClick;
markers.add(o);
return o;
} else {
if (internalLightweightCmp != null) {
PointLayer pl = new PointLayer(location, text, icon);
if (points == null) {
points = new PointsLayer();
internalLightweightCmp.addLayer(points);
}
points.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
PointLayer point = (PointLayer) evt.getSource();
for (MapObject o : markers) {
if (o.point == point) {
if (o.callback != null) {
o.callback.actionPerformed(new ActionEvent(o));
}
evt.consume();
return;
}
}
}
});
points.addPoint(pl);
MapObject o = new MapObject();
o.point = pl;
o.callback = onClick;
markers.add(o);
internalLightweightCmp.revalidate();
return o;
} else {
String uri = null;
int iconWidth = 0;
int iconHeight = 0;
int anchorU = 0;
int anchorV = 0;
if (icon != null) {
uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
iconWidth = icon.getWidth();
iconHeight = icon.getHeight();
anchorU = (int) (icon.getWidth() * opts.anchorU);
anchorV = (int) (icon.getHeight() * opts.anchorV);
}
// browserBridge.waitForReady();
MapObject o = new MapObject();
o.callback = onClick;
o.pending = true;
final String fUri = uri;
final int fIconWidth = iconWidth;
final int fIconHeight = iconHeight;
final float fAnchorU = anchorU;
final float fAnchorV = anchorV;
browserBridge.ready(() -> {
internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
o.mapKey = jsres.getInt();
o.pending = false;
});
});
// MapObject o = new MapObject();
// o.mapKey = res.getInt();
// o.callback = onClick;
markers.add(o);
// System.out.println("MapKey added "+o.mapKey);
return o;
}
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class VServAds method getPendingAd.
/**
* {@inheritDoc}
*/
protected Component getPendingAd() {
if (imageURL == null) {
return null;
}
if (renderNotify != null && renderNotify.length() > 0) {
ConnectionRequest c = new ConnectionRequest();
c.setFailSilently(true);
c.setUrl(renderNotify);
c.setPost(false);
NetworkManager.getInstance().addToQueue(c);
}
if ("image".equalsIgnoreCase(contentType)) {
Button adComponent = new Button() {
public void setIcon(Image icon) {
if (icon != null && isScaleMode()) {
icon = icon.scaledWidth(Display.getInstance().getDisplayWidth());
}
super.setIcon(icon);
}
};
adComponent.setUIID("Container");
adComponent.getStyle().setBgColor(backgroundColor);
adComponent.getStyle().setOpacity(0xff);
ImageDownloadService imd = new ImageDownloadService(imageURL, adComponent);
NetworkManager.getInstance().addToQueueAndWait(imd);
/*adComponent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(getAdDestination());
}
});*/
return adComponent;
} else {
WebBrowser wb = new WebBrowser();
if (wb.getInternal() instanceof BrowserComponent) {
BrowserComponent bc = (BrowserComponent) wb.getInternal();
bc.setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(final String url) {
unlock(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(url);
}
});
return false;
}
});
}
wb.setURL(imageURL);
return wb;
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class MediaPlayer method initUI.
private void initUI() {
removeAll();
setLayout(new BorderLayout());
if (video != null && video.getVideoComponent() != null) {
Component videoComponent = video.getVideoComponent();
if (videoComponent != null) {
addComponent(BorderLayout.CENTER, videoComponent);
}
}
buttonsBar = new Container(new FlowLayout(Container.CENTER));
addComponent(BorderLayout.SOUTH, buttonsBar);
if (usesNativeVideoControls() || !showControls) {
buttonsBar.setVisible(false);
buttonsBar.setHidden(true);
}
// if(video == null || !video.isNativePlayerMode()){
Button back = new Button();
back.setUIID("MediaPlayerBack");
if (backIcon != null) {
back.setIcon(backIcon);
} else {
back.setText("Back");
}
buttonsBar.addComponent(back);
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (video == null) {
return;
}
int t = video.getTime();
video.setTime(t - 2);
}
});
// }
final Button play = new Button();
play.setUIID("MediaPlayerPlay");
if (playIcon != null) {
play.setIcon(playIcon);
} else {
play.setText("play");
}
if (autoplay) {
if (video != null && !video.isPlaying()) {
if (getPauseIcon() != null) {
play.setIcon(getPauseIcon());
} else {
play.setText("pause");
}
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
if (isInitialized()) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (video != null && !video.isPlaying() && isInitialized()) {
video.play();
}
}
});
}
}
}, 300l);
// video.play();
}
}
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (video == null) {
return;
}
if (!video.isPlaying()) {
video.play();
play.setUIID("MediaPlayerPause");
if (getPauseIcon() != null) {
play.setIcon(getPauseIcon());
} else {
play.setText("pause");
}
play.repaint();
} else {
video.pause();
play.setUIID("MediaPlayerPlay");
if (getPlayIcon() != null) {
play.setIcon(getPlayIcon());
} else {
play.setText("play");
}
play.repaint();
}
}
});
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (video != null && video.isPlaying()) {
play.setUIID("MediaPlayerPause");
if (getPauseIcon() != null) {
play.setIcon(getPauseIcon());
} else {
play.setText("pause");
}
} else if (video != null && !video.isPlaying()) {
play.setUIID("MediaPlayerPlay");
if (getPlayIcon() != null) {
play.setIcon(getPlayIcon());
} else {
play.setText("play");
}
}
}
});
buttonsBar.addComponent(play);
// if(video == null || !video.isNativePlayerMode()){
Button fwd = new Button();
fwd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (video == null) {
return;
}
int t = video.getTime();
video.setTime(t + 1);
}
});
fwd.setUIID("MediaPlayerFwd");
if (fwdIcon != null) {
fwd.setIcon(fwdIcon);
} else {
fwd.setText("fwd");
}
buttonsBar.addComponent(fwd);
// }
if (isInitialized()) {
revalidate();
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class MasterDetail method bindTabletLandscapeMaster.
/**
* @deprecated this was a half baked idea that made it into the public API
*/
public static void bindTabletLandscapeMaster(final Form rootForm, Container parentContainer, Component landscapeUI, final Component portraitUI, final String commandTitle, Image commandIcon) {
landscapeUI.setHideInPortrait(true);
parentContainer.addComponent(BorderLayout.WEST, landscapeUI);
final Command masterCommand = new Command(commandTitle, commandIcon) {
public void actionPerformed(ActionEvent ev) {
Dialog dlg = new Dialog();
dlg.setLayout(new BorderLayout());
dlg.setDialogUIID("Container");
dlg.getContentPane().setUIID("Container");
Container titleArea = new Container(new BorderLayout());
dlg.addComponent(BorderLayout.NORTH, titleArea);
titleArea.setUIID("TitleArea");
Label title = new Label(commandTitle);
titleArea.addComponent(BorderLayout.CENTER, title);
title.setUIID("Title");
Container body = new Container(new BorderLayout());
body.setUIID("Form");
body.addComponent(BorderLayout.CENTER, portraitUI);
dlg.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 250));
dlg.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 250));
dlg.addComponent(BorderLayout.CENTER, body);
dlg.setDisposeWhenPointerOutOfBounds(true);
dlg.showStetched(BorderLayout.WEST, true);
dlg.removeComponent(portraitUI);
}
};
if (Display.getInstance().isPortrait()) {
if (rootForm.getCommandCount() > 0) {
rootForm.addCommand(masterCommand, 1);
} else {
rootForm.addCommand(masterCommand);
}
}
rootForm.addOrientationListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (portraitUI.getParent() != null) {
Form f = Display.getInstance().getCurrent();
if (f instanceof Dialog) {
((Dialog) f).dispose();
}
}
if (Display.getInstance().isPortrait()) {
rootForm.addCommand(masterCommand, 1);
} else {
rootForm.removeCommand(masterCommand);
rootForm.revalidate();
}
}
});
}
Aggregations