📄 catalogpanel.java
字号:
package org.trinet.util.graphics.table;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import org.trinet.jasi.*;
import org.trinet.jdbc.*;
import org.trinet.jdbc.datatypes.*;
// DK CLEANUP import org.trinet.jdbc.table.*;
import org.trinet.util.*;
import org.trinet.util.gazetteer.*;
import org.trinet.util.graphics.*;
import org.trinet.jasi.EventSelectionProperties;
/** Displays a SolutionList in a JTable in a JPanel and allows table data to be sent to database.
* example code on usage: <BR>
CatalogPanel catPanel = new CatalogPanel(solutionList, textArea, false, false); <BR>
catPanel.addWindowListener(windowFrame); <BR>
windowFrame.getContentPane().add(catPanel); <BR>
catPanel.setUpdateDB(true); <BR>
*/
// 1/22/02 DDG added "COMMENT"
public class CatalogPanel extends JPanel implements Observer, TableModelListener {
public static final String VERSION = "1.20000202";
private static final int ID_COLUMN = 0;
private static final String ORIGIN = "Origin";
private static final String EVENT = "Event";
private static final JLabel statusLabel = new JLabel(" ");
private static final ImageIcon LIGHT_BULB = new ImageIcon("/home/tpp/bin/imageicon/lightBulb.jpg");
private static final ImageIcon DARK_BULB = new ImageIcon("/home/tpp/bin/imageicon/darkBulb.jpg");
//private String [] columnOrderByName = CatalogTableConstants.columnNames;
protected String [] columnOrderByName = {
"DATETIME", "LAT", "LON", "MAG", "MTYP", "Z", "AUTH", "SRC", "GAP", "DIST",
"RMS", "ERR_T", "ERR_H", "ERR_Z", "OBS", "USED", "S", "FM", "Q", "V",
"ETYPE", "ST", "ZFIX", "HFIX", "TFIX", "WRECS", "PR", "COMMENT" };
private JTextArea textArea = null;
// private Connection connection = null;
private SolutionList solutionList = null;
private TablePanel tablePanel;
/** JPanel containing action buttons */
private JPanel buttonPanel;
private boolean updateDB = false;
private boolean tableModified = false;
// private DataSource dataSource = new DataSource(); // unfortunately assumes default connection parameters;
// CatalogPanel Constructors follow:
/** Constructor sets defaults SolutionList == null, JTextArea == null, updateDB == false.
*/
public CatalogPanel() { }
public CatalogPanel(String [] tableColumnNames) {
setColumnOrder(tableColumnNames);
initPanel(); // Need to rework via manangingFocus()?
}
public CatalogPanel(SolutionList solutionList) {
this(solutionList, null, false);
}
public CatalogPanel(SolutionList solutionList, JTextArea textArea) {
this(solutionList, textArea, false);
}
/** Constructor
* @param solutionList org.trinet.jasi.SolutionList can be null.
* @param textArea javax.swing.JTextArea to which output is appended can be null.
* @param updateDB true == allows table edits and database updates.
*/
public CatalogPanel(SolutionList solutionList, JTextArea textArea, boolean updateDB) {
this.solutionList = solutionList;
this.textArea = textArea;
this.updateDB = updateDB;
initPanel(); // Need to rework via manangingFocus()?
}
// Begin CatalogPanel methods
private void initPanel() {
setLayout(new BorderLayout());
JPanel masterPanel = new JPanel();
masterPanel.setLayout(new BorderLayout());
tablePanel = new TablePanel();
masterPanel.add(tablePanel, BorderLayout.CENTER);
this.add(masterPanel,BorderLayout.CENTER);
statusLabel.setForeground(Color.red);
statusLabel.setHorizontalAlignment(JLabel.LEFT);
JPanel statusPanel = new JPanel();
statusPanel.setBorder(BorderFactory.createEtchedBorder());
statusPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
statusPanel.add(statusLabel);
this.add(statusPanel, BorderLayout.SOUTH);
// Debug.println("CatalogPanel layout prefSize: " + ((BorderLayout) getLayout()).preferredLayoutSize(this));
if (solutionList != null) tablePanel.startTableThread();
}
public String [] getTableColumnHeaders() {
if (tablePanel == null || tablePanel.catalogTable == null) return null;
return tablePanel.catalogTable.getTableColumnHeaders();
}
public static void main(String[] args) {
String dbURLText = "jdbc:oracle:thin:@makalu.gps.caltech.edu:1521:makaludb";
String dbDriverText = "oracle.jdbc.driver.OracleDriver";
String dbUserText = "trinetdb";
String dbPasswordText = "calgs";
// Connection connect = new JDBConn(dbURLText, dbDriverText, dbUserText, dbPasswordText).conn;
DataSource ds = new DataSource(dbURLText, dbDriverText, dbUserText, dbPasswordText);
long now = System.currentTimeMillis()/1000;
long then = now - 14400;
if (args.length > 0 ) {
// Debug.println("input arg[0]: " + args[0]);
try {
then = now - (long) (3600 * Integer.parseInt(args[0]));
}
catch (NumberFormatException ex) {
System.err.println("ERROR Input argument cannot be parsed as integer hours: " + args[0]);
System.exit(0);
}
}
EventSelectionProperties eventProps = new EventSelectionProperties();
eventProps.setDateTime("startTime", new DateTime(then));
eventProps.setDateTime("endTime", new DateTime(now));
DataSource.setWriteBackEnabled(true);
SolutionList sl = new SolutionList();
// sl.setConnection(connect);
sl.setProperties(eventProps);
sl.fetchByProperties();
// if (sl.size() > 1) sl.getSolution(1).delete();
JTextArea jta = new JTextArea(10,132);
jta.setEditable(true);
// CatalogPanel cp = new CatalogPanel(sl, jta, true);
// String [] testCols = { "DATETIME", "LAT", "LON", "MAG", "MTYP", "Z", "AUTH", "SRC", "GAP", "DIST", };
String [] testCols = {
"DATETIME", "LAT", "LON", "MAG", "MTYP", "Z", "AUTH", "SRC", "GAP", "DIST",
"RMS", "ERR_T", "ERR_H", "ERR_Z", "OBS", "USED", "S", "FM", "Q", "V",
"ETYPE", "ST", "ZFIX", "HFIX", "TFIX", "WRECS", "PR", "COMMENT" };
CatalogPanel cp = new CatalogPanel(testCols);
cp.setSolutionList(sl);
cp.setUpdateDB(true);
/* Next line of code you have to add to a listener or in main app after table is created.
String [] saveHeader = cp.getTableColumnHeaders();
*/
JFrame aFrame = new JFrame();
aFrame.addWindowListener(new WindowAdapter () {
public void windowClosing(WindowEvent evt) {
Window wnd = evt.getWindow();
wnd.setVisible(false);
wnd.dispose();
System.exit(0);
}
});
cp.addWindowListener(aFrame);
aFrame.setTitle("Catalog");
JTabbedPane jtb = new JTabbedPane();
JPanel testPanel = new JPanel();
testPanel.setLayout(new BorderLayout());
JButton jbTest = new JButton("Nada");
testPanel.add(jbTest, BorderLayout.NORTH);
testPanel.add(jtb, BorderLayout.CENTER);
jtb.insertTab("Catalog", null, cp, "solutions", 0);
JScrollPane textScrollPane = new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSplitPane split =
new JSplitPane(JSplitPane.VERTICAL_SPLIT,
false, // don't repaint until resizing is done
testPanel, // top component
textScrollPane); // bottom component
split.setOneTouchExpandable(true);
// aFrame.getContentPane().add(testPanel, BorderLayout.CENTER);
aFrame.getContentPane().add(split, BorderLayout.CENTER);
aFrame.pack();
// Debug.println("aFrame layout prefSize: "
// + ((BorderLayout) aFrame.getLayout()).preferredLayoutSize(aFrame));
// Debug.println("aFrame insets: " + aFrame.getInsets().toString());
// aFrame.setBounds(50,100,229,202);
aFrame.setBounds(50,100,256,256);
aFrame.setVisible(true);
split.setDividerLocation((int) (0.75*aFrame.getSize().height));
} // end of main test method
/** Return the JPanel containing the action button. Can be used to add more
* buttons. */
public JPanel getButtonPanel() {
return buttonPanel;
}
/** Required method to implement TableModelListener interface */
public void tableChanged(TableModelEvent event) {
// Debug.println("TME getSource(): " + event.getSource());
tableModified = true;
}
/** Required method to implement the observer interface.
*/
public void update(Observable observable, Object arg) {
logTextnl("DEBUG CatalogPanel update() : Observable = " + observable.getClass().getName());
logTextnl("DEBUG CatalogPanel update() : Argument = " + arg.getClass().getName());
logTextnl("DEBUG CatalogPanel update() : fireTableDataChanged()");
if (org.trinet.jasi.SolutionList.class.isInstance(arg)) {
setSolutionList((SolutionList) arg);
}
// note the the catalog table data model is aliased solutionList reference in the class's constructor.
// tablePanel.catalogTable.getModel().fireTableDataChanged();
}
/**
* Returns Solution array containing selected rows in table row header.
*/
public Solution [] getSelectedSolutions() {
if (tablePanel.catalogTable.getRowHeader() == null) return null;
int selected [] = tablePanel.catalogTable.getRowHeader().getSelectedRows();
int nselected = selected.length;
if (nselected == 0) return null;
Solution [] solutions = new Solution [nselected];
SolutionList solutionList = tablePanel.catalogTable.getModel().getList();
Solution [] master = solutionList.getArray();
int masterLength = master.length;
if (nselected > masterLength)
throw new ArrayIndexOutOfBoundsException("CatalogPanel: getSelectedSolutions() SelectedRows>SolutionList.size().");
for (int index = 0; index < nselected; index++) {
solutions[index] = master[tablePanel.catalogTable.getSortedRowModelIndex(selected[index])];
}
return solutions;
}
/** Returns long array containing selected event ids from table row header.
*/
public long [] getSelectedIds() {
if (tablePanel.catalogTable.getRowHeader() == null) return null;
int rowIds[] = tablePanel.catalogTable.getRowHeader().getSelectedRows();
int nrows = rowIds.length;
if (nrows == 0) return null;
long [] ids = new long [nrows];
CatalogTableModel ctm = tablePanel.catalogTable.getModel();
for (int index = 0; index < nrows; index++) {
ids[index] =
((DataObject) ctm.getValueAt(tablePanel.catalogTable.getSortedRowModelIndex(rowIds[index]), ID_COLUMN)).longValue();
}
return ids;
}
public boolean hasNullConnection() {
if (solutionList == null) return true;
else return (solutionList.getConnection() == null) ;
}
public int updateDB() {
return tablePanel.updateDB();
}
public boolean hasModifiedTable() {
return tableModified;
}
public boolean hasUnmodifiedTable() {
return ! tableModified;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -