private void add() { // validate input.. if (nameTxt.getText().equals("") || nameTxt.getText() == null && urlTxt.getText().equals("") || urlTxt.getText() == null) { // invalid input.. Dialog.alert("Enter Feed Name and URL"); } else { // get the new feed name and url String feedName = nameTxt.getText(); String feedUrl = urlTxt.getText(); if (feedUrl.indexOf("http://") == -1) { Dialog.alert("Feed URL must begin with http://"); } else { // create new Feed object/store it at same time Feed newFeed = new Feed(feedName, feedUrl); databaseManager.getFeeds().addElement(newFeed); // add the new Feed to the end of the feeds list list.insert(list.getSize()); FeedsListCallback callback = (FeedsListCallback) list.getCallback(); callback.insert(newFeed); // close dialog.. close(); } } }
public class AddFeedDialog extends PopupScreen { private ObjectListField list; private EditField nameTxt; private EditField urlTxt; private ButtonField addBtn; private ButtonField cancelBtn; private DatabaseManager databaseManager = DatabaseManager.getInstance(); public AddFeedDialog(ObjectListField list) { super( new VerticalFieldManager(VERTICAL_SCROLLBAR | VERTICAL_SCROLL), DEFAULT_MENU | Field.FOCUSABLE); this.list = list; // title label to sit at top of screen LabelField titleLbl = new LabelField("Add a Feed", Field.FIELD_HCENTER); titleLbl.setFont(titleLbl.getFont().derive(Font.BOLD)); titleLbl.setMargin(new XYEdges(0, 0, 10, 0)); // top,right,bottom,left // set up fields.. LabelField nameLbl = new LabelField("Name"); nameTxt = new TextField(500); LabelField urlLbl = new LabelField("Address"); urlTxt = new TextField(500, EditField.FILTER_URL); addBtn = new ButtonField("Add", ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER); cancelBtn = new ButtonField("Cancel", ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER); // add everything to the screen.. HorizontalFieldManager buttonsManager = new HorizontalFieldManager(Field.FIELD_HCENTER); buttonsManager.setMargin(new XYEdges(10, 0, 5, 0)); // top,right,bottom,left buttonsManager.add(addBtn); buttonsManager.add(cancelBtn); this.add(titleLbl); this.add(nameLbl); this.add(nameTxt); this.add(urlLbl); this.add(urlTxt); this.add(buttonsManager); // add action listeners to buttons.. addBtn.setChangeListener( new FieldChangeListener() { public void fieldChanged(Field field, int context) { add(); } }); cancelBtn.setChangeListener( new FieldChangeListener() { public void fieldChanged(Field field, int context) { cancel(); } }); } public void showDialog() { UiApplication.getUiApplication().pushScreen(this); } private void add() { // validate input.. if (nameTxt.getText().equals("") || nameTxt.getText() == null && urlTxt.getText().equals("") || urlTxt.getText() == null) { // invalid input.. Dialog.alert("Enter Feed Name and URL"); } else { // get the new feed name and url String feedName = nameTxt.getText(); String feedUrl = urlTxt.getText(); if (feedUrl.indexOf("http://") == -1) { Dialog.alert("Feed URL must begin with http://"); } else { // create new Feed object/store it at same time Feed newFeed = new Feed(feedName, feedUrl); databaseManager.getFeeds().addElement(newFeed); // add the new Feed to the end of the feeds list list.insert(list.getSize()); FeedsListCallback callback = (FeedsListCallback) list.getCallback(); callback.insert(newFeed); // close dialog.. close(); } } } private void cancel() { close(); } /* * to handle if the user presses the escape button to * get rid of the dialog/cancel it */ public boolean keyChar(char key, int status, int time) { if (key == Characters.ESCAPE) { this.close(); } else if (key == Characters.ENTER) { add(); } return super.keyChar(key, status, time); } }