PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jchempaint-3.0.1/org.openscience.cdk.jchempaint30/src/main/org/openscience/jchempaint/dialog/TemplateBrowser.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 256 lines | 183 code | 12 blank | 61 comment | 19 complexity | eb0486018793c82148427bb80819ac3e MD5 | raw file
  1. /* Copyright (C) 2009 Stefan Kuhn <stefan.kuhn@ebi.ac.uk>
  2. *
  3. * Contact: cdk-jchempaint@lists.sourceforge.net
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 2.1
  8. * of the License, or (at your option) any later version.
  9. * All we ask is that proper credit is given for our work, which includes
  10. * - but is not limited to - adding the above copyright notice to the beginning
  11. * of your source code files, and to any copyright notice that you may distribute
  12. * with programs based on this work.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. package org.openscience.jchempaint.dialog;
  24. import java.awt.BorderLayout;
  25. import java.awt.Dimension;
  26. import java.awt.GridLayout;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.InputStream;
  32. import java.net.JarURLConnection;
  33. import java.net.URL;
  34. import java.util.ArrayList;
  35. import java.util.Enumeration;
  36. import java.util.HashMap;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.TreeMap;
  41. import java.util.jar.JarEntry;
  42. import java.util.jar.JarFile;
  43. import java.util.zip.ZipException;
  44. import javax.swing.Icon;
  45. import javax.swing.ImageIcon;
  46. import javax.swing.JButton;
  47. import javax.swing.JDialog;
  48. import javax.swing.JFrame;
  49. import javax.swing.JPanel;
  50. import javax.swing.JTabbedPane;
  51. import javax.swing.SwingConstants;
  52. import org.openscience.cdk.CDKConstants;
  53. import org.openscience.cdk.DefaultChemObjectBuilder;
  54. import org.openscience.cdk.interfaces.IMolecule;
  55. import org.openscience.cdk.io.MDLV2000Reader;
  56. import org.openscience.cdk.io.IChemObjectReader.Mode;
  57. import org.openscience.jchempaint.GT;
  58. import org.openscience.jchempaint.dialog.templates.DummyClass;
  59. /**
  60. * This class shows a list of templates. The one chosen by the user can queried
  61. * with getChosenmolecule(). The templates are organized in tabs. The headers of
  62. * the tabs are the names of all directories in TEMPLATES_PACKAGE. _ in
  63. * directory name is replaced by a space. All files in
  64. * theses directories named *.mol are read as MOL files and put as a template on
  65. * the respective tab. The first line of the MOL file is used as name to
  66. * display. If there is a *.png file in the same directy, it is used
  67. * as icon. Do not put anything else in these directories. TEMPLATES_PACKAGE must
  68. * contain a class called DummyClass for the directory being located.
  69. * If wished, the tab can be added to the Templates menu with an action like:
  70. * menuitemnameAction=org.openscience.jchempaint.action.CopyPasteAction@pasteX
  71. * where X is the directory name.
  72. */
  73. public class TemplateBrowser extends JDialog implements ActionListener {
  74. private static final long serialVersionUID = -7684345027847830963L;
  75. private JPanel myPanel;
  76. private JButton yesButton;
  77. private JTabbedPane tabbedPane;
  78. private Map<JButton, IMolecule> mols = new HashMap<JButton, IMolecule>();
  79. private IMolecule chosenmolecule;
  80. public final static String TEMPLATES_PACKAGE = "org/openscience/jchempaint/dialog/templates";
  81. /**
  82. * The molecule chosen by the user.
  83. *
  84. * @return The molecule, null if cancelled.
  85. */
  86. public IMolecule getChosenmolecule() {
  87. return chosenmolecule;
  88. }
  89. /**
  90. * Constructor for TemplateBrowser.
  91. * @param tabToSelect a tab with that name will be shown at startup.
  92. */
  93. public TemplateBrowser(String tabToSelect) {
  94. super((JFrame)null, GT._("Structure Templates"), true);
  95. this.setName("templates");
  96. myPanel = new JPanel();
  97. getContentPane().add(myPanel);
  98. myPanel.setLayout(new BorderLayout());
  99. yesButton = new JButton(GT._("Cancel"));
  100. yesButton.addActionListener(this);
  101. JPanel bottomPanel =new JPanel();
  102. bottomPanel.add(yesButton);
  103. myPanel.add(bottomPanel, BorderLayout.SOUTH);
  104. tabbedPane = new JTabbedPane();
  105. Map<String,List<IMolecule>> entriesMol = new TreeMap<String,List<IMolecule>>();
  106. Map<IMolecule, String> entriesMolName = new HashMap<IMolecule, String>();
  107. Map<String, Icon> entriesIcon = new HashMap<String, Icon>();
  108. JPanel allPanel = new JPanel();
  109. GridLayout experimentLayout = new GridLayout(0,8);
  110. allPanel.setLayout(experimentLayout);
  111. tabbedPane.addTab(GT._("All"), allPanel );
  112. try{
  113. createTemplatesMaps(entriesMol, entriesMolName, entriesIcon, true);
  114. myPanel.add( tabbedPane, BorderLayout.CENTER );
  115. Iterator<String> it = entriesMol.keySet().iterator();
  116. int count=0;
  117. while(it.hasNext()) {
  118. String key=it.next();
  119. JPanel panel = new JPanel();
  120. panel.setLayout(experimentLayout);
  121. for(int k=0;k<entriesMol.get(key).size();k++){
  122. IMolecule cdkmol = entriesMol.get(key).get(k);
  123. Icon icon = entriesIcon.get(entriesMolName.get(cdkmol));
  124. JButton button = new JButton();
  125. if(icon!=null)
  126. button.setIcon(icon);
  127. panel.add(button);
  128. button.setPreferredSize(new Dimension(100,120));
  129. button.setMaximumSize(new Dimension(100,120));
  130. button.addActionListener(this);
  131. button.setVerticalTextPosition(SwingConstants.BOTTOM);
  132. button.setHorizontalTextPosition(SwingConstants.CENTER);
  133. button.setText((String)cdkmol.getProperty(CDKConstants.TITLE));
  134. button.setToolTipText((String)cdkmol.getProperty(CDKConstants.TITLE));
  135. button.setFont(button.getFont().deriveFont(10f));
  136. button.setName((String)cdkmol.getProperty(CDKConstants.TITLE));
  137. mols.put(button, cdkmol);
  138. JButton allButton = new JButton();
  139. if(icon!=null)
  140. allButton.setIcon(icon);
  141. panel.add(button);
  142. allButton.setPreferredSize(new Dimension(100,120));
  143. allButton.setMaximumSize(new Dimension(100,120));
  144. allButton.addActionListener(this);
  145. allButton.setVerticalTextPosition(SwingConstants.BOTTOM);
  146. allButton.setHorizontalTextPosition(SwingConstants.CENTER);
  147. allButton.setText((String)cdkmol.getProperty(CDKConstants.TITLE));
  148. allButton.setToolTipText((String)cdkmol.getProperty(CDKConstants.TITLE));
  149. allButton.setFont(allButton.getFont().deriveFont(10f));
  150. mols.put(allButton, cdkmol);
  151. allPanel.add(allButton);
  152. }
  153. tabbedPane.addTab(GT.getStringNoExtraction(key.replace('_', ' ')), panel );
  154. if(tabToSelect.equals(key.replace('_',' '))){
  155. tabbedPane.setSelectedIndex(count+1);
  156. }
  157. count++;
  158. }
  159. pack();
  160. setVisible(true);
  161. } catch (Exception e1) {
  162. // TODO Auto-generated catch block
  163. e1.printStackTrace();
  164. }
  165. }
  166. /* (non-Javadoc)
  167. * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  168. */
  169. public void actionPerformed(ActionEvent e) {
  170. if(e.getSource()!=yesButton){
  171. chosenmolecule = mols.get(e.getSource());
  172. chosenmolecule.removeProperty(CDKConstants.TITLE);
  173. }
  174. this.setVisible(false);
  175. }
  176. /**
  177. * Extracts templates from directories.
  178. *
  179. * @param entriesMol A map of category names and structure.
  180. * @param entriesMolName A map of structures and names.
  181. * @param entriesIcon A map of structures and images.
  182. * @param withsubdirs true=all of the above will be filled, false=only names in entriesMol will be filled (values in entriesMol will be empty, other maps as well, these can be passed as null).
  183. * @throws Exception Problems reading directories.
  184. */
  185. public static void createTemplatesMaps(Map<String, List<IMolecule>> entriesMol,
  186. Map<IMolecule, String> entriesMolName, Map<String, Icon> entriesIcon, boolean withsubdirs) throws Exception{
  187. DummyClass dummy = new DummyClass();
  188. try{
  189. // Create a URL that refers to a jar file on the net
  190. URL url = new URL("jar:"+dummy.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()+"!/");
  191. // Get the jar file
  192. JarURLConnection conn = (JarURLConnection)url.openConnection();
  193. JarFile jarfile = conn.getJarFile();
  194. for (Enumeration<JarEntry> e = jarfile.entries() ; e.hasMoreElements() ;) {
  195. JarEntry entry = e.nextElement();
  196. if(entry.getName().indexOf(TEMPLATES_PACKAGE+"/")==0){
  197. String restname = entry.getName().substring(new String(TEMPLATES_PACKAGE+"/").length());
  198. if(restname.length()>2){
  199. if(restname.indexOf("/")==restname.length()-1){
  200. entriesMol.put(restname.substring(0,restname.length()-1), new ArrayList<IMolecule>());
  201. }else if(restname.indexOf("/")>-1 && withsubdirs){
  202. if(entry.getName().indexOf(".mol")>-1){
  203. InputStream ins = dummy.getClass().getClassLoader().getResourceAsStream(entry.getName());
  204. MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT);
  205. IMolecule cdkmol = (IMolecule)reader.read(DefaultChemObjectBuilder.getInstance().newMolecule());
  206. entriesMol.get(restname.substring(0,restname.indexOf("/"))).add(cdkmol);
  207. entriesMolName.put(cdkmol,entry.getName().substring(0,entry.getName().length()-4));
  208. }else{
  209. Icon icon = new ImageIcon(new URL(url.toString()+entry.getName()));
  210. entriesIcon.put(entry.getName().substring(0,entry.getName().length()-4),icon);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }catch(ZipException ex){
  217. //This is a version we fall back to if no jar available. This should be in Eclipse only.
  218. File file = new File(new File(dummy.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath()+File.separator+TEMPLATES_PACKAGE.replace('/', File.separatorChar));
  219. for (int i=0;i<file.listFiles().length ; i++) {
  220. if(file.listFiles()[i].isDirectory()){
  221. File dir = file.listFiles()[i];
  222. if(!dir.getName().startsWith(".")) {
  223. entriesMol.put(dir.getName(), new ArrayList<IMolecule>());
  224. if(withsubdirs){
  225. for(int k=0;k<dir.list().length;k++){
  226. if(dir.listFiles()[k].getName().indexOf(".mol")>-1){
  227. MDLV2000Reader reader = new MDLV2000Reader(new FileInputStream(dir.listFiles()[k]), Mode.STRICT);
  228. IMolecule cdkmol = (IMolecule)reader.read(DefaultChemObjectBuilder.getInstance().newMolecule());
  229. entriesMol.get(dir.getName()).add(cdkmol);
  230. entriesMolName.put(cdkmol,dir.listFiles()[k].getName().substring(0,dir.listFiles()[k].getName().length()-4));
  231. }else{
  232. Icon icon = new ImageIcon(dir.listFiles()[k].getAbsolutePath());
  233. if ( dir.listFiles()[k].getName().toLowerCase().endsWith("png")) {
  234. entriesIcon.put(dir.listFiles()[k].getName().substring(0,dir.listFiles()[k].getName().length()-4),icon);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }