Skip to content
Snippets Groups Projects
Commit a29cd448 authored by MADELA Patrick's avatar MADELA Patrick
Browse files

Merge dockingcore into docking

parents 95fb60ee 3d34e3b8
No related branches found
No related tags found
No related merge requests found
Showing
with 2032 additions and 0 deletions
.project
.classpath
.settings
target
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fr.soleil.maven.parent</groupId>
<artifactId>java-updated-versions</artifactId>
<version>0-SNAPSHOT</version>
</parent>
<groupId>fr.soleil.lib</groupId>
<artifactId>DockingCore</artifactId>
<version>1.1.4</version>
<name>Docking Core</name>
<description>A project that defines some common abstractions in docking</description>
<developers>
<developer>
<id>girardot</id>
<name>Raphaël GIRARDOT</name>
<email>raphael.girardot@synchrotron-soleil.fr</email>
<organization>SOLEIL</organization>
<organizationUrl>http://www.synchrotron-soleil.fr</organizationUrl>
<roles>
<role>Manager</role>
</roles>
<timezone>1</timezone>
</developer>
<developer>
<id>viguier</id>
<name>Grégory VIGUIER</name>
<email>gregory.viguier@synchrotron-soleil.fr</email>
<organization>SOLEIL</organization>
<organizationUrl>http://www.synchrotron-soleil.fr</organizationUrl>
<roles>
<role>Java Developer</role>
</roles>
<timezone>1</timezone>
</developer>
</developers>
<scm>
<connection>scm:git:git@gitlab.synchrotron-soleil.fr:software-control-system/libraries/java/dockingcore.git</connection>
<developerConnection>scm:git:git@gitlab.synchrotron-soleil.fr:software-control-system/libraries/java/dockingcore.git</developerConnection>
<url>https://gitlab.synchrotron-soleil.fr/software-control-system/libraries/java/dockingcore</url>
</scm>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>fr.soleil.lib</groupId>
<artifactId>BasicUtilities</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import fr.soleil.docking.action.SaveDefaultPerspertiveAction;
import fr.soleil.docking.exception.DockingException;
import fr.soleil.docking.perspective.IPerspective;
import fr.soleil.docking.perspective.IPerspectiveFactory;
import fr.soleil.docking.view.IViewFactory;
import fr.soleil.lib.project.ObjectUtils;
/**
* A class that prepares docking. Obtain your RootWindow from this class.
*
* @author HARDION
* @author GIRARDOT
*/
public abstract class ADockingManager implements PropertyChangeListener {
private boolean automaticallySavePerspective = true;
public boolean isAutomaticallySavePerspective() {
return automaticallySavePerspective;
}
public void setAutomaticallySavePerspective(boolean automaticallySavePerspective) {
this.automaticallySavePerspective = automaticallySavePerspective;
}
protected IPerspectiveFactory perspectiveFactory = null;
protected IViewFactory viewFactory = null;
public ADockingManager(IViewFactory viewFactory, IPerspectiveFactory perspectiveFactory) {
this.viewFactory = viewFactory;
this.viewFactory.addPropertyChangeListener(this);
this.perspectiveFactory = perspectiveFactory;
this.perspectiveFactory.addPropertyChangeListener(this);
this.initDockingArea();
}
public List<Action> getActionList() {
List<Action> result = null;
if (viewFactory != null) {
result = viewFactory.getActionList();
}
if (Boolean.getBoolean("DEBUG")) {
result.add(new SaveDefaultPerspertiveAction(this));
}
return result;
}
/**
* initialize this {@link ADockingManager}'s main docking area
*
*/
protected abstract void initDockingArea();
/**
* Returns this {@link ADockingManager}'s main docking area
*
* @return A {@link JComponent}
*/
public abstract JComponent getDockingArea();
/**
* Creates a new docking area
*
* @param areaBackground The preferred area background. May be <code>null</code>, in which case this
* {@link ADockingManager} chooses the {@link Color} to use
*
* @return A {@link JComponent}
*/
public abstract JComponent createNewDockingArea(Color areaBackground);
/**
* Changes the background of a docking area
*
* @param dockingArea The docking area
* @param areaBackground The background to set
*/
public abstract void setDockingAreaBeackground(JComponent dockingArea, Color areaBackground);
/**
* Returns the perspective factory
*
* @return The perspective factory
*/
public IPerspectiveFactory getPerspectiveFactory() {
return perspectiveFactory;
}
/**
* Returns the view factory
*
* @return The view factory
*/
public IViewFactory getViewFactory() {
return viewFactory;
}
public void loadPreferences(Preferences prefs) throws DockingException {
this.perspectiveFactory.loadPreferences(prefs);
this.viewFactory.loadPreferences(prefs);
this.applyPerspective(perspectiveFactory.getSelectedPerspective());
}
public void resetLayout() throws DockingException {
applyPerspective(perspectiveFactory.getDefault());
}
/**
* Applies a perspective to the main docking area
*
* @param perspective The perspective to apply
* @throws DockingException If a problem occurred while trying to apply the perspective
*/
public void applyPerspective(IPerspective perspective) throws DockingException {
applyPerspective(perspective, getDockingArea());
}
/**
* Applies a perspective to a docking area
*
* @param perspective The perspective to apply
* @param dockingArea The docking area
* @throws DockingException If a problem occurred while trying to apply the perspective
*/
public abstract void applyPerspective(IPerspective perspective, JComponent dockingArea) throws DockingException;
public void savePreferences(Preferences prefs) throws DockingException {
// Save current Perspective
this.updatePerspective(this.perspectiveFactory.getSelectedPerspective());
// Save Preferences of perspective Factory
this.perspectiveFactory.savePreferences(prefs);
// Save Preferences of Views
this.viewFactory.savePreferences(prefs);
}
/**
* Writes main docking area layout in a perspective
*
* @param perspective The perspective
*/
protected void updatePerspective(IPerspective perspective) {
try {
updatePerspective(perspective, getDockingArea());
} catch (DockingException e) {
JOptionPane.showMessageDialog(getDockingArea(), e.getMessage() + " (see traces)", getClass()
.getSimpleName() + " Docking - Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
/**
* Writes a docking area layout in a perspective
*
* @param perspective The perspective
* @param dockingArea The docking area
* @throws DockingException If a problem occurred while writing the layout
*/
public abstract void updatePerspective(IPerspective perspective, JComponent dockingArea) throws DockingException;
public void saveDefault(File file) throws DockingException {
this.updatePerspective(this.perspectiveFactory.getSelectedPerspective());
this.perspectiveFactory.saveSelected(file);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt != null) {
if (ObjectUtils.sameObject(perspectiveFactory, evt.getSource())) {
if (IPerspectiveFactory.SELECTED_PERSPECTIVE.equals(evt.getPropertyName())) {
IPerspective oldd = (IPerspective) evt.getOldValue();
IPerspective neww = (IPerspective) evt.getNewValue();
try {
if (automaticallySavePerspective) {
this.updatePerspective(oldd);
}
this.applyPerspective(neww);
} catch (DockingException e) {
e.printStackTrace();
}
}
} else if (ObjectUtils.sameObject(viewFactory, evt.getSource())) {
if (IViewFactory.VIEWS.equals(evt.getPropertyName())) {
this.updateViews(evt);
}
}
}
}
protected abstract void updateViews(PropertyChangeEvent evt);
public void saveSelectedPerspective() throws DockingException {
this.updatePerspective(this.perspectiveFactory.getSelectedPerspective());
}
/**
* Enable the docking or not If the docking is disable, the inherit class must disable all
* functionality of the docking. I must be comparable to a JPanel composed by JtabbedPane,
* JSplitPane etc...
*
* @param enabledDocking
*/
public void setEnabledDocking(boolean enabledDocking) {
setEnabledDocking(enabledDocking, getDockingArea());
}
/**
* Get the Enabled Docking property
*
* @return true if the docking is enabled
*/
public boolean isEnabledDocking() {
return isEnabledDocking(getDockingArea());
}
/**
* Enable the docking or not in a docking area. If the docking is disable, the inherit class must disable all
* functionality of the docking for the docking area. I must be comparable to a JPanel composed by JtabbedPane,
* JSplitPane etc...
*
* @param enabledDocking Whetehr to enable docking
* @param dockingArea The docking area for which to enable/disable docking
*/
public abstract void setEnabledDocking(boolean enabledDocking, JComponent dockingArea);
/**
* Get the Enabled Docking property for a docking area
*
* @param dockingArea The docking area to test
*
* @return true if the docking is enabled
*/
public abstract boolean isEnabledDocking(JComponent dockingArea);
/**
* Returns whether a view, once closed, is sure to be restored at the exact previous position once shown again
*
* @return A <code>boolean</code>
*/
public abstract boolean canRestoreExactViewPosition();
/**
* Configures a docking area to accept or not component undocking (window creation outside of the docking area for
* given component)
*
* @param enabled Whether to accept component undocking
* @param dockingArea The concerned docking area
*/
public abstract void setUndockEnabled(boolean enabled, JComponent dockingArea);
/**
* Configures main docking area to accept or not component undocking
*
* @param enabled Whether to accept component undocking
*/
public void setUndockEnabled(boolean enabled) {
setUndockEnabled(enabled, getDockingArea());
}
/**
* Returns whether a docking area accepts component undocking (window creation outside of the docking area for
* given component)
*
* @param dockingArea The concerned docking area
* @return A <code>boolean</code>
*/
public abstract boolean isUndockEnabled(JComponent dockingArea);
/**
* Returns whether main docking area accepts component undocking
*
* @return A <code>boolean</code>
*/
public boolean isUndockEnabled() {
return isUndockEnabled(getDockingArea());
}
}
/*
* Created on 10 juin 2005
* with Eclipse
*/
package fr.soleil.docking.action;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import fr.soleil.docking.perspective.IPerspective;
import fr.soleil.docking.perspective.IPerspectiveFactory;
public class NewPerspectiveAction extends AbstractAction {
private static final long serialVersionUID = -3295304363885371351L;
private final IPerspectiveFactory factory;
public NewPerspectiveAction(IPerspectiveFactory factory) {
super();
this.factory = factory;
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
// The following values are completely optional
putValue(NAME, "New");
// Set tool tip text
putValue(SHORT_DESCRIPTION, "New Perspective");
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(LONG_DESCRIPTION, "Adding a new perspective");
// Set an icon
// Icon icon = new ImageIcon("icon.gif");
// putValue(Action.SMALL_ICON, icon);
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
putValue(MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_N));
// Set an accelerator key; this value is used by menu items
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl shift N"));
}
@Override
public void actionPerformed(ActionEvent e) {
IPerspective current = factory.getSelectedPerspective();
String result = JOptionPane.showInputDialog("Enter the name of the new perspective");
if ((result != null) && (!result.trim().isEmpty())) {
IPerspective p = factory.createPerspective(result);
byte[] array = current.getByteArray();
byte[] copy = (array == null ? null : array.clone());
p.setByteArray(copy);
factory.setSelectedPerspective(p);
}
}
}
\ No newline at end of file
/*
* Created on 10 juin 2005
* with Eclipse
*/
package fr.soleil.docking.action;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import fr.soleil.docking.perspective.IPerspective;
import fr.soleil.docking.perspective.IPerspectiveFactory;
public class RemovePerspectiveAction extends AbstractAction {
private static final long serialVersionUID = 4431356413525394103L;
private final IPerspectiveFactory factory;
public RemovePerspectiveAction(IPerspectiveFactory factory) {
super();
this.factory = factory;
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
// The following values are completely optional
putValue(NAME, "Remove");
// Set tool tip text
putValue(SHORT_DESCRIPTION, "Remove Perspective");
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(LONG_DESCRIPTION, "Removing a perspective");
// Set an icon
// Icon icon = new ImageIcon("icon.gif");
// putValue(Action.SMALL_ICON, icon);
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
putValue(MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_R));
// Set an accelerator key; this value is used by menu items
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl shift R"));
}
@Override
public void actionPerformed(ActionEvent e) {
IPerspective result = (IPerspective) JOptionPane.showInputDialog(null, "Select the perspective to remove",
"Remove perspective", JOptionPane.PLAIN_MESSAGE, null, factory.getPerspectives(),
factory.getSelectedPerspective());
if (result != null) {
factory.removePerspective(result);
}
}
}
\ No newline at end of file
/*
* Created on 10 juin 2005
* with Eclipse
*/
package fr.soleil.docking.action;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import fr.soleil.docking.ADockingManager;
import fr.soleil.lib.project.ObjectUtils;
public class SaveDefaultPerspertiveAction extends AbstractAction {
private static final long serialVersionUID = -5058889377558638684L;
private final ADockingManager dockingManager;
public SaveDefaultPerspertiveAction(ADockingManager manager) {
super();
this.dockingManager = manager;
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
// The following values are completely optional
putValue(NAME, "Save Default");
// Set tool tip text
putValue(SHORT_DESCRIPTION, "Save this perspective to file system");
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(LONG_DESCRIPTION, "Save this perspective to file system");
// Set an icon
// Icon icon = new ImageIcon("icon.gif");
// putValue(Action.SMALL_ICON, view.getIcon());
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
// putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_N));
// Set an accelerator key; this value is used by menu items
// putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt shift N"));
}
@Override
public void actionPerformed(ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
if (!file.exists())
file.createNewFile();
dockingManager.saveDefault(file);
} catch (Exception e) {
JOptionPane.showMessageDialog((Component) evt.getSource(), ObjectUtils.printStackTrace(e));
}
}
}
@Override
public boolean isEnabled() {
return true;
}
}
\ No newline at end of file
/*
* Created on 10 juin 2005
* with Eclipse
*/
package fr.soleil.docking.action;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import fr.soleil.docking.perspective.IPerspective;
import fr.soleil.docking.perspective.IPerspectiveFactory;
import fr.soleil.lib.project.ObjectUtils;
public class SelectPerspectiveAction extends AbstractAction implements PropertyChangeListener {
private static final long serialVersionUID = 6402682388656946588L;
private final IPerspectiveFactory factory;
private final IPerspective perspective;
public SelectPerspectiveAction(IPerspectiveFactory factory, IPerspective perspective) {
super();
this.factory = factory;
this.factory.addPropertyChangeListener(this);
this.perspective = perspective;
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
// The following values are completely optional
putValue(NAME, perspective.getName());
// Set tool tip text
putValue(SHORT_DESCRIPTION, "Select " + perspective.getName());
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(LONG_DESCRIPTION, "Select " + perspective.getName() + " as current perspective");
// Set an icon
// Icon icon = new ImageIcon("icon.gif");
// putValue(Action.SMALL_ICON, icon);
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
putValue(MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_N));
// Set an accelerator key; this value is used by menu items
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl shift N"));
// Set selected
putValue(SELECTED_KEY, this.factory.getSelectedPerspective() == this.perspective);
}
@Override
public void actionPerformed(ActionEvent e) {
factory.setSelectedPerspective(perspective);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt != null) {
if (IPerspectiveFactory.SELECTED_PERSPECTIVE.equals(evt.getPropertyName())) {
if (ObjectUtils.sameObject(evt.getNewValue(), this.perspective)) {
putValue(SELECTED_KEY, true);
} else {
putValue(SELECTED_KEY, false);
}
}
}
}
}
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.action;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import fr.soleil.docking.view.IView;
public class ViewAction extends AbstractAction {
private static final long serialVersionUID = 2102313023512752659L;
private final IView view;
public ViewAction(IView view) {
super();
this.view = view;
// This is an instance initializer; it is executed just after the
// constructor of the superclass is invoked
// The following values are completely optional
putValue(NAME, view.getTitle());
// Set tool tip text
putValue(SHORT_DESCRIPTION, view.getTitle());
// This text is not directly used by any Swing component;
// however, this text could be used in a help system
putValue(LONG_DESCRIPTION, view.getTitle());
// Set an icon
// Icon icon = new ImageIcon("icon.gif");
putValue(SMALL_ICON, view.getIcon());
// Set a mnemonic character. In most look and feels, this causes the
// specified character to be underlined This indicates that if the component
// using this action has the focus and In some look and feels, this causes
// the specified character in the label to be underlined and
// putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_N));
// Set an accelerator key; this value is used by menu items
// putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt shift N"));
}
@Override
public void actionPerformed(ActionEvent e) {
if (!view.isVisible()) {
view.setVisible(true);
} else {
view.setVisible(false);
}
}
@Override
public boolean isEnabled() {
return view.isEnabled();
}
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import fr.soleil.docking.action.NewPerspectiveAction;
import fr.soleil.docking.perspective.IPerspectiveFactory;
public class PerspectiveMenu extends javax.swing.JMenu implements PropertyChangeListener {
private static final long serialVersionUID = -6492472049072385936L;
protected IPerspectiveFactory factory;
public PerspectiveMenu(IPerspectiveFactory factory) {
super();
this.setText("Perspective");
this.factory = factory;
factory.addPropertyChangeListener(this);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt != null) {
if (evt.getSource() instanceof IPerspectiveFactory) {
if (IPerspectiveFactory.PERSPECTIVES.equals(evt.getPropertyName())) {
// TODO
}
}
}
}
@Override
public JMenuItem add(Action a) {
if (a instanceof NewPerspectiveAction) {
this.addSeparator();
}
return super.add(a);
}
@Override
public void setPopupMenuVisible(boolean flag) {
if (flag) {
this.removeAll();
List<Action> actions = factory.getActionList();
for (Action action : actions) {
add(action);
// JRadioButtonMenuItem item = (JRadioButtonMenuItem) this.add(action);
// Boolean b = (Boolean) action.getValue("SELECTED_KEY");
// if (b != null) {
// item.setSelected(b);
// }
}
}
super.setPopupMenuVisible(flag);
}
@Override
protected JMenuItem createActionComponent(Action a) {
JMenuItem mi = new JRadioButtonMenuItem() {
private static final long serialVersionUID = -3334691946468856486L;
@Override
protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
PropertyChangeListener pcl = createActionChangeListener(this);
if (pcl == null) {
pcl = super.createActionPropertyChangeListener(a);
}
return pcl;
}
};
mi.setHorizontalTextPosition(JButton.TRAILING);
mi.setVerticalTextPosition(JButton.CENTER);
return mi;
}
}
package fr.soleil.docking.event;
import java.util.EventObject;
import fr.soleil.docking.view.IView;
/**
* An {@link EventObject} that describes the changes in an {@link IView}
*
* @author GIRARDOT
*/
public class ViewEvent extends EventObject {
private static final long serialVersionUID = 1725475008782759147L;
/**
* An <code>int</code> used to notify that the associated view was closed
*/
public static final int VIEW_CLOSED = 0;
/**
* An <code>int</code> used to notify that the associated view gained the focus
*/
public static final int FOCUS_GAINED = 1;
/**
* An <code>int</code> used to notify that the associated view lost the focus
*/
public static final int FOCUS_LOST = 2;
protected final int reason;
public ViewEvent(IView source, int reason) {
super(source);
this.reason = reason;
}
@Override
public IView getSource() {
return (IView) super.getSource();
}
/**
* Returns the reason of the changes
*
* @return An <code>int</code>
* @see #VIEW_CLOSED
* @see #FOCUS_GAINED
* @see #FOCUS_LOST
*/
public int getReason() {
return reason;
}
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.exception;
/**
* An Exception thrown in case of problems with docking
*
* @author girardot
*/
public class DockingException extends Exception {
private static final long serialVersionUID = 5753611012723925293L;
public DockingException() {
super();
}
public DockingException(String message) {
super(message);
}
public DockingException(Throwable cause) {
super(cause);
}
public DockingException(String message, Throwable cause) {
super(message, cause);
}
}
package fr.soleil.docking.listener;
import java.util.EventListener;
import fr.soleil.docking.event.ViewEvent;
import fr.soleil.docking.view.IView;
/**
* An {@link EventListener} that listens to the changes in an {@link IView}
*
* @author GIRARDOT
*/
public interface IViewListener extends EventListener {
/**
* Notifies this {@link IViewListener} for some changes in an {@link IView}
*
* @param event The {@link ViewEvent} that describes the concerned {@link IView} and changes
*/
public void viewChanged(ViewEvent event);
}
package fr.soleil.docking.listener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.WeakHashMap;
import fr.soleil.docking.event.ViewEvent;
/**
* A class delegated to {@link IViewListener}s management
*
* @author GIRARDOT
*/
public class ViewListenerDelegate {
protected final Collection<IViewListener> listeners;
public ViewListenerDelegate() {
listeners = Collections.newSetFromMap(new WeakHashMap<IViewListener, Boolean>());
}
/**
* Warns all {@link IViewListener}s for some changes
*
* @param event The {@link ViewEvent} that describes the changes
*/
public void warnListeners(ViewEvent event) {
List<IViewListener> copy;
synchronized (listeners) {
copy = new ArrayList<IViewListener>(listeners.size());
copy.addAll(listeners);
}
for (IViewListener listener : copy) {
if (listener != null) {
listener.viewChanged(event);
}
}
copy.clear();
}
/**
* Adds a new {@link IViewListener}
*
* @param listener the {@link IViewListener} to add
*/
public void addViewListener(final IViewListener listener) {
if (listener != null) {
synchronized (listeners) {
listeners.add(listener);
}
}
}
/**
* Removes an {@link IViewListener}
*
* @param listener The {@link IViewListener} to remove
*/
public void removeViewListener(IViewListener listener) {
if (listener != null) {
synchronized (listeners) {
listeners.remove(listener);
}
}
}
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.perspective;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@Deprecated
public class FilePerspective extends Perspective {
public FilePerspective(String name, File file) throws IOException {
super(name);
if ((file != null) && (file.isFile())) {
InputStream in = new FileInputStream(file);
if (in != null) {
byteArray = PerspectiveFactory.readByteArray(in);
}
}
}
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.perspective;
/**
* Objects that implement this interface should know where to put each dockable component.
*
* @author Hardion
* @author GIRARDOT
*/
public interface IPerspective {
public String getName();
/**
* @return the byteArray
*/
public byte[] getByteArray();
/**
* @param byteArray the byteArray to set
*/
public void setByteArray(byte[] byteArray);
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.perspective;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.Action;
import fr.soleil.docking.exception.DockingException;
/**
* Factory that manages Perspectives
*
* @author Hardion
* @author GIRARDOT
*/
public interface IPerspectiveFactory {
public static final String SELECTED_PERSPECTIVE = "selectedPerspective";
public static final String PERSPECTIVES = "perspectives";
/**
* Returns the Perspective associated with this id.
*
* @param id The id of the desired Perspective.
* @return The Perspective associated with this id.
*/
public IPerspective getPerspective(Object id);
/**
* Returns the selected IPerspective
*
* @return The selected IPerspective
*/
public IPerspective getSelectedPerspective();
/**
* Sets the selected IPerspective
*
* @param selectedPerspective The IPerspective to set as selected
*/
public void setSelectedPerspective(IPerspective selectedPerspective);
public void setSelectedPerspective(String perspective);
public void loadPreferences(Preferences prefs);
public void savePreferences(Preferences prefs);
public List<Action> getActionList();
public void saveSelected(File file) throws DockingException;
public void savePerspective(File file, IPerspective perspective) throws DockingException;
public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener);
public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener);
public IPerspective getDefault();
public IPerspective createPerspective(final String name);
public IPerspective removePerspective(IPerspective perspective);
public IPerspective[] getPerspectives();
public IPerspective loadPerspectiveFromFile(File perspectiveFile, String perpectiveName) throws DockingException;
public void loadFileInPerspective(File perspectiveFile, IPerspective perspective) throws DockingException;
public IPerspective loadPerspectiveFromResource(String resource, String perpectiveName) throws DockingException;
public void loadResourceInPerspective(String resource, IPerspective perspective) throws DockingException;
}
/**
*
*/
package fr.soleil.docking.perspective;
public class Perspective implements IPerspective {
private final String name;
protected byte[] byteArray;
public Perspective(String name) {
this.name = name;
byteArray = new byte[0];
}
@Override
public byte[] getByteArray() {
return byteArray;
}
@Override
public String getName() {
return name;
}
@Override
public void setByteArray(byte[] byteArray) {
this.byteArray = byteArray;
}
@Override
public String toString() {
return this.name;
}
}
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.perspective;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.Action;
import fr.soleil.docking.action.NewPerspectiveAction;
import fr.soleil.docking.action.RemovePerspectiveAction;
import fr.soleil.docking.action.SelectPerspectiveAction;
import fr.soleil.docking.exception.DockingException;
import fr.soleil.lib.project.ObjectUtils;
public class PerspectiveFactory implements IPerspectiveFactory {
private static final String DEFAULT_PERSPECTIVE_NAME = "Default";
protected PropertyChangeSupport support;
private final List<IPerspective> perspectives;
private IPerspective selectedPerspective;
private final String defaultPerspectiveName;
public PerspectiveFactory() {
this(new Perspective(DEFAULT_PERSPECTIVE_NAME));
}
public PerspectiveFactory(IPerspective defaultt) {
perspectives = new ArrayList<IPerspective>(1);
defaultPerspectiveName = defaultt.getName();
perspectives.add(defaultt);
selectedPerspective = defaultt;
support = new PropertyChangeSupport(this);
}
@Override
public IPerspective createPerspective(final String name) {
IPerspective result = this.getPerspective(name);
if (result == null) {
result = new Perspective(name);
this.add(result);
}
return result;
}
/**
* @param e
* @return
* @see java.util.ArrayList#add(java.lang.Object)
*/
public boolean add(IPerspective e) {
boolean result = perspectives.add(e);
if (result) {
support.fireIndexedPropertyChange(PERSPECTIVES, perspectives.indexOf(e), null, result);
}
return result;
}
@Override
public IPerspective getPerspective(Object id) {
IPerspective result = null;
if (id instanceof String) {
String name = (String) id;
for (IPerspective perspective : perspectives) {
if (name.equalsIgnoreCase(perspective.getName())) {
result = perspective;
break;
}
}
}
return result;
}
@Override
public IPerspective getSelectedPerspective() {
return selectedPerspective;
}
@Override
public void setSelectedPerspective(IPerspective selectedPerspective) {
if ((selectedPerspective != null) && (!selectedPerspective.equals(this.selectedPerspective))
&& perspectives.contains(selectedPerspective)) {
IPerspective old = this.selectedPerspective;
this.selectedPerspective = selectedPerspective;
support.firePropertyChange(SELECTED_PERSPECTIVE, old, this.selectedPerspective);
}
}
@Override
public void setSelectedPerspective(String perspective) {
setSelectedPerspective(getPerspective(perspective));
}
@Override
public void loadPreferences(Preferences prefs) {
Preferences factoryPrefs = prefs.node("PerspectiveFactory");
Preferences perspectivesPrefs = factoryPrefs.node("Perspectives");
String[] keys = new String[0];
try {
keys = perspectivesPrefs.keys();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
byte[] ko = new byte[0];
byte[] layout = null;
IPerspective perspective = null;
for (int i = 0; i < keys.length; i++) {
perspective = this.getPerspective(keys[i]);
if (perspective == null) {
perspective = this.createPerspective(keys[i]);
}
layout = perspectivesPrefs.getByteArray(keys[i], ko);
if (layout != ko) {
perspective.setByteArray(layout);
}
}
// get the value of
String selected = factoryPrefs.get("selected", selectedPerspective.getName());
this.setSelectedPerspective(this.getPerspective(selected));
}
@Override
public void savePreferences(Preferences prefs) {
Preferences factoryPrefs = prefs.node("PerspectiveFactory");
Preferences perspectivesPrefs = factoryPrefs.node("Perspectives");
try {
factoryPrefs.clear();
perspectivesPrefs.clear();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
factoryPrefs.put("selected", selectedPerspective.getName());
for (IPerspective perspective : perspectives) {
perspectivesPrefs.putByteArray(perspective.getName(), perspective.getByteArray());
}
}
@Override
public List<Action> getActionList() {
List<Action> result = new ArrayList<Action>(1);
for (IPerspective perspective : perspectives) {
result.add(new SelectPerspectiveAction(this, perspective));
}
result.add(new NewPerspectiveAction(this));
result.add(new RemovePerspectiveAction(this));
return result;
}
@Override
public void saveSelected(File file) throws DockingException {
savePerspective(file, selectedPerspective);
}
@Override
public void savePerspective(File file, IPerspective perspective) throws DockingException {
if ((file != null) && (perspective != null)) {
DockingException dockingException = null;
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
try {
out.write(perspective.getByteArray());
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error during file writing", e);
}
} finally {
try {
out.close();
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error while releasing file access", e);
}
}
}
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error during file access", e);
}
}
if (dockingException != null) {
throw dockingException;
}
}
}
/**
* @param listener
* @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
*/
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
/**
* @param listener
* @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
*/
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}
@Override
public IPerspective getDefault() {
return this.getPerspective(defaultPerspectiveName);
}
@Override
public IPerspective[] getPerspectives() {
return perspectives.toArray(new IPerspective[perspectives.size()]);
}
@Override
public IPerspective removePerspective(final IPerspective p) {
int index;
if (p == null) {
index = -1;
} else {
index = perspectives.indexOf(p);
}
if ((index > -1) && (!ObjectUtils.sameObject(p.getName(), defaultPerspectiveName))) {
// Exist and it's not the default perspective
perspectives.remove(index);
support.fireIndexedPropertyChange(PERSPECTIVES, index, p, null);
}
return p;
}
public IPerspective removePerspective(final String name) {
return this.removePerspective(this.getPerspective(name));
}
/**
* Reads a <code>byte</code> array from an {@link InputStream} and returns it
*
* @param in The {@link InputStream}
* @return A <code>byte[]</code>
* @throws IOException If a problem occurred while trying to read the {@link InputStream}
*/
protected static byte[] readByteArray(InputStream in) throws IOException {
byte[] result = null;
if (in != null) {
result = new byte[in.available()];
int length = in.read(result);
if (length != result.length) {
result = null;
}
}
return result;
}
@Override
public IPerspective loadPerspectiveFromFile(File perspectiveFile, String perpectiveName) throws DockingException {
Perspective perspective;
if (perpectiveName == null) {
perspective = null;
} else {
perspective = new Perspective(perpectiveName);
}
loadFileInPerspective(perspectiveFile, perspective);
return perspective;
}
@Override
public void loadFileInPerspective(File perspectiveFile, IPerspective perspective) throws DockingException {
if ((perspectiveFile != null) && perspectiveFile.isFile() && (perspective != null)) {
try {
perspective.setByteArray(readByteArray(new FileInputStream(perspectiveFile)));
} catch (Exception e) {
throw new DockingException("Error during file reading", e);
}
}
}
@Override
public IPerspective loadPerspectiveFromResource(String resource, String perpectiveName) throws DockingException {
Perspective perspective;
if (perpectiveName == null) {
perspective = null;
} else {
perspective = new Perspective(perpectiveName);
}
loadResourceInPerspective(resource, perspective);
return perspective;
}
@Override
public void loadResourceInPerspective(String resource, IPerspective perspective) throws DockingException {
if ((resource != null) && (!resource.trim().isEmpty()) && (perspective != null)) {
try {
perspective.setByteArray(readByteArray(getClass().getResourceAsStream(resource)));
} catch (Exception e) {
throw new DockingException("Error during resource reading", e);
}
}
}
}
/*******************************************************************************
* Copyright (c) 2008-2019 Synchrotron SOLEIL
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
******************************************************************************/
package fr.soleil.docking.perspective;
import java.io.IOException;
import java.io.InputStream;
@Deprecated
public class ResourcePerspective extends Perspective {
public ResourcePerspective(String name, String resource) throws IOException {
super(name);
InputStream in = ResourcePerspective.this.getClass().getResourceAsStream(resource);
if (in != null) {
byteArray = PerspectiveFactory.readByteArray(in);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment