001/*
002 *  Copyright (c) 2003-2010 The Regents of the University of California.
003 *  All rights reserved.
004 *  Permission is hereby granted, without written agreement and without
005 *  license or royalty fees, to use, copy, modify, and distribute this
006 *  software and its documentation for any purpose, provided that the above
007 *  copyright notice and the following two paragraphs appear in all copies
008 *  of this software.
009 *  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
010 *  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
011 *  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
012 *  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
013 *  SUCH DAMAGE.
014 *  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
015 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
016 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
017 *  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
018 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
019 *  ENHANCEMENTS, OR MODIFICATIONS.
020 *  PT_COPYRIGHT_VERSION_2
021 *  COPYRIGHTENDKEY
022 */
023package org.kepler.gui.kar;
024
025import java.awt.BorderLayout;
026import java.awt.Cursor;
027import java.awt.Dimension;
028import java.awt.GridLayout;
029import java.awt.event.ActionEvent;
030import java.awt.event.ActionListener;
031import java.awt.event.KeyEvent;
032
033import javax.swing.ButtonGroup;
034import javax.swing.JPanel;
035import javax.swing.JRadioButton;
036import javax.swing.JTextArea;
037
038import org.kepler.configuration.ConfigurationManager;
039import org.kepler.configuration.ConfigurationManagerException;
040import org.kepler.configuration.ConfigurationProperty;
041import org.kepler.gui.PreferencesTab;
042import org.kepler.gui.PreferencesTabFactory;
043import org.kepler.gui.TabManager;
044import org.kepler.kar.KARCacheManager;
045import org.kepler.kar.KARFile;
046import org.kepler.objectmanager.library.LibIndex;
047import org.kepler.objectmanager.library.LibraryManager;
048
049import ptolemy.actor.gui.TableauFrame;
050import ptolemy.kernel.util.IllegalActionException;
051import ptolemy.kernel.util.NameDuplicationException;
052import ptolemy.kernel.util.NamedObj;
053
054
055
056public class KARPreferencesTab extends JPanel implements PreferencesTab, ActionListener {
057
058        private JPanel _KARPrefPanel = new JPanel();
059        private String _tabName = "KAR Preferences";
060        
061        ConfigurationManager cman = ConfigurationManager.getInstance();
062        ConfigurationProperty coreProperty = cman.getProperty(KARFile.KARFILE_CONFIG_PROP_MODULE);
063        ConfigurationProperty KARComplianceProp = coreProperty.getProperty(KARFile.KAR_COMPLIANCE_PROPERTY_NAME);
064        private String lastUserComplianceSetting = null;
065        
066
067        public String getTabName() {
068                return _tabName;
069        }
070
071        public void setTabName(String tabName) {
072                _tabName = tabName;
073        }
074
075        public void initializeTab() throws Exception {
076                
077                _KARPrefPanel.setLayout(new BorderLayout());
078                
079                //user has a core configuration.xml from before KARComplianceProp existed, need to add it.
080                // if KARPreferencesTab.areAllModuleDependenciesSatisfied hasn't already done so.
081                if (KARComplianceProp == null){
082                        KARComplianceProp = new ConfigurationProperty(KARFile.KARFILE_CONFIG_PROP_MODULE, KARFile.KAR_COMPLIANCE_PROPERTY_NAME, KARFile.KAR_COMPLIANCE_DEFAULT);
083                        coreProperty.addProperty(KARComplianceProp);
084                }
085                lastUserComplianceSetting = KARComplianceProp.getValue();
086                
087                setupButtonPanel();
088                setupDescription();
089                
090                this.add(_KARPrefPanel, BorderLayout.WEST);
091        }
092
093        private void setupButtonPanel(){
094                                
095            JRadioButton strictButton = new JRadioButton(KARFile.KAR_COMPLIANCE_STRICT);
096            strictButton.setMnemonic(KeyEvent.VK_S);
097            strictButton.setActionCommand(KARFile.KAR_COMPLIANCE_STRICT);
098            strictButton.addActionListener(this);
099            JRadioButton relaxedButton = new JRadioButton(KARFile.KAR_COMPLIANCE_RELAXED);
100            relaxedButton.setMnemonic(KeyEvent.VK_R);
101            relaxedButton.setActionCommand(KARFile.KAR_COMPLIANCE_RELAXED);
102            relaxedButton.addActionListener(this);
103            String KARCompliance = KARComplianceProp.getValue();
104            
105                if (KARCompliance.equals(KARFile.KAR_COMPLIANCE_STRICT)){
106                        strictButton.setSelected(true);
107                }
108                else{
109                        relaxedButton.setSelected(true);
110                }
111            
112            ButtonGroup group = new ButtonGroup();
113            group.add(strictButton);
114            group.add(relaxedButton);
115            
116        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
117        radioPanel.add(strictButton);
118        radioPanel.add(relaxedButton);
119        
120        _KARPrefPanel.add(radioPanel, BorderLayout.NORTH);              
121        }
122        
123        private void setupDescription() {
124                
125                String desciption = "Select your KAR opening compliance mode.\n\n" +
126                                "* Strict * In order to open a KAR in Strict mode, you must be running Kepler with the " +
127                                "exact same modules*, in the same order, that the KAR was created with. You will be prompted " +
128                                "if you need to change or install modules. This may mean restarting with an older version of " +
129                                "a module you're currently using. Strict mode enables " +
130                                "maximum compatibility.\n\n" +
131                                "* Relaxed * In order to open a KAR in Relaxed mode, you must simply be running version(s) newer than or equal to" +
132                                ", in some order, of the modules used to create it*. However maximum compatibility is not guaranteed."+
133                                "\n\n*A caveat: OS specific modules are currently ignored in dependency checks." + 
134                                "\n\nChanging this setting rebuilds your library, please be patient...";
135
136                JTextArea descriptionTextArea = new JTextArea(desciption);
137                descriptionTextArea.setEditable(false);
138                descriptionTextArea.setLineWrap(true);
139                descriptionTextArea.setWrapStyleWord(true);
140                descriptionTextArea.setPreferredSize(new Dimension(400, 400));
141                descriptionTextArea.setBackground(TabManager.BGCOLOR);
142                JPanel descriptionPanel = new JPanel(new BorderLayout());
143                descriptionPanel.setBackground(TabManager.BGCOLOR);
144                descriptionPanel.add(descriptionTextArea, BorderLayout.CENTER);
145
146                _KARPrefPanel.add(descriptionPanel, BorderLayout.SOUTH);
147        }
148        
149        /**
150         * If compliance level changes, rebuild library.
151         * Very similar to ComponentLibraryPreferencesTab.rebuildLibrary(), but no need
152         * to setCheckpoint.
153         */
154        public void rebuildLibrary(){
155                
156                KARCacheManager kcm = KARCacheManager.getInstance();
157                kcm.clearKARCache();
158                
159                LibraryManager lm = LibraryManager.getInstance();
160                LibIndex index = lm.getIndex();
161                index.clear();
162                lm.buildLibrary();
163                try {
164                        lm.refreshJTrees();
165                } catch (IllegalActionException e) {
166                        // TODO Auto-generated catch block
167                        e.printStackTrace();
168                }
169        }
170        
171        
172        public void onCancel() {
173                //do nothing
174        }
175
176        /**
177         * If any KARCompliance change by the user is made and it's different 
178         * from serialized KARCompliance, serialize it and rebuild library so that
179         * new KARCompliance setting will take effect (so KARs may display warning icons,
180         * KARS_CACHED, and KARS_ERRORS tables are rebuilt, etc)
181         */
182        public void onClose() {
183                
184                try {
185                        
186                        String KARCompliance = KARComplianceProp.getValue();
187                        
188                        if (!lastUserComplianceSetting.equals(KARCompliance)){
189                                //serialize their preference
190                                KARComplianceProp.setValue(lastUserComplianceSetting);
191                                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
192                                rebuildLibrary();
193                                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
194                        }
195                } catch (ConfigurationManagerException e1) {
196                        // TODO Auto-generated catch block
197                        e1.printStackTrace();
198                }
199                
200        }
201
202        public void setParent(TableauFrame frame) {             
203        }
204
205        
206        /**
207         * A factory that creates the ServicesListModification panel for the
208         * PreferencesFrame.
209         */
210        public static class Factory extends PreferencesTabFactory {
211                /**
212                 * Create a factory with the given name and container.
213                 * 
214                 *@param container
215                 *            The container.
216                 *@param name
217                 *            The name of the entity.
218                 *@exception IllegalActionException
219                 *                If the container is incompatible with this attribute.
220                 *@exception NameDuplicationException
221                 *                If the name coincides with an attribute already in the
222                 *                container.
223                 */
224                public Factory(NamedObj container, String name)
225                                throws IllegalActionException, NameDuplicationException {
226                        super(container, name);
227                }
228
229
230                /*
231                 * (non-Javadoc)
232                 * @see org.kepler.gui.PreferencesTabFactory#createPreferencesTab()
233                 */
234                public KARPreferencesTab createPreferencesTab() {
235                
236                        KARPreferencesTab kpt = new KARPreferencesTab();
237                        kpt.setTabName(this.getName());
238                        return kpt;
239                }
240        }
241
242        
243        public void actionPerformed(ActionEvent e) {
244                //use this onClose()
245                lastUserComplianceSetting = e.getActionCommand();
246        }
247        
248        
249}