📄 listbasedspreadsheetpanel.java
字号:
* The Swing model for the spreadsheet. * * This is mostly GUI code. */ private class ListTableModel extends AbstractTableModel { /** The name of each column (in the XML). */ private List<String> columnNames = new ArrayList<String>(); /** All entries of the list. */ private List<ListEntryModel> list = new ArrayList<ListEntryModel>(); /** * Resets all the entries from a new list. * * This also tries to figure out what the set of valid columns is. */ public synchronized void resetEntries(List<ListEntry> entries) { TreeSet<String> columnSet = new TreeSet<String>(); list.clear(); columnNames.clear(); for (ListEntry entry : entries) { list.add(new ListEntryModel(entry)); columnSet.addAll(entry.getCustomElements().getTags()); } // Always have an empty row to edit. list.add(new ListEntryModel()); columnNames.add("(Edit)"); columnNames.addAll(columnSet); fireTableStructureChanged(); fireTableDataChanged(); } /** Writes back all modified entries to the actual spreadsheet. */ public synchronized void commitAll() { for (ListEntryModel entryModel : list) { entryModel.commit(); } fireTableDataChanged(); } /** Reverts all entries, losing all changes. */ public synchronized void revertAll() { for (ListEntryModel entryModel : list) { entryModel.revert(); } fireTableDataChanged(); } /** Delete one entry by index. */ public synchronized void deleteOne(int row) { if (!list.get(row).isBeingEdited()) { list.get(row).delete(); list.remove(row); fireTableRowsDeleted(row, row); } } /** Commit one entry by index. */ public synchronized void commitOne(int row) { if (row >= 0 && row < list.size()) { if (list.get(row).isBeingEdited()) { list.get(row).commit(); fireTableRowsUpdated(row, row); } } } /** Revert one entry by index. */ public synchronized void revertOne(int row) { if (list.get(row).isBeingEdited()) { list.get(row).revert(); fireTableRowsUpdated(row, row); } } /** Gets whether this is the special column. */ private boolean isSpecialColumn(int col) { return col == 0; } /** * Implements the Swing method for handling cell edits. */ public synchronized void setValueAt(Object value, int row, int col) { ListEntryModel entryModel = list.get(row); if (isSpecialColumn(col)) { setRowEditing(row, ((Boolean) value).booleanValue()); } else { setRowEditing(row, true); entryModel.setContents(columnNames.get(col), value.toString()); fireTableCellUpdated(row, col); } } /** Sets whether a row is being edited. */ private void setRowEditing(int row, boolean edit) { ListEntryModel entryModel = list.get(row); if (edit && !entryModel.isBeingEdited()) { if (entryModel.isBlank()) { // Always have at least two blank rows. list.add(new ListEntryModel()); fireTableRowsInserted(list.size() - 1, list.size() - 1); } entryModel.startEdit(); fireTableRowsUpdated(row, row); } else if (!edit) { commitOne(row); } } /** Tells Swing the value at a particular location. */ public synchronized Object getValueAt(int row, int col) { ListEntryModel entryModel = list.get(row); if (isSpecialColumn(col)) { return Boolean.valueOf(entryModel.isBeingEdited()); } else { return entryModel.getContents(columnNames.get(col)); } } /** Tells Swing whether the cell is editable. */ public synchronized boolean isCellEditable(int row, int col) { return true; } /** Gets the column name by index. */ public synchronized String getColumnName(int columnIndex) { return columnNames.get(columnIndex); } /** Gets the column class for editing. */ public synchronized Class<?> getColumnClass(int columnIndex) { if (isSpecialColumn(columnIndex)) { return Boolean.class; } else { return String.class; } } /** Tells Swing how many rows are in this table. */ public synchronized int getRowCount() { return list.size(); } /** Tells Swing how many columns are in this table. */ public synchronized int getColumnCount() { return columnNames.size(); } } // GUI code public static JFrame createWindow(SpreadsheetService service, URL listFeedUrl) { JFrame frame = new JFrame(); frame.setSize(600, 480); frame.getContentPane().add(new ListBasedSpreadsheetPanel( service, listFeedUrl)); frame.setVisible(true); frame.setTitle("List Demo - Row-based Editing"); return frame; } private void initializeGui() { table = new JTable(model); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scrollpane = new JScrollPane(table); setLayout(new BorderLayout()); add(scrollpane, BorderLayout.CENTER); Container southPanel = new JPanel(); southPanel.setLayout(new GridBagLayout()); deleteOneButton = new JButton("Delete"); deleteOneButton.addActionListener(new ActionHandler()); southPanel.add(deleteOneButton, getTopConstraints(0)); revertOneButton = new JButton("Revert"); revertOneButton.addActionListener(new ActionHandler()); southPanel.add(revertOneButton, getTopConstraints(1)); commitOneButton = new JButton("Commit"); commitOneButton.addActionListener(new ActionHandler()); southPanel.add(commitOneButton, getTopConstraints(2)); commitAllButton = new JButton("Commit All"); commitAllButton.addActionListener(new ActionHandler()); southPanel.add(commitAllButton, getTopConstraints(3)); refreshButton = new JButton("Refresh"); refreshButton.addActionListener(new ActionHandler()); southPanel.add(refreshButton, getTopConstraints(4)); southPanel.add(new JLabel("Full text:"), getLeftConstraints(1)); fulltextField = new JTextField(); southPanel.add(fulltextField, getRightConstraints(1)); southPanel.add(new JLabel("Structured:"), getLeftConstraints(2)); spreadsheetQueryField = new JTextField(); southPanel.add(spreadsheetQueryField, getRightConstraints(2)); southPanel.add(new JLabel("Order By:"), getLeftConstraints(3)); orderbyField = new JTextField(); southPanel.add(orderbyField, getRightConstraints(3)); add(southPanel, BorderLayout.SOUTH); refreshFromServer(); } private GridBagConstraints getTopConstraints(int col) { GridBagConstraints c = new GridBagConstraints(); c.gridy = 0; c.gridx = col; c.fill = GridBagConstraints.HORIZONTAL; return c; } private GridBagConstraints getLeftConstraints(int row) { GridBagConstraints c = new GridBagConstraints(); c.gridy = row; c.gridx = 0; c.fill = GridBagConstraints.HORIZONTAL; return c; } private GridBagConstraints getRightConstraints(int row) { GridBagConstraints c = new GridBagConstraints(); c.gridy = row; c.gridx = 1; c.gridwidth = 4; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; // take up as much space as possible return c; } private class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent ae) { if (ae.getSource() == refreshButton) { refreshFromServer(); } else if (ae.getSource() == deleteOneButton) { model.deleteOne(table.getSelectedRow()); } else if (ae.getSource() == revertOneButton) { model.revertOne(table.getSelectedRow()); } else if (ae.getSource() == commitOneButton) { model.commitOne(table.getSelectedRow()); } else if (ae.getSource() == commitAllButton) { model.commitAll(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -