viewersList.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) value;
JLabel label = (JLabel) super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
label.setText(viewer.getTitle());
return label;
}
});
viewersList.setDropMode(DropMode.INSERT);
viewersList.enableInputMethods(true);
viewersList.setDragEnabled(true);
viewersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
viewersList.getSelectionModel().addListSelectionListener(this);
viewersList.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport info) {
// we only import NodeViewers
if (!info
.isDataFlavorSupported(ZooInspectorNodeViewer.nodeViewerDataFlavor)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) info
.getDropLocation();
if (dl.getIndex() == -1) {
return false;
}
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport info) {
JList.DropLocation dl = (JList.DropLocation) info
.getDropLocation();
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
int index = dl.getIndex();
boolean insert = dl.isInsert();
// Get the string that is being dropped.
Transferable t = info.getTransferable();
String data;
try {
data = (String) t
.getTransferData(ZooInspectorNodeViewer.nodeViewerDataFlavor);
} catch (Exception e) {
return false;
}
try {
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class
.forName(data).newInstance();
if (listModel.contains(viewer)) {
listModel.removeElement(viewer);
}
if (insert) {
listModel.add(index, viewer);
} else {
listModel.set(index, viewer);
}
return true;
} catch (Exception e) {
LoggerFactory.getLogger().error(
"Error instantiating class: " + data, e);
return false;
}
}
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
@Override
protected Transferable createTransferable(JComponent c) {
JList list = (JList) c;
ZooInspectorNodeViewer value = (ZooInspectorNodeViewer) list
.getSelectedValue();
return value;
}
});
JScrollPane scroller = new JScrollPane(viewersList);
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.gridwidth = 3;
c1.gridheight = 3;
c1.weightx = 0;
c1.weighty = 1;
c1.anchor = GridBagConstraints.CENTER;
c1.fill = GridBagConstraints.BOTH;
c1.insets = new Insets(5, 5, 5, 5);
c1.ipadx = 0;
c1.ipady = 0;
panel.add(scroller, c1);
upButton = new JButton(ZooInspectorIconResources.getUpIcon());
downButton = new JButton(ZooInspectorIconResources.getDownIcon());
removeButton = new JButton(ZooInspectorIconResources
.getDeleteNodeIcon());
addButton = new JButton(ZooInspectorIconResources.getAddNodeIcon());
upButton.setEnabled(false);
downButton.setEnabled(false);
removeButton.setEnabled(false);
addButton.setEnabled(true);
upButton.setToolTipText("Move currently selected node viewer up");
downButton.setToolTipText("Move currently selected node viewer down");
removeButton.setToolTipText("Remove currently selected node viewer");
addButton.setToolTipText("Add node viewer");
final JTextField newViewerTextField = new JTextField();
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 3;
c2.gridy = 0;
c2.gridwidth = 1;
c2.gridheight = 1;
c2.weightx = 0;
c2.weighty = 0;
c2.anchor = GridBagConstraints.NORTH;
c2.fill = GridBagConstraints.HORIZONTAL;
c2.insets = new Insets(5, 5, 5, 5);
c2.ipadx = 0;
c2.ipady = 0;
panel.add(upButton, c2);
GridBagConstraints c3 = new GridBagConstraints();
c3.gridx = 3;
c3.gridy = 2;
c3.gridwidth = 1;
c3.gridheight = 1;
c3.weightx = 0;
c3.weighty = 0;
c3.anchor = GridBagConstraints.NORTH;
c3.fill = GridBagConstraints.HORIZONTAL;
c3.insets = new Insets(5, 5, 5, 5);
c3.ipadx = 0;
c3.ipady = 0;
panel.add(downButton, c3);
GridBagConstraints c4 = new GridBagConstraints();
c4.gridx = 3;
c4.gridy = 1;
c4.gridwidth = 1;
c4.gridheight = 1;
c4.weightx = 0;
c4.weighty = 0;
c4.anchor = GridBagConstraints.NORTH;
c4.fill = GridBagConstraints.HORIZONTAL;
c4.insets = new Insets(5, 5, 5, 5);
c4.ipadx = 0;
c4.ipady = 0;
panel.add(removeButton, c4);
GridBagConstraints c5 = new GridBagConstraints();
c5.gridx = 0;
c5.gridy = 3;
c5.gridwidth = 3;
c5.gridheight = 1;
c5.weightx = 0;
c5.weighty = 0;
c5.anchor = GridBagConstraints.CENTER;
c5.fill = GridBagConstraints.BOTH;
c5.insets = new Insets(5, 5, 5, 5);
c5.ipadx = 0;
c5.ipady = 0;
panel.add(newViewerTextField, c5);
GridBagConstraints c6 = new GridBagConstraints();
c6.gridx = 3;
c6.gridy = 3;
c6.gridwidth = 1;
c6.gridheight = 1;
c6.weightx = 0;
c6.weighty = 0;
c6.anchor = GridBagConstraints.CENTER;
c6.fill = GridBagConstraints.BOTH;
c6.insets = new Insets(5, 5, 5, 5);
c6.ipadx = 0;
c6.ipady = 0;
panel.add(addButton, c6);
upButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList
.getSelectedValue();
int index = viewersList.getSelectedIndex();
if (listModel.contains(viewer)) {
listModel.removeElementAt(index);
listModel.insertElementAt(viewer, index - 1);
viewersList.setSelectedValue(viewer, true);
}
}
});
downButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList
.getSelectedValue();
int index = viewersList.getSelectedIndex();
if (listModel.contains(viewer)) {
listModel.removeElementAt(index);
listModel.insertElementAt(viewer, index + 1);
viewersList.setSelectedValue(viewer, true);
}
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList
.getSelectedValue();
int index = viewersList.getSelectedIndex();
if (listModel.contains(viewer)) {
listModel.removeElement(viewer);
viewersList
.setSelectedIndex(index == listModel.size() ? index - 1
: index);
}
}
});
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String className = newViewerTextField.getText();
if (className == null || className.length() == 0) {
JOptionPane
.showMessageDialog(
ZooInspectorNodeViewersDialog.this,
"Please enter the full class name for a Node Viewer and click the add button",
"Input Error", JOptionPane.ERROR_MESSAGE);
} else {
try {
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class
.forName(className).newInstance();
if (listModel.contains(viewer)) {
JOptionPane
.showMessageDialog(
ZooInspectorNodeViewersDialog.this,
"Node viewer already exists. Each node viewer can only be added once.",
"Input Error",
JOptionPane.ERROR_MESSAGE);
} else {
listModel.addElement(viewer);
}
} catch (Exception ex) {
LoggerFactory
.getLogger()
.error(
"An error occurred while instaniating the node viewer. ",
ex);
JOptionPane.showMessageDialog(
ZooInspectorNodeViewersDialog.this,
"An error occurred while instaniating the node viewer: "
+ ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
});
saveFileButton = new JButton("Save");
loadFileButton = new JButton("Load");
setDefaultsButton = new JButton("Set As Defaults");
saveFileButton
.setToolTipText("Save current node viewer configuration to file");
loadFileButton
.setToolTipText("Load node viewer configuration frm file");
setDefaultsButton
.setToolTipText("Set current configuration asd defaults");
GridBagConstraints c7 = new GridBagConstraints();
c7.gridx = 0;
c7.gridy = 4;
c7.gridwidth = 1;
c7.gridheight = 1;
c7.weightx = 1;
c7.weighty = 0;
c7.anchor = GridBagConstraints.WEST;
c7.fill = GridBagConstraints.VERTICAL;
c7.insets = new Insets(5, 5, 5, 5);
c7.ipadx = 0;
c7.ipady = 0;
panel.add(saveFileButton, c7);
GridBagConstraints c8 = new GridBagConstraints();
c8.gridx = 1;
c8.gridy = 4;
c8.gridwidth = 1;
c8.gridheight = 1;
c8.weightx = 0;
c8.weighty = 0;
c8.anchor = GridBagConstraints.WEST;
c8.fill = GridBagConstraints.VERTICAL;
c8.insets = new Insets(5, 5, 5, 5);
c8.ipadx = 0;
c8.ipady = 0;
panel.add(loadFileButton, c8);
GridBagConstraints c9 = new GridBagConstraints();
c9.gridx = 2;
c9.gridy = 4;
c9.gridwidth = 1;
c9.gridheight = 1;
c9.weightx = 0;
c9.weighty = 0;
c9.anchor = GridBagConstraints.WEST;
c9.fill = GridBagConstraints.VERTICAL;
c9.insets = new Insets(5, 5, 5, 5);
c9.ipadx = 0;
c9.ipady = 0;
panel.add(setDefaultsButton, c9);
saveFileButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = fileChooser
.showSaveDialog(ZooInspectorNodeViewersDialog.this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
int answer = JOptionPane.YES_OPTION;
if (selectedFile.exists()) {
answer = JOptionPane
.showConfirmDialog(
ZooInspectorNodeViewersDialog.this,
"The specified file already exists. do you want to overwrite it?",
"Confirm Overwrite",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
}
if (answer == JOptionPane.YES_OPTION) {
DefaultListModel listModel = (DefaultListModel) viewersList
.getModel();
List<String> nodeViewersClassNames = new ArrayList<String>();
Object[] modelContents = listModel.toArray();
for (Object o : modelContents) {
nodeViewersClassNames
.add(((ZooInspectorNodeViewer) o)
.getClass().getCanonicalName());
}
try {
manager.saveNodeViewersFile(selectedFile,
nodeViewersClassNames);
} catch (IOException ex) {
LoggerFactory
.getLogger()
.error(
"Error saving node veiwer configuration from file.",
ex);
JOptionPane.showMessageDialog(
ZooInspectorNodeViewersDialog.this,
"Error saving node veiwer configuration from file: "
+ ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
});
loadFileButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = fileChooser
.showOpenDialog(ZooInspectorNodeViewersDialog.this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
List<String> nodeViewersClassNames = manager
.loadNodeViewersFile(fileChooser
.getSelectedFile());
List<ZooInspectorNodeViewer> nodeViewers = new ArrayList<ZooInspectorNodeViewer>();
for (String nodeViewersClassName : nodeViewersClassNames) {
ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class
.forName(nodeViewersClassName)
.newInstance();
nodeViewers.add(viewer);
}
DefaultListModel model = new DefaultListModel();