Search in sources :

Example 26 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class AndroidImplementation method getAppArg.

@Override
public String getAppArg() {
    if (super.getAppArg() != null) {
        // behaviour the existed when AppArg was just another Display property.
        return super.getAppArg();
    }
    if (getActivity() == null) {
        return null;
    }
    android.content.Intent intent = getActivity().getIntent();
    if (intent != null) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        intent.removeExtra(Intent.EXTRA_TEXT);
        Uri u = intent.getData();
        String scheme = intent.getScheme();
        if (u == null && intent.getExtras() != null) {
            if (intent.getExtras().keySet().contains("android.intent.extra.STREAM")) {
                try {
                    u = (Uri) intent.getParcelableExtra("android.intent.extra.STREAM");
                    scheme = u.getScheme();
                    System.out.println("u=" + u);
                } catch (Exception ex) {
                    Log.d("Codename One", "Failed to load parcelable extra from intent: " + ex.getMessage());
                }
            }
        }
        if (u != null) {
            // String scheme = intent.getScheme();
            intent.setData(null);
            if ("content".equals(scheme)) {
                try {
                    InputStream attachment = getActivity().getContentResolver().openInputStream(u);
                    if (attachment != null) {
                        String name = getContentName(getActivity().getContentResolver(), u);
                        if (name != null) {
                            String filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            if (filePath.startsWith("file:")) {
                                filePath = filePath.substring(5);
                            }
                            File f = new File(filePath);
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = attachment.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            attachment.close();
                            setAppArg(addFile(filePath));
                            return addFile(filePath);
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                /*
                    // Why do we need this special case?  u.toString()
                    // will include the full URL including query string.
                    // This special case causes urls like myscheme://part1/part2
                    // to only return "/part2" which is obviously problematic and
                    // is inconsistent with iOS.  Is this special case necessary
                    // in some versions of Android?
                    String encodedPath = u.getEncodedPath();
                    if (encodedPath != null && encodedPath.length() > 0) {
                        String query = u.getQuery();
                        if(query != null && query.length() > 0){
                            encodedPath += "?" + query;
                        }
                        setAppArg(encodedPath);
                        return encodedPath;
                    }
                    */
                if (sharedText != null) {
                    setAppArg(sharedText);
                    return sharedText;
                } else {
                    setAppArg(u.toString());
                    return u.toString();
                }
            }
        } else if (sharedText != null) {
            setAppArg(sharedText);
            return sharedText;
        }
    }
    return null;
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) android.content(android.content) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MediaException(com.codename1.media.AsyncMedia.MediaException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Paint(android.graphics.Paint)

Example 27 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class ResetableTextWatcher method edit.

/**
 * Entry point for using this class
 * @param impl The current running activity
 * @param component Any subclass of com.codename1.ui.TextArea
 * @param inputType One of the TextArea's input-type constants
 */
public static void edit(final AndroidImplementation impl, final Component component, final int inputType) {
    if (impl.getActivity() == null) {
        throw new IllegalArgumentException("activity is null");
    }
    if (component == null) {
        throw new IllegalArgumentException("component is null");
    }
    if (!(component instanceof TextArea)) {
        throw new IllegalArgumentException("component must be instance of TextArea");
    }
    final TextArea textArea = (TextArea) component;
    textArea.registerAsInputDevice();
    final String initialText = textArea.getText();
    textArea.putClientProperty("InPlaceEditView.initialText", initialText);
    // The very first time we try to edit a string, let's determine if the
    // system default is to do async editing.  If the system default
    // is not yet set, we set it here, and it will be used as the default from now on
    // We do this because the nativeInstance.isAsyncEditMode() value changes
    // to reflect the currently edited field so it isn't a good way to keep a
    // system default.
    String defaultAsyncEditingSetting = Display.getInstance().getProperty("android.VKBAlwaysOpen", null);
    if (defaultAsyncEditingSetting == null) {
        defaultAsyncEditingSetting = impl.isAsyncEditMode() ? "true" : "false";
        Display.getInstance().setProperty("android.VKBAlwaysOpen", defaultAsyncEditingSetting);
    }
    boolean asyncEdit = "true".equals(defaultAsyncEditingSetting) ? true : false;
    // Check if the form has any setting for asyncEditing that should override
    // the application defaults.
    final Form parentForm = component.getComponentForm();
    if (parentForm == null) {
        com.codename1.io.Log.p("Attempt to edit text area that is not on a form.  This is not supported");
        return;
    }
    if (parentForm.getClientProperty("asyncEditing") != null) {
        Object async = parentForm.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.getClientProperty("android.asyncEditing") != null) {
        Object async = parentForm.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to ios.asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.isFormBottomPaddingEditingMode()) {
        asyncEdit = true;
    }
    // then this will override all other settings.
    if (component.getClientProperty("asyncEditing") != null) {
        Object async = component.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field asyncEditing client property: "+asyncEdit);
        }
    }
    if (component.getClientProperty("android.asyncEditing") != null) {
        Object async = component.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field ios.asyncEditing client property: "+asyncEdit);
        }
    }
    final boolean resizeEditMode = "resize".equalsIgnoreCase(String.valueOf(component.getClientProperty("android.editMode")));
    final boolean panEditMode = "pan".equalsIgnoreCase(String.valueOf(component.getClientProperty("android.editMode")));
    // if true, then in async mode we are currently editing and are switching to another field
    final boolean isEditedFieldSwitch;
    // If we are already editing, we need to finish that up before we proceed to edit the next field.
    synchronized (editingLock) {
        if (mIsEditing) {
            if (impl.isAsyncEditMode()) {
                // Using isEditedFieldSwitch was causing issues with cursors not showing up.
                // https://github.com/codenameone/CodenameOne/issues/2353
                // https://stackoverflow.com/questions/49004370/focus-behaviour-in-textarea-in-cn1
                // Disabling this feature by default now, but can be re-enabled by setting
                // Display.getInstance().setProperty("android.reuseTextEditorOnSwitch", "true");
                // This editedFieldSwitch feature was added a while back to improve experience on older
                // Android devices where the field switching was going too slow.
                // https://github.com/codenameone/CodenameOne/issues/2012
                // This issue was resolved in this commit (https://github.com/jaanushansen/CodenameOne/commit/f3e53a80704149e4d7cde276d01c1368bcdcfe2c)
                // which was submitted as part of a pull request.  This fix has been the source of several
                // regressions, mostly related to properties not being propagated properly when a text field is changed
                // However, this issue (with the cursor not showing up), doesn't appear to have a simple solution
                // so, I'm disabling this feature for now.
                isEditedFieldSwitch = "true".equals(Display.getInstance().getProperty("android.reuseTextEditorOnSwitch", "false"));
                final String[] out = new String[1];
                TextArea prevTextArea = null;
                if (sInstance != null && sInstance.mLastEditText != null) {
                    prevTextArea = sInstance.mLastEditText.getTextArea();
                }
                if (prevTextArea != null) {
                    final TextArea fPrevTextArea = prevTextArea;
                    final String retVal = sInstance.mLastEditText.getText().toString();
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            Display.getInstance().onEditingComplete(fPrevTextArea, retVal);
                            textArea.requestFocus();
                        }
                    });
                }
                InPlaceEditView.setEditedTextField(textArea);
                nextTextArea = null;
            } else {
                isEditedFieldSwitch = false;
                final InPlaceEditView instance = sInstance;
                if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) {
                    instance.showTextEditorAgain();
                    return;
                }
                if (!isClosing && sInstance != null && sInstance.mEditText != null) {
                    isClosing = true;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            instance.endEditing(REASON_UNDEFINED, true, 0);
                        }
                    });
                }
                afterClose = new Runnable() {

                    @Override
                    public void run() {
                        impl.callHideTextEditor();
                        Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText());
                    }
                };
                return;
            }
        } else {
            isEditedFieldSwitch = false;
        }
        mIsEditing = true;
        isClosing = false;
        afterClose = null;
    }
    impl.setAsyncEditMode(asyncEdit);
    // textArea.setPreferredSize(prefSize);
    if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
        ((TextField) textArea).setEditable(false);
    }
    final boolean scrollableParent = isScrollableParent(textArea);
    // We wrap the text area so that we can safely pass data across to the
    // android UI thread.
    final TextAreaData textAreaData = new TextAreaData(textArea);
    impl.getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (!isEditedFieldSwitch) {
                releaseEdit();
                if (sInstance == null) {
                    sInstance = new InPlaceEditView(impl);
                    impl.relativeLayout.addView(sInstance);
                }
            // Let's try something new here
            // We'll ALWAYS try resize edit mode (since it just works better)
            // But we'll detect whether the field is still covered by the keyboard
            // and switch to pan mode if necessary.
            }
            if (panEditMode) {
                setEditMode(false);
            } else if (resizeEditMode) {
                setEditMode(true);
            } else if (parentForm.isFormBottomPaddingEditingMode()) {
                setEditMode(true);
            } else if (scrollableParent) {
                setEditMode(false);
            } else {
                trySetEditMode(true);
            }
            sInstance.startEditing(impl.getActivity(), textAreaData, initialText, inputType, isEditedFieldSwitch);
        }
    });
    final String[] out = new String[1];
    // In case the contents of the text area are changed while editing is in progress
    // we should propagate the changes to the native text field.
    final DataChangedListener textAreaDataChanged = new DataChangedListener() {

        @Override
        public void dataChanged(int type, int index) {
            if (suppressDataChangedEvent) {
                // https://github.com/codenameone/CodenameOne/issues/3343
                return;
            }
            TextArea currTextArea = getCurrentTextArea();
            if (currTextArea != textArea) {
                // This is not the active text area anymore
                textArea.removeDataChangedListener(this);
                return;
            }
            final String newText = textArea.getText();
            EditView currEditView = getCurrentEditView();
            if (currEditView == null || currEditView.mTextArea != textArea) {
                textArea.removeDataChangedListener(this);
                return;
            }
            String existingText = currEditView.getText().toString();
            // because Objects.equals was not available until API 19
            if (!com.codename1.compat.java.util.Objects.equals(newText, existingText)) {
                impl.getActivity().runOnUiThread(new Runnable() {

                    public void run() {
                        TextArea currTextArea = getCurrentTextArea();
                        EditView currEditView = getCurrentEditView();
                        if (currTextArea != textArea || currEditView == null || currEditView.mTextArea != textArea) {
                            return;
                        }
                        String existingText = currEditView.getText().toString();
                        // because Objects.equals was not available until API 19
                        if (!com.codename1.compat.java.util.Objects.equals(newText, existingText)) {
                            // We need to suppress the Android text change events
                            // to prevent weird things from happening.  E.g. https://github.com/codenameone/CodenameOne/issues/3349
                            suppressTextChangeEvent = true;
                            currEditView.setText(newText);
                            suppressTextChangeEvent = false;
                        }
                    }
                });
            }
        }
    };
    textArea.addDataChangedListener(textAreaDataChanged);
    // In order to reuse the code the runs after edit completion, we will wrap it in a runnable
    // For sync edit mode, we will just run onComplete.run() at the end of this method.  For
    // Async mode we add the Runnable to the textarea as a client property, then run it
    // when editing eventually completes.
    Runnable onComplete = new Runnable() {

        public void run() {
            textArea.removeDataChangedListener(textAreaDataChanged);
            if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
                ((TextField) textArea).setEditable(true);
            }
            textArea.setPreferredSize(null);
            if (sInstance != null && sInstance.mLastEditText != null && sInstance.mLastEditText.mTextArea == textArea) {
                String retVal = sInstance.mLastEditText.getText().toString();
                if (!impl.isAsyncEditMode()) {
                    sInstance.mLastEditText = null;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        public void run() {
                            releaseEdit();
                        }
                    });
                }
                out[0] = retVal;
            } else {
                out[0] = initialText;
            }
            Display.getInstance().onEditingComplete(component, out[0]);
            if (impl.isAsyncEditMode()) {
                impl.callHideTextEditor();
            } else {
                // lock.
                if (sInstance != null) {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (sInstance != null) {
                                com.codename1.io.Util.sleep(5);
                            }
                        }
                    });
                }
            }
            // Release the editing flag
            synchronized (editingLock) {
                mIsEditing = false;
            }
            // as a runnable ... this should take priority over the "nextTextArea" setting
            if (afterClose != null) {
                Display.getInstance().callSerially(afterClose);
            } else if (nextTextArea != null) {
                final TextArea next = nextTextArea;
                nextTextArea = null;
                next.requestFocus();
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        Display.getInstance().editString(next, next.getMaxSize(), next.getConstraint(), next.getText());
                    }
                });
            }
        }
    };
    textArea.requestFocus();
    textArea.repaint();
    if (impl.isAsyncEditMode()) {
        component.putClientProperty("android.onAsyncEditingComplete", onComplete);
        return;
    }
    // Make this call synchronous
    // We set this flag so that waitForEditCompletion can block on it.
    // The flag will be released inside the endEditing method which will
    // allow the method to proceed.
    waitingForSynchronousEditingCompletion = true;
    waitForEditCompletion();
    onComplete.run();
}
Also used : TextArea(com.codename1.ui.TextArea) Form(com.codename1.ui.Form) DataChangedListener(com.codename1.ui.events.DataChangedListener) TextField(com.codename1.ui.TextField)

Example 28 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class CodenameOneActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    final Form currentForm = Display.getInstance().getCurrent();
    if (currentForm == null) {
        return false;
    }
    Command cmd = null;
    final boolean[] tmpProp = new boolean[1];
    if (item.getItemId() == android.R.id.home) {
        cmd = currentForm.getBackCommand();
        if (cmd == null) {
            return false;
        }
        cmd.putClientProperty("source", "ActionBar");
        tmpProp[0] = true;
    }
    int commandIndex = item.getItemId();
    if (cmd == null) {
        cmd = currentForm.getCommand(commandIndex);
    }
    final Command command = cmd;
    final ActionEvent actionEvent = new ActionEvent(command);
    // stop edit if the keybaord is open
    AndroidImplementation.stopEditing();
    // Protect ourselves from commands that misbehave. A crash here will crash the entire application
    Display.getInstance().callSerially(new Runnable() {

        @Override
        public void run() {
            try {
                currentForm.dispatchCommand(command, actionEvent);
                // remove the temp source property
                if (tmpProp[0]) {
                    command.putClientProperty("source", null);
                }
            } catch (Throwable e) {
                Log.e("CodenameOneActivity.onOptionsItemSelected", e.toString() + Log.getStackTraceString(e));
            }
        }
    });
    return true;
}
Also used : Form(com.codename1.ui.Form) Command(com.codename1.ui.Command) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 29 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class SEBrowserComponent method init.

private static void init(SEBrowserComponent self, BrowserComponent p) {
    final WeakReference<SEBrowserComponent> weakSelf = new WeakReference<>(self);
    final WeakReference<BrowserComponent> weakP = new WeakReference<>(p);
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            SEBrowserComponent self = weakSelf.get();
            if (self == null) {
                return;
            }
            self.cnt = new InternalJPanel(self.instance, self);
            // <--- Important if container is opaque it will cause
            self.cnt.setOpaque(false);
            // all kinds of flicker due to painting conflicts with CN1 pipeline.
            self.cnt.setLayout(new BorderLayout());
            self.cnt.add(BorderLayout.CENTER, self.panel);
        // cnt.setVisible(false);
        }
    });
    self.web.getEngine().getLoadWorker().messageProperty().addListener(new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> ov, String t, String t1) {
            SEBrowserComponent self = weakSelf.get();
            BrowserComponent p = weakP.get();
            if (self == null || p == null) {
                return;
            }
            if (t1.startsWith("Loading http:") || t1.startsWith("Loading file:") || t1.startsWith("Loading https:")) {
                String url = t1.substring("Loading ".length());
                if (!url.equals(self.currentURL)) {
                    p.fireWebEvent("onStart", new ActionEvent(url));
                }
                self.currentURL = url;
            } else if ("Loading complete".equals(t1)) {
            }
        }
    });
    self.web.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {

        @Override
        public void handle(WebEvent<String> t) {
            BrowserComponent p = weakP.get();
            if (p == null) {
                return;
            }
            String msg = t.getData();
            if (msg.startsWith("!cn1_message:")) {
                System.out.println("Receiving message " + msg);
                p.fireWebEvent("onMessage", new ActionEvent(msg.substring("!cn1_message:".length())));
            }
        }
    });
    self.web.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {

        @Override
        public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
            System.out.println("Received exception: " + t1.getMessage());
            if (ov.getValue() != null) {
                ov.getValue().printStackTrace();
            }
            if (t != ov.getValue() && t != null) {
                t.printStackTrace();
            }
            if (t1 != ov.getValue() && t1 != t && t1 != null) {
                t.printStackTrace();
            }
        }
    });
    self.web.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {

        @Override
        public void changed(ObservableValue ov, State oldState, State newState) {
            SEBrowserComponent self = weakSelf.get();
            BrowserComponent p = weakP.get();
            try {
                netscape.javascript.JSObject w = (netscape.javascript.JSObject) self.web.getEngine().executeScript("window");
                if (w == null) {
                    System.err.println("Could not get window");
                } else {
                    Bridge b = new Bridge(p);
                    self.putClientProperty("SEBrowserComponent.Bridge.jconsole", b);
                    w.setMember("jconsole", b);
                }
            } catch (Throwable t) {
                Log.e(t);
            }
            if (self == null || p == null) {
                return;
            }
            String url = self.web.getEngine().getLocation();
            if (newState == State.SCHEDULED) {
                p.fireWebEvent("onStart", new ActionEvent(url));
            } else if (newState == State.RUNNING) {
                p.fireWebEvent("onLoadResource", new ActionEvent(url));
            } else if (newState == State.SUCCEEDED) {
                if (!p.isNativeScrollingEnabled()) {
                    self.web.getEngine().executeScript("document.body.style.overflow='hidden'");
                }
                // let's just add a client property to the BrowserComponent to enable firebug
                if (Boolean.TRUE.equals(p.getClientProperty("BrowserComponent.firebug"))) {
                    self.web.getEngine().executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
                }
                netscape.javascript.JSObject window = (netscape.javascript.JSObject) self.web.getEngine().executeScript("window");
                Bridge b = new Bridge(p);
                self.putClientProperty("SEBrowserComponent.Bridge.cn1application", b);
                window.setMember("cn1application", b);
                self.web.getEngine().executeScript("while (window._cn1ready && window._cn1ready.length > 0) {var f = window._cn1ready.shift(); f();}");
                // System.out.println("cn1application is "+self.web.getEngine().executeScript("window.cn1application && window.cn1application.shouldNavigate"));
                self.web.getEngine().executeScript("window.addEventListener('unload', function(e){console.log('unloading...');return 'foobar';});");
                p.fireWebEvent("onLoad", new ActionEvent(url));
            }
            self.currentURL = url;
            self.repaint();
        }
    });
    self.web.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {

        @Override
        public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
            BrowserComponent p = weakP.get();
            if (p == null) {
                return;
            }
            t1.printStackTrace();
            if (t1 == null) {
                if (t == null) {
                    p.fireWebEvent("onError", new ActionEvent("Unknown error", -1));
                } else {
                    p.fireWebEvent("onError", new ActionEvent(t.getMessage(), -1));
                }
            } else {
                p.fireWebEvent("onError", new ActionEvent(t1.getMessage(), -1));
            }
        }
    });
    // Monitor the location property so that we can send the shouldLoadURL event.
    // This allows us to cancel the loading of a URL if we want to handle it ourself.
    self.web.getEngine().locationProperty().addListener(new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> prop, String before, String after) {
            SEBrowserComponent self = weakSelf.get();
            BrowserComponent p = weakP.get();
            if (self == null || p == null) {
                return;
            }
            if (!p.fireBrowserNavigationCallbacks(self.web.getEngine().getLocation())) {
                self.web.getEngine().getLoadWorker().cancel();
            }
        }
    });
    self.adjustmentListener = new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    SEBrowserComponent self = weakSelf.get();
                    if (self == null) {
                        return;
                    }
                    self.onPositionSizeChange();
                }
            });
        }
    };
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) ObservableValue(javafx.beans.value.ObservableValue) BorderLayout(java.awt.BorderLayout) WeakReference(java.lang.ref.WeakReference) WebEvent(javafx.scene.web.WebEvent) AdjustmentEvent(java.awt.event.AdjustmentEvent) State(javafx.concurrent.Worker.State) AdjustmentListener(java.awt.event.AdjustmentListener)

Example 30 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class SQLMap method select.

/**
 * Fetches the components from the database matching the given cmp description, the fields that aren't
 * null within the cmp will match the where clause
 * @param cmp the component to match
 * @param orderBy the column to order by, can be null to ignore order
 * @param ascending true to indicate ascending order
 * @param maxElements the maximum number of elements returned can be 0 or lower to ignore
 * @param page  the page within the query to match the max elements value
 * @return the result of the query
 */
public java.util.List<PropertyBusinessObject> select(PropertyBusinessObject cmp, Property orderBy, boolean ascending, int maxElements, int page) throws IOException, InstantiationException {
    String tableName = getTableName(cmp);
    StringBuilder createStatement = new StringBuilder("SELECT * FROM ");
    createStatement.append(tableName);
    ArrayList<Object> params = new ArrayList<Object>();
    createStatement.append(" WHERE ");
    boolean found = false;
    for (PropertyBase p : cmp.getPropertyIndex()) {
        if (p instanceof Property) {
            if (((Property) p).get() != null) {
                if (found) {
                    createStatement.append(" AND ");
                }
                found = true;
                params.add(((Property) p).get());
                createStatement.append(getColumnName(p));
                createStatement.append(" = ?");
            }
        }
    }
    // all properties are null undo the where append
    if (!found) {
        createStatement = new StringBuilder("SELECT * FROM ");
        createStatement.append(tableName);
    }
    if (orderBy != null) {
        createStatement.append(" ORDER BY ");
        createStatement.append(getColumnName(orderBy));
        if (!ascending) {
            createStatement.append(" DESC");
        }
    }
    if (maxElements > 0) {
        createStatement.append(" LIMIT ");
        createStatement.append(maxElements);
        if (page > 0) {
            createStatement.append(" OFFSET ");
            createStatement.append(page * maxElements);
        }
    }
    Cursor c = null;
    try {
        ArrayList<PropertyBusinessObject> response = new ArrayList<PropertyBusinessObject>();
        c = executeQuery(createStatement.toString(), params.toArray());
        while (c.next()) {
            PropertyBusinessObject pb = (PropertyBusinessObject) cmp.getClass().newInstance();
            for (PropertyBase p : pb.getPropertyIndex()) {
                Row currentRow = c.getRow();
                SqlType t = getSqlType(p);
                if (t == SqlType.SQL_EXCLUDE) {
                    continue;
                }
                Object value = t.getValue(currentRow, c.getColumnIndex(getColumnName(p)), p);
                if (p instanceof Property) {
                    ((Property) p).set(value);
                }
            }
            response.add(pb);
        }
        c.close();
        return response;
    } catch (Throwable t) {
        Log.e(t);
        if (c != null) {
            c.close();
        }
        if (t instanceof IOException) {
            throw ((IOException) t);
        } else {
            throw new IOException(t.toString());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(com.codename1.db.Cursor) Row(com.codename1.db.Row)

Aggregations

Entity (com.codename1.rad.models.Entity)22 Property (com.codename1.rad.models.Property)16 IOException (java.io.IOException)11 ResultParser (com.codename1.rad.io.ResultParser)10 ArrayList (java.util.ArrayList)10 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)9 Component (com.codename1.ui.Component)9 Element (com.codename1.xml.Element)8 ParseException (com.codename1.l10n.ParseException)7 Container (com.codename1.ui.Container)7 Label (com.codename1.ui.Label)7 SortedProperties (com.codename1.ant.SortedProperties)6 Log (com.codename1.io.Log)6 Form (com.codename1.ui.Form)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 Map (java.util.Map)6 Thing (com.codename1.rad.schemas.Thing)5 XMLParser (com.codename1.xml.XMLParser)5 StringReader (java.io.StringReader)5 List (java.util.List)5