From: Egon W. <eg...@us...> - 2005-08-18 22:17:13
|
Update of /cvsroot/cdk/cdk-plugins/aatemplates/src/org/openscience/cdkplugin/aatemplate In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15782/src/org/openscience/cdkplugin/aatemplate Modified Files: AminoAcidTemplatesPlugin.java Log Message: First compilable version. Index: AminoAcidTemplatesPlugin.java =================================================================== RCS file: /cvsroot/cdk/cdk-plugins/aatemplates/src/org/openscience/cdkplugin/aatemplate/AminoAcidTemplatesPlugin.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AminoAcidTemplatesPlugin.java 18 Aug 2005 21:36:58 -0000 1.1 +++ AminoAcidTemplatesPlugin.java 18 Aug 2005 22:16:37 -0000 1.2 @@ -19,94 +19,46 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -package org.openscience.cdkplugin.dirbrowser; +package org.openscience.cdkplugin.aatemplate; -import org.openscience.cdk.ChemModel; +import org.openscience.cdk.AminoAcid; import org.openscience.cdk.applications.plugin.*; -import org.openscience.cdk.applications.swing.SortedTableModel; +import org.openscience.cdk.applications.swing.MoleculeListPanel; +import org.openscience.cdk.applications.swing.MoleculeViewer2D; import org.openscience.cdk.event.ChemObjectChangeEvent; -import org.openscience.cdk.io.ReaderFactory; -import org.openscience.cdk.io.formats.ChemFormat; +import org.openscience.cdk.templates.AminoAcids; import org.openscience.cdk.tools.LoggingTool; -// import java.util.Properties; -import java.io.File; -import java.io.BufferedInputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.IOException; -import java.util.Date; -import java.util.Vector; -import java.util.zip.GZIPInputStream; import javax.swing.JMenu; import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTable; import java.awt.BorderLayout; -import java.awt.FlowLayout; -import java.awt.event.*; -import javax.swing.ListSelectionModel; -import javax.swing.JFileChooser; -import javax.swing.JButton; -import javax.swing.JLabel; -import javax.swing.JTextField; -import javax.swing.event.*; -import javax.swing.table.AbstractTableModel; - /** - * Plugin that lists the directory content of a given directory. - * The directory can be selected using a button which triggers a - * file open chooser. Each file is in the directory is typed - * in a thread and if it is a chemical format, the format name is - * listed. + * Plugin that offers amino acid templates which can be pasted into + * a 2D editor. * * @author Egon Willighagen <eg...@sc...> */ -public class DirBrowserPlugin implements CDKPluginInterface { +public class AminoAcidTemplatesPlugin implements CDKPluginInterface { private final String implementedCDKPluginAPIVersion = "1.11"; - private final String pluginVersion = "0.15"; + private final String pluginVersion = "0.1"; - private CDKEditBus editBus = null; - private ContentModel channelContent = null; - private SortedTableModel sortedContent = null; private JPanel pluginPanel = null; - private JTextField textField; - - private ReaderFactory readerFactory = null; - private org.openscience.cdk.tools.LoggingTool logger; - private File currentDirectory; + private LoggingTool logger; - private Thread fileGuessingThread = null; - private boolean resetFileGuessing; - - public DirBrowserPlugin() { + public AminoAcidTemplatesPlugin() { logger = new LoggingTool(this); - currentDirectory = new File(System.getProperty("user.dir")); } - public void start() { - readerFactory = new ReaderFactory(); - parseDirectoryIntoTable(); - resetFileGuessing = true; - if (fileGuessingThread == null) { - fileGuessingThread = new FileFormatGuessingThread(); - fileGuessingThread.start(); - } - } + public void start() {} - public void stop() { - } + public void stop() {} - public void setEditBus(CDKEditBus editBus) { - this.editBus = editBus; - } + public void setEditBus(CDKEditBus editBus) {} public String getName() { - return "DirBrowser"; + return "AminoAcidTemplates"; } public String getAPIVersion() { @@ -139,56 +91,16 @@ private JPanel createPanel() { JPanel browserPanel = new JPanel(new BorderLayout()); - // directory panel - JPanel dirPanel = new JPanel(); - dirPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); - JButton dirSelect = new JButton("Select Directory"); - dirSelect.addActionListener(new ActionListener() { - - public void actionPerformed(ActionEvent e) { - // select new directory - JFileChooser chooser = new JFileChooser(currentDirectory); - chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - int returnVal = chooser.showOpenDialog(null); - - if (returnVal == JFileChooser.APPROVE_OPTION) { - if (chooser.getSelectedFile().isDirectory()) { - currentDirectory = chooser.getSelectedFile(); - parseDirectoryIntoTable(); - textField.setText(currentDirectory.toString()); - resetFileGuessing = true; - } // else no directory selected - } // else disregard selection - } - }); - dirPanel.add(new JLabel("Current directory:")); - textField = new JTextField(this.currentDirectory.toString()); - textField.setEditable(false); - textField.setColumns(40); - dirPanel.add(textField); - dirPanel.add(dirSelect); - dirPanel.validate(); + MoleculeListPanel panel = new MoleculeListPanel(); + AminoAcid[] acids = AminoAcids.createAAs(); + for (int i=0; i<acids.length; i++) { + panel.addStructure( + new MoleculeViewer2D(acids[i]), acids[i].getID() + ); + } + browserPanel.add(panel, BorderLayout.CENTER); - // A table showing the entries in one channel - channelContent = new ContentModel(); - sortedContent = new SortedTableModel(channelContent); - JTable channelTable = new JTable(sortedContent); - sortedContent.addMouseListenerToHeaderInTable(channelTable); - channelTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - ListSelectionModel rowSM = channelTable.getSelectionModel(); - rowSM.addListSelectionListener( - new ItemsTableListener(sortedContent, channelContent) - ); - channelTable.validate(); - - // wrap in scroller - JScrollPane contentPane = new JScrollPane(channelTable); - contentPane.validate(); - - browserPanel.add(dirPanel, BorderLayout.NORTH); - browserPanel.add(contentPane, BorderLayout.CENTER); browserPanel.validate(); - return browserPanel; }; @@ -200,274 +112,6 @@ return null; }; - class ContentModel extends AbstractTableModel { - - private Vector models; - - final String[] columnNames = { - "filename", "date", "format" - }; - - public ContentModel() { - models = new Vector(); - } - - public void setValueAt(Object value, int row, int column) { - // read only table - return; - } - - public void setValueAt(ChemModel model, int row) { - if (row > getRowCount()) { - return; // skip everything outside current table - } - models.setElementAt(model, row); - fireTableCellUpdated(row, 1); - } - - public int getColumnCount() { - return columnNames.length; - } - - public int getRowCount() { - return models.size(); - } - - public String getColumnName(int col) { - return columnNames[col]; - } - - public Class getColumnClass(int col) { - Object o = getValueAt(0,col); - if (o == null) { - return (new String()).getClass(); - } else { - return o.getClass(); - } - } - - public Object getValueAt(int row, int column) { - if (row > getRowCount()-1 || column > getColumnCount()-1) { - return "Error"; // skip everything outside current table - } - // "title", "date", "time", "chemFormula", "description", "link" - ChemModel model = (ChemModel)models.elementAt(row); - if (model == null) { - return ""; - } - // return the table content - File fileObject = (File)model.getProperty("org.openscience.cdk.io.java.io.File"); - if (fileObject == null) { - return ""; - } else { - if (column == 0) { - return fileObject.getName(); - } else if (column == 1) { - return new Date(fileObject.lastModified()).toString(); - } else if (column == 2) { - // detect the format - String format = (String)model.getProperty("org.openscience.cdk.io.FileFormat"); - if (format == null) { - format = ""; // let the thread determine it first - } - return format; - } else if (column == 3) { - return ""; - } - } - return ""; - } - - public ChemModel getValueAt(int row) { - return (ChemModel)models.elementAt(row); - } - - public boolean isCellEditable(int row, int col) { - return false; - } - - public void cleanTable() { - models.clear(); - fireTableDataChanged(); - } - - private void insertBlankRow(int row) { - models.addElement(null); - fireTableRowsInserted(row+1, row+1); - } - - } - - /* private Properties readProperties(String directory) { - Properties props = null; - File uhome = new File(System.getProperty("user.home")); - File propsFile = new File(directory + "/dirbrowser.props"); - logger.debug("User plugin dir: " + propsFile); - logger.debug(" exists: " + propsFile.exists()); - if (propsFile.exists()) { - try { - FileInputStream fis = new FileInputStream(propsFile); - props = new Properties(); - props.load(fis); - fis.close(); - } catch (Exception exception) { - logger.error("Error while reading dirbrowser props: " + exception.toString()); - // logger.debug(exception); - } - } - return props; - } */ - - private void parseDirectoryIntoTable() { - File dir = currentDirectory; - if (!dir.exists()) { - logger.warn("File/Directory does not exist"); - } else if (!dir.isDirectory()) { - logger.warn("File is not a directory"); - } else { - // clear the current contents of the table - channelContent.cleanTable(); - // fill table with files in the current directory - File[] files = dir.listFiles(); - for (int i=0; i<files.length; i++) { - if (!files[i].isDirectory()) { - int lastLine = channelContent.getRowCount(); - channelContent.insertBlankRow(lastLine); - ChemModel model = new ChemModel(); - model.setProperty("org.openscience.cdk.io.java.io.File", files[i]); - channelContent.setValueAt(model, lastLine); - } - } - } - } - - class ItemsTableListener implements ListSelectionListener { - - private SortedTableModel sortedModelContent = null; - private ContentModel modelContent = null; - - public ItemsTableListener(SortedTableModel sortedModelContent, - ContentModel modelContent) { - this.modelContent = modelContent; - this.sortedModelContent = sortedModelContent; - } - - public void valueChanged(ListSelectionEvent e) { - // Ignore extra messages - if (e.getValueIsAdjusting()) return; - - ListSelectionModel lsm = (ListSelectionModel)e.getSource(); - if (lsm.isSelectionEmpty()) { - // no rows are selected - } else { - int selectedRow = lsm.getMinSelectionIndex(); - - ChemModel model = modelContent.getValueAt(sortedModelContent.getSortedIndex(selectedRow)); - File fileObject = (File)model.getProperty("org.openscience.cdk.io.java.io.File"); - if (fileObject == null) { - logger.warn("Cannot open file if source is not stored"); - return; - } - try { - logger.debug("Passing FileReader for: ", fileObject); - FileInputStream input = new FileInputStream(fileObject); - BufferedInputStream bistream = new BufferedInputStream(input, 8192); - InputStream istreamToRead = bistream; // if gzip test fails, then take default - bistream.mark(5); - int countRead = 0; - try { - byte[] abMagic = new byte[4]; - countRead = bistream.read(abMagic, 0, 4); - bistream.reset(); - if (countRead == 4) { - if (abMagic[0] == (byte)0x1F && abMagic[1] == (byte)0x8B) { - istreamToRead = new GZIPInputStream(bistream); - } - } - } catch (IOException exception) { - logger.error(exception.getMessage()); - logger.debug(exception); - } - editBus.showChemFile(new InputStreamReader(istreamToRead)); - } catch (Exception exception) { - String error = "Error while reading file: " + exception.getMessage(); - logger.error(error); - // logger.debug(exception); - return; - } - logger.warn("Not displaying model with unknown content"); - } - } - } - - /** - * The file guessing is done in a thread of this Class. - * This methods performs the guessing. - */ - class FileFormatGuessingThread extends Thread { - - private int currentModel; - private int formatsToDetermine; - - public FileFormatGuessingThread() { - super("DirBrowser:FileFormatGuessing"); - } - - public void run() { - Thread myThread = Thread.currentThread(); - while (fileGuessingThread == myThread) { - if (resetFileGuessing) { - currentModel = 0; - formatsToDetermine = channelContent.getRowCount(); - resetFileGuessing = false; - } - if (currentModel == formatsToDetermine) { - // nothing to do - try { - Thread.sleep(1000); - } catch (InterruptedException e){ - // the VM doesn't want us to sleep anymore, - // so get back to work - } - } else { - determineFormatOfNextModel(); - try { - Thread.sleep(10); - } catch (InterruptedException e){ - // the VM doesn't want us to sleep anymore, - // so get back to work - } - } - } - } - - private void determineFormatOfNextModel() { - ChemModel model = (ChemModel)channelContent.getValueAt(currentModel); - File fileObject = (File)model.getProperty("org.openscience.cdk.io.java.io.File"); - if (fileObject == null) { - logger.warn("Thread: Cannot open file if source is not stored"); - return; - } - String formatString = ""; - try { - ChemFormat format = readerFactory.guessFormat( - new FileInputStream(fileObject) - ); - if (format != null) { - formatString = format.getFormatName(); - } - } catch (FileNotFoundException exception) { - // cannot occur - } catch (IOException exception) { - formatString = "IOException"; - } - model.setProperty("org.openscience.cdk.io.FileFormat", formatString); - currentModel++; - channelContent.fireTableDataChanged(); - } - - } - } |