📄 fjreport.java
字号:
public BoldFontAction(){
super("", new ImageIcon(instance.getClass().getResource("resources/bold.gif")));
}
public void actionPerformed(ActionEvent arg0) {
if (currentPage == null || currentPage.currentCell == null) return;
Font f = currentPage.currentCell.getFont();
currentPage.currentCell.setFont(new Font(f.getName(), f.getStyle() ^ Font.BOLD, f.getSize()));
updateUI();
}
}
class ItalicFontAction extends AbstractAction{
public ItalicFontAction(){
super("", new ImageIcon(instance.getClass().getResource("resources/italic.gif")));
}
public void actionPerformed(ActionEvent arg0) {
if (currentPage == null || currentPage.currentCell == null) return;
Font f = currentPage.currentCell.getFont();
currentPage.currentCell.setFont(new Font(f.getName(), f.getStyle() ^ Font.ITALIC, f.getSize()));
updateUI();
}
}
/**
* get toolbar pane of report.
* @return the panel containing newPageButton, removePageButton,
* prePageButton, nextPageButton, chooseCellFontNameComboBox,
* chooseCellFontSizeComboBox and 3 alignment buttons.
* All components are already combined with corresponding actionlistener.
*/
public JPanel getEditToolBarPane(){
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
fl.setVgap(0);
fl.setHgap(4);
JPanel editPane = new JPanel(fl);
comboFont = new JComboBox(CommonUtil.fontNames);
comboSize = new JComboBox(CommonUtil.fontSizes);
// comboSize.setEditable(true);
comboFont.setToolTipText(StringResource.getString("comboFontHint"));
comboSize.setToolTipText(StringResource.getString("comboSizeHint"));
comboFont.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (currentPage == null || currentPage.currentCell == null) return;
Font f = currentPage.currentCell.getFont();
currentPage.currentCell.setFont(new Font((String) comboFont.getSelectedItem(), f.getStyle(), f.getSize()));
updateUI();
}});
comboSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (currentPage == null || currentPage.currentCell == null) return;
Font f = currentPage.currentCell.getFont();
currentPage.currentCell.setFont(new Font(f.getFontName(), f.getStyle(), Integer.parseInt((String)comboSize.getSelectedItem())));
updateUI();
}});
JButton btnBold = new JButton(new BoldFontAction());
JButton btnItalic = new JButton(new ItalicFontAction());
JButton btnLeft = new JButton(new LeftAlignAction());
JButton btnCenter = new JButton(new CenterAlignAction());
JButton btnRight = new JButton(new RightAlignAction());
JButton btnNewPage = new JButton(new NewPageAction(this));
JButton btnRemovePage = new JButton(new RemovePageAction(this));
JButton btnPrePage = new JButton(new PrePageAction(this));
JButton btnNextPage = new JButton(new NextPageAction(this));
btnBold.setToolTipText(StringResource.getString("btnBoldHint"));
btnItalic.setToolTipText(StringResource.getString("btnItalicHint"));
btnLeft.setToolTipText(StringResource.getString("btnLeftHint"));
btnCenter.setToolTipText(StringResource.getString("btnCenterHint"));
btnRight.setToolTipText(StringResource.getString("btnRightHint"));
btnBold.setPreferredSize(new Dimension(26, 28));
btnItalic.setPreferredSize(new Dimension(28, 28));
btnLeft.setPreferredSize(new Dimension(28, 28));
btnCenter.setPreferredSize(new Dimension(28, 28));
btnRight.setPreferredSize(new Dimension(28, 28));
btnNewPage.setPreferredSize(new Dimension(28, 28));
btnRemovePage.setPreferredSize(new Dimension(28, 28));
btnPrePage.setPreferredSize(new Dimension(28, 28));
btnNextPage.setPreferredSize(new Dimension(28, 28));
ButtonGroup bg = new ButtonGroup();
bg.add(btnLeft);
bg.add(btnCenter);
bg.add(btnRight);
editPane.add(btnNewPage);
editPane.add(btnRemovePage);
editPane.add(btnPrePage);
editPane.add(btnNextPage);
editPane.add(comboFont);
editPane.add(comboSize);
editPane.add(btnBold);
editPane.add(btnItalic);
editPane.add(btnLeft);
editPane.add(btnCenter);
editPane.add(btnRight);
return editPane;
}
/**
* get pages of report.
*/
public List getPages() {
return pages;
}
/**
* set pages of report.
* @param pages the new pages
*/
public void setPages(List pages) {
this.pages = pages;
}
/**
* @return current page
*/
public FJReportPage getCurrentPage() {
return currentPage;
}
/**
* set current page index, page 0 is first page.
* @param currentPageIndex
*/
public void setCurrentPageIndex(int currentPageIndex) {
if (pages == null || pages.size() == 0) return;
if (currentPageIndex >= pages.size()) currentPageIndex = pages.size() - 1;
else if (currentPageIndex < 0) currentPageIndex = 0;
this.currentPageIndex = currentPageIndex;
this.currentPage = (FJReportPage) pages.get(currentPageIndex);
if (state == EDIT_STATE) addAllEditor();
String pageMsg = StringResource.getString("pageMessage");
pageMsg = pageMsg.replace("%p", String.valueOf(currentPageIndex + 1));
pageMsg = pageMsg.replace("%totalpage", String.valueOf(pages.size()));
notifyStatusChange(1, pageMsg);
updateUI();
}
public void nextPage(){
setCurrentPageIndex(currentPageIndex + 1);
}
public void prePage(){
setCurrentPageIndex(currentPageIndex - 1);
}
public void lastPage() {
setCurrentPageIndex(pages.size() - 1);
}
public void firstPage() {
setCurrentPageIndex(0);
}
public void insertPage(){
if (pages == null) pages = new ArrayList();
// pages.add(currentPageIndex, new FJReportPage());
// setCurrentPageIndex(currentPageIndex);
pages.add(new FJReportPage());
lastPage();
addBorders();
reCalcCells();
updateUI();
}
public void removePage(FJReportPage page){
if (pages == null || pages.size() < 2 || page == null) return;
pages.remove(page);
setCurrentPageIndex(currentPageIndex);
}
private List statusListeners = new ArrayList();
/*
* when page change, a "page n of m" message will be fired.
* @see net.sf.fjreport.statusbar.HasStatus#addStatusChangeListener(net.sf.fjreport.statusbar.StatusChangeListener)
*/
public void addStatusChangeListener(StatusChangeListener statusListener) {
statusListeners.add(statusListener);
}
public void removeStatusChangeListener(StatusChangeListener statusListener) {
statusListeners.remove(statusListener);
}
private void notifyStatusChange(int messageType, String message){
for (int i = 0; i < statusListeners.size(); i++) {
((StatusChangeListener)statusListeners.get(i)).statusChange(messageType, message);
}
}
/**
* printer page setup
*/
public void printSetup() {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.validatePage(job.pageDialog(pageFormat));
if (pf != null && !CommonUtil.isPageEqual(pf, pageFormat))
setPageFormat(pf);
}
/**
* save report to a file
* @param fileName the file name the report be saved to
*/
public void saveReport(String fileName){
try {
FJReportIO.saveReport(fileName, this);
} catch (TransformerException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
/**
* load a report from file.
* @param fileName file name of the report be loaded from
*/
public void loadReport(String fileName) {
try {
FJReportIO.loadReport(fileName, this);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadReport(InputStream is) {
try {
FJReportIO.loadReport(is, this);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* a save file chooser will be poped out before calling saveReport() function.
*/
public File saveDialog() {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new ReportFileFilter());
if(chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
saveReport(chooser.getSelectedFile().getAbsolutePath());
return chooser.getSelectedFile();
} return null;
}
/**
* an open file chooser will be poped out before calling loadReport() function.
*/
public File loadDialog() {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new ReportFileFilter());
if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
loadReport(chooser.getSelectedFile().getAbsolutePath());
return chooser.getSelectedFile();
} return null;
}
/**
* Each cell except type_null cell is identified by a string which is the
* cell's name. Each null cell has value (mostly is instanceof String) which
* is shown to end user. The cell value can be access via
* FJReport.getValue(String name) and FJReport.setValue(String name, Object
* value). Use FJReport.getCellByName(String name) to access a specific
* cell.
*
* @param name
* @param value
*/
public void setValue(String name, Object value) {
Cell cell = getCellByName(name);
if (cell != null) cell.setValue(value);
}
/**
* @see FJReport#setValue(String, Object) setValue
* @param name
* @return value
*/
public Object getValue(String name) {
Cell cell = getCellByName(name);
if (cell != null) return cell.getValue();
else return null;
}
/**
* @see FJReport#setValue(String, Object) setValue
* @param name
* @return cell
*/
public Cell getCellByName(String name){
return (Cell) cellMap.get(name);
}
public void buildMap(){
cellMap = new HashMap();
for(int i = 0; i < pages.size(); i ++) {
List cells = ((FJReportPage)pages.get(i)).cells;
for(int j = cells.size() - 1; j >= 0 ; j--) {
Cell cell = (Cell) cells.get(j);
cellMap.put(cell.getName(), cell);
}
}
}
private List<CellValueChangeListener> valueChangedActions;
/**
* when user change a cell's value, a ValueChanged event will occur.
* @param listener
*/
public void addValueChangedActionListener(CellValueChangeListener listener){
if (valueChangedActions == null) valueChangedActions = new ArrayList();
valueChangedActions.add(listener);
}
public void removeValueChangeActionListener(CellValueChangeListener listener) {
valueChangedActions.remove(listener);
}
public boolean hasValueChangedListener(){
return (valueChangedActions != null && valueChangedActions.size() > 0);
}
public void fireValueChanged(Cell cell, Object newValue, Object oldValue){
for(int i = valueChangedActions.size() - 1; i >= 0; i--) {
((CellValueChangeListener)valueChangedActions.get(i)).valueChangeAction(cell, newValue, oldValue);
}
}
private List<CellEnterActionListener> cellEnterActions;
/**
*
* cellEnter, occurs when cell grabs focus.
* Note that only cells with editors can activate this event
* @param listener
*/
public void addCellEnterActionListener(CellEnterActionListener listener){
if (cellEnterActions == null) cellEnterActions = new ArrayList();
cellEnterActions.add(listener);
}
public void removeCellEnterActionListener(CellEnterActionListener listener) {
cellEnterActions.remove(listener);
}
public boolean hasCellEnterListener(){
return (cellEnterActions != null && cellEnterActions.size() > 0);
}
public void fireCellEnter(Cell cell){
for(int i = cellEnterActions.size() - 1; i >= 0; i--) {
((CellEnterActionListener)cellEnterActions.get(i)).cellEnterAction(cell);
}
}
private List<CellLeaveActionListener> cellLeaveActions;
/**
* cellLeave, occurs when cell loose focus.
* Note that only cells with editors can activate this event
* @param listener
*/
public void addCellLeaveActionListener(CellLeaveActionListener listener){
if (cellLeaveActions == null) cellLeaveActions = new ArrayList();
cellLeaveActions.add(listener);
}
public void removeCellLeaveActionListener(CellLeaveActionListener listener) {
cellLeaveActions.remove(listener);
}
public boolean hasCellLeaveListener(){
return (cellLeaveActions != null && cellLeaveActions.size() > 0);
}
public void fireCellLeave(Cell cell){
for(int i = cellLeaveActions.size() - 1; i >= 0; i--) {
((CellLeaveActionListener)cellLeaveActions.get(i)).cellLeaveActionListener(cell);
}
}
public void preview() {}
public int getCurrentPageIndex() {
return currentPageIndex;
}
// class MyKeyboardManager extends DefaultKeyboardFocusManager {
//
// public boolean dispatchKeyEvent(KeyEvent e) {
// System.out.println(e.getID());
// if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
// System.out.println("Key Pressed");
// return true;
// }
// return super.dispatchKeyEvent(e);
// }
// }
public int getLineID() {
return currentPage.getLineID();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -