📄 jpagination.java
字号:
if (newValue != oldValue && newValue > 0)
{
firePropertyChange("elementsPerPage", new Integer(oldValue), new Integer(newValue));
}
}
/**
* Returns elements per page parameter.
* @return
*/
public int getElementsPerPage()
{
return paginationModel.getElementsPerPage();
}
/**
* Returns true if group actions are eanbled, else false.
* @return
*/
public boolean isGroupActionEnabled()
{
return isGroupActionEnabled;
}
/**
* This function propagates the selectionEvent to selectionModel, where all the selections
* are maintained by selectionModels datastructure.
* @param pageSelectionEvent
*/
protected void fireSelectionValueChanged(PageSelectionEvent pageSelectionEvent)
{
pageSelectionModel.fireSelectionValueChanged(pageSelectionEvent);
}
/**
* Returns a collection of selected page elements user object.
* @return
*/
public Vector getSelectedPageElementsUserObjects()
{
Vector selectedUserObjects = new Vector();
Vector<PageElementIndex> selectedPageIndices = pageSelectionModel.getSelectedPageIndices();
//System.out.println(" ##### selectedPageIndices #####"+selectedPageIndices);
for (int i = 0; i < selectedPageIndices.size(); i++)
{
PageElementIndex pageElementIndex = selectedPageIndices.get(i);
PageElement selectedPageElement = paginationModel.getPageElement(pageElementIndex);
if (selectedPageElement == null)
continue;
Object userObjectOfSelectedPageElement = selectedPageElement.getUserObject();
selectedUserObjects.add(userObjectOfSelectedPageElement);
}
return selectedUserObjects;
}
/**
* Property change listener method.
*/
public void propertyChange(PropertyChangeEvent pcEvent)
{
String propertyName = pcEvent.getPropertyName();
Object newValue = pcEvent.getNewValue();
//System.out.println("propertyChange "+propertyName);
if (propertyName.equals("isGroupActionEnabled"))
{
Boolean newBoolValue = (Boolean) newValue;
if (newBoolValue)
{
this.isGroupActionEnabled = newBoolValue;
//initGUI(); // if this function is called it will automatically set the first page, which is not desirable.
groupActionPanel = new JGroupActionPanel();
add(groupActionPanel, BorderLayout.NORTH);
this.revalidate();
}
else
{
this.isGroupActionEnabled = newBoolValue;
this.remove(groupActionPanel);
this.revalidate();
}
}
else if (propertyName.equals("elementsPerPage"))
{
int newElementsPerPage = ((Integer) newValue).intValue();
if (getElementsPerPage() != newElementsPerPage)
{
paginationModel.setElementsPerPage(newElementsPerPage);
//Vector<PageElement> firstPage = paginationModel.firstPage();
//JXPanel newPagePanel = this.getPagePanel(firstPage);
//this.changePagePanel(newPagePanel);
// TODO page bar should also get updated.
initGUI();
this.revalidate();
}
}
}
/**
* A Panel to hold common page action hyperlinks.
*
* @author chetan_bh
*/
private class JGroupActionPanel extends JXPanel implements ActionListener
{
private Cab2bHyperlink selectAllHyperlink;
private Cab2bHyperlink clearAllHyperlink;
private Cab2bHyperlink invertSelectionHyperlink;
private String selectAllText = PaginationConstants.SELECT_ALL_TEXT;
private String clearAllText = PaginationConstants.CLEAR_ALL_TEXT;
private String invertSelectionText = PaginationConstants.INVERT_SELECTION_TEXT;
public JGroupActionPanel()
{
this(PaginationConstants.SELECT_ALL_TEXT, PaginationConstants.CLEAR_ALL_TEXT,
PaginationConstants.INVERT_SELECTION_TEXT);
}
public JGroupActionPanel(String selectAllText, String clearAllText,
String invertSelectionText)
{
if (selectAllText != null && !(selectAllText.trim().equals("")))
this.selectAllText = selectAllText;
if (clearAllText != null && !(clearAllText.trim().equals("")))
this.clearAllText = clearAllText;
if (invertSelectionText != null && !(invertSelectionText.trim().equals("")))
this.invertSelectionText = invertSelectionText;
initGUI();
}
/**
* Initialize the GUI for Group Action panel.
*/
private void initGUI()
{
this.setLayout(new RiverLayout());
//selectAllHyperlink = new JXHyperlink();
selectAllHyperlink = new Cab2bHyperlink();
selectAllHyperlink.setText(selectAllText);
selectAllHyperlink.addActionListener(this);
this.add("br", selectAllHyperlink);
clearAllHyperlink = new Cab2bHyperlink();
clearAllHyperlink.setText(clearAllText);
clearAllHyperlink.addActionListener(this);
this.add("tab", clearAllHyperlink);
invertSelectionHyperlink = new Cab2bHyperlink();
invertSelectionHyperlink.setText(invertSelectionText);
invertSelectionHyperlink.addActionListener(this);
this.add("tab", invertSelectionHyperlink);
}
/**
* Action listener for Group action hyperlinks.
*/
public void actionPerformed(ActionEvent e)
{
JXHyperlink source = (JXHyperlink) e.getSource();
String sourceText = source.getText();
String currentPageIndex = paginationModel.getCurrentPageIndex();
if (sourceText.equals(selectAllText))
{
pageSelectionModel.selectPage(currentPageIndex);
}
else if (sourceText.equals(clearAllText))
{
pageSelectionModel.clearPage(currentPageIndex);
}
else
{
pageSelectionModel.invertPageSelection(currentPageIndex);
}
JXPanel currentPagePanel = getPagePanel(paginationModel.getPage(currentPageIndex));
changePagePanel(currentPagePanel);
}
}
/**
* This is a component listener for page panel as well as a parent component.
* TODO Needs to do some more work on this.
*
* @author chetan_bh
*/
class PaginationComponentListener extends ComponentAdapter
{
public void componentResized(ComponentEvent e)
{
/**
* There are many conditions that needs to be checked before starting rezise.
* 1) automaticPageResize should be enabled.
* 2) No page resize for last numeric pager.
* 3) No page resize for non-numeric level-1 pager.
*/
// For Condition 1.
if(automaticPageResize == false)
return;
// For Condition 3.
if(!paginationModel.getPagerName().equals(PaginationConstants.NUMERIC_PAGER))
return;
Component parentComponent = e.getComponent();
Dimension pagePanelSize = pagePanel.getSize();
Dimension pagePanelPreferredSize = pagePanel.getPreferredSize();
//System.out.println("pagePanel Size " + pagePanelSize);
//System.out.println("pagePanel PreferredSize "+pagePanel.getPreferredSize());
/*
* On an approximate page elements need 47 pixels each; <<-- this is a kind of
* assumption which may not be correct all the time. TODO Need to find proper
* solution.
*/
int difference = Math.round(Math.round(pagePanelSize.getHeight()-pagePanelPreferredSize.getHeight()));
System.out.println("differenr "+difference);
int currentElementsPerPage = getElementsPerPage();
// For Condition 2.
if(! paginationModel.hasNextPage() && difference > 50)
return;
// TODO Need to remove these hardcoding like 50, -10.
if( difference > 55)
{
int increment = (int)difference / 50;
//System.out.println("number of more page elements that can be accomodated "+increment);
setElementsPerPage(currentElementsPerPage+increment);
}else if(difference < -1)
{
int decrement = (int) difference/50;
decrement -= 1;
//System.out.println("decrement "+decrement);
setElementsPerPage(currentElementsPerPage+decrement);
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
Vector<PageElement> elements = new Vector<PageElement>();
for (int i = 0; i < 132; i++)
{
PageElement element = new PageElementImpl();
element.setDisplayName("ABC-" + i);
elements.add(element);
}
//elements = RealDataForPagination.getRealData(RealDataForPagination.realData);
elements = RealDataForPagination.getRealData(RealDataForPagination.realData);
//Pager pager = new NumericPager(elements);
// Vector<PageElement> elements = new Vector<PageElement>();
//
// String alphabets = "ABCDEFGHHJASDGEWYVCBZXHSH";
// for(int i = 0; i < alphabets.length(); i++)
// {
// PageElement element = new PageElementImpl();
// element.setDisplayName(alphabets.charAt(i)+"-123");
// elements.add(element);
// }
AlphabeticPager pager = new AlphabeticPager(elements);
JFrame frame = new JFrame("JPagination Demo");
JPagination pagination = new JPagination(elements, pager, frame, false);
pagination.setPageElementActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("page element action performed " + e.getSource().getClass());
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(pagination);
frame.setSize(new Dimension(300, 400));
frame.setVisible(true);
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
pagination.setSelectableEnabled(false);
try
{
Thread.sleep(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
pagination.setGroupActionEnabled(false);
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
pagination.setSelectableEnabled(true);
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
pagination.setGroupActionEnabled(true);
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
Vector selectedPageElemntsUserObject = pagination.getSelectedPageElementsUserObjects();
System.out.println("selectedPageelemtns user object lenght "
+ selectedPageElemntsUserObject.size());
//pagination.setElementsPerPage(4); // default is 6;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -