|
From: Nicolas P. <nic...@us...> - 2005-04-09 21:56:56
|
Update of /cvsroot/hipgmap/gmap/com/trileet/gmap In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9664/com/trileet/gmap Modified Files: HitBox.java SearchWindow.java Log Message: Make search results much prettier, ahh, the joys of bold. Index: HitBox.java =================================================================== RCS file: /cvsroot/hipgmap/gmap/com/trileet/gmap/HitBox.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** HitBox.java 3 Apr 2005 11:21:53 -0000 1.10 --- HitBox.java 9 Apr 2005 21:56:48 -0000 1.11 *************** *** 2,5 **** --- 2,6 ---- import danger.ui.*; + import danger.util.LineBreaker; import danger.app.*; *************** *** 23,27 **** private int debug = 0; ! public HitBox(Hit h) { // Do all the usual StaticTextBox stuff first. super(); --- 24,32 ---- private int debug = 0; ! /** Horizontal and vertical margins for our text */ ! public static final int V_MARGIN = 4; ! public static final int H_MARGIN = 3; ! ! public HitBox(Hit h, int width) { // Do all the usual StaticTextBox stuff first. super(); *************** *** 32,60 **** // Set our default click to show the hit this.setEvent(new Event(Application.getCurrentApp(),SHOW_HIT,0,0,m_hit)); ! this.setAutoResize(true); ! String s = new String(); ! if (h.title.length() > 0) { ! if (s.length()>0) s+="\n"; ! s += h.title; ! } ! if (h.address1.length() > 0) { ! if (s.length()>0) s+="\n"; ! s += h.address1; ! } ! if (h.address2.length() > 0) { ! if (s.length()>0) s+="\n"; ! s += h.address2; ! } ! if (h.address3.length() > 0) { ! if (s.length()>0) s+="\n"; ! s += h.address3; ! } ! if (h.phone.length() > 0) { ! if (s.length()>0) s+="\n"; ! s += h.phone; ! } ! this.setText(s); ! } --- 37,56 ---- // Set our default click to show the hit this.setEvent(new Event(Application.getCurrentApp(),SHOW_HIT,0,0,m_hit)); + this.setAutoResize(false); ! setWidth(width); ! ! // height starts out being our top and bottom vertical margins ! int currentY = V_MARGIN * 2; ! Font bf = Font.findBoldSystemFont(); ! Font f = Font.findSystemFont(); ! ! currentY = paintLine(null, bf, m_hit.title, currentY); ! currentY = paintLine(null, f, m_hit.address1, currentY); ! currentY = paintLine(null, f, m_hit.address2, currentY); ! currentY = paintLine(null, f, m_hit.address3, currentY); ! currentY = paintLine(null, bf, m_hit.phone, currentY); ! ! setHeight(currentY); } *************** *** 62,74 **** p.push(); if (isFocused()) { ! this.setBackgroundColor(Color.BLUE); p.setColor(Color.WHITE); } else { ! this.setBackgroundColor(Color.WHITE); p.setColor(Color.BLACK); } ! super.paint(p); p.pop(); } void adjustContextMenuState(Menu menu) { --- 58,132 ---- p.push(); if (isFocused()) { ! p.setColor(Color.BLUE); ! p.fillRoundRect(0, 0, getWidth(), getHeight(), 3, 3); p.setColor(Color.WHITE); } else { ! p.setColor(Color.WHITE); ! p.fillRect(0, 0, getWidth(), getHeight()); p.setColor(Color.BLACK); } ! ! Font bf = Font.findBoldSystemFont(); ! Font f = Font.findSystemFont(); ! ! int currentY = bf.getAscent() + V_MARGIN; ! ! p.setFont(bf); ! currentY = paintLine(p, bf, m_hit.title, currentY); ! ! p.setFont(f); ! currentY = paintLine(p, f, m_hit.address1, currentY); ! currentY = paintLine(p, f, m_hit.address2, currentY); ! currentY = paintLine(p, f, m_hit.address3, currentY); ! ! p.setFont(bf); ! currentY = paintLine(p, bf, m_hit.phone, currentY); ! p.pop(); } + + /** + * Paints the passed in line, using linebreaks as necessary + * + * + * @param p The pen to draw to, optionally null if no painting is desired + * @param f The font to use + * @param line The line to paint + * @param currentY The current Y position to start at + * @return The new Y position to start drawing at + */ + private int paintLine(Pen p, Font f, String line, int currentY){ + // Nothing to do, move on + if (line.length() <= 0){ + return currentY; + } + + // Will this line fit? + int availWidth = getWidth() - H_MARGIN * 2; + + // If not, break it + if (f.getWidth(line) >= availWidth){ + LineBreaker breaker = new LineBreaker(line, f, availWidth); + int lastBreak = 0; + int currentBreak = 0; + while((currentBreak = breaker.nextLineBreak(lastBreak)) < line.length()){ + String segment = line.substring(lastBreak, currentBreak); + if (p != null) p.drawText(H_MARGIN, currentY, segment); + currentY += f.getAscent() + f.getDescent(); + lastBreak = currentBreak; + } + + String segment = line.substring(lastBreak); + if (p != null) p.drawText(H_MARGIN, currentY, segment); + currentY += f.getAscent() + f.getDescent(); + } + // Otherwise, draw it + else { + if (p != null) p.drawText(H_MARGIN, currentY, line); + currentY += f.getAscent() + f.getDescent(); + } + + return currentY; + } void adjustContextMenuState(Menu menu) { Index: SearchWindow.java =================================================================== RCS file: /cvsroot/hipgmap/gmap/com/trileet/gmap/SearchWindow.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** SearchWindow.java 4 Apr 2005 03:00:19 -0000 1.16 --- SearchWindow.java 9 Apr 2005 21:56:48 -0000 1.17 *************** *** 69,83 **** if(false) { Vector hits = new Vector(); ! Hit hit = new Hit("test", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "asdf"; hits.addElement(hit); ! hit = new Hit("Another test", new hipfloat(1), new hipfloat(1)); hits.addElement(hit); ! hit = new Hit("test3", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "asdf"; ! hit.phone="8005551212"; hits.addElement(hit); ! hit = new Hit("test4", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "aoeu"; hit.phone="8005551212"; hits.addElement(hit); --- 69,86 ---- if(false) { Vector hits = new Vector(); ! Hit hit = new Hit("Hit numero uno", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "10100 ne 7th"; ! hit.address2 = "Santa Cruz, CA 12452"; ! hit.phone = "(235) 124-5323"; hits.addElement(hit); ! hit = new Hit("Hit Numero Deux", new hipfloat(1), new hipfloat(1)); hits.addElement(hit); ! hit = new Hit("Three Hits, I love counting Hits", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "1400 Hit Ave"; ! hit.address2 = "Hollywood, CA 12532"; ! hit.phone="(800) 555-1212"; hits.addElement(hit); ! hit = new Hit("Four Hits, The fourth longer than the previous three, Four hits I say!", new hipfloat(1), new hipfloat(1)); ! hit.address1 = "More Addresses for the Fourth Hit"; hit.phone="8005551212"; hits.addElement(hit); *************** *** 89,96 **** setHits(); GMap.getMapWindow().setHits(); - //*/ } else { ! // Show our help cells showHelp(); } --- 92,98 ---- setHits(); GMap.getMapWindow().setHits(); } else { ! // Show our help cells showHelp(); } *************** *** 346,351 **** private void addHit(Hit h) { ! HitBox hb = new HitBox(h); ! hb.setSize(218, 10); m_results.addChild(hb); hb.setFont(Font.findFont("Bort9")); --- 348,352 ---- private void addHit(Hit h) { ! HitBox hb = new HitBox(h, 218); m_results.addChild(hb); hb.setFont(Font.findFont("Bort9")); *************** *** 354,379 **** m_currentY += hb.getHeight(); } public void handleVCard( IPCMessage ipcm) { ! String vcard = ipcm.findString("vcard"); ! StringBuffer munged = new StringBuffer(); ! /** Parse the VCard content and get the searchable address. ! * TODO: This needs a lot of improvement... ! **/ ! int i = vcard.indexOf("ADR;"); ! if (i<0) { ! return; } ! i = vcard.indexOf(":",i); ! int j; ! do { ! j = vcard.indexOf("\r\n",i); ! munged.append(vcard.substring(i+1,j)); ! i = j+2; ! } while (vcard.substring(j+2,j+3).equals(" ")); ! vcard = munged.toString(); ! vcard = vcard.replace(';',','); ! m_searchField.insert(vcard); ! ! } /** Our search field */ private TextField m_searchField; --- 355,382 ---- m_currentY += hb.getHeight(); } + public void handleVCard( IPCMessage ipcm) { ! String vcard = ipcm.findString("vcard"); ! StringBuffer munged = new StringBuffer(); ! /** ! * Parse the VCard content and get the searchable address. TODO: This ! * needs a lot of improvement... ! */ ! int i = vcard.indexOf("ADR;"); ! if (i < 0) { ! return; ! } ! i = vcard.indexOf(":", i); ! int j; ! do { ! j = vcard.indexOf("\r\n", i); ! munged.append(vcard.substring(i + 1, j)); ! i = j + 2; ! } while (vcard.substring(j + 2, j + 3).equals(" ")); ! vcard = munged.toString(); ! vcard = vcard.replace(';', ','); ! m_searchField.insert(vcard); } ! /** Our search field */ private TextField m_searchField; |