Commit 8546282b authored by Vincent van Beveren's avatar Vincent van Beveren

added overlay manager

parent f1fe12db
......@@ -2,6 +2,7 @@ package nl.nikhef.sfp.ddmi;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -20,23 +21,8 @@ public class DDMI extends DDMIGroup {
private Map<String, DataSource> _dataSources = new HashMap<String, DataSource>();
private Map<String, DDMIElement> _elementIdCache = new HashMap<String, DDMIElement>();
private static final DDMI _SINGLETON;
private static DDMI _SINGLETON;
static {
DDMILoader loader = new DDMILoader();
LOG.info("Loading internal DDMI description from " + DDMI_XML );
// , DDMI.class.getResource(MAXIM_XML)
try {
loader.load(DDMI.class.getResource(DDMI_XML));
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
_SINGLETON = loader.getDDMI();
}
public DDMI() {
}
......@@ -57,7 +43,21 @@ public class DDMI extends DDMIGroup {
}
public static final void initSingleton(URL[] overlays) {
DDMILoader loader = new DDMILoader();
LOG.info("Loading internal DDMI description from " + DDMI_XML );
// , DDMI.class.getResource(MAXIM_XML)
try {
loader.load(DDMI.class.getResource(DDMI_XML), overlays);
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
_SINGLETON = loader.getDDMI();
}
public static final DDMI getDefault() {
return _SINGLETON;
......
package nl.nikhef.safaripark;
import java.util.prefs.Preferences;
public class Config {
public static final Preferences PREFS = Preferences.userNodeForPackage(Config.class);
}
package nl.nikhef.safaripark;
import java.awt.Component;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.prefs.Preferences;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import nl.nikhef.safaripark.extra.JCheckBoxList;
import nl.nikhef.safaripark.res.Resources;
public class OverlayManager {
public static final OverlayManager SINGLETON = new OverlayManager();
private Map<String, Boolean> _overlays = new HashMap<String, Boolean>();
private File _overlayDir;
private Preferences _prefs = Config.PREFS.node("overlays");
public void scanDirectory(File dir) {
_overlayDir = dir;
String[] files = _overlayDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
});
for (String file : files)
{
_overlays.put(file, _prefs.getBoolean(file, false));
}
}
public URL[] getOverlays()
{
List<URL> lst = new ArrayList<URL>();
for (Map.Entry<String, Boolean> entries : _overlays.entrySet())
{
if (!entries.getValue()) continue;
try {
lst.add(new File(_overlayDir, entries.getKey()).toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return lst.toArray(new URL[lst.size()]);
}
public void showDialog(Component parent)
{
DefaultListModel<JCheckBox> cblm = new DefaultListModel<JCheckBox>();
for (Map.Entry<String, Boolean> entries : _overlays.entrySet())
{
cblm.addElement(new JCheckBox(entries.getKey(), entries.getValue()));
}
JCheckBoxList cbl = new JCheckBoxList(cblm);
JScrollPane jsp = new JScrollPane(cbl);
Component[] comp = new Component[] {
jsp,
new JLabel("Changes to overlays require a restart to be applied", Resources.getIcon("emblem-notice"),
SwingConstants.CENTER)
};
if (JOptionPane.showOptionDialog(parent, comp, "Select overlays to load",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null) != JOptionPane.OK_OPTION) {
return;
}
for (int i = 0; i < cblm.getSize(); i++)
{
JCheckBox cb = cblm.getElementAt(i);
_overlays.put(cb.getText(), cb.isSelected());
_prefs.putBoolean(cb.getText(), cb.isSelected());
}
}
}
......@@ -8,6 +8,7 @@ import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.logging.Level;
......@@ -33,8 +34,10 @@ import nl.nikhef.safaripark.devmgr.BaySelectionListener;
import nl.nikhef.safaripark.devmgr.DeviceManager;
import nl.nikhef.safaripark.editpane.EditPane;
import nl.nikhef.safaripark.monitor.Monitor;
import nl.nikhef.safaripark.res.Resources;
import nl.nikhef.sfp.SFPDevice;
import nl.nikhef.sfp.SFPManager;
import nl.nikhef.sfp.ddmi.DDMI;
@SuppressWarnings("serial")
public class SaFariPark extends JFrame implements BaySelectionListener, WindowListener, DeferredListener {
......@@ -55,10 +58,24 @@ public class SaFariPark extends JFrame implements BaySelectionListener, WindowLi
private Timer _timer;
private int _scanForNewDevices = SCAN_DEVICE_DELAY;
private Action _selectOverlays = new ExtendedAbstractAction("Select overlays", Resources.getIcon("books"), "Select which SFP+ overlays to load") {
@Override
public void actionPerformed(ActionEvent e)
{
OverlayManager.SINGLETON.showDialog(SaFariPark.this);
}
};
public SaFariPark()
{
SINGLETON = this;
OverlayManager.SINGLETON.scanDirectory(new File("overlays"));
DDMI.initSingleton(OverlayManager.SINGLETON.getOverlays());
setLayout(new BorderLayout());
setTitle("SaFariPark");
setSize(640, 480);
......@@ -113,6 +130,7 @@ public class SaFariPark extends JFrame implements BaySelectionListener, WindowLi
private void makeToolbar()
{
JToolBar tb = new JToolBar();
tb.add(_selectOverlays);
add(tb, BorderLayout.NORTH);
}
......
package nl.nikhef.safaripark.extra;
import java.awt.Component;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class JCheckBoxList extends JList<JCheckBox> {
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public JCheckBoxList() {
setCellRenderer(new CellRenderer());
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (index != -1) {
JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index);
checkbox.setSelected(!checkbox.isSelected());
repaint();
}
}
});
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
public JCheckBoxList(ListModel<JCheckBox> model){
this();
setModel(model);
}
protected class CellRenderer implements ListCellRenderer<JCheckBox> {
public Component getListCellRendererComponent(
JList<? extends JCheckBox> list, JCheckBox value, int index,
boolean isSelected, boolean cellHasFocus) {
JCheckBox checkbox = value;
//Drawing checkbox, change the appearance here
checkbox.setBackground(isSelected ? getSelectionBackground()
: getBackground());
checkbox.setForeground(isSelected ? getSelectionForeground()
: getForeground());
checkbox.setEnabled(isEnabled());
checkbox.setFont(getFont());
checkbox.setFocusPainted(false);
checkbox.setBorderPainted(true);
checkbox.setBorder(isSelected ? UIManager
.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
return checkbox;
}
}
}
\ No newline at end of file
......@@ -6,7 +6,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
......@@ -21,8 +20,6 @@ import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
......
......@@ -34,6 +34,7 @@ import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import nl.nikhef.safaripark.Config;
import nl.nikhef.safaripark.ExtendedAbstractAction;
import nl.nikhef.safaripark.res.Resources;
import nl.nikhef.sfp.ddmi.DDMI;
......@@ -256,7 +257,7 @@ public class ValueSelectionPane extends JPanel implements ItemListener {
tree.setModel(new DefaultTreeModel(_root));
Preferences p = Preferences.userNodeForPackage(ValueSelectionPane.class);
Preferences p = Config.PREFS.node("vsp");
_presets = p.node(name);
Vector<String> options = new Vector<String>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment