Skip to content

Commit

Permalink
Made DSView implement IPropertyChangedListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
olas committed Jun 29, 2010
1 parent fbd8726 commit 61b0bda
Showing 1 changed file with 105 additions and 71 deletions.
176 changes: 105 additions & 71 deletions plugins/net.bioclipse.ds.ui/src/net/bioclipse/ds/ui/views/DSView.java
Expand Up @@ -76,7 +76,7 @@
* @author ola
*
*/
public class DSView extends ViewPart implements IPartListener2 {
public class DSView extends ViewPart implements IPartListener2, IPropertyChangeListener {

private static final Logger logger = Logger.getLogger(DSView.class);

Expand Down Expand Up @@ -301,6 +301,7 @@ public void run() {
IDSManager ds = net.bioclipse.ds.Activator.getDefault().getJavaManager();
try {
viewer.setInput(ds.getFullEndpoints().toArray());
viewer.expandAll();
} catch ( BioclipseException e ) {
LogUtils.handleException( e, logger, Activator.PLUGIN_ID );
viewer.setInput(new String[]{"Error initializing tests"});
Expand All @@ -324,7 +325,7 @@ public void run() {
.getActivePage().getActiveEditor();

if (openEditor!=null){
handleEditor(openEditor);
activateEditor(openEditor);

if (isAutorun()){
// doRunAllTests();
Expand Down Expand Up @@ -973,15 +974,7 @@ public void setFocus() {


private void updateView() {

if (getSite()==null) return;
if (getSite().getWorkbenchWindow()==null) return;
if (getSite().getWorkbenchWindow().getActivePage()==null) return;

if (getSite().getWorkbenchWindow().getActivePage().getActiveEditor()==null){
activeTestRuns=null;
}


viewer.refresh();
// viewer.expandToLevel( 1 );
viewer.expandAll();
Expand Down Expand Up @@ -1265,8 +1258,8 @@ public void partActivated(IWorkbenchPartReference partRef) {

@Override
public void partBroughtToTop(IWorkbenchPartReference partRef) {
logger.debug("Part brouight to top: " + partRef.getId() + " detected in Eventview.");
//TODO: Switch view model here
logger.debug("Part brought to top: " + partRef.getId() + " detected in Eventview.");
activatePartRef(partRef);
}

@Override
Expand All @@ -1275,19 +1268,21 @@ public void partClosed(IWorkbenchPartReference partRef) {

IEditorPart editor = getSupportedEditor(partRef);
JChemPaintEditor jcp=getJCPfromEditor(editor);

//We are currently unable to unregister listeners for ICDKMolecules in MolTable
//TODO: Get all mols in MolTable and unregister them, or unregister on page change also
//TODO: Unregister last mol in moltable (get last JCP page)
if (jcp!=null){
jcp.removePropertyChangedListener(this);
logger.debug("Removed prop-listener from JCP");

ICDKMolecule mol=jcp.getCDKMolecule();

molListenerMap.remove( mol );
if (molTestMap.keySet().contains( mol )){
molTestMap.remove( mol );
}
}

//If no more editor, clear all
//If no more editor, clear all in view
if (getSite()==null) return;
if (getSite().getWorkbenchWindow()==null) return;
if (getSite().getWorkbenchWindow().getActivePage()==null) return;
Expand All @@ -1299,19 +1294,6 @@ public void partClosed(IWorkbenchPartReference partRef) {

}

private void deactivateView() {
// molTestMap.clear();
activeTestRuns=null;
IDSManager ds = net.bioclipse.ds.Activator.getDefault().getJavaManager();
try {
for (Endpoint ep : ds.getFullEndpoints()){
if (ep.getTestruns()!=null)
ep.getTestruns().clear();
}
} catch (BioclipseException e) {
e.printStackTrace();
}
}

@Override
public void partDeactivated(IWorkbenchPartReference partRef) {
Expand All @@ -1321,6 +1303,7 @@ public void partDeactivated(IWorkbenchPartReference partRef) {
@Override
public void partHidden(IWorkbenchPartReference partRef) {
logger.debug("Part Hidden: " + partRef.getId() + " detected in Eventview.");
deactivatePartRef(partRef);
}

@Override
Expand All @@ -1330,8 +1313,8 @@ public void partInputChanged(IWorkbenchPartReference partRef) {

@Override
public void partOpened(IWorkbenchPartReference partRef) {
logger.debug("Part Opened: " + partRef.getId() + " detected in Eventview.");
handlePartRef(partRef);
// logger.debug("Part Opened: " + partRef.getId() + " detected in Eventview.");
//TODO: Cache results on load?
}

@Override
Expand All @@ -1343,15 +1326,37 @@ public void partVisible(IWorkbenchPartReference partRef) {
/*==============================
* Remake of part handling logic
==============================*/

private void deactivateView() {
// molTestMap.clear();
activeTestRuns=null;
IDSManager ds = net.bioclipse.ds.Activator.getDefault().getJavaManager();
try {
for (Endpoint ep : ds.getFullEndpoints()){
if (ep.getTestruns()!=null)
ep.getTestruns().clear();
}
} catch (BioclipseException e) {
e.printStackTrace();
}
}


private void activatePartRef(IWorkbenchPartReference partRef){
IEditorPart editor = getSupportedEditor(partRef);
if (editor!=null)
activateEditor(editor);

}

private void handlePartRef(IWorkbenchPartReference partRef){
private void deactivatePartRef(IWorkbenchPartReference partRef){
IEditorPart editor = getSupportedEditor(partRef);
if (editor!=null)
handleEditor(editor);
deactivateEditor(editor);

}


private IEditorPart getSupportedEditor(IWorkbenchPartReference partRef) {
if (!(partRef instanceof IEditorReference)) return null;

Expand All @@ -1369,10 +1374,46 @@ else if ( editorRef.getId().equals("net.bioclipse.cdk.ui.sdfeditor")) {
return editor;
}

private void handleEditor(IEditorPart editor){

private void deactivateEditor(IEditorPart editor){
if (editor instanceof JChemPaintEditor) {
JChemPaintEditor jcp = (JChemPaintEditor) editor;
jcp.removePropertyChangedListener(this);
logger.debug("Removed prop-listener from JCP");
}
else if (editor instanceof MultiPageMoleculesEditorPart) {
MultiPageMoleculesEditorPart moltable = (MultiPageMoleculesEditorPart) editor;
//TODO: how do we handle a hidden moltable?
}
}

private void activateEditor(IEditorPart editor){

if (editor instanceof JChemPaintEditor) {
registerJCPListeners((JChemPaintEditor) editor);
JChemPaintEditor jcp = (JChemPaintEditor) editor;
jcp.addPropertyChangedListener(this);
logger.debug("Activating JCP: Added prop-listener");

ICDKMolecule mol = jcp.getCDKMolecule();

if (mol==null){
logger.debug("Molecule is null, probably not loaded yet.");
return;
}

logger.debug("Mol exists, looking up in cache.");

//Use cached if exists, else set up new
if (molTestMap.containsKey(mol)){
logger.debug("Using cached results.");

doSetUpTestRuns(mol);

updateView();
}else{
logger.debug("Clear and creating new results.");
doClearAndSetUpNewTestRuns(mol);
}
}
else if (editor instanceof MultiPageMoleculesEditorPart) {
MultiPageMoleculesEditorPart moltable = (MultiPageMoleculesEditorPart) editor;
Expand All @@ -1385,9 +1426,15 @@ public void pageChanged(PageChangedEvent event) {
System.out.println("Moltable changed page to: " + obj);
if (obj instanceof JChemPaintEditor) {
JChemPaintEditor jcp = (JChemPaintEditor) obj;
registerJCPListeners(jcp);
jcp.addPropertyChangedListener(DSView.getInstance());
logger.debug("Added prop-listener to JCP");

ICDKMolecule mol = jcp.getCDKMolecule();
if (mol==null){
logger.debug("Molecule is null, not loaded yet?");
return;
}

//Use cached if exists, else set up new
if (molTestMap.containsKey(mol)){

Expand All @@ -1403,6 +1450,7 @@ public void pageChanged(PageChangedEvent event) {
logger.debug("No JCP page visible anymore in moltable.");
deactivateView();
updateView();
return;
}
}
});
Expand Down Expand Up @@ -1441,40 +1489,25 @@ else if (editor instanceof MultiPageMoleculesEditorPart) {



/**
* Register listeners on the CDKMolecule of the active JCPeditor.
* Only one per instance.
*
* @param jcp
*/
private void registerJCPListeners(JChemPaintEditor jcp) {

//If editor already is registered, skip adding property listeners
if (molListenerMap.keySet().contains(jcp.getCDKMolecule())){
logger.debug(" Skipped registering listeners, cdkmol already in map.");
return;
@Override
public void propertyChange(PropertyChangeEvent event) {

logger.debug("Property changed!!!");

if(event.getProperty().equals( JChemPaintEditor.
STRUCUTRE_CHANGED_EVENT )) {
JChemPaintEditor jcp=(JChemPaintEditor)event.getSource();
ICDKMolecule cdkmol = jcp.getCDKMolecule();
JCPModelChanged(cdkmol);
}
else if(event.getProperty().equals( JChemPaintEditor.
MODEL_LOADED )) {
JChemPaintEditor jcp=(JChemPaintEditor)event.getSource();
ICDKMolecule cdkmol = jcp.getCDKMolecule();
JCPModelLoaded(cdkmol);
}
}

IPropertyChangeListener jcplistener = new IPropertyChangeListener() {
public void propertyChange( PropertyChangeEvent event ) {

if(event.getProperty().equals( JChemPaintEditor.
STRUCUTRE_CHANGED_EVENT )) {
JChemPaintEditor jcp=(JChemPaintEditor)event.getSource();
ICDKMolecule cdkmol = jcp.getCDKMolecule();
JCPModelChanged(cdkmol);
}
else if(event.getProperty().equals( JChemPaintEditor.
MODEL_LOADED )) {
JChemPaintEditor jcp=(JChemPaintEditor)event.getSource();
ICDKMolecule cdkmol = jcp.getCDKMolecule();
JCPModelLoaded(cdkmol);
}
}
};
jcp.addPropertyChangedListener(jcplistener);
molListenerMap.put(jcp.getCDKMolecule(), jcplistener);
}

private void JCPModelLoaded(ICDKMolecule cdkmol){
logger.debug ("EventView reacting: JCP model is loaded. Molecule: " + cdkmol);
Expand Down Expand Up @@ -1569,7 +1602,7 @@ private void doSetUpTestRuns( ICDKMolecule mol ) {
for (TestRun tr : activeTestRuns){
if (tr.getTest().getId().equals( epTest.getId() )){
ep.addTestRun( tr );
logger.debug("Added cached TestRun: " + tr);
// logger.debug("Added cached TestRun: " + tr);
}
}

Expand All @@ -1582,6 +1615,7 @@ private void doSetUpTestRuns( ICDKMolecule mol ) {

updateView();

}
}


}

0 comments on commit 61b0bda

Please sign in to comment.