001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2010-12-23 19:01:04 +0000 (Thu, 23 Dec 2010) $' 
007 * '$Revision: 26600 $'
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
030/**
031 * 
032 */
033package org.kepler.gui.lsid;
034
035import java.awt.BorderLayout;
036import java.awt.HeadlessException;
037import java.awt.event.ActionEvent;
038import java.awt.event.ActionListener;
039import java.util.List;
040
041import javax.swing.BorderFactory;
042import javax.swing.JButton;
043import javax.swing.JFrame;
044import javax.swing.JLabel;
045import javax.swing.JOptionPane;
046import javax.swing.JPanel;
047import javax.swing.JScrollPane;
048import javax.swing.JTextArea;
049import javax.swing.JTextField;
050
051import org.apache.commons.logging.Log;
052import org.apache.commons.logging.LogFactory;
053import org.kepler.gui.GUIUtil;
054import org.kepler.moml.NamedObjId;
055import org.kepler.moml.NamedObjIdReferralList;
056import org.kepler.objectmanager.ObjectManager;
057import org.kepler.objectmanager.lsid.KeplerLSID;
058import org.kepler.objectmanager.lsid.LSIDGenerator;
059
060import ptolemy.kernel.util.NamedObj;
061
062/**
063 * A popup dialog box that shows the current LSID and LSID Referral List for the
064 * NamedObj that is passed to the initialize(NamedObj) method.
065 * 
066 * @author Aaron Schultz
067 * 
068 */
069public class LSIDViewer extends JFrame implements ActionListener {
070        private static final long serialVersionUID = 3894368355858308419L;
071        private static final Log log = LogFactory
072                        .getLog(LSIDViewer.class.getName());
073        private static final boolean isDebugging = log.isDebugEnabled();
074
075        private NamedObj _no;
076        private JTextField _lsidField;
077        private JTextArea _derivedFromField;
078        private JScrollPane _derivedFromScrollPane;
079        
080        private boolean _editingEnabled = false;
081
082        /**
083         * @throws HeadlessException
084         */
085        public LSIDViewer() throws HeadlessException {
086                this("LSID Viewer");
087        }
088
089        /**
090         * @param title
091         * @throws HeadlessException
092         */
093        public LSIDViewer(String title) throws HeadlessException {
094                super(title);
095        }
096
097        public void initialize(NamedObj no) {
098                if (isDebugging) {
099                        log.debug("initialize( " + no.getName() + " " + no.getClass().getName() + " )");
100                }
101                _no = no;
102
103                JPanel layoutPanel = new JPanel();
104                layoutPanel.setLayout(new BorderLayout());
105
106                JPanel lsidPanel = new JPanel();
107                lsidPanel.setLayout(new BorderLayout());
108                
109                JLabel lsidLabel = new JLabel("LSID:");
110                lsidPanel.add(lsidLabel,BorderLayout.WEST);
111                
112                _lsidField = new JTextField("");
113                _lsidField.setEditable(false);
114                lsidPanel.add(_lsidField, BorderLayout.CENTER);
115
116                JPanel dfPanel = new JPanel();
117                dfPanel.setLayout(new BorderLayout());
118                
119                _derivedFromField = new JTextArea("");
120                _derivedFromField.setEditable(false);
121                _derivedFromScrollPane = new JScrollPane(_derivedFromField);
122                _derivedFromScrollPane.setVerticalScrollBarPolicy(
123                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
124                _derivedFromScrollPane.setBorder(
125                    BorderFactory.createCompoundBorder(
126                        BorderFactory.createCompoundBorder(
127                                        BorderFactory.createTitledBorder("Derived From"),
128                                        BorderFactory.createEmptyBorder(5,5,5,5)),
129                                        _derivedFromScrollPane.getBorder()));
130                dfPanel.add(_derivedFromScrollPane, BorderLayout.CENTER);
131                
132                JPanel bottomPanel = new JPanel();
133                bottomPanel.setLayout(new BorderLayout());
134                
135                String infStr = "Name: "+_no.getName();
136                JLabel objInfo = new JLabel(infStr);
137                if (isEditingEnabled()) {
138                        bottomPanel.add(objInfo,BorderLayout.NORTH);
139                } else {
140                        bottomPanel.add(objInfo,BorderLayout.CENTER);
141                }
142                
143                JPanel buttonPanel = new JPanel();
144                
145                if (isEditingEnabled()) {
146                        JButton newObjButton = new JButton("New Object ID");
147                        newObjButton.addActionListener(this);
148                        buttonPanel.add(newObjButton);
149                        
150                        JButton revisionButton = new JButton("Roll Revision");
151                        revisionButton.addActionListener(this);
152                        buttonPanel.add(revisionButton);
153                }
154                
155                JButton refreshButton = new JButton("Refresh");
156                refreshButton.addActionListener(this);
157                buttonPanel.add(refreshButton);
158                
159                bottomPanel.add(buttonPanel,BorderLayout.EAST);
160
161                layoutPanel.add(lsidPanel, BorderLayout.NORTH);
162                layoutPanel.add(dfPanel, BorderLayout.CENTER);
163                layoutPanel.add(bottomPanel, BorderLayout.SOUTH);
164
165                getContentPane().add(layoutPanel);
166
167                refreshValues();
168        }
169        
170        public void setEditingEnabled(boolean editingEnabled) {
171                _editingEnabled = editingEnabled;
172        }
173        
174        public boolean isEditingEnabled() {
175                return _editingEnabled;
176        }
177
178        /**
179         * Update the textfield and textarea to reflect the current LSID and LSID
180         * referral list values.
181         */
182        public void refreshValues() {
183
184                KeplerLSID lsid = null;
185                String derivedLSIDs = new String();
186                ObjectManager om = ObjectManager.getInstance();
187                try {
188                        if (_no != null) {
189                                NamedObjId noi = NamedObjId.getIdAttributeFor(_no);
190                                if (noi == null) {
191                                        log.error("NamedObjId is null");
192                                        lsid = null;
193                                } else {
194                                        lsid = noi.getId();
195                                }
196                                NamedObjIdReferralList noirl = NamedObjId
197                                                .getIDListAttributeFor(_no);
198                                if (noirl == null) {
199                                        derivedLSIDs = "null";
200                                } else {
201                                        List<KeplerLSID> derivedFrom = noirl.getReferrals();
202                                        if (derivedFrom.size() <= 0) {
203                                                derivedLSIDs = "empty";
204                                        } else {
205                                                for (KeplerLSID derlsid : derivedFrom) {
206                                                        derivedLSIDs += derlsid.toString() + "\n";
207                                                }
208                                        }
209                                }
210                        }
211                } catch (Exception e) {
212                        e.printStackTrace();
213                }
214
215                if (isDebugging) {
216                        log.debug(lsid.toString());
217                        log.debug(derivedLSIDs);
218                }
219                if (lsid == null) {
220                        _lsidField.setText("null");
221                } else {
222                        _lsidField.setText(lsid.toString());
223                }
224                _derivedFromField.setText(derivedLSIDs);
225        }
226        
227        private void rollRevision() {
228                if (isEditingEnabled()) {
229                        NamedObjId noi = NamedObjId.getIdAttributeFor(_no);
230                        try {
231                                noi.updateRevision();
232                        } catch (Exception e) {
233                                JOptionPane.showMessageDialog(GUIUtil
234                                                .getParentWindow(_derivedFromScrollPane), e
235                                                .getMessage());
236                                e.printStackTrace();
237                        }
238                }
239        }
240        
241        private void assignNewObjectId() {
242                if (isEditingEnabled()) {
243                        LSIDGenerator lg = LSIDGenerator.getInstance();
244                        try {
245                                NamedObjId.assignIdTo(_no,lg.getNewLSID());
246                        } catch (Exception e) {
247                                JOptionPane.showMessageDialog(GUIUtil
248                                                .getParentWindow(_derivedFromScrollPane), e
249                                                .getMessage());
250                                e.printStackTrace();
251                        }
252                }
253        }
254
255        /*
256         * (non-Javadoc)
257         * 
258         * @see
259         * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
260         */
261        public void actionPerformed(ActionEvent e) {
262                Object o = e.getSource();
263                if (o instanceof JButton) {
264                        JButton b = (JButton) o;
265                        if (b.getText().equals("Refresh")) {
266                                if (isDebugging) 
267                                        log.debug("Refresh");
268                                refreshValues();
269                        } else if (b.getText().equals("Roll Revision")) {
270                                if (isDebugging)
271                                        log.debug("Roll Revision");
272                                rollRevision();
273                                refreshValues();
274                        } else if (b.getText().equals("New Object ID")) {
275                                if (isDebugging)
276                                        log.debug("New Object ID");
277                                assignNewObjectId();
278                                refreshValues();
279                        }
280                }
281        }
282
283}