From c83652cc2b12f7ad593a8a94d10fb1fb0f60f6a4 Mon Sep 17 00:00:00 2001 From: Philippe Charles Date: Wed, 5 Sep 2018 11:00:44 +0200 Subject: [PATCH] Added temp fix for Ts deadlock. --- .../FixedRegTsProcessingViewer.java | 151 ++++++++++++++++++ .../TsDisaggregationModelTopComponent.java | 7 +- 2 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/FixedRegTsProcessingViewer.java diff --git a/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/FixedRegTsProcessingViewer.java b/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/FixedRegTsProcessingViewer.java new file mode 100644 index 0000000..466071c --- /dev/null +++ b/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/FixedRegTsProcessingViewer.java @@ -0,0 +1,151 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package ec.nbdemetra.disaggregation; + +import com.google.common.collect.ObjectArrays; +import ec.tss.Ts; +import ec.tss.TsInformationType; +import ec.tss.documents.MultiTsDocument; +import ec.tstoolkit.algorithm.IProcSpecification; +import ec.ui.interfaces.ITsCollectionView.TsUpdateMode; +import ec.ui.interfaces.ITsList.InfoType; +import ec.ui.list.JTsList; +import ec.ui.view.tsprocessing.DefaultProcessingViewer; +import java.awt.Dimension; +import java.util.Arrays; +import javax.swing.Box; +import javax.swing.JLabel; +import javax.swing.JToolBar; + +/** + * + * @author Jean + */ +final class FixedRegTsProcessingViewer extends DefaultProcessingViewer { + + // FACTORY METHODS > + public static FixedRegTsProcessingViewer create(MultiTsDocument doc) { + FixedRegTsProcessingViewer viewer = new FixedRegTsProcessingViewer(DefaultProcessingViewer.Type.APPLY); + if (doc != null) { + viewer.setDocument(doc); + } + return viewer; + } + // visual components + private final JTsList yList, xList; + private final JLabel specLabel; + private boolean quietRefresh; + + public FixedRegTsProcessingViewer(DefaultProcessingViewer.Type type) { + super(type); + yList = new JTsList(); + yList.setVisible(true); + yList.setShowHeader(false); + yList.setTsUpdateMode(TsUpdateMode.Single); + yList.setInformation(new InfoType[]{InfoType.Name}); + xList = new JTsList(); + yList.setVisible(true); + xList.setShowHeader(false); + xList.setInformation(new InfoType[]{InfoType.Name}); + this.specLabel = new JLabel("Spec: "); + specLabel.setVisible(true); + xList.setPreferredSize(new Dimension(50, 60)); + yList.setPreferredSize(new Dimension(50, 60)); + + toolBar.add(Box.createHorizontalStrut(3), 0); + toolBar.add(new JLabel("Y: "), 1); + toolBar.add(new JToolBar.Separator(), 2); + toolBar.add(yList, 3); + toolBar.add(new JToolBar.Separator(), 4); + toolBar.add(new JLabel("X: "), 5); + toolBar.add(new JToolBar.Separator(), 6); + toolBar.add(xList, 7); + toolBar.add(new JToolBar.Separator(), 8); + toolBar.add(specLabel, 9); + toolBar.add(new JToolBar.Separator(), 10); + toolBar.add(Box.createHorizontalStrut(100), 11); + + toolBar.setVisible(true); + + // FIXME: there is a deadlock with Ts.Master#freeze() & TsFactory#update(TsInformation); + // these two lines force loading to prevent deadlock but might freeze EDT + xList.setFreezeOnImport(true); + yList.setFreezeOnImport(true); + + xList.addPropertyChangeListener(JTsList.TS_COLLECTION_PROPERTY, evt -> { + if (!quietRefresh) { + updateDocument(); + } + }); + yList.addPropertyChangeListener(JTsList.TS_COLLECTION_PROPERTY, evt -> { + if (!quietRefresh) { + updateDocument(); + } + }); + } + + private void updateDocument() { + try { + + quietRefresh = true; + Ts[] y = yList.getTsCollection().toArray(); + if (y == null || y.length == 0) { + getDocument().setInput(null); + } + Ts[] x = xList.getTsCollection().toArray(); + if (x == null || x.length == 0) { + getDocument().setInput(y); + } else { + getDocument().setInput(ObjectArrays.concat(y[0], x)); + } + refreshAll(); + } catch (Exception err) { + } finally { + quietRefresh = false; + } + } + + private void updateList() { + Ts[] s = getDocument().getTs(); + if (s == null || s.length == 0) { + yList.getTsCollection().clear(); + xList.getTsCollection().clear(); + } else { + yList.getTsCollection().replace(s[0]); + xList.getTsCollection().replace(Arrays.copyOfRange(s, 1, s.length)); + } + } + + public void refreshSpec() { + MultiTsDocument doc = getDocument(); + IProcSpecification spec = doc.getSpecification(); + specLabel.setText("Spec: " + (spec != null ? spec.toString() : "")); + } + + @Override + public void refreshHeader() { + try { + if (quietRefresh) { + return; + } + refreshSpec(); + updateList(); + } catch (Exception err) { + } + } + + @Override + public void refreshView() { + super.refreshView(); + refreshSpec(); + } + + @Override + public void dispose() { + super.dispose(); + yList.dispose(); + xList.dispose(); + } +} diff --git a/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/TsDisaggregationModelTopComponent.java b/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/TsDisaggregationModelTopComponent.java index d9052f4..6857c13 100644 --- a/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/TsDisaggregationModelTopComponent.java +++ b/nbdemetra-benchmarking/src/main/java/ec/nbdemetra/disaggregation/TsDisaggregationModelTopComponent.java @@ -17,14 +17,11 @@ package ec.nbdemetra.disaggregation; import ec.nbdemetra.ui.NbUtilities; -import ec.nbdemetra.ws.WorkspaceFactory; import ec.nbdemetra.ws.WorkspaceItem; import ec.nbdemetra.ws.ui.WorkspaceTopComponent; import ec.tss.disaggregation.documents.TsDisaggregationModelDocument; import ec.ui.view.tsprocessing.RegTsProcessingViewer; import org.netbeans.api.settings.ConvertAsProperties; -import org.openide.awt.ActionID; -import org.openide.awt.ActionReference; import org.openide.util.NbBundle; import org.openide.windows.TopComponent; @@ -47,7 +44,7 @@ }) public final class TsDisaggregationModelTopComponent extends WorkspaceTopComponent { - protected RegTsProcessingViewer panel; + protected FixedRegTsProcessingViewer panel; public TsDisaggregationModelTopComponent() { super(null); @@ -63,7 +60,7 @@ public void initDocument() { setName(getDocument().getDisplayName()); setToolTipText(Bundle.HINT_TsDisaggregationModelTopComponent()); initComponents(); - panel = RegTsProcessingViewer.create(this.getDocument().getElement()); + panel = FixedRegTsProcessingViewer.create(this.getDocument().getElement()); add(panel); }