use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class SliderBridge method bindProgress.
/**
* Allows binding progress to an arbitrary slider
*
* @param sources the source connection request (null for all network activity)
* @param s the slider
*/
public static void bindProgress(final ConnectionRequest[] sources, final Slider s) {
Vector v = null;
int portions = 100;
if (sources != null) {
v = new Vector();
int slen = sources.length;
for (int iter = 0; iter < slen; iter++) {
v.addElement(sources[iter]);
}
portions = portions / slen;
}
final Vector sourceVec = v;
final int portionPerSource = portions;
NetworkManager.getInstance().addProgressListener(new ActionListener() {
private float currentLength;
private int soFar;
/**
* {@inheritDoc}
*/
public void actionPerformed(ActionEvent evt) {
if (sources != null) {
if (!sourceVec.contains(evt.getSource())) {
return;
}
}
NetworkEvent e = (NetworkEvent) evt;
switch(e.getProgressType()) {
case NetworkEvent.PROGRESS_TYPE_COMPLETED:
s.setInfinite(false);
// s.setProgress(s.getMaxValue());
soFar += portionPerSource;
s.setProgress(soFar);
if (sources != null) {
NetworkManager.getInstance().removeProgressListener(this);
}
break;
case NetworkEvent.PROGRESS_TYPE_INITIALIZING:
s.setInfinite(true);
break;
case NetworkEvent.PROGRESS_TYPE_INPUT:
case NetworkEvent.PROGRESS_TYPE_OUTPUT:
if (e.getLength() > 0) {
currentLength = e.getLength();
// s.setMaxValue(1000);
s.setInfinite(false);
float sentReceived = e.getSentReceived();
sentReceived = sentReceived / currentLength * portionPerSource;
s.setProgress((int) sentReceived + soFar);
// s.setProgress(e.getSentReceived());
// s.setMaxValue(e.getLength());
} else {
s.setInfinite(true);
}
break;
}
}
});
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class ToastBar method updateStatus.
/**
* Updates the ToastBar UI component with the settings of the current status.
*/
private void updateStatus() {
final ToastBarComponent c = getToastBarComponent();
if (c != null) {
try {
if (updatingStatus) {
pendingUpdateStatus = true;
return;
}
updatingStatus = true;
if (c.currentlyShowing != null && !statuses.contains(c.currentlyShowing)) {
c.currentlyShowing = null;
}
if (c.currentlyShowing == null || statuses.isEmpty()) {
if (!statuses.isEmpty()) {
c.currentlyShowing = statuses.get(statuses.size() - 1);
} else {
setVisible(false);
return;
}
}
Status s = c.currentlyShowing;
Label l = new Label(s.getMessage() != null ? s.getMessage() : "");
c.leadButton.getListeners().clear();
c.leadButton.addActionListener(s.getListener());
c.leadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
if (c.currentlyShowing != null && !c.currentlyShowing.showProgressIndicator) {
c.currentlyShowing.clear();
}
ToastBar.this.setVisible(false);
}
});
c.progressLabel.setVisible(s.isShowProgressIndicator());
if (c.progressLabel.isVisible()) {
if (!c.contains(c.progressLabel)) {
c.addComponent(BorderLayout.EAST, c.progressLabel);
}
Image anim = c.progressLabel.getAnimation();
if (anim != null && anim.getWidth() > 0) {
c.progressLabel.setWidth(anim.getWidth());
}
if (anim != null && anim.getHeight() > 0) {
c.progressLabel.setHeight(anim.getHeight());
}
} else {
if (c.contains(c.progressLabel)) {
c.removeComponent(c.progressLabel);
}
}
c.progressBar.setVisible(s.getProgress() >= -1);
if (s.getProgress() >= -1) {
if (!c.contains(c.progressBar)) {
c.addComponent(BorderLayout.SOUTH, c.progressBar);
}
if (s.getProgress() < 0) {
c.progressBar.setInfinite(true);
} else {
c.progressBar.setInfinite(false);
c.progressBar.setProgress(s.getProgress());
}
} else {
c.removeComponent(c.progressBar);
}
c.icon.setVisible(s.getIcon() != null);
if (s.getIcon() != null && c.icon.getIcon() != s.getIcon()) {
c.icon.setIcon(s.getIcon());
}
if (s.getIcon() == null && c.contains(c.icon)) {
c.removeComponent(c.icon);
} else if (s.getIcon() != null && !c.contains(c.icon)) {
c.addComponent(BorderLayout.WEST, c.icon);
}
String oldText = c.label.getText();
if (!oldText.equals(l.getText())) {
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
if (c.isVisible()) {
TextArea newLabel = new TextArea();
// newLabel.setColumns(l.getText().length()+1);
// newLabel.setRows(l.getText().length()+1);
newLabel.setFocusable(false);
newLabel.setEditable(false);
// newLabel.getAllStyles().setFgColor(0xffffff);
if (s.getMessageUIID() != null) {
newLabel.setUIID(s.getMessageUIID());
} else if (defaultMessageUIID != null) {
newLabel.setUIID(defaultMessageUIID);
} else {
newLabel.setUIID(c.label.getUIID());
}
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
newLabel.setWidth(c.label.getWidth());
newLabel.setText(l.getText());
Dimension oldTextAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(c.label, true);
Dimension newTexAreaSize = UIManager.getInstance().getLookAndFeel().getTextAreaSize(newLabel, true);
// https://stackoverflow.com/questions/46172993/codename-one-toastbar-nullpointerexception
if (c.label.getParent() != null) {
c.label.getParent().replaceAndWait(c.label, newLabel, CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 300));
c.label = newLabel;
if (oldTextAreaSize.getHeight() != newTexAreaSize.getHeight()) {
c.label.setPreferredH(newTexAreaSize.getHeight());
c.getParent().animateHierarchyAndWait(300);
}
}
} else {
if (s.getMessageUIID() != null) {
c.label.setUIID(s.getMessageUIID());
} else if (defaultMessageUIID != null) {
c.label.setUIID(defaultMessageUIID);
}
if (s.getUiid() != null) {
c.setUIID(s.getUiid());
} else if (defaultUIID != null) {
c.setUIID(defaultUIID);
}
c.label.setText(l.getText());
// c.label.setColumns(l.getText().length()+1);
// c.label.setRows(l.getText().length()+1);
c.label.setPreferredW(c.getWidth());
c.revalidate();
}
} else {
c.revalidate();
}
} finally {
updatingStatus = false;
if (pendingUpdateStatus) {
pendingUpdateStatus = false;
Display.getInstance().callSerially(new Runnable() {
public void run() {
updateStatus();
}
});
}
}
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class ToastBar method showMessage.
/**
* Simplifies a common use case of showing a message with an icon that fades out after a few seconds
* @param msg the message
* @param icon the material icon to show from {@link com.codename1.ui.FontImage}
* @param timeout the timeout value in milliseconds
* @param listener the action to perform when the ToastBar is tapped
*/
public static void showMessage(String msg, char icon, int timeout, ActionListener listener) {
ToastBar.Status s = ToastBar.getInstance().createStatus();
Style stl = UIManager.getInstance().getComponentStyle(s.getMessageUIID());
s.setIcon(FontImage.createMaterial(icon, stl, 4));
s.setMessage(msg);
if (listener != null) {
s.setListener(listener);
}
s.setExpires(timeout);
s.show();
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class SwipeBackSupport method bind.
/**
* Binds support for swiping to the given forms
*
* @param currentForm the current form
* @param destination the destination form which can be created lazily
*/
protected void bind(final Form currentForm, final LazyValue<Form> destination) {
pointerDragged = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (sideSwipePotential) {
final int x = evt.getX();
final int y = evt.getY();
if (Math.abs(y - initialDragY) > x - initialDragX) {
sideSwipePotential = false;
return;
}
evt.consume();
if (dragActivated) {
currentX = x;
Display.getInstance().getCurrent().repaint();
} else {
if (x - initialDragX > Display.getInstance().convertToPixels(currentForm.getUIManager().getThemeConstant("backGestureThresholdInt", 5), true)) {
dragActivated = true;
destinationForm = destination.get();
startBackTransition(currentForm, destinationForm);
}
}
}
}
};
pointerReleased = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (dragActivated) {
int destNumberX = Display.getInstance().getDisplayWidth();
int incrementsX = Display.getInstance().convertToPixels(3, true);
if (currentX < destNumberX / 2) {
destinationForm = currentForm;
destNumberX = 0;
incrementsX *= -1;
}
final int destNumber = destNumberX;
final int increments = incrementsX;
Display.getInstance().getCurrent().registerAnimated(new Animation() {
public boolean animate() {
currentX += increments;
if (currentX > 0 && currentX >= destNumber || currentX < 0 && currentX <= destNumber) {
currentX = destNumber;
Transition t = destinationForm.getTransitionInAnimator();
destinationForm.setTransitionInAnimator(CommonTransitions.createEmpty());
destinationForm.show();
destinationForm.setTransitionInAnimator(t);
destinationForm = null;
dragActivated = false;
return false;
}
return true;
}
public void paint(Graphics g) {
}
});
}
}
};
pointerPressed = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sideSwipePotential = false;
int displayWidth = Display.getInstance().getDisplayWidth();
sideSwipePotential = !transitionRunning && evt.getX() < displayWidth / currentForm.getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
initialDragX = evt.getX();
initialDragY = evt.getY();
/*if (sideSwipePotential) {
Component c = Display.getInstance().getCurrent().getComponentAt(initialDragX, initialDragY);
if (c != null && c.shouldBlockSideSwipe()) {
sideSwipePotential = false;
}
}*/
}
};
currentForm.addPointerDraggedListener(pointerDragged);
currentForm.addPointerReleasedListener(pointerReleased);
currentForm.addPointerPressedListener(pointerPressed);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class L10nEditor method initTable.
private void initTable() {
bundleTable.setModel(new AbstractTableModel() {
public int getRowCount() {
return keys.size();
}
public int getColumnCount() {
return 1 + localeList.size();
}
public boolean isCellEditable(int row, int col) {
boolean b = col != 0;
if (b) {
String s = (String) getValueAt(row, col);
return s == null || !s.contains("\n");
}
return b;
}
public String getColumnName(int columnIndex) {
if (columnIndex == 0) {
return "Key";
}
return (String) localeList.get(columnIndex - 1);
}
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return keys.get(rowIndex);
}
Hashtable h = res.getL10N(localeName, (String) localeList.get(columnIndex - 1));
return h.get(keys.get(rowIndex));
}
public void setValueAt(Object val, int rowIndex, int columnIndex) {
res.setModified();
if (columnIndex == 0) {
if (!keys.contains(val)) {
// ...
}
return;
}
// Hashtable h = (Hashtable)bundle.get(localeList.get(columnIndex - 1));
// h.put(keys.get(rowIndex), val);
String currentKey = (String) keys.get(rowIndex);
res.setLocaleProperty(localeName, (String) localeList.get(columnIndex - 1), currentKey, val);
if (currentKey.equals("@im")) {
StringTokenizer tok = new StringTokenizer((String) val, "|");
boolean modified = false;
while (tok.hasMoreTokens()) {
String currentIm = tok.nextToken();
if ("ABC".equals(currentIm) || "123".equals(currentIm) || "Abc".equals(currentIm) || "abc".equals(currentIm)) {
continue;
}
String prop = "@im-" + currentIm;
if (!keys.contains(prop)) {
keys.add(prop);
for (Object locale : localeList) {
res.setLocaleProperty(localeName, (String) locale, prop, "");
}
modified = true;
}
}
if (modified) {
fireTableDataChanged();
}
return;
}
if (currentKey.equals("@vkb")) {
boolean modified = false;
StringTokenizer tok = new StringTokenizer((String) val, "|");
while (tok.hasMoreTokens()) {
String currentIm = tok.nextToken();
if ("ABC".equals(currentIm) || "123".equals(currentIm) || ".,123".equals(currentIm) || ".,?".equals(currentIm)) {
continue;
}
String prop = "@vkb-" + currentIm;
if (!keys.contains(prop)) {
keys.add(prop);
for (Object locale : localeList) {
res.setLocaleProperty(localeName, (String) locale, prop, "");
}
modified = true;
}
}
if (modified) {
fireTableDataChanged();
}
}
}
});
bundleTable.setDefaultRenderer(Object.class, new SwingRenderer() {
private JCheckBox chk = new JCheckBox();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (column > 0) {
// constant value
String key = (String) keys.get(row);
if (key.startsWith("@")) {
if (key.equalsIgnoreCase("@rtl")) {
chk.setSelected(value != null && "true".equalsIgnoreCase(value.toString()));
updateComponentSelectedState(chk, isSelected, table, row, column, hasFocus);
return chk;
}
if (key.startsWith("@vkb") || key.startsWith("@im")) {
JButton b = new JButton("...");
updateComponentSelectedState(b, isSelected, table, row, column, hasFocus);
return b;
}
}
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
bundleTable.setDefaultEditor(Object.class, new DefaultCellEditor(new JTextField()) {
private Object currentValue;
String editedKey;
private DefaultCellEditor standardEditor = new DefaultCellEditor(new JTextField());
private DefaultCellEditor buttonEditor = new DefaultCellEditor(new JTextField()) {
private JButton button = new JButton("...");
{
button.setBorderPainted(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editedKey.equals("@vkb") || editedKey.equals("@im")) {
currentValue = editInputModeOrder((String) currentValue, editedKey.equals("@vkb"));
fireEditingStoppedExt();
return;
}
/*if(editedKey.startsWith("@vkb")) {
VKBEditor v = new VKBEditor(button, editedKey.substring(5), (String)currentValue);
currentValue = v.getValue();
fireEditingStoppedExt();
return;
}*/
if (editedKey.startsWith("@im")) {
currentValue = editTextFieldInputMode((String) currentValue);
fireEditingStoppedExt();
return;
}
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
editedKey = (String) keys.get(row);
return button;
}
};
private DefaultCellEditor checkBoxEditor = new DefaultCellEditor(new JCheckBox()) {
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return super.getTableCellEditorComponent(table, new Boolean("true".equalsIgnoreCase((String) value)), isSelected, row, column);
}
public Object getCellEditorValue() {
Boolean b = (Boolean) super.getCellEditorValue();
if (b.booleanValue()) {
return "true";
}
return "false";
}
};
private TableCellEditor current = standardEditor;
{
buttonEditor.setClickCountToStart(1);
checkBoxEditor.setClickCountToStart(1);
}
private void updateEditor(int row) {
// constant value
final String key = (String) keys.get(row);
if (key.startsWith("@")) {
if (key.equalsIgnoreCase("@rtl")) {
current = checkBoxEditor;
return;
}
if (key.startsWith("@vkb") || key.startsWith("@im")) {
current = buttonEditor;
return;
}
}
current = standardEditor;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
updateEditor(row);
currentValue = value;
return current.getTableCellEditorComponent(table, value, isSelected, row, column);
}
public void fireEditingStoppedExt() {
fireEditingStopped();
}
public Object getCellEditorValue() {
if (current == buttonEditor) {
return currentValue;
}
return current.getCellEditorValue();
}
public boolean stopCellEditing() {
return current.stopCellEditing();
}
public void cancelCellEditing() {
current.cancelCellEditing();
}
public void addCellEditorListener(CellEditorListener l) {
current.addCellEditorListener(l);
super.addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
current.removeCellEditorListener(l);
super.removeCellEditorListener(l);
}
public boolean isCellEditable(EventObject anEvent) {
return current.isCellEditable(anEvent);
}
public boolean shouldSelectCell(EventObject anEvent) {
return current.shouldSelectCell(anEvent);
}
});
locales.setModel(new DefaultComboBoxModel(localeList.toArray()));
removeLocale.setEnabled(localeList.size() > 1);
}
Aggregations