1 29 30 package echo2example.email; 31 32 import java.io.Serializable ; 33 import java.util.EventListener ; 34 import java.util.EventObject ; 35 36 import nextapp.echo2.app.Button; 37 import nextapp.echo2.app.Extent; 38 import nextapp.echo2.app.Label; 39 import nextapp.echo2.app.Row; 40 import nextapp.echo2.app.TextField; 41 import nextapp.echo2.app.event.ActionEvent; 42 import nextapp.echo2.app.event.ActionListener; 43 44 47 public class PageNavigator extends Row { 48 49 53 public static interface PageIndexChangeListener extends EventListener , Serializable { 54 55 60 public void pageIndexChanged(PageIndexChangeEvent e); 61 } 62 63 66 public class PageIndexChangeEvent extends EventObject { 67 68 private int newPageIndex; 69 70 75 private PageIndexChangeEvent(int newPageIndex) { 76 super(PageNavigator.this); 77 this.newPageIndex = newPageIndex; 78 } 79 80 85 public int getNewPageIndex() { 86 return newPageIndex; 87 } 88 } 89 90 private Label totalPagesLabel; 91 private TextField pageField; 92 private int pageIndex, totalPages; 93 94 97 public PageNavigator() { 98 super(); 99 setCellSpacing(new Extent(20)); 100 101 Button previousPageButton = new Button(Styles.ICON_24_LEFT_ARROW); 102 previousPageButton.setRolloverEnabled(true); 103 previousPageButton.setRolloverIcon(Styles.ICON_24_LEFT_ARROW_ROLLOVER); 104 previousPageButton.addActionListener(new ActionListener() { 105 public void actionPerformed(ActionEvent e) { 106 setPageIndex(getPageIndex() - 1); 107 } 108 }); 109 add(previousPageButton); 110 111 Row entryRow = new Row(); 112 entryRow.setCellSpacing(new Extent(5)); 113 add(entryRow); 114 115 Label itemLabel = new Label(Messages.getString("PageNavigator.ItemLabel")); 116 entryRow.add(itemLabel); 117 118 pageField = new TextField(); 119 pageField.setStyleName("PageNavigator.PageField"); 120 pageField.setWidth(new Extent(4, Extent.EX)); 121 pageField.setText("1"); 122 pageField.addActionListener(new ActionListener() { 123 public void actionPerformed(ActionEvent e) { 124 try { 125 setPageIndex(Integer.parseInt(pageField.getText()) - 1); 126 } catch (NumberFormatException ex) { 127 setPageIndex(getPageIndex()); 128 } 129 } 130 }); 131 entryRow.add(pageField); 132 133 Label prepositionLabel = new Label(Messages.getString("PageNavigator.PrepositionLabel")); 134 entryRow.add(prepositionLabel); 135 136 totalPagesLabel = new Label("1"); 137 entryRow.add(totalPagesLabel); 138 139 Button nextPageButton = new Button(Styles.ICON_24_RIGHT_ARROW); 140 nextPageButton.setRolloverEnabled(true); 141 nextPageButton.setRolloverIcon(Styles.ICON_24_RIGHT_ARROW_ROLLOVER); 142 nextPageButton.addActionListener(new ActionListener() { 143 public void actionPerformed(ActionEvent e) { 144 setPageIndex(getPageIndex() + 1); 145 } 146 }); 147 add(nextPageButton); 148 } 149 150 155 public void addPageIndexChangeListener(PageIndexChangeListener l) { 156 getEventListenerList().addListener(PageIndexChangeListener.class, l); 157 } 158 159 163 private void firePageIndexChanged() { 164 EventListener [] listeners = getEventListenerList().getListeners(PageIndexChangeListener.class); 165 PageIndexChangeEvent e = new PageIndexChangeEvent(getPageIndex()); 166 for (int i = 0; i < listeners.length; ++i) { 167 ((PageIndexChangeListener) listeners[i]).pageIndexChanged(e); 168 } 169 } 170 171 176 public int getPageIndex() { 177 return pageIndex; 178 } 179 180 185 public int getTotalPages() { 186 return totalPages; 187 } 188 189 194 public void removePageIndexChangeListener(PageIndexChangeListener l) { 195 getEventListenerList().removeListener(PageIndexChangeListener.class, l); 196 } 197 198 203 public void setPageIndex(int pageIndex) { 204 if (pageIndex < 0) { 205 pageIndex = 0; 206 } 207 if (pageIndex > 0 && pageIndex > totalPages - 1) { 208 pageIndex = totalPages - 1; 209 } 210 this.pageIndex = pageIndex; 211 pageField.setText(Integer.toString(pageIndex + 1)); 212 firePageIndexChanged(); 213 } 214 215 220 public void setTotalPages(int totalPages) { 221 this.totalPages = totalPages; 222 if (totalPages == 0) { 223 totalPagesLabel.setText("1"); 224 } else { 225 totalPagesLabel.setText(Integer.toString(totalPages)); 226 } 227 } 228 } 229 | Popular Tags |