/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ package SDDS.java.SDDSedit; import java.io.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.lang.reflect.*; import java.util.*; import java.beans.*; import SDDS.java.SDDS.*; public class sddsEdit extends JFrame implements Printable { static JTable columnTable; static JTable parameterTable; static JScrollPane columnScrollPane; static JScrollPane parameterScrollPane; static JTextArea textArea; static JMenu menu, submenu; static JMenuItem menuItem; static sddsEdit frame; private JMenuBar menubar; private Hashtable commands; public static final String newAction = "new"; public static final String openAction = "open"; public static final String saveAction = "save"; public static final String saveAsAction = "saveas"; public static final String printAction = "print"; public static final String exitAction = "exit"; public static final String attributesParameterAction = "attributesParameter"; public static final String insertParameterAction = "insertParameter"; public static final String deleteParameterAction = "deleteParameter"; public static final String attributesColumnAction = "attributesColumn"; public static final String insertColumnAction = "insertColumn"; public static final String deleteColumnAction = "deleteColumn"; public static final String insertRowAction = "insertRow"; public static final String deleteRowAction = "deleteRow"; public static final String goToPageAction = "goToPage"; public static final String insertPageAction = "insertPage"; public static final String insertAndCopyPageAction = "insertAndCopyPage"; public static final String appendPageAction = "appendPage"; public static final String deletePageAction = "deletePage"; public static final String searchColumnAction = "searchColumn"; public static final String displayInfoAction = "displayInfo"; protected FileDialog fileDialog; static JRadioButton[] asciiBinaryButtons = new JRadioButton[2]; static SDDSFile sdds; static JComboBox pageChooser; static boolean disablePageChooser=false; static int currentPage=0; static DefaultListModel column_dlm = new DefaultListModel(); static DefaultListModel parameter_dlm = new DefaultListModel(); static String insertRows = "1"; static SearchDialog searchDialog = null; static MatchList matchList = null; static DefaultTableModel column_dtm, parameter_dtm; static DefaultTableColumnModel column_dtcm, parameter_dtcm; static JList parameterRowHeader; int [] subTableSplit = null; boolean pageinfoCalculated=false; int totalNumPages=0; int prevPageIndex = 0; int subPageIndex = 0; int subTableSplitSize = 0; int fontHeight, fontDesent; double tableHeightOnFullPage, headerHeight; double pageWidth, pageHeight; double tableHeight, rowHeight; private Action[] defaultActions = { new NewAction(), new OpenAction(), new SaveAction(), new SaveAsAction(), new PrintAction(), new ExitAction(), new AttributesParameterAction(), new InsertParameterAction(), new DeleteParameterAction(), new AttributesColumnAction(), new InsertColumnAction(), new DeleteColumnAction(), new InsertRowAction(), new DeleteRowAction(), new GoToPageAction(), new InsertPageAction(), new InsertAndCopyPageAction(), new AppendPageAction(), new DeletePageAction(), new SearchColumnAction(), new DisplayInfoAction() }; /** * Fetch the list of actions supported by this * editor. It is implemented to return the list * of actions supported by the embedded JTextComponent * augmented with the actions defined locally. */ public Action[] getActions() { return defaultActions; } protected Action getAction(String cmd) { return (Action) commands.get(cmd); } // Yarked from JMenu, ideally this would be public. protected PropertyChangeListener createActionChangeListener(JMenuItem b) { return new ActionChangedListener(b); } // Yarked from JMenu, ideally this would be public. private class ActionChangedListener implements PropertyChangeListener { JMenuItem menuItem; ActionChangedListener(JMenuItem mi) { super(); this.menuItem = mi; } public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if (e.getPropertyName().equals(Action.NAME)) { String text = (String) e.getNewValue(); menuItem.setText(text); } else if (propertyName.equals("enabled")) { Boolean enabledState = (Boolean) e.getNewValue(); menuItem.setEnabled(enabledState.booleanValue()); } } } protected JMenuBar getMenubar() { return menubar; } protected JMenuBar createMenubar() { JMenuItem mi, sub_mi; JMenuBar mb = new JMenuBar(); JMenu m, sub_m; m = new JMenu("File"); mb.add(m); mi = new JMenuItem("New"); SetMenuAction("new",mi); m.add(mi); mi = new JMenuItem("Open..."); SetMenuAction("open",mi); m.add(mi); m.addSeparator(); mi = new JMenuItem("Save"); SetMenuAction("save",mi); m.add(mi); mi = new JMenuItem("Save as..."); SetMenuAction("saveas",mi); m.add(mi); m.addSeparator(); mi = new JMenuItem("Print..."); SetMenuAction("print",mi); m.add(mi); m.addSeparator(); mi = new JMenuItem("Quit"); SetMenuAction("exit",mi); m.add(mi); m = new JMenu("Edit"); mb.add(m); sub_m = new JMenu("Parameter"); m.add(sub_m); mi = new JMenuItem("Attributes..."); SetMenuAction("attributesParameter",mi); sub_m.add(mi); sub_m.addSeparator(); mi = new JMenuItem("Insert..."); SetMenuAction("insertParameter",mi); sub_m.add(mi); mi = new JMenuItem("Delete..."); SetMenuAction("deleteParameter",mi); sub_m.add(mi); sub_m = new JMenu("Column"); m.add(sub_m); mi = new JMenuItem("Attributes..."); SetMenuAction("attributesColumn",mi); sub_m.add(mi); sub_m.addSeparator(); mi = new JMenuItem("Insert..."); SetMenuAction("insertColumn",mi); sub_m.add(mi); mi = new JMenuItem("Delete..."); SetMenuAction("deleteColumn",mi); sub_m.add(mi); sub_m = new JMenu("Row"); m.add(sub_m); mi = new JMenuItem("Insert..."); SetMenuAction("insertRow",mi); sub_m.add(mi); mi = new JMenuItem("Delete..."); SetMenuAction("deleteRow",mi); sub_m.add(mi); sub_m = new JMenu("Page"); m.add(sub_m); mi = new JMenuItem("Go to Page..."); SetMenuAction("goToPage",mi); sub_m.add(mi); sub_m.addSeparator(); mi = new JMenuItem("Insert Page"); SetMenuAction("insertPage",mi); sub_m.add(mi); mi = new JMenuItem("Insert and Copy Current Page"); SetMenuAction("insertAndCopyPage",mi); sub_m.add(mi); mi = new JMenuItem("Append Page"); SetMenuAction("appendPage",mi); sub_m.add(mi); mi = new JMenuItem("Delete Page"); SetMenuAction("deletePage",mi); sub_m.add(mi); m = new JMenu("Search"); mb.add(m); mi = new JMenuItem("Column..."); SetMenuAction("searchColumn",mi); m.add(mi); m = new JMenu("Info"); mb.add(m); m.setActionCommand("displayInfo"); mi = new JMenuItem("About..."); SetMenuAction("displayInfo",mi); m.add(mi); return mb; } protected void SetMenuAction(String actionCommand, JMenuItem mi) { mi.setActionCommand(actionCommand); Action a = getAction(actionCommand); if (a != null) { mi.addActionListener(a); a.addPropertyChangeListener(createActionChangeListener(mi)); mi.setEnabled(a.isEnabled()); } else { mi.setEnabled(false); } } public sddsEdit() { super("SDDS Editor"); // install the command table commands = new Hashtable(); Action[] actions = getActions(); for (int i = 0; i < actions.length; i++) { Action a = actions[i]; commands.put(a.getValue(Action.NAME), a); } menubar = createMenubar(); getContentPane().add(menubar, BorderLayout.NORTH); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setOneTouchExpandable(true); getContentPane().add(splitPane, BorderLayout.CENTER); textArea = new JTextArea(); textArea.setFont(new Font("Courier", Font.PLAIN, 12)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.setEditable(false); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(640, 80)); splitPane.setTopComponent(areaScrollPane); JPanel panel1 = new JPanel(new BorderLayout()); splitPane.setBottomComponent(panel1); JSplitPane dataPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); dataPane.setOneTouchExpandable(true); panel1.add(dataPane, BorderLayout.CENTER); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); JPanel panel2 = new JPanel(gridbag); dataPane.setTopComponent(panel2); JLabel pageChooserLabel = new JLabel("Page", JLabel.CENTER); pageChooserLabel.setForeground(Color.black); c.ipadx = 10; c.weightx = 0.0; c.gridwidth = 1; gridbag.setConstraints(pageChooserLabel, c); panel2.add(pageChooserLabel); pageChooser = new JComboBox(); c.ipadx = 0; gridbag.setConstraints(pageChooser, c); panel2.add(pageChooser); pageChooser.addItem(new Integer(1)); pageChooser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!disablePageChooser) { JComboBox cb = (JComboBox)e.getSource(); int page = cb.getSelectedIndex(); if (currentPage != page) { if (savePage()) { clearPage(); loadPage(page+1); } else pageChooser.setSelectedIndex(currentPage); } } } }); JLabel parameterLabel = new JLabel("Parameters", JLabel.CENTER); parameterLabel.setForeground(Color.black); c.weightx = 1.0; gridbag.setConstraints(parameterLabel, c); panel2.add(parameterLabel); asciiBinaryButtons[0] = new JRadioButton("ascii"); asciiBinaryButtons[1] = new JRadioButton("binary"); ButtonGroup dataTypeButtonGroup = new ButtonGroup(); dataTypeButtonGroup.add(asciiBinaryButtons[0]); dataTypeButtonGroup.add(asciiBinaryButtons[1]); c.weightx = 0.0; gridbag.setConstraints(asciiBinaryButtons[0], c); panel2.add(asciiBinaryButtons[0]); c.gridwidth = c.REMAINDER; gridbag.setConstraints(asciiBinaryButtons[1], c); panel2.add(asciiBinaryButtons[1]); asciiBinaryButtons[0].setSelected(true); parameterTable = new JTable(new DefaultTableModel()) { protected void configureEnclosingScrollPane() { Container p = getParent(); if (p instanceof JViewport) { Container gp = p.getParent(); if (gp instanceof JScrollPane) { JScrollPane scrollPane = (JScrollPane)gp; // Make certain we are the // viewPort's view and not, for // example, the rowHeaderView of the scrollPane - // an implementor of fixed columns might do this. JViewport viewport = scrollPane.getViewport(); if (viewport==null || viewport.getView()!=this) { return; } //scrollPane.setColumnHeaderView(getTableHeader()); scrollPane.getViewport().setBackingStoreEnabled(true); scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder")); } } } }; parameterTable.setColumnSelectionAllowed(false); parameterTable.setPreferredScrollableViewportSize(new Dimension(240, 100)); parameterTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); parameterScrollPane = new JScrollPane(parameterTable); parameterRowHeader = new JList(parameter_dlm); parameterRowHeader.setBackground(parameterTable.getTableHeader().getBackground()); //parameterRowHeader.setFixedCellHeight(parameterTable.getRowHeight() + parameterTable.getRowMargin()); parameterRowHeader.setFixedCellHeight(parameterTable.getRowHeight()); parameterRowHeader.setCellRenderer(new RowHeaderRenderer("parameter")); parameterScrollPane.setRowHeaderView(parameterRowHeader); c.fill = c.BOTH; c.weightx = 1.0; c.weighty = 0.2; gridbag.setConstraints(parameterScrollPane, c); panel2.add(parameterScrollPane); JPanel panel3 = new JPanel(gridbag); dataPane.setBottomComponent(panel3); JLabel columnLabel = new JLabel("Columns", JLabel.CENTER); columnLabel.setForeground(Color.black); c.fill = c.HORIZONTAL; c.weighty = 0.0; c.gridx = 2; c.gridy = 3; c.gridwidth = 1; gridbag.setConstraints(columnLabel, c); panel3.add(columnLabel); columnTable = new JTable(new DefaultTableModel()); columnTable.setCellSelectionEnabled(true); columnTable.setPreferredScrollableViewportSize(new Dimension(240, 150)); columnTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); columnScrollPane = new JScrollPane(columnTable); JList rowHeader; rowHeader = new JList(column_dlm); rowHeader.setBackground(columnTable.getTableHeader().getBackground()); rowHeader.setFixedCellWidth(50); //rowHeader.setFixedCellHeight(columnTable.getRowHeight() + columnTable.getRowMargin()); rowHeader.setFixedCellHeight(columnTable.getRowHeight()); rowHeader.setCellRenderer(new RowHeaderRenderer("column")); columnScrollPane.setRowHeaderView(rowHeader); c.fill = c.BOTH; c.weighty = 1.0; c.gridx = 0; c.gridy = 4; c.gridwidth = c.REMAINDER; c.gridheight = c.REMAINDER; gridbag.setConstraints(columnScrollPane, c); panel3.add(columnScrollPane); column_dtm = (DefaultTableModel)columnTable.getModel(); parameter_dtm = (DefaultTableModel)parameterTable.getModel(); column_dtcm = (DefaultTableColumnModel)columnTable.getColumnModel(); parameter_dtcm = (DefaultTableColumnModel)parameterTable.getColumnModel(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); sdds = new SDDSFile(); } protected static void appendStatus(String s) { textArea.append(s + "\n"); textArea.setCaretPosition(textArea.getDocument().getLength()); } public static void main(String[] args) { String[][] commandLineArgs; String initFile=null; int arguments; frame = new sddsEdit(); frame.pack(); frame.setVisible(true); appendStatus("Ready..."); frame.setTitle("SDDS Editor"); commandLineArgs = SDDSUtil.parseCommandLine(args); arguments = Array.getLength(commandLineArgs); if (arguments==1) { initFile = commandLineArgs[0][0]; frame.loadFile(initFile); } } public void loadFile(String fileName) { int pages; int i; sdds = new SDDSFile(fileName); if (!sdds.readFile()) { appendStatus("Error: " + sdds.getErrors()); return; } clearPage(); loadPage(1); disablePageChooser = true; pageChooser.setSelectedIndex(-1); pageChooser.removeAllItems(); pages = sdds.pageCount(); pageChooser.addItem(new Integer(1)); for (i=2;i<=pages;i++) pageChooser.addItem(new Integer(i)); disablePageChooser = false; if (sdds.asciiFile) asciiBinaryButtons[0].setSelected(true); else asciiBinaryButtons[1].setSelected(true); appendStatus(fileName + " loaded"); frame.setTitle("SDDS Editor - " + fileName); } public void clearPage() { if (columnTable.isEditing()) { columnTable.getCellEditor().stopCellEditing(); } if (parameterTable.isEditing()) { parameterTable.getCellEditor().stopCellEditing(); } columnTable.getSelectionModel().clearSelection(); columnTable.getColumnModel().getSelectionModel().clearSelection(); parameterTable.getSelectionModel().clearSelection(); column_dtm.setNumRows(0); column_dtm.setDataVector(new Object[0][0], new Object[0]); parameter_dtm.setNumRows(0); parameter_dtm.setDataVector(new Object[0][0], new Object[0]); } public void loadPage(int page) { String[] columnNames=null, parameterNames=null; Object[][] columnValues=null; Object[][] columnMajorOrderValues=null; Object[][] parameterValues=null; int i, j, numberOfColumns=0, numberOfParameters=0; int rows; DefaultCellEditor dce; parameterNames = sdds.getParameterNames(); if (parameterNames != null) { numberOfParameters = Array.getLength(parameterNames); parameterValues = new Object[numberOfParameters][1]; parameter_dlm.clear(); for (i=0;i= 0) { TableColumn tc; String name = (String)parameter_dlm.getElementAt(i); int index = sdds.getParameterIndex(name); if (index < 0) { appendStatus("Error: " + sdds.getErrors()); return; } sddsEditAttributesDialog sddsEditAttributesDialog = new sddsEditAttributesDialog(frame); sddsEditAttributesDialog.pack(); sddsEditAttributesDialog.setAttributesMode(true); sddsEditAttributesDialog.setName(name); sddsEditAttributesDialog.setSymbol(sdds.getParameterSymbol(index)); sddsEditAttributesDialog.setUnits(sdds.getParameterUnits(index)); sddsEditAttributesDialog.setDescription(sdds.getParameterDescription(index)); sddsEditAttributesDialog.setFormatString(sdds.getParameterFormatString(index)); sddsEditAttributesDialog.setType(sdds.getParameterType(index)); sddsEditAttributesDialog.setFixedValue(sdds.getParameterFixedValue(index)); sddsEditAttributesDialog.setLocationRelativeTo(frame); sddsEditAttributesDialog.setVisible(true); if (sddsEditAttributesDialog.getName() == null) return; sdds.setParameterSymbol(index, sddsEditAttributesDialog.getSymbol()); sdds.setParameterUnits(index, sddsEditAttributesDialog.getUnits()); sdds.setParameterDescription(index, sddsEditAttributesDialog.getDescription()); sdds.setParameterFormatString(index, sddsEditAttributesDialog.getFormatString()); sdds.setParameterFixedValue(index, sddsEditAttributesDialog.getFixedValue()); sdds.renameParameter(name, sddsEditAttributesDialog.getName()); parameter_dlm.set(i,sddsEditAttributesDialog.getName()); } } } class InsertParameterAction extends AbstractAction { InsertParameterAction() { super(insertParameterAction); } public void actionPerformed(ActionEvent e) { sddsEditAttributesDialog sddsEditAttributesDialog = new sddsEditAttributesDialog(frame); sddsEditAttributesDialog.pack(); sddsEditAttributesDialog.setInsertMode(true); sddsEditAttributesDialog.setLocationRelativeTo(frame); sddsEditAttributesDialog.setVisible(true); if (sddsEditAttributesDialog.getName() == null) return; if (sdds.defineParameter(sddsEditAttributesDialog.getName(), sddsEditAttributesDialog.getSymbol(), sddsEditAttributesDialog.getUnits(), sddsEditAttributesDialog.getDescription(), sddsEditAttributesDialog.getFormatString(), sddsEditAttributesDialog.getType(), sddsEditAttributesDialog.getFixedValue()) == -1) { appendStatus("Error: " + sdds.getErrors()); return; } int pages, i; String[] emptyData = new String[1]; String emptyString = ""; DefaultCellEditor dce; emptyData[0] = emptyString; pages = sdds.pageCount(); if (pages > 0) { emptyData = new String[pages]; for (i=0;i 0) { int i; String[] emptyData = new String[1]; String emptyString = ""; int[] selectedIndexes = new int[selectedRowCount]; emptyData[0] = emptyString; selectedIndexes = parameterTable.getSelectedRows(); if (parameterTable.isEditing()) parameterTable.getCellEditor().stopCellEditing(); for (i=selectedRowCount-1;i>=0;i--) { sdds.deleteParameter((String)parameter_dlm.getElementAt(selectedIndexes[i])); parameter_dlm.remove(selectedIndexes[i]); parameter_dtm.removeRow(selectedIndexes[i]); appendStatus("parameter removed"); } } } } class AttributesColumnAction extends AbstractAction { AttributesColumnAction() { super(attributesColumnAction); } public void actionPerformed(ActionEvent e) { int i, j, k; i = columnTable.getSelectedColumn(); if (i >= 0) { String name = columnTable.getColumnName(i); int index = sdds.getColumnIndex(name); if (index < 0) { appendStatus("Error: " + sdds.getErrors()); return; } sddsEditAttributesDialog sddsEditAttributesDialog = new sddsEditAttributesDialog(frame); sddsEditAttributesDialog.pack(); sddsEditAttributesDialog.setAttributesMode(false); sddsEditAttributesDialog.setName(name); sddsEditAttributesDialog.setSymbol(sdds.getColumnSymbol(index)); sddsEditAttributesDialog.setUnits(sdds.getColumnUnits(index)); sddsEditAttributesDialog.setFormatString(sdds.getColumnFormatString(index)); sddsEditAttributesDialog.setType(sdds.getColumnType(index)); sddsEditAttributesDialog.setFieldLength(sdds.getColumnFieldLength(index)); sddsEditAttributesDialog.setLocationRelativeTo(frame); sddsEditAttributesDialog.setVisible(true); if (sddsEditAttributesDialog.getName() == null) return; sdds.setColumnSymbol(index, sddsEditAttributesDialog.getSymbol()); sdds.setColumnUnits(index, sddsEditAttributesDialog.getUnits()); sdds.setColumnDescription(index, sddsEditAttributesDialog.getDescription()); sdds.setColumnFormatString(index, sddsEditAttributesDialog.getFormatString()); sdds.setColumnFieldLength(index, sddsEditAttributesDialog.getFieldLength()); sdds.renameColumn(name, sddsEditAttributesDialog.getName()); String[] columnNames=null; j = column_dtm.getColumnCount(); columnNames = new String[j]; TableColumn tc; int width; int[] widtharray = new int[j]; for (k=0;k widtharray[k]) widtharray[k] = width; } tc.setPreferredWidth(widtharray[k]); tc.setWidth(widtharray[k]); } } } } class InsertColumnAction extends AbstractAction { InsertColumnAction() { super(insertColumnAction); } public void actionPerformed(ActionEvent e) { sddsEditAttributesDialog sddsEditAttributesDialog = new sddsEditAttributesDialog(frame); sddsEditAttributesDialog.pack(); sddsEditAttributesDialog.setInsertMode(false); sddsEditAttributesDialog.setLocationRelativeTo(frame); sddsEditAttributesDialog.setVisible(true); if (sddsEditAttributesDialog.getName() == null) return; int rows, i, j, k, pages, num; String[] emptyData; String emptyString = ""; rows = column_dtm.getRowCount(); if (sdds.defineColumn(sddsEditAttributesDialog.getName(), sddsEditAttributesDialog.getSymbol(), sddsEditAttributesDialog.getUnits(), sddsEditAttributesDialog.getDescription(), sddsEditAttributesDialog.getFormatString(), sddsEditAttributesDialog.getType(), sddsEditAttributesDialog.getFieldLength()) == -1) { appendStatus("Error: " + sdds.getErrors()); return; } emptyData = new String[rows]; for (i=0;i 0) { emptyData = new String[rows]; for (j=0;j= 0) { tc = columnTable.getColumn(columnTable.getColumnName(i)); sdds.deleteColumn(columnTable.getColumnName(i)); columnTable.removeColumn(tc); appendStatus("column removed"); i = columnTable.getSelectedColumn(); } } } class InsertRowAction extends AbstractAction { InsertRowAction() { super(insertRowAction); } public void actionPerformed(ActionEvent e) { int selectedRowCount; selectedRowCount = columnTable.getSelectedRowCount(); if ((selectedRowCount > 0) || (columnTable.getRowCount() == 0)) { TextInsertDialog textInsertDialog = new TextInsertDialog(frame); textInsertDialog.pack(); textInsertDialog.setTitleInfo("Insert Rows"); textInsertDialog.setEntryLabel("Number of new rows:"); textInsertDialog.setEntryText(insertRows); textInsertDialog.setLocationRelativeTo(frame); textInsertDialog.setVisible(true); int rows; insertRows = textInsertDialog.getText(); if (insertRows == null) return; try { rows = (Integer.valueOf(insertRows)).intValue(); } catch (NumberFormatException ex) { return; } if (rows > 0) { int cols, i; String[] emptyData=null; String emptyString = ""; int selectedIndex; cols = column_dtm.getColumnCount(); emptyData = new String[cols]; for (i=0;i 0) { int cols, i; String[] emptyData=null; String emptyString = ""; int selectedIndexes[]; cols = column_dtm.getColumnCount(); emptyData = new String[cols]; for (i=0;i=0;i--) { sdds.deleteRow(currentPage+1, selectedIndexes[i]+1); column_dtm.removeRow(selectedIndexes[i]); } column_dlm.clear(); int rows; rows = column_dtm.getRowCount(); for (i=0;i 1) { TextInsertDialog textInsertDialog = new TextInsertDialog(frame); textInsertDialog.pack(); textInsertDialog.setTitleInfo("Go To Page"); textInsertDialog.setEntryLabel("Page number from 1 to " + pages); textInsertDialog.setEntryText(String.valueOf(currentPage+1)); textInsertDialog.setLocationRelativeTo(frame); textInsertDialog.setVisible(true); int page; String dialogOutput; dialogOutput = textInsertDialog.getText(); if (dialogOutput == null) return; try { page = (Integer.valueOf(dialogOutput)).intValue(); } catch (NumberFormatException ex) { return; } if ((page > 0) && (page <= pages)) { if (currentPage != page-1) { disablePageChooser = true; if (savePage()) { clearPage(); loadPage(page); currentPage = page - 1; } pageChooser.setSelectedIndex(currentPage); disablePageChooser = false; } } } autoSearch(); } } class InsertPageAction extends AbstractAction { InsertPageAction() { super(insertPageAction); } public void actionPerformed(ActionEvent e) { String[] columnNames=null, parameterNames=null; parameterNames = sdds.getParameterNames(); columnNames = sdds.getColumnNames(); if ((parameterNames == null) || (columnNames == null)) { appendStatus("Unable to insert page"); } else if ((Array.getLength(parameterNames) == 0) && (Array.getLength(columnNames) == 0)) { appendStatus("Define parameters or columns first"); } else { if (savePage()) { clearPage(); if (!sdds.insertPage(currentPage+1)) { appendStatus("Error: " + sdds.getErrors()); return; } loadPage(currentPage+1); pageChooser.addItem(new Integer(sdds.pageCount())); appendStatus("Page inserted"); } } autoSearch(); } } class InsertAndCopyPageAction extends AbstractAction { InsertAndCopyPageAction() { super(insertAndCopyPageAction); } public void actionPerformed(ActionEvent e) { String[] columnNames=null, parameterNames=null; parameterNames = sdds.getParameterNames(); columnNames = sdds.getColumnNames(); if ((parameterNames == null) || (columnNames == null)) { appendStatus("Unable to insert page"); } else if ((Array.getLength(parameterNames) == 0) && (Array.getLength(columnNames) == 0)) { appendStatus("Define parameters or columns first"); } else { if (savePage()) { if (!sdds.insertPage(currentPage+2)) { appendStatus("qError: " + sdds.getErrors()); return; } currentPage++; if (savePage()) { } currentPage--; pageChooser.addItem(new Integer(sdds.pageCount())); appendStatus("Page inserted and copied"); } } autoSearch(); } } class AppendPageAction extends AbstractAction { AppendPageAction() { super(appendPageAction); } public void actionPerformed(ActionEvent e) { String[] columnNames=null, parameterNames=null; parameterNames = sdds.getParameterNames(); columnNames = sdds.getColumnNames(); if ((parameterNames == null) || (columnNames == null)) { appendStatus("Unable to append page"); } else if ((Array.getLength(parameterNames) == 0) && (Array.getLength(columnNames) == 0)) { appendStatus("Define parameters or columns first"); } else { if (savePage()) { if (!sdds.insertPage(currentPage+2)) { appendStatus("Error: " + sdds.getErrors()); return; } pageChooser.addItem(new Integer(sdds.pageCount())); appendStatus("Page appended"); } } autoSearch(); } } class DeletePageAction extends AbstractAction { DeletePageAction() { super(deletePageAction); } public void actionPerformed(ActionEvent e) { int pages; pages = sdds.pageCount(); if (pages == 1) { if (!sdds.deletePage(currentPage+1)) { appendStatus("Error: " + sdds.getErrors()); return; } clearPage(); loadPage(currentPage+1); } else { if (!sdds.deletePage(currentPage+1)) { appendStatus("Error: " + sdds.getErrors()); return; } disablePageChooser = true; clearPage(); pages--; if (pages == currentPage) currentPage--; loadPage(currentPage+1); pageChooser.removeAllItems(); pageChooser.addItem(new Integer(1)); int i; for (i=2;i<=pages;i++) pageChooser.addItem(new Integer(i)); pageChooser.setSelectedIndex(currentPage); disablePageChooser = false; } autoSearch(); } } class SearchColumnAction extends AbstractAction { SearchColumnAction() { super(searchColumnAction); } public void actionPerformed(ActionEvent e) { String[] columnNames=null; boolean[] columnMatch=null; int cols, rows, i, j, index=0; cols = column_dtm.getColumnCount(); rows = column_dtm.getRowCount(); if (cols < 1) return; columnNames = new String[cols]; for (i=0;i= pageWidth) { scale = pageWidth / tableWidth; } double headerHeightOnPage= columnTable.getTableHeader().getHeight()*scale; double tableWidthOnPage=tableWidth*scale; double oneRowHeight=(columnTable.getRowHeight()+ columnTable.getRowMargin())*scale; int numRowsOnAPage= (int)((pageHeight-headerHeightOnPage)/oneRowHeight); double pageHeightForTable=oneRowHeight*numRowsOnAPage; int totalNumPages= (int)Math.ceil(( (double)columnTable.getRowCount())/numRowsOnAPage); if(pageIndex>=totalNumPages) { return NO_SUCH_PAGE; } g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35, (int)(pageHeight+fontHeight-fontDesent));//bottom center g2.translate(0f,headerHeightOnPage); g2.translate(0f,-pageIndex*pageHeightForTable); //If this piece of the table is smaller than the size available, //clip to the appropriate bounds. if (pageIndex + 1 == totalNumPages) { int lastRowPrinted = numRowsOnAPage * pageIndex; int numRowsLeft = columnTable.getRowCount() - lastRowPrinted; g2.setClip(0, (int)(pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft)); } //else clip to the entire area available. else{ g2.setClip(0, (int)(pageHeightForTable*pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable)); } g2.scale(scale,scale); columnTable.paint(g2); g2.scale(1/scale,1/scale); g2.translate(0f,pageIndex*pageHeightForTable); g2.translate(0f, -headerHeightOnPage); g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage)); g2.scale(scale,scale); columnTable.getTableHeader().paint(g2);//paint header at top return Printable.PAGE_EXISTS; } public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { Graphics2D g2=(Graphics2D)g; if(!pageinfoCalculated) { getPageInfo(g, pageFormat); } g2.setColor(Color.black); if (pageIndex>=totalNumPages) { return NO_SUCH_PAGE; } if (prevPageIndex != pageIndex) { subPageIndex++; if ( subPageIndex == subTableSplitSize -1) { subPageIndex=0; } } g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); int rowIndex = pageIndex/ (subTableSplitSize -1); printTablePart(g2, pageFormat, rowIndex, subPageIndex); prevPageIndex= pageIndex; return Printable.PAGE_EXISTS; } public void getPageInfo(Graphics g, PageFormat pageFormat) { subTableSplit = null; subTableSplitSize = 0; subPageIndex = 0; prevPageIndex = 0; fontHeight=g.getFontMetrics().getHeight(); fontDesent=g.getFontMetrics().getDescent(); JTableHeader header = columnTable.getTableHeader(); double headerWidth = header.getWidth(); headerHeight = header.getHeight() + columnTable.getRowMargin(); pageHeight = pageFormat.getImageableHeight(); pageWidth = pageFormat.getImageableWidth(); double tableWidth = columnTable.getColumnModel().getTotalColumnWidth(); tableHeight = columnTable.getHeight(); rowHeight = columnTable.getRowHeight() + columnTable.getRowMargin(); tableHeightOnFullPage = (int)(pageHeight - headerHeight - fontHeight*2); tableHeightOnFullPage = tableHeightOnFullPage/rowHeight * rowHeight; TableColumnModel tableColumnModel = header.getColumnModel(); int columns = tableColumnModel.getColumnCount(); int columnMargin = tableColumnModel.getColumnMargin(); int [] temp = new int[columns]; int columnIndex = 0; temp[0] = 0; int columnWidth; int length = 0; subTableSplitSize = 0; while ( columnIndex < columns ) { columnWidth = tableColumnModel.getColumn(columnIndex).getWidth(); if ( length + columnWidth + columnMargin > pageWidth ) { temp[subTableSplitSize+1] = temp[subTableSplitSize] + length; length = columnWidth; subTableSplitSize++; } else { length += columnWidth + columnMargin; } columnIndex++; } //while if ( length > 0 ) { // if are more columns left, part page temp[subTableSplitSize+1] = temp[subTableSplitSize] + length; subTableSplitSize++; } subTableSplitSize++; subTableSplit = new int[subTableSplitSize]; for ( int i=0; i < subTableSplitSize; i++ ) { subTableSplit[i]= temp[i]; } totalNumPages = (int)(tableHeight/tableHeightOnFullPage); if ( tableHeight%tableHeightOnFullPage >= rowHeight ) { // at least 1 more row left totalNumPages++; } totalNumPages *= (subTableSplitSize-1); pageinfoCalculated = true; } public void printTablePart(Graphics2D g2, PageFormat pageFormat, int rowIndex, int columnIndex) { String pageNumber = "Page: "+(rowIndex+1); if ( subTableSplitSize > 1 ) { pageNumber += "-" + (columnIndex+1); } int pageLeft = subTableSplit[columnIndex]; int pageRight = subTableSplit[columnIndex + 1]; int pageWidth = pageRight-pageLeft; // page number message g2.drawString(pageNumber, pageWidth/2-35, (int)(pageHeight - fontHeight)); double clipHeight = Math.min(tableHeightOnFullPage, tableHeight - rowIndex*tableHeightOnFullPage); g2.translate(-subTableSplit[columnIndex], 0); g2.setClip(pageLeft ,0, pageWidth, (int)headerHeight); JTableHeader header = columnTable.getTableHeader(); header.paint(g2); // draw the header on every page g2.translate(0, headerHeight); g2.translate(0, -tableHeightOnFullPage*rowIndex); // cut table image and draw on the page g2.setClip(pageLeft, (int)tableHeightOnFullPage*rowIndex, pageWidth, (int)clipHeight); columnTable.paint(g2); double pageTop = tableHeightOnFullPage*rowIndex - headerHeight; double pageBottom = pageTop + clipHeight + headerHeight; g2.drawRect(pageLeft, (int)pageTop, pageWidth, (int)(clipHeight+ headerHeight)); } }