From 8861bd1fe381d7b127428449ef35d74852e57946 Mon Sep 17 00:00:00 2001 From: Mathieu Bastian Date: Tue, 27 Sep 2011 18:47:13 -0700 Subject: [PATCH] Refactor preview ui model and rationalize the way how components communicate with it --- .../gephi/desktop/preview/Bundle.properties | 2 - .../preview/PreviewSettingsTopComponent.java | 53 +++++-- .../desktop/preview/PreviewTopComponent.form | 5 - .../desktop/preview/PreviewTopComponent.java | 69 +++++---- .../preview/PreviewUIControllerImpl.java | 136 ++++++------------ .../desktop/preview/PreviewUIModelImpl.java | 19 +++ .../preview/api/PreviewUIController.java | 12 ++ .../desktop/preview/api/PreviewUIModel.java | 8 +- 8 files changed, 162 insertions(+), 142 deletions(-) diff --git a/DesktopPreview/src/org/gephi/desktop/preview/Bundle.properties b/DesktopPreview/src/org/gephi/desktop/preview/Bundle.properties index d1cbf4aff..d018e648a 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/Bundle.properties +++ b/DesktopPreview/src/org/gephi/desktop/preview/Bundle.properties @@ -28,8 +28,6 @@ PreviewSettingsTopComponent.refreshButton.text=Refresh PreviewSettingsTopComponent.labelPreset.text=Presets PreviewSettingsTopComponent.ratio.minimum = Minimum PreviewSettingsTopComponent.propertySheetTab = Settings - -PreviewTopComponent.southBusyLabel.text= PreviewTopComponent.backgroundButton.text=Background PreviewTopComponent.resetZoomButton.text=Reset zoom PreviewSettingsTopComponent.svgExportButton.text=SVG/PDF/PNG diff --git a/DesktopPreview/src/org/gephi/desktop/preview/PreviewSettingsTopComponent.java b/DesktopPreview/src/org/gephi/desktop/preview/PreviewSettingsTopComponent.java index 8e80c7252..ca39d93ab 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/PreviewSettingsTopComponent.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/PreviewSettingsTopComponent.java @@ -25,6 +25,8 @@ License, or (at your option) any later version. import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.Serializable; import java.text.NumberFormat; import java.util.logging.Logger; @@ -58,7 +60,7 @@ License, or (at your option) any later version. * * @author Jérémy Subtil, Mathieu Bastian */ -public final class PreviewSettingsTopComponent extends TopComponent { +public final class PreviewSettingsTopComponent extends TopComponent implements PropertyChangeListener { private static PreviewSettingsTopComponent instance; static final String ICON_PATH = "org/gephi/desktop/preview/resources/settings.png"; @@ -94,16 +96,6 @@ private PreviewSettingsTopComponent() { propertiesPanel.add(propertySheet, BorderLayout.CENTER); } - - // checks the state of the workspace - ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); - if (pc.getCurrentWorkspace() != null) { - enableRefreshButton(); - } - - // forces the controller instanciation - PreviewUIController puic = Lookup.getDefault().lookup(PreviewUIController.class); - //Ratio ratioSlider.addChangeListener(new ChangeListener() { @@ -116,6 +108,8 @@ public void stateChanged(ChangeEvent e) { } else { ratioLabel.setText(formatter.format(val)); } + PreviewUIController puic = Lookup.getDefault().lookup(PreviewUIController.class); + puic.setVisibilityRatio(getVisibilityRatio()); } }); @@ -142,13 +136,41 @@ public void actionPerformed(ActionEvent e) { ui.action(); } }); - refreshModel(); + setup(null); + + PreviewUIController controller = Lookup.getDefault().lookup(PreviewUIController.class); + controller.addPropertyChangeListener(this); + + PreviewUIModel m = controller.getModel(); + if (m != null) { + setup(m); + enableRefreshButton(); + } + } + + public void propertyChange(PropertyChangeEvent evt) { + if (evt.getPropertyName().equals(PreviewUIController.SELECT)) { + PreviewUIModel model = (PreviewUIModel) evt.getNewValue(); + setup(model); + if (model != null) { + enableRefreshButton(); + } else { + disableRefreshButton(); + } + } else if (evt.getPropertyName().equals(PreviewUIController.REFRESHED)) { + } else if (evt.getPropertyName().equals(PreviewUIController.REFRESHING)) { + boolean refrehsing = (Boolean) evt.getNewValue(); + if (refrehsing) { + disableRefreshButton(); + } else { + enableRefreshButton(); + } + } } - public void refreshModel() { + public void setup(PreviewUIModel previewModel) { propertySheet.setNodes(new Node[]{new PreviewNode(propertySheet)}); PreviewUIController previewUIController = Lookup.getDefault().lookup(PreviewUIController.class); - PreviewUIModel previewModel = previewUIController.getModel(); if (previewModel != null) { ratioSlider.setValue((int) (previewModel.getVisibilityRatio() * 100)); } @@ -208,6 +230,9 @@ public void refreshModel() { } } } + + public void unsetup() { + } /** * Returns the graph visibility ratio set in the visibilityRatioSpinner diff --git a/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.form b/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.form index 7b31ae1ab..326bc0a4e 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.form +++ b/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.form @@ -17,11 +17,6 @@ - - - - - diff --git a/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.java b/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.java index e649d09cf..cdb6a76a6 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/PreviewTopComponent.java @@ -53,13 +53,14 @@ License, or (at your option) any later version. * * @author Jérémy Subtil, Mathieu Bastian */ -public final class PreviewTopComponent extends TopComponent { +public final class PreviewTopComponent extends TopComponent implements PropertyChangeListener { private static PreviewTopComponent instance; static final String ICON_PATH = "org/gephi/desktop/preview/resources/preview.png"; private static final String PREFERRED_ID = "PreviewTopComponent"; private final transient ProcessingListener processingListener = new ProcessingListener(); //Data + private transient PreviewUIModel model; private transient ProcessingTarget target; private transient PApplet sketch; @@ -106,17 +107,36 @@ public void actionPerformed(ActionEvent e) { target.zoomMinus(); } }); + + PreviewUIController controller = Lookup.getDefault().lookup(PreviewUIController.class); + controller.addPropertyChangeListener(this); + + PreviewUIModel m = controller.getModel(); + if (m != null) { + this.model = m; + initTarget(model); + } } - public void refreshModel() { - PreviewUIController puic = Lookup.getDefault().lookup(PreviewUIController.class); - PreviewUIModel previewUIModel = puic.getModel(); - if (previewUIModel == null) { - //Disable - initTarget(null); - } else { - initTarget(previewUIModel); - refreshPreview(); + public void propertyChange(PropertyChangeEvent evt) { + if (evt.getPropertyName().equals(PreviewUIController.SELECT)) { + this.model = (PreviewUIModel) evt.getNewValue(); + initTarget(model); + } else if (evt.getPropertyName().equals(PreviewUIController.REFRESHED)) { + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + target.refresh(); + } + }); + } else if (evt.getPropertyName().equals(PreviewUIController.REFRESHING)) { + setRefresh((Boolean) evt.getNewValue()); + } else if (evt.getPropertyName().equals(PreviewUIController.GRAPH_CHANGED)) { + if ((Boolean) evt.getNewValue()) { + showBannerPanel(); + } else { + hideBannerPanel(); + } } } @@ -180,12 +200,16 @@ public void run() { * @see PreviewUIController#showRefreshNotification() */ public void showBannerPanel() { - bannerPanel.setVisible(true); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + bannerPanel.setVisible(true); + } + }); } public void setBackgroundColor(Color color) { ((JColorButton) backgroundButton).setColor(color); - } /** @@ -194,7 +218,12 @@ public void setBackgroundColor(Color color) { * @see PreviewUIController#hideRefreshNotification() */ public void hideBannerPanel() { - bannerPanel.setVisible(false); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + bannerPanel.setVisible(false); + } + }); } /** This method is called from within the constructor to @@ -221,8 +250,6 @@ private void initComponents() { plusButton = new javax.swing.JButton(); setLayout(new java.awt.GridBagLayout()); - - org.openide.awt.Mnemonics.setLocalizedText(southBusyLabel, org.openide.util.NbBundle.getMessage(PreviewTopComponent.class, "PreviewTopComponent.southBusyLabel.text")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; @@ -412,16 +439,4 @@ public Object readResolve() { return PreviewTopComponent.getDefault(); } } - - /** - * Refresh the preview applet. - */ - private void refreshPreview() { - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - target.refresh(); - } - }); - } } diff --git a/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIControllerImpl.java b/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIControllerImpl.java index c3e39ee40..799ebacf2 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIControllerImpl.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIControllerImpl.java @@ -20,9 +20,13 @@ License, or (at your option) any later version. */ package org.gephi.desktop.preview; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.beans.PropertyEditorManager; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.swing.SwingUtilities; import org.gephi.data.attributes.api.AttributeController; @@ -63,6 +67,7 @@ License, or (at your option) any later version. @ServiceProvider(service = PreviewUIController.class) public class PreviewUIControllerImpl implements PreviewUIController, GraphListener { + private final List listeners; private final PreviewController previewController; private final GraphController graphController; private final PresetUtils presetUtils = new PresetUtils(); @@ -70,15 +75,9 @@ public class PreviewUIControllerImpl implements PreviewUIController, GraphListen private GraphModel graphModel = null; public PreviewUIControllerImpl() { - previewController = Lookup.getDefault().lookup(PreviewController.class); - graphController = Lookup.getDefault().lookup(GraphController.class); - PreviewModel previewModel = previewController.getModel(); - if (previewModel != null) { - graphModel = graphController.getModel(); - graphModel.addGraphListener(this); - } - + listeners = new ArrayList(); ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); + graphController = Lookup.getDefault().lookup(GraphController.class); pc.addWorkspaceListener(new WorkspaceListener() { public void initialize(Workspace workspace) { @@ -98,16 +97,7 @@ public void select(Workspace workspace) { model = new PreviewUIModelImpl(); workspace.add(model); } - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - PreviewSettingsTopComponent settingsTopComponent = PreviewSettingsTopComponent.findInstance(); - if (settingsTopComponent.isVisible()) { - settingsTopComponent.refreshModel(); - PreviewTopComponent.findInstance().refreshModel(); - } - } - }); + fireEvent(SELECT, model); } public void unselect(Workspace workspace) { @@ -115,6 +105,7 @@ public void unselect(Workspace workspace) { graphModel.removeGraphListener(PreviewUIControllerImpl.this); graphModel = null; } + fireEvent(UNSELECT, model); } public void close(Workspace workspace) { @@ -125,16 +116,8 @@ public void disable() { graphModel.removeGraphListener(PreviewUIControllerImpl.this); graphModel = null; } - disableRefresh(); - - //When project is closed, clear graph preview instead of keeping it: - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - PreviewSettingsTopComponent.findInstance().refreshModel(); - PreviewTopComponent.findInstance().refreshModel(); - } - }); + fireEvent(SELECT, null); + model = null; } }); if (pc.getCurrentWorkspace() != null) { @@ -143,8 +126,12 @@ public void run() { model = new PreviewUIModelImpl(); pc.getCurrentWorkspace().add(model); } + graphModel = graphController.getModel(); + graphModel.addGraphListener(this); } + previewController = Lookup.getDefault().lookup(PreviewController.class); + //Register editors PropertyEditorManager.registerEditor(EdgeColor.class, EdgeColorPropertyEditor.class); PropertyEditorManager.registerEditor(DependantOriginalColor.class, DependantOriginalColorPropertyEditor.class); @@ -159,29 +146,30 @@ public void run() { * @see GraphListener#graphChanged(org.gephi.graph.api.GraphEvent) */ public void graphChanged(GraphEvent event) { - showRefreshNotification(); + boolean previous = model.isWorkspaceBarVisible(); + model.setWorkspaceBarVisible(true); + if (!previous) { + fireEvent(GRAPH_CHANGED, true); + } } /** * Refreshes the preview applet. */ public void refreshPreview() { - final PreviewTopComponent previewTopComponent = PreviewTopComponent.findInstance(); - final float visibilityRatio = PreviewSettingsTopComponent.findInstance().getVisibilityRatio(); - setVisibilityRatio(visibilityRatio); - Thread refreshThread = new Thread(new Runnable() { public void run() { - previewTopComponent.setRefresh(true); - disableRefresh(); - hideRefreshNotification(); + model.setRefreshing(true); + fireEvent(REFRESHING, true); + previewController.refreshPreview(); - previewTopComponent.refreshModel(); + fireEvent(REFRESHED, model); - previewTopComponent.setRefresh(false); - enableRefresh(); + model.setRefreshing(false); + fireEvent(REFRESHING, false); + fireEvent(GRAPH_CHANGED, false); } }, "Refresh Preview"); refreshThread.start(); @@ -200,59 +188,6 @@ public void run() { }); } - /** - * Disables the preview refresh action. - * - * The preview refresh notification is also hidden. - */ - private void disableRefresh() { - hideRefreshNotification(); - - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - PreviewSettingsTopComponent previewSettingsTopComponent = PreviewSettingsTopComponent.findInstance(); - previewSettingsTopComponent.disableRefreshButton(); - } - }); - } - private boolean showingRefresh = false; - - /** - * Shows a notification to invite the user to refresh the preview. - * - * The refresh action is enabled. - */ - private void showRefreshNotification() { - if (showingRefresh) { - return; - } - showingRefresh = true; - enableRefresh(); - - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - PreviewTopComponent previewTopComponent = PreviewTopComponent.findInstance(); - previewTopComponent.showBannerPanel(); - } - }); - } - - /** - * Hides the preview refresh notification. - */ - private void hideRefreshNotification() { - showingRefresh = false; - SwingUtilities.invokeLater(new Runnable() { - - public void run() { - PreviewTopComponent previewTopComponent = PreviewTopComponent.findInstance(); - previewTopComponent.hideBannerPanel(); - } - }); - } - public void setVisibilityRatio(float visibilityRatio) { if (model != null) { model.setVisibilityRatio(visibilityRatio); @@ -297,4 +232,21 @@ public void savePreset(String name) { model.setCurrentPreset(preset); } } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + if (!listeners.contains(listener)) { + listeners.add(listener); + } + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + listeners.remove(listener); + } + + private void fireEvent(String eventName, Object data) { + PropertyChangeEvent event = new PropertyChangeEvent(this, eventName, null, data); + for (PropertyChangeListener l : listeners) { + l.propertyChange(event); + } + } } diff --git a/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIModelImpl.java b/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIModelImpl.java index 3b31f0641..ec1f7fcf7 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIModelImpl.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/PreviewUIModelImpl.java @@ -33,8 +33,11 @@ License, or (at your option) any later version. */ public class PreviewUIModelImpl implements PreviewUIModel { + //Data private float visibilityRatio = 1f; private PreviewPreset currentPreset; + private boolean refreshing; + private boolean workspaceBarVisible; public PreviewUIModelImpl() { currentPreset = new DefaultPreset(); @@ -57,4 +60,20 @@ public void setVisibilityRatio(float visibilityRatio) { public void setCurrentPreset(PreviewPreset preset) { currentPreset = preset; } + + public boolean isRefreshing() { + return refreshing; + } + + public boolean isWorkspaceBarVisible() { + return workspaceBarVisible; + } + + public void setRefreshing(boolean refreshing) { + this.refreshing = refreshing; + } + + public void setWorkspaceBarVisible(boolean workspaceBarVisible) { + this.workspaceBarVisible = workspaceBarVisible; + } } diff --git a/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIController.java b/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIController.java index b1be37a18..d2baefe77 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIController.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIController.java @@ -20,6 +20,7 @@ License, or (at your option) any later version. */ package org.gephi.desktop.preview.api; +import java.beans.PropertyChangeListener; import org.gephi.preview.api.PreviewPreset; /** @@ -28,6 +29,13 @@ License, or (at your option) any later version. */ public interface PreviewUIController { + //Property + public static final String SELECT = "select"; + public static final String UNSELECT = "unselect"; + public static final String REFRESHED = "refreshed"; + public static final String REFRESHING = "refreshing"; + public static final String GRAPH_CHANGED = "graph_changed"; + public void refreshPreview(); public void setCurrentPreset(PreviewPreset preset); @@ -43,4 +51,8 @@ public interface PreviewUIController { public void savePreset(String name); public PreviewUIModel getModel(); + + public void addPropertyChangeListener(PropertyChangeListener listener); + + public void removePropertyChangeListener(PropertyChangeListener listener); } diff --git a/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIModel.java b/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIModel.java index 6a143fc7a..b9a27de0b 100644 --- a/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIModel.java +++ b/DesktopPreview/src/org/gephi/desktop/preview/api/PreviewUIModel.java @@ -27,8 +27,12 @@ License, or (at your option) any later version. * @author Mathieu Bastian */ public interface PreviewUIModel { - + public PreviewPreset getCurrentPreset(); - + public float getVisibilityRatio(); + + public boolean isRefreshing(); + + public boolean isWorkspaceBarVisible(); }