use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Oauth2 method showAuthentication.
/**
* This method shows an authentication for login form
*
* @param al a listener that will receive at its source either a token for
* the service or an exception in case of a failure
* @return a component that should be displayed to the user in order to
* perform the authentication
*/
public void showAuthentication(ActionListener al) {
final Form old = Display.getInstance().getCurrent();
InfiniteProgress inf = new InfiniteProgress();
final Dialog progress = inf.showInifiniteBlocking();
Form authenticationForm = new Form("Login");
authenticationForm.setScrollable(false);
if (old != null) {
Command cancel = new Command("Cancel") {
public void actionPerformed(ActionEvent ev) {
if (Display.getInstance().getCurrent() == progress) {
progress.dispose();
}
old.showBack();
}
};
if (authenticationForm.getToolbar() != null) {
authenticationForm.getToolbar().addCommandToLeftBar(cancel);
} else {
authenticationForm.addCommand(cancel);
}
authenticationForm.setBackCommand(cancel);
}
authenticationForm.setLayout(new BorderLayout());
authenticationForm.addComponent(BorderLayout.CENTER, createLoginComponent(al, authenticationForm, old, progress));
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Oauth2 method createLoginComponent.
private Component createLoginComponent(final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
String URL = oauth2URL + "?client_id=" + clientId + "&redirect_uri=" + Util.encodeUrl(redirectURI);
if (scope != null) {
URL += "&scope=" + scope;
}
if (clientSecret != null) {
URL += "&response_type=code";
} else {
URL += "&response_type=token";
}
if (additionalParams != null) {
Enumeration e = additionalParams.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String val = additionalParams.get(key).toString();
URL += "&" + key + "=" + val;
}
}
DocumentInfo.setDefaultEncoding(DocumentInfo.ENCODING_UTF8);
final WebBrowser[] web = new WebBrowser[1];
web[0] = new WebBrowser() {
@Override
public void onLoad(String url) {
handleURL(url, this, al, frm, backToForm, progress);
}
public void onStart(String url) {
}
};
web[0].setURL(URL);
return web[0];
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class ImageDownloadService method createImageToFileSystem.
/**
* Constructs an image request that will automatically populate the given list
* when the response arrives, it will cache the file locally as a file
* in the file storage.
* This assumes the GenericListCellRenderer style of
* list which relies on a map based model approach.
*
* @param url the image URL
* @param targetList the list that should be updated when the data arrives
* @param targetOffset the offset within the list to insert the image
* @param targetKey the key for the map in the target offset
* @param destFile local file to store the data into the given path
*/
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
if (Display.getInstance().isEdt()) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
}
});
return;
}
// image not found on cache go and download from the url
ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
i.targetModel = targetModel;
i.maintainAspectRatio = maintainAspectRatio;
Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
if (im != null) {
i.setEntryInListModel(targetOffset, im);
targetList.repaint();
return;
}
i.cacheImages = true;
i.destinationFile = destFile;
i.toScale = toScale;
i.placeholder = placeholderImage;
i.setPriority(priority);
i.setFailSilently(true);
NetworkManager.getInstance().addToQueue(i);
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class ImageDownloadService method postResponse.
/**
* {@inheritDoc}
*/
protected void postResponse() {
// trigger an exception in case of an invalid image
result.getWidth();
Image image = result;
if (toScale != null && toScale.getWidth() != image.getWidth() && toScale.getHeight() != image.getHeight()) {
image = scaleImage(image, toScale, maintainAspectRatio);
}
final Image i = image;
if (parentLabel != null) {
final Dimension pref = parentLabel.getPreferredSize();
if (parentLabel.getComponentForm() != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (isDownloadToStyles()) {
parentLabel.getUnselectedStyle().setBgImage(i);
parentLabel.getSelectedStyle().setBgImage(i);
parentLabel.getPressedStyle().setBgImage(i);
} else {
parentLabel.setIcon(i);
}
Dimension newPref = parentLabel.getPreferredSize();
// sized image in place or has a hardcoded preferred size.
if (pref.getWidth() != newPref.getWidth() || pref.getHeight() != newPref.getHeight()) {
parentLabel.getComponentForm().revalidate();
}
}
});
} else {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (isDownloadToStyles()) {
parentLabel.getUnselectedStyle().setBgImage(i);
parentLabel.getSelectedStyle().setBgImage(i);
parentLabel.getPressedStyle().setBgImage(i);
} else {
parentLabel.setIcon(i);
}
}
});
}
parentLabel.repaint();
return;
} else {
if (targetList != null) {
setEntryInListModel(targetOffset, image);
// revalidate only once to avoid multiple revalidate refreshes during scroll
if (targetList.getParent() != null) {
if (alwaysRevalidate) {
targetList.getParent().revalidate();
} else {
if (targetList.getClientProperty("$imgDSReval") == null) {
targetList.putClientProperty("$imgDSReval", Boolean.TRUE);
targetList.getParent().revalidate();
} else {
targetList.repaint();
}
}
}
}
}
// if this is a list cell renderer component
fireResponseListener(new NetworkEvent(this, result));
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class CodenameOneInputConnection method commitText.
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
Component txtCmp = Display.getInstance().getCurrent().getFocused();
if (txtCmp != null && txtCmp instanceof TextField) {
TextField t = (TextField) txtCmp;
String textFieldText = t.getText();
int cursorPosition = t.getCursorPosition();
StringBuilder sb = new StringBuilder(textFieldText);
if (text.equals("\n")) {
// System.out.println("hello backslash");
}
if (composingText.length() > 0) {
if (text.equals(" ")) {
return commitText(composingText + " ", newCursorPosition);
}
sb.replace(sb.length() - composingText.length(), sb.length(), text.toString());
composingText = "";
} else {
sb.insert(cursorPosition, text);
}
t.setText(sb.toString());
t.setCursorPosition(cursorPosition + text.length());
updateExtractedText();
}
}
return super.commitText(text, newCursorPosition);
}
Aggregations