📄 listbasedspreadsheetpanel.java
字号:
/* Copyright (c) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package sample.spreadsheet.gui;import com.google.gdata.client.spreadsheet.ListQuery;import com.google.gdata.client.spreadsheet.SpreadsheetService;import com.google.gdata.data.TextContent;import com.google.gdata.data.spreadsheet.CustomElementCollection;import com.google.gdata.data.spreadsheet.ListEntry;import com.google.gdata.data.spreadsheet.ListFeed;import com.google.gdata.util.ServiceException;import com.google.gdata.util.VersionConflictException;import java.awt.BorderLayout;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.TreeSet;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.AbstractTableModel;/** * Widget for displaying and editing a spreadsheet. * * This is just for demonstrative purposes, it is not very featureful. * * */public class ListBasedSpreadsheetPanel extends JPanel { /** The Google Spreadsheets service. */ private SpreadsheetService service; /** The list feed URL. */ private URL listFeedUrl; /** The model that Swing uses to represent the table. */ private ListTableModel model; /** The table widget. */ private JTable table; private JButton refreshButton; private JButton deleteOneButton; private JButton revertOneButton; private JButton commitOneButton; private JButton commitAllButton; private JTextField fulltextField; private JTextField spreadsheetQueryField; private JTextField orderbyField; /** * Creates a list-based spreadsheet editing panel. * @param service the spreadsheet service * @param listFeedUrl the URL of the list feed */ public ListBasedSpreadsheetPanel( SpreadsheetService service, URL listFeedUrl) { this.service = service; this.listFeedUrl = listFeedUrl; model = new ListTableModel(); initializeGui(); } /** * Refreshes the contents of the table, applying any queries the user * specifies. */ private void refreshFromServer() { try { ListQuery query = new ListQuery(listFeedUrl); if (!fulltextField.getText().equals("")) { query.setFullTextQuery(fulltextField.getText()); } if (!spreadsheetQueryField.getText().equals("")) { query.setSpreadsheetQuery(spreadsheetQueryField.getText()); } if (!orderbyField.getText().equals("")) { query.setOrderBy(orderbyField.getText()); } ListFeed feed = service.query(query, ListFeed.class); model.resetEntries(feed.getEntries()); } catch (ServiceException e) { SpreadsheetApiDemo.showErrorBox(e); } catch (IOException e) { SpreadsheetApiDemo.showErrorBox(e); } } /** * This class models one particular row in the list, tracking both its old * contents and its new contents. * * This is responsible for the "commit/revert/delete" behavior that you * see in the GUI. */ private class ListEntryModel { /** * The original entry downloaded. * If this is an entry to add, originalEntry will be null. */ private ListEntry originalEntry = null; /** * The new contents to add. * If this is not being edited, this will be null. */ private CustomElementCollection newContents = null; /** Makes an existing row to be edited. */ public ListEntryModel(ListEntry originalEntry) { this.originalEntry = originalEntry; } /** Creates a blank row to be edited. */ public ListEntryModel() { } /** Gets the visible contents of a particular column. */ public String getContents(String column) { String result = null; if (newContents != null) { result = newContents.getValue(column); } else if (originalEntry != null) { result = originalEntry.getCustomElements().getValue(column); } if (result == null) { result = ""; } return result; } /** Gets whether this row neither has data nor is being edited. */ public boolean isBlank() { return originalEntry == null && newContents == null; } /** Gets whether this row is oepn for edit. */ public boolean isBeingEdited() { return newContents != null; } /** Open the row up for editing. */ public void startEdit() { newContents = new CustomElementCollection(); if (originalEntry != null) { newContents.replaceWithLocal(originalEntry.getCustomElements()); } } /** Sets the contents of a particular cell. */ public void setContents(String column, String newText) { if (!isBeingEdited()) { startEdit(); } newContents.setValueLocal(column, newText); } /** Loses all changes. */ public void revert() { newContents = null; } /** Actually adds this new entry to the spreadsheet. */ private void doAddNew() throws ServiceException, IOException { ListEntry newEntry = new ListEntry(); newEntry.getCustomElements().replaceWithLocal(newContents); originalEntry = service.insert(listFeedUrl, newEntry); newContents = null; } /** Actually updates an existing entry on the spreadsheet, * checking for edit conflicts. */ private void doUpdateExisting() throws ServiceException, IOException { try { originalEntry.getCustomElements().replaceWithLocal(newContents); originalEntry = originalEntry.update(); newContents = null; } catch (VersionConflictException e) { originalEntry = originalEntry.getSelf(); TextContent content = (TextContent) originalEntry.getContent(); JOptionPane.showMessageDialog(null, "Someone has edited the row in the meantime to:\n" + originalEntry.getTitle().getPlainText() + " (" + content.getContent().getPlainText() + ")\n" + "Commit again to confirm.", "Version Conflict", JOptionPane.WARNING_MESSAGE); } } /** Commits all changes. */ public void commit() { if (isBeingEdited()) { boolean success = false; try { if (originalEntry != null) { doUpdateExisting(); } else { doAddNew(); } success = true; } catch (ServiceException e) { SpreadsheetApiDemo.showErrorBox(e); } catch (IOException e) { SpreadsheetApiDemo.showErrorBox(e); } } } /** Deletes this entry from the backend, but not from the list. */ public void delete() { if (originalEntry != null) { try { originalEntry.delete(); } catch (ServiceException e) { SpreadsheetApiDemo.showErrorBox(e); } catch (IOException e) { SpreadsheetApiDemo.showErrorBox(e); } } originalEntry = null; newContents = null; } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -