📄 localmidlet.java
字号:
package com.sun.me.web.sample.local;import com.sun.lwuit.Button;import com.sun.lwuit.ComboBox;import com.sun.lwuit.Command;import com.sun.lwuit.Component;import com.sun.lwuit.Container;import com.sun.lwuit.Dialog;import com.sun.lwuit.events.DataChangedListener;import com.sun.lwuit.events.SelectionListener;import com.sun.me.web.request.Response;import java.io.IOException;import javax.microedition.midlet.MIDlet;import com.sun.lwuit.Display;import com.sun.lwuit.Font;import com.sun.lwuit.Form;import com.sun.lwuit.Graphics;import com.sun.lwuit.Image;import com.sun.lwuit.Label;import com.sun.lwuit.List;import com.sun.lwuit.TextField;import com.sun.lwuit.animations.CommonTransitions;import com.sun.lwuit.animations.Transition3D;import com.sun.lwuit.events.ActionEvent;import com.sun.lwuit.events.FocusListener;import com.sun.lwuit.layouts.BorderLayout;import com.sun.lwuit.layouts.BoxLayout;import com.sun.lwuit.list.DefaultListCellRenderer;import com.sun.lwuit.list.ListCellRenderer;import com.sun.lwuit.list.ListModel;import com.sun.lwuit.plaf.Border;import com.sun.lwuit.plaf.Style;import com.sun.me.web.path.Result;import com.sun.me.web.path.ResultException;import com.sun.me.web.request.Arg;import com.sun.me.web.request.Request;import com.sun.me.web.request.RequestListener;import java.io.InputStream;import java.util.Enumeration;import java.util.Vector;import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;/** * Implements the local business search UI of the makeover demo * * @author Shai Almog */public class LocalMIDlet extends MIDlet { /* Demo mode is activiated if network connection fails */ private boolean demoMode = false; private static final int DEFAULT_MAP_ZOOM = 6; private int zoom = DEFAULT_MAP_ZOOM; /* TODO: Get own APP ID for Yahoo Local and Maps APIs */ private static final String APPID = "VS2gtQrV34ElS4obpTabGJ0lxYxDjwPzrjgaj_xTo.VbdnpA24586Jul4oDCXpO3UVN7"; private static final String LOCAL_BASE = "http://local.yahooapis.com/LocalSearchService/V2/localSearch"; private static final String MAP_BASE = "http://api.local.yahoo.com/MapsService/V1/mapImage"; static final Object LOADING_MARKER = new Object(); private boolean started; private Command exitCommand = new Command("Exit") { public void actionPerformed(ActionEvent ev) { notifyDestroyed(); } }; private Command defaultThemeCommand = new Command("Default Theme") { public void actionPerformed(ActionEvent ev) { setTheme("/theme.res"); } }; private Command javaThemeCommand = new Command("Java Theme") { public void actionPerformed(ActionEvent ev) { setTheme("/javaTheme.res"); } }; private void setTheme(String name) { try { com.sun.lwuit.util.Resources res = com.sun.lwuit.util.Resources.open(name); com.sun.lwuit.plaf.UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0])); Display.getInstance().getCurrent().refreshTheme(); } catch(java.io.IOException err) { err.printStackTrace(); } } private static Image getImage(String name) { try { return Image.createImage(name); } catch (IOException ex) { ex.printStackTrace(); return null; } } protected void startApp() { Display.init(this); // distinguish between start and resume from pause if(!started) { started = true; // show your LWUIT form here e.g.: new MyForm().show(); // this is a good place to set your default theme using // the UIManager class e.g.: try { com.sun.lwuit.util.Resources res = com.sun.lwuit.util.Resources.open("/theme.res"); com.sun.lwuit.plaf.UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0])); } catch(java.io.IOException err) { err.printStackTrace(); } showMainForm(); } } private void showMainForm() { Form mainForm = createForm("Local Search"); mainForm.setTransitionInAnimator(Transition3D.createCube(500, false)); mainForm.setTransitionOutAnimator(Transition3D.createCube(500, true)); mainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); mainForm.addComponent(new Label("search for:")); final TextField searchFor = new TextField("coffee", 50); mainForm.addComponent(searchFor); mainForm.addComponent(new Label("location:")); final TextField location = new TextField("95054", 50); mainForm.addComponent(location); mainForm.addComponent(new Label("street:")); final TextField street = new TextField(50); mainForm.addComponent(street); mainForm.addComponent(new Label("sort results by:")); final ComboBox sortResults = new ComboBox(new String[] {"Distance", "Title", "Rating", "Relevance"}); mainForm.addComponent(sortResults); mainForm.addCommand(exitCommand); mainForm.addCommand(defaultThemeCommand); mainForm.addCommand(javaThemeCommand); mainForm.addCommand(new Command("Search") { public void actionPerformed(ActionEvent ev) { showSearchResultForm(searchFor.getText(), location.getText(), street.getText(), (String) sortResults.getSelectedItem()); } }); mainForm.show(); } private void exception(Exception ex) { ex.printStackTrace(); Dialog.show("Error", "Error connecting to search service - Turning on DEMO MODE", "OK", null); demoMode = true; showMainForm(); } private void showSearchResultForm(String searchFor, String location, String street, String sortOrder) { final Form resultForm = createForm("result list"); resultForm.setScrollable(false); resultForm.setLayout(new BorderLayout()); InfiniteProgressIndicator tempIndicator = null; try { tempIndicator = new InfiniteProgressIndicator(Image.createImage("/wait-circle.png")); } catch (IOException ex) { tempIndicator = null; ex.printStackTrace(); } final InfiniteProgressIndicator indicator = tempIndicator; final List resultList = new List(new LocalResultModel(searchFor, location, sortOrder, street)) { public boolean animate() { boolean val = super.animate(); // return true of animate only if there is data loading, this saves battery and CPU if(indicator.animate()) { int index = getSelectedIndex(); index = Math.max(0, index - 4); ListModel model = getModel(); int dest = Math.min(index + 4, model.getSize()); for(int iter = index ; iter < dest ; iter++) { if(model.getItemAt(index) == LOADING_MARKER) { return true; } } } return val; } }; Links pro = new Links(); pro.title = "prototype"; pro.tel = "9999999999"; pro.distance = "9999999"; pro.address = "Long address string"; pro.rating = "5"; resultList.setRenderingPrototype(pro); resultList.setFixedSelection(List.FIXED_NONE_CYCLIC); resultList.getStyle().setBorder(null); resultList.setListCellRenderer(new DefaultListCellRenderer(false) { private Label focus; private Container selected; private Label firstLine; private Label secondLine; private boolean loading; { selected = new Container(new BoxLayout(BoxLayout.Y_AXIS)); firstLine = new Label("First Line"); secondLine = new Label("Second Line"); int iconWidth = 20; firstLine.getStyle().setMargin(LEFT, iconWidth); secondLine.getStyle().setMargin(LEFT, iconWidth); selected.addComponent(firstLine); selected.addComponent(secondLine); } public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) { if(value == null || value == LOADING_MARKER) { loading = true; if(isSelected) { firstLine.setText("Loading..."); secondLine.setText("Loading..."); return selected; } return indicator; } loading = false; if(isSelected) { int listSelectionColor = list.getStyle().getFgSelectionColor(); firstLine.getStyle().setFgColor(listSelectionColor); secondLine.getStyle().setFgColor(listSelectionColor); firstLine.getStyle().setBgTransparency(0); secondLine.getStyle().setBgTransparency(0); Links l = (Links)value; firstLine.setText(l.address + " " + l.tel); secondLine.setText(l.distance + " miles " + ("".equals(l.rating) ? "" : ", " + l.rating + "*")); return selected; } super.getListCellRendererComponent(list, ((Links)value).title, index, isSelected); return this; } public void paint(Graphics g) { if(loading) { indicator.setX(getX()); indicator.setY(getY()); indicator.setWidth(getWidth()); indicator.setHeight(getHeight()); indicator.paint(g); } else { super.paint(g); } } public Component getListFocusComponent(List list) { if(focus == null) { try { focus = new Label(Image.createImage("/svgSelectionMarker.png")); focus.getStyle().setBgTransparency(0); } catch (IOException ex1) { ex1.printStackTrace(); } } return focus; } }); resultForm.addComponent(BorderLayout.CENTER, resultList); resultForm.addCommand(new Command("Map") { public void actionPerformed(ActionEvent ev) { showMap(resultForm, resultList.getSelectedItem()); } }); resultForm.addCommand(new Command("Details") { public void actionPerformed(ActionEvent ev) { showDetails(resultForm, resultList.getSelectedItem()); } }); resultForm.addCommand(new Command("Back") { public void actionPerformed(ActionEvent ev) { showMainForm(); } }); resultForm.addCommand(exitCommand); resultForm.show(); } private Label createSubLabel(String text, int fgColor, Font f) { Label l = new Label(text); Style s = l.getStyle(); s.setFgColor(fgColor); s.setFont(f); return l; } private void showDetails(final Form resultForm, final Object selectedItem) { if(selectedItem != null && selectedItem instanceof Links) { Links l = (Links)selectedItem; int fgColor = resultForm.getStyle().getFgColor(); Font standardFont = resultForm.getStyle().getFont(); Form details = createForm(l.title); details.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); details.addComponent(createSubLabel("address", fgColor, standardFont)); details.addComponent(new Label(l.address)); details.addComponent(createSubLabel("distance", fgColor, standardFont)); details.addComponent(new Label(l.distance + " mi.")); details.addComponent(createSubLabel("average rating", fgColor, standardFont)); details.addComponent(new Label(l.rating + "*")); details.addComponent(createSubLabel("telephone", fgColor, standardFont)); details.addComponent(new Label(l.tel)); details.addCommand(exitCommand); details.addCommand(new Command("Back") { public void actionPerformed(ActionEvent ev) { resultForm.show(); } });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -