001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Apr 1, 2003 005 * Time: 7:38:54 PM 006 */ 007 008 package EVolve.util.phasedetectors; 009 010 import java.awt.Component; 011 import java.util.ArrayList; 012 013 import javax.swing.JScrollPane; 014 015 import EVolve.Scene; 016 import EVolve.util.HelperFuncs; 017 import EVolve.util.phasedetectors.phasedetectorUI.PhaseDetectorToolBarState; 018 import EVolve.visualization.AxesPanel; 019 import EVolve.visualization.XYViz.XYVisualization; 020 021 public abstract class PhaseDetector implements Cloneable{ 022 protected int interval; 023 protected ArrayList data; 024 protected PhaseDetectorToolBarState toolbarState; 025 protected PhaseUndoRedo undoRedo; 026 protected AxesPanel panel; 027 028 public PhaseDetector(XYVisualization visual) { 029 interval = 1; 030 data = new ArrayList(); 031 undoRedo = new PhaseUndoRedo((AxesPanel)((JScrollPane)visual.getPanel()).getViewport().getView()); 032 panel = (AxesPanel)((JScrollPane)visual.getPanel()).getViewport().getView(); 033 toolbarState = new PhaseDetectorToolBarState(); 034 } 035 036 public PhaseDetector(XYVisualization visual, int interval) { 037 this(visual); 038 this.interval = interval; 039 } 040 041 public void reset() { 042 data.clear(); 043 undoRedo.reset(); 044 } 045 046 public void drawPhase(boolean append) { 047 if (data.size() > 0) { 048 ArrayList phase = autoDetectPhase(); 049 undoRedo.registerAction(phase, PhaseUndoRedo.detectedPhases, append); 050 } 051 052 panel.setPhases(undoRedo.getCurrentFramePhases()); 053 } 054 055 public ArrayList getPhase() { 056 return undoRedo.getCurrentFramePhases(); 057 } 058 059 public void addPhaseManually(int added) { 060 undoRedo.registerAction(new Integer(added),PhaseUndoRedo.singlePhase,true); 061 undoRedo.update(); 062 } 063 064 public void removePhaseManually(int removed) { 065 ArrayList phases = undoRedo.getCurrentFramePhases(); 066 boolean found = false; 067 for (int i=0; i<phases.size(); i++) { 068 Integer aPhase = (Integer)phases.get(i); 069 if (aPhase.intValue() == removed) { 070 found = true; 071 break; 072 } 073 } 074 075 if (!found) return; 076 077 undoRedo.registerAction(new Integer(-1*removed), PhaseUndoRedo.singlePhase, true); 078 undoRedo.update(); 079 } 080 081 public void pastePhases(ArrayList pasted) { 082 undoRedo.registerAction(pasted, PhaseUndoRedo.pastedPhases, true); 083 undoRedo.update(); 084 } 085 086 public void undo() { 087 undoRedo.undo(); 088 } 089 090 public void redo() { 091 undoRedo.redo(); 092 } 093 094 public boolean undoable() { 095 return undoRedo.undoable(); 096 } 097 098 public boolean redoable() { 099 return undoRedo.redoable(); 100 } 101 102 public PhaseDetectorToolBarState getToolBarState() { 103 return toolbarState; 104 } 105 106 public void updateToolBarState() { 107 Scene.getUIManager().getPhaseDetectorToolBar().updateToolBarState(toolbarState); 108 } 109 110 protected abstract void refreshDetectorParameters(); 111 112 public abstract void saveSetting(); 113 114 public abstract String getName(); 115 116 public abstract void collectData(long xMappedId, long yMappedId); 117 118 public abstract Component[] createDetectorParamsControls(); 119 120 public abstract void triggerPhases(int noiseTolerance, float threshold, boolean append); 121 122 public abstract void entitySetPhases(int noiseTolerance, float threshold, boolean append); 123 124 protected abstract ArrayList autoDetectPhase(); 125 126 public Object clone() { 127 PhaseDetector o = null; 128 try { 129 o = (PhaseDetector)super.clone(); 130 } catch (CloneNotSupportedException e) { 131 System.out.println(e.getStackTrace()); 132 return o; 133 } 134 o.data = HelperFuncs.cloneArrayList(data); 135 o.toolbarState = (PhaseDetectorToolBarState)toolbarState.clone(); 136 o.undoRedo = (PhaseUndoRedo)undoRedo.clone(); 137 138 return o; 139 } 140 141 public void setPanel(AxesPanel panel) { 142 this.panel = panel; 143 undoRedo.setPanel(panel); 144 } 145 146 }