📄 catalogpanel.java
字号:
public void unsetTableModified() {
tableModified = false;
}
public boolean isModifiable() {
return (hasUpdateEnabled() && ! hasNullConnection());
}
public boolean isNotModifiable() {
return (hasUpdateDisabled() || hasNullConnection());
}
/** Returns true is table model data exists. */
public boolean hasValidTable() {
return tablePanel.hasValidTable();
}
public boolean hasInvalidTable() {
return ! hasValidTable();
}
public boolean isCommittable() {
if (hasValidTable()) return isModifiable();
return false;
}
public int commitModifiedTable() {
if (hasUnmodifiedTable()) return 0;
return commit();
}
public int commit() {
if (isCommittable()) {
boolean status = false;
if (hasModifiedTable()) {
status = commitConfirmed("modified table");
}
else {
status = commitConfirmed("unmodified table");
}
if (status) return updateDB();
}
return 0;
}
private boolean commitConfirmed(String message) {
return ( JOptionPane.showConfirmDialog(this,
"Commit " + message,
"Verify Commit", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE)
== JOptionPane.YES_OPTION );
}
/** Set the column order */
public void setColumnOrder(String [] order) {
columnOrderByName = order;
}
/** Sets the event solution list and creates new table.
*/
public void setSolutionList(SolutionList list) {
commitModifiedTable();
solutionList = (SolutionList) list ;
// tablePanel.catalogTable.getModel().setList(list);
tablePanel.startTableThread();
}
public SolutionList getSolutionList() {
return tablePanel.catalogTable.getModel().getList();
}
public boolean hasNullSolutionList() {
return (solutionList == null);
}
/** Sets the JTextArea to which output text information is appended.
*/
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
public JTextArea getTextArea() {
return this.textArea;
}
/** Toggles allowing enabling of database updates from table using panel actions.
* @param updateDB true == allow updates, default == false.
*/
public void setUpdateDB(boolean updateDB) {
this.updateDB = updateDB;
tablePanel.catalogTable.getModel().setCellEditable(updateDB);
}
/** Return the value of the database update switch.
*/
public boolean hasUpdateEnabled() {
return this.updateDB;
}
/** Return negation of the value of the database update switch.
*/
public boolean hasUpdateDisabled() {
return ! this.updateDB;
}
/** Adds a window listener to Window into which this component is added.
* Needed to cleanup editing, or save work, if window is closed inadvertently.
*/
public void addWindowListener() {
JRootPane jrp = this.getRootPane();
if (jrp != null) this.addWindowListener((Window) jrp.getParent());
}
/** Adds a window listener to specified Window.
* Needed to cleanup editing, or save work, if specified input window is closed inadvertently.
*/
public void addWindowListener(Window window) {
window.addWindowListener(this.new WindowCloser());
}
void logText(String text) {
if (textArea != null) textArea.append(text);
else System.out.print(text);
}
void logTextnl(String text) {
if (textArea != null) textArea.append(text + "\n");
else System.out.println(text);
}
public void stopTableCellEditing() {
if (tablePanel != null) {
if (tablePanel.catalogTable != null) {
JTable jtable = tablePanel.catalogTable.getTable();
if (jtable != null) {
if (jtable.isEditing()) {
jtable.getCellEditor().stopCellEditing();
}
jtable = tablePanel.catalogTable.getRowHeader();
if (jtable.isEditing()) {
jtable.getCellEditor().stopCellEditing();
}
}
}
}
}
// CatalogPanel inner classes
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
stopTableCellEditing();
}
}
// Inner class containing essential tables and actions
private class TablePanel extends JPanel implements Runnable {
CatalogTable catalogTable;
private JScrollPane spane;
private Object inputEventSrc;
private Thread tableThread;
private Runnable tableThreadPanelUpdate = new UpdateTablePanel();
private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);
private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
private boolean imageFlag = true;
private JLabel waitRequestLabel = new JLabel();
{
waitRequestLabel.setText("Table results displayed here");
waitRequestLabel.setHorizontalAlignment(JLabel.CENTER);
}
private javax.swing.Timer tableTimer = new javax.swing.Timer(250, new tableTimerActionListener());
JButton jbInsert = new JButton("Insert");
JButton jbDelete = new JButton("Delete");
JButton jbCommit = new JButton("Commit");
JButton jbFile = new JButton("File");
JButton jbWheres = new JButton("Where");
private WhereIsEngine whereFrom = null;
private int nrows = 0;
private boolean interruptRequestFlag = false;
private boolean tableRowHeadSelectionFlag;
// TablePanel constructor most user gui activity is performed by listeners of TablePanel class
TablePanel () {
TablePanel.this.setLayout(new BorderLayout());
TablePanel.this.add(waitRequestLabel, BorderLayout.CENTER);
CatalogTableModel ctm = new CatalogTableModel();
ctm.addTableModelListener(CatalogPanel.this);
ctm.setCellEditable(CatalogPanel.this.hasUpdateEnabled());
catalogTable = new CatalogTable(ctm);
if (columnOrderByName == null) System.out.println("column name order not initialized");
catalogTable.setColumnNameOrder(columnOrderByName);
configureButtonPanel();
} // end of TablePanel() constructor
// TablePanel method to run thread to get data from database and generate table
private void startTableThread() {
if (spane != null) {
commitModifiedTable();
try {
// Debug.println("DEBUG: getModel().setList()");
TablePanel.this.catalogTable.getModel().setList(solutionList);
catalogTable.initColumnSizes(catalogTable.getTable());
// spane.revalidate(); // what if column widths change in new list?
}
catch(NullPointerException ex) {
ex.printStackTrace();
return;
}
/*
TablePanel.this.remove(spane);
spane = null;
TablePanel.this.add(waitRequestLabel, BorderLayout.CENTER);
disableModifyButtons();
jbFile.setEnabled(false);
*/
TablePanel.this.revalidate();
// Debug.println("DEBUG: unsetTableModified()");
unsetTableModified();
return;
}
// TablePanel.this.catalogTable.getModel().createList(solutionList);
tableThread = new Thread(TablePanel.this, "solutionListTable");
tableTimer.start();
tableThread.start();
setCursor(waitCursor);
}
public void run() {
Thread thisThread = Thread.currentThread();
nrows = 0;
if (hasNullSolutionList()) {
System.err.println("CatalogPanel TablePanel Thread run() SolutionList object is null.");
SwingUtilities.invokeLater(tableThreadPanelUpdate);
return;
}
try {
thisThread.sleep(10l);
TablePanel.this.catalogTable.getModel().createList(solutionList);
nrows = CatalogPanel.this.solutionList.size();
if (nrows > 0) {
createTable();
unsetTableModified();
}
else if (nrows <= 0) {
// if (thisThread.isInterrupted() || interruptRequestFlag) resetConnection();
logTextnl("CatalogPanel No solutions found in list.");
}
}
catch (InterruptedException ex) {
System.err.println("Stopping CatalogPanel table creation thread");
}
catch (Exception ex) {
System.err.println("Generic exception caught by run() this CatalogPanel table creation thread.");
System.err.println(ex.getMessage());
ex.printStackTrace();
}
finally {
// Better to use a invokeLater(runnable) to make panel changes in the event-dispatch thread:
SwingUtilities.invokeLater(tableThreadPanelUpdate);
}
}
private void configureButtonPanel() {
jbInsert.setActionCommand("Insert");
jbInsert.setToolTipText("Adds a new row to catalog table; DB insert on commit.");
jbInsert.addActionListener(new ModifyTableRowsActionListener());
jbInsert.setMargin(new Insets(2,2,2,2));
jbCommit.setActionCommand("Commit");
jbCommit.setToolTipText("Updates DB with changes to this catalog.");
jbCommit.addActionListener(new ModifyTableRowsActionListener());
jbCommit.setMargin(new Insets(2,2,2,2));
jbDelete.setActionCommand("Delete");
jbDelete.setToolTipText("Deletes a row from the table; DB delete on commit.");
jbDelete.addActionListener(new ModifyTableRowsActionListener());
jbDelete.setMargin(new Insets(2,2,2,2));
jbFile.setActionCommand("File");
jbFile.setToolTipText("Saves the field contents of table to ASCII file.");
jbFile.addActionListener(new SaveTableActionListener());
jbFile.setMargin(new Insets(2,2,2,2));
jbWheres.setActionCommand("Where");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -