import chemaxon.marvin.beans.MViewPane;
import chemaxon.marvin.io.MDocSource;
import chemaxon.marvin.view.swing.TableSupport;
import chemaxon.marvin.view.swing.TableOptions;
import chemaxon.formats.MolImporter;
import chemaxon.formats.MolFormatException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
@author
@version
@since
public class ViewTable {
public static void main(String[] args) {
final String fileName = args[0];
final MDocSource docSource = createDocSource(fileName);
if(docSource != null) {
EventQueue.invokeLater( new Runnable() {
public void run() {
createAndShowGUI(docSource, fileName);
}
});
}
}
@param
@return
static MDocSource createDocSource(String fname) {
try {
return new MolImporter(fname);
} catch(FileNotFoundException ex) {
System.err.println("File " + fname+" not found");
} catch(MolFormatException ex) {
System.err.println("File " + fname
+ " is corrupted or not a structure file.\n"
+ ex.getMessage());
} catch (IOException e) {
System.err.println("Error reading file " + fname+"\n");
}
return null;
}
@param
@param
static void createAndShowGUI(MDocSource docSource, String inputName) {
MViewPane viewPane = new MViewPane();
viewPane.setBorderWidth(1);
viewPane.setBackground(Color.LIGHT_GRAY);
viewPane.setMolbg(Color.WHITE);
JFrame win = new JFrame();
JMenuBar menubar = new JMenuBar();
win.setJMenuBar(menubar);
win.setTitle("MarvinView Table Layout Example");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setLayout(new GridLayout(1, 1));
win.getContentPane().add(viewPane);
TableSupport tableSupport = viewPane.getTableSupport();
JMenu menu;
tableSupport.makeTableMenu(menu = new JMenu("Table"));
menu.setMnemonic('t');
menubar.add(menu);
makeHighlightMenu(menu = new JMenu("Highlight"), viewPane);
menu.setMnemonic('h');
menubar.add(menu);
TableOptions tblopts = tableSupport.getTableOptions();
tblopts.setMaxRows(5);
tblopts.setMaxCols(5);
win.pack();
win.setLocationRelativeTo(null);
win.setVisible(true);
tableSupport.start(docSource, inputName);
}
static void makeHighlightMenu(JMenu menu, final MViewPane viewPane) {
final ArrayList<Integer> highlightedCells = new ArrayList<Integer>();
JMenuItem highlightMenuItem, clearMenuItem;
highlightMenuItem = new JMenuItem("Highlight selected cell");
menu.add(highlightMenuItem);
highlightMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
int selected = viewPane.getSelectedIndex();
if(!highlightedCells.contains(selected)) {
highlightedCells.add(selected);
viewPane.setRecordIDBackground(selected,
SystemColor.textHighlight);
viewPane.setRecordIDForeground(selected,
SystemColor.textHighlightText);
}
}
});
clearMenuItem = new JMenuItem("Remove highlight from cells");
menu.add(clearMenuItem);
clearMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
for(int i : highlightedCells) {
viewPane.setRecordIDBackground(i, null);
viewPane.setRecordIDForeground(i, null);
}
highlightedCells.clear();
}
});
}
}