001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:22:04 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31121 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.ecogrid.quicksearch;
031
032import java.awt.event.ActionEvent;
033import java.io.Reader;
034import java.net.URL;
035
036import javax.swing.JOptionPane;
037import javax.swing.ProgressMonitor;
038
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041
042import ptolemy.actor.gui.Configuration;
043import ptolemy.actor.gui.TableauFrame;
044import ptolemy.kernel.util.NamedObj;
045import ptolemy.vergil.toolbox.FigureAction;
046import util.StaticUtil;
047
048//import ptolemy.kernel.util.Configurable;
049/**
050 * This is an action to get metadata for search result item in data search
051 * result panel
052 * 
053 * @author Jing Tao
054 */
055public class GetMetadataAction extends FigureAction {
056  private static final String XMLFILEEXTENSION = ".xml";
057  private static final String HTMLFILEEXTENSION = ".html";
058        private Reader metadataSource = null;
059        private String nameSpace = null;
060        private String htmlFileName = null;
061        private URL metadata = null;
062        private ResultRecord item = null;
063        private Configuration configuration = null;
064        private static final String LABEL = "Get Metadata";
065
066        protected final static Log log;
067        static {
068                log = LogFactory
069                                .getLog("org.ecoinformatics.seek.ecogrid.GetMetadataAction");
070        }
071
072        /**
073         * Constructor base a given ResultRecord object
074         * 
075         * @param item
076         *            ResultRecord
077         */
078        public GetMetadataAction(ResultRecord item) {
079                super(LABEL);
080                this.item = item;
081
082        }
083        
084        public GetMetadataAction(TableauFrame parent) 
085        {
086    super("Get Metadata");
087        }
088
089        /**
090         * Method to get the metadata(the generated html file after transfering
091         * original metadata)
092         * 
093         * @return URL
094         */
095        public URL getMetadata() {
096                return metadata;
097        }
098
099        /**
100         * Invoked when an action occurs. It will transfer the original metadata
101         * into a html file. The namespace will be the key to find stylesheet. If no
102         * sytlesheet found, metadata will be null.
103         * 
104         * @param e
105         *            ActionEvent
106         */
107        public void actionPerformed(ActionEvent e) {
108
109                super.actionPerformed(e);
110                NamedObj object = getTarget();
111    if(object instanceof ResultRecord)
112    {
113      this.item = (ResultRecord)object;
114    }
115    
116                if(item == null)
117                {
118                  JOptionPane.showMessageDialog(null, "There is no metadata associated with this component.");
119                  return;
120                }
121                
122                ProgressMonitor progressMonitor = new ProgressMonitor(null, 
123        "Acquiring Metadata ", "", 0, 5);
124    progressMonitor.setMillisToDecideToPopup(100);
125    progressMonitor.setMillisToPopup(10);
126    progressMonitor.setProgress(0);
127    this.metadataSource = item.getFullRecord();
128    progressMonitor.setProgress(1);
129    this.nameSpace = item.getNamespace();
130    progressMonitor.setProgress(2);
131    this.htmlFileName = item.getRecordId();
132    //System.out.println("the html file name is ====== "+htmlFileName);
133    progressMonitor.setProgress(3);
134    
135                if (configuration == null) {
136                        configuration = getConfiguration();
137                }
138                if (metadataSource != null && nameSpace != null && htmlFileName != null
139                                && configuration != null) {
140      if(htmlFileName.endsWith(XMLFILEEXTENSION)) {
141        htmlFileName = htmlFileName+HTMLFILEEXTENSION;
142      }
143                        try {
144                          progressMonitor.setProgress(4);
145                                metadata = StaticUtil.getMetadataHTMLurl(metadataSource, nameSpace,
146                                                htmlFileName);
147                                //System.out.println("before open html page");
148                                configuration.openModel(null, metadata, metadata
149                                                .toExternalForm());
150                                progressMonitor.setProgress(5);
151                                //System.out.println("after open html page");
152                                progressMonitor.close(); 
153                        } catch (Exception ee) {
154                                log.debug("The error to get metadata html ", ee);
155                        }
156
157                }
158        }
159
160        /*
161         * Method to get Configuration object in the top level container
162         */
163        private Configuration getConfiguration() {
164                Configuration config = (Configuration)Configuration.configurations().get(0);
165                return config;
166        }
167}