📄 solutionlist.java
字号:
package org.trinet.jasi;
import java.text.*;
import java.util.*;
import java.net.URL;
import java.sql.Connection;
import org.trinet.jasi.*;
import org.trinet.util.IndexSort;
import org.trinet.util.graphics.ColorList;
import java.awt.Color;
/**
* A list of solutions that make a catalog. It is constrained by ranges or
* limits to parameters like start-end time, magnitude range, region, etc. that
* are set in an instance of EventSelectionProperties. (Only box regional
* selection is implemented). Specific Solutions objects can be added or
* removed.<p> Uses an existing JASI DataSource. */
public class SolutionList extends ActiveArrayList {
/* An array list is similar to a Vector but is consistent with the new
* Collection interface. Sun strongly recommends use of ArrayList because
* that makes it trivial to substitute another Collection type later if
* necessary. You can't do that with vector because it has a bunch of
* "extra" methods that aren't in the Collection interface. */
EventSelectionProperties props;
// the currently selected event. This is the nexus of the MVC model for
// event selection. NOTE: id rather than Solution object is used because
// Object can be replaced but ID is unique.
//private static Solution selected = null;
private static long selectedId = -1;
private static final int InitCapacity = 50;
/** Connection that will be used for dbase access */
protected Connection conn; // DK
// DK protected Connection conn = DataSource.getConnection();
/** hash table to keep track of the unique color of each solution in the list */
// Hashtable colorTable = new Hashtable(10); // initial capacity = 10
Hashtable colorTable = new Hashtable(); // initial capacity = 11
int maxColor = 0;
/** Make an empty SolutionList */
public SolutionList ()
{
super(InitCapacity);
}
/** Create a SolutionList based on this property description */
public SolutionList (EventSelectionProperties properties)
{
super(InitCapacity);
props = properties;
fetchByProperties();
}
/** Create a SolutionList containing this solution */
public SolutionList (long evid)
{
super(InitCapacity);
fetchById(evid);
}
/** Create a SolutionList for this time window */
public SolutionList (double start, double stop)
{
super(InitCapacity);
fetchByTime(start, stop);
}
/** Copy a SolutionList */
public SolutionList (Collection coll)
{
super(InitCapacity);
addAll(coll);
// copy 'selected' solution if arg is a SolutionList
if (coll instanceof SolutionList) {
setSelected( ((SolutionList)coll).getSelected(), true);
}
}
/**
* Define the connection
*/
public void setConnection (Connection conn) {
this.conn = conn;
}
/**
* Define the properties
*/
public void setProperties (EventSelectionProperties object) {
this.props = object;
}
/**
* Return the event selection properties
*/
public EventSelectionProperties getProperties () {
return this.props;
}
/**
* Returns the connection
*/
public Connection getConnection () {
return conn;
}
/**
* Returns the currently selected event
*/
public Solution getSelected() {
if (getSelectedId() <= -1) return null;
int idx = getSelectedIndex();
if (idx < 0) return null;
return (Solution) get(idx);
}
public long getSelectedId() {
return selectedId;
}
/**
* Returns the index of the currently selected event
*/
public int getSelectedIndex() {
return getIndex(getSelectedId());
}
/**
* Set the selected event. Only works if the event is already in the list.
* Returns true if the Solution is in the list and was set as the selected Solution.
* Notifies listeners.
*/
public boolean setSelected(Solution sel) {
return setSelected (sel , true);
}
/**
* Set the selected event. Only works if the event is already in the list.
* Returns true if the Solution is in the list and was set as the selected Solution.
* Only notify listeners if 'notify' = true.
*/
public boolean setSelected(Solution sel, boolean notify) {
// System.out.println ("SolList.setSelected: notify = "+notify);
if (sel == null ||
sel == getSelected()) return true; // prevent infinite loops, don't RE-select
return setSelected(sel.getId().longValue(), notify);
}
/**
* Set the selected event by this id. Only works if the event is already in the list.
* Returns true if the Solution is in the list and was set as the selected Solution.
* Notify listeners.
*/
public boolean setSelected(long id) {
return setSelected(id, true);
}
/**
* Set the selected event by this id. Only works if the event is already in the list.
* Returns true if the Solution is in the list and was set as the selected Solution.
* Only notify listeners if selected ID changes and 'notify' is true.
*/
public boolean setSelected(long id, boolean notify) {
// already selected
if (id == getSelectedId()) return true;
if (this.contains(id)) {
selectedId = id;
if (notify) fireStateChanged(getSelected());
return true;
} else {
return false;
}
}
/**
* Thin wrapper just to do the cast from Object to Solution.
*/
/*
* Tried to call it 'get' and got error:
The method get(int) cannot override the method of the same signature declared
in class java.util.ArrayList. They must have the same return type.
*/
public Solution getSolution(int index)
{
return (Solution) get(index);
}
/**
* Return a new SolutionList that contains all the same Solutions but is sorted
* by time. The arg is either SolutionList.ASCENDING or SolutionList.DESCENDING
* where ASCENDING in chronological order and DESCENDING is reverse.
*/
public static final int ASCENDING = 0;
public static final int DESCENDING = 1;
public SolutionList getTimeSortedList(int order) {
Solution sol[] = this.getArray();
// make the time array that is needed by IndexSort
double tArray[] = new double[sol.length];
for ( int i = 0; i < sol.length; i++)
{
tArray[i] = sol[i].datetime.doubleValue();
}
// sort by time
IndexSort indexSort = new IndexSort();
int idx[] = indexSort.getSortedIndexes(tArray);
SolutionList sl = new SolutionList();
if (order == ASCENDING) {
for (int i = 0; i < sol.length; i++)
{
sl.add( sol[idx[i]] ); // add sols in sorted order
}
} else {
for (int i = sol.length-1; i >=0; i--)
{
sl.add( sol[idx[i]] ); // add sols in reverse sorted order
}
}
return sl;
}
/**
* Populate this list with Solutions that meet the EventSelectionProperties given.
*/
public void fetchByProperties(EventSelectionProperties props)
{
addSolutions(Solution.create().getByProperties(props));
}
/**
* Populate this list with Solutions that meet the EventSelectionProperties
* already set with the setProperties() method. Does nothing if instance properties
* are null.
*/
public void fetchByProperties()
{
if (props != null) fetchByProperties(props);
}
/**
* Create list with one entry for this evid.
*/
public void fetchById(long evid)
{
if (conn == null) {
this.add( Solution.create().getById(evid) );
} else {
this.add( Solution.create().getById(conn, evid) );
}
}
/**
* Create list of Solutions for this time window.
*/
public void fetchValidByTime(double start, double stop)
{
Solution sol[] = null;
if (conn == null) {
sol = Solution.create().getValidByTime(start, stop);
} else {
sol = Solution.create().getValidByTime(conn, start, stop);
}
addSolutions(sol);
}
/**
* Create list of Solutions for this time window.
*/
public void fetchByTime(double start, double stop)
{
Solution sol[] = null;
if (conn == null) {
sol = Solution.create().getByTime(start, stop);
} else {
sol = Solution.create().getByTime(conn, start, stop);
}
addSolutions(sol);
}
/** Add an array of Solution objects to this list.*/
public void addSolutions(Solution [] sol) {
if (sol == null) {
System.out.println("No data; solution array null");
return;
}
// may be a better way to do this, addByTime(Solution) instead ??? aww
this.ensureCapacity(size()+sol.length);
for (int i=0; i<sol.length;i++) {
this.add(sol[i]);
}
}
/**
* Add an object. Will not add object if its already in the list.
* Returns 'true' if object was added.
*/
public boolean add(Object obj){
return add(obj, true);
}
/**
* Add an object. Will not add object if its already in the list.
* Returns 'true' if object was added. If notify = false, change event will not be
* fired.
*/
public boolean add(Object obj, boolean notify) {
// must set color BEFORE firing the event, else listeners get the wrong color!
if (super.add(obj, false)) {
setColor((Solution) obj); //sets unique event color
if (notify) this.fireStateChanged(obj);
return true;
} else {
return false;
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -