001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-07-10 23:34:53 +0000 (Tue, 10 Jul 2012) $' 
007 * '$Revision: 30149 $'
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.kepler.gui;
031
032import java.awt.BorderLayout;
033import java.awt.Color;
034import java.awt.Cursor;
035import java.awt.Dimension;
036import java.awt.Window;
037import java.awt.event.ActionEvent;
038import java.awt.event.ActionListener;
039import java.io.File;
040import java.util.Vector;
041
042import javax.swing.BoxLayout;
043import javax.swing.JButton;
044import javax.swing.JFileChooser;
045import javax.swing.JOptionPane;
046import javax.swing.JPanel;
047import javax.swing.JScrollPane;
048import javax.swing.JTable;
049import javax.swing.JTextArea;
050import javax.swing.ListSelectionModel;
051import javax.swing.ScrollPaneConstants;
052import javax.swing.table.AbstractTableModel;
053import javax.swing.table.TableColumn;
054import javax.swing.table.TableModel;
055
056import org.apache.commons.logging.Log;
057import org.apache.commons.logging.LogFactory;
058import org.kepler.kar.KARCacheManager;
059import org.kepler.objectmanager.cache.LocalRepositoryManager;
060import org.kepler.objectmanager.library.LibIndex;
061import org.kepler.objectmanager.library.LibraryManager;
062import org.kepler.objectmanager.repository.Repository;
063import org.kepler.objectmanager.repository.RepositoryManager;
064import org.kepler.util.StaticResources;
065
066import ptolemy.actor.gui.TableauFrame;
067import ptolemy.gui.JFileChooserBugFix;
068import ptolemy.gui.PtFileChooser;
069import ptolemy.kernel.util.IllegalActionException;
070import ptolemy.kernel.util.NameDuplicationException;
071import ptolemy.kernel.util.NamedObj;
072
073/**
074 * @author Aaron Schultz
075 * 
076 */
077public class ComponentLibraryPreferencesTab extends JPanel implements
078                PreferencesTab {
079        private static final long serialVersionUID = -5778742096181302681L;
080        private static final Log log = LogFactory
081                        .getLog(ComponentLibraryPreferencesTab.class.getName());
082        private static final boolean isDebugging = log.isDebugEnabled();
083
084        private TableauFrame _frame;
085        private String _tabName;
086
087        private JButton _newSource;
088        private JButton _removeSource;
089        private JButton _defaultSources;
090        private JButton _buildLibrary;
091        private JTable _sourceList;
092
093        /**
094         * Keep a reference to the instance of LocalRepositoryManager for ease of
095         * use.
096         */
097        private LocalRepositoryManager _localRepoManager;
098        /**
099         * Keep a reference to the instance of RepositoryManager for ease of use.
100         */
101        private RepositoryManager _repositoryManager;
102
103        /**
104         * 
105         */
106        public ComponentLibraryPreferencesTab() {
107                _localRepoManager = LocalRepositoryManager.getInstance();
108                _localRepoManager.setCheckpoint();
109        }
110
111        /*
112         * (non-Javadoc)
113         * 
114         * @see org.kepler.gui.PreferencesTab#getTabName()
115         */
116        public String getTabName() {
117                return _tabName;
118        }
119
120        public void setTabName(String name) {
121                _tabName = name;
122        }
123
124        /*
125         * (non-Javadoc)
126         * 
127         * @see org.kepler.gui.PreferencesTab#initializeTab()
128         */
129        public void initializeTab() throws Exception {
130
131                _repositoryManager = RepositoryManager.getInstance();
132
133                setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
134
135                initDoc();
136                initTopPanel();
137                initSourceList();
138
139                this.setMinimumSize(new Dimension(300, 300));
140                this.setPreferredSize(new Dimension(300, 300));
141                this.setBackground(TabManager.BGCOLOR);
142        }
143
144        /**
145         * Initialize the top panel that contains controls for the table.
146         */
147        private void initTopPanel() {
148
149                _newSource = new JButton(
150                                StaticResources.getDisplayString(
151                                                "general.ADD",
152                                                "Add"));
153                _newSource.setPreferredSize(new Dimension(100, 50));
154                _newSource.addActionListener(new AddSourceListener());
155
156                _removeSource = new JButton(
157                                StaticResources.getDisplayString(
158                                                "general.REMOVE",
159                                                "Remove"));
160                _removeSource.setPreferredSize(new Dimension(100, 50));
161                _removeSource.addActionListener(new RemoveSourceListener());
162
163                _defaultSources = new JButton(
164                                StaticResources.getDisplayString(
165                                                "preferences.useDefaults",
166                                                "Use Defaults"));
167                _defaultSources.setPreferredSize(new Dimension(100, 50));
168                _defaultSources.addActionListener(new DefaultSourcesListener());
169
170                _buildLibrary = new JButton(
171                                StaticResources.getDisplayString(
172                                                "preferences.build",
173                                                "Build"));
174                _buildLibrary.setPreferredSize(new Dimension(100, 50));
175                _buildLibrary.addActionListener(new BuildLibraryListener());
176
177                JPanel buttonPanel = new JPanel();
178                buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
179                buttonPanel.setBackground(TabManager.BGCOLOR);
180                buttonPanel.setAlignmentX(RIGHT_ALIGNMENT);
181                buttonPanel.add(_newSource);
182                buttonPanel.add(_removeSource);
183                buttonPanel.add(_defaultSources);
184                buttonPanel.add(_buildLibrary);
185
186                LibraryManager lm = LibraryManager.getInstance();
187
188                JPanel topPanel = new JPanel(new BorderLayout());
189                topPanel.add(buttonPanel, BorderLayout.CENTER);
190
191                add(topPanel);
192
193        }
194
195        /**
196         * Initialize the source list table.
197         */
198        private void initSourceList() {
199
200                try {
201
202                        ComponentSourceTableModel cstm = new ComponentSourceTableModel();
203                        _sourceList = new JTable(cstm);
204                        _sourceList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
205                        _sourceList.setShowGrid(true);
206                        _sourceList.setShowHorizontalLines(true);
207                        _sourceList.setShowVerticalLines(true);
208                        _sourceList.setGridColor(Color.lightGray);
209                        _sourceList.setIntercellSpacing(new Dimension(5, 5));
210                        _sourceList.setRowHeight(_sourceList.getRowHeight() + 10);
211                        _sourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
212                        if (isDebugging) {
213                                log.debug("intercellspacing: "
214                                                + _sourceList.getIntercellSpacing().toString());
215                                log.debug("getRowHeight(): " + _sourceList.getRowHeight());
216                        }
217
218                        // Search column
219                        TableColumn c0 = _sourceList.getColumnModel().getColumn(0);
220                        c0.setMinWidth(50);
221                        c0.setPreferredWidth(60);
222                        c0.setMaxWidth(100);
223                        c0.setResizable(true);
224
225                        // Save column
226                        TableColumn c1 = _sourceList.getColumnModel().getColumn(1);
227                        c1.setMinWidth(50);
228                        c1.setPreferredWidth(60);
229                        c1.setMaxWidth(100);
230                        c1.setResizable(true);
231
232                        // Type column
233                        TableColumn c2 = _sourceList.getColumnModel().getColumn(2);
234                        c2.setMinWidth(50);
235                        c2.setPreferredWidth(60);
236                        c2.setMaxWidth(100);
237                        c2.setResizable(true);
238
239                        // Name column
240                        TableColumn c3 = _sourceList.getColumnModel().getColumn(3);
241                        c3.setMinWidth(50);
242                        c3.setPreferredWidth(100);
243                        c3.setMaxWidth(200);
244                        c3.setResizable(true);
245
246                        // Source column
247                        TableColumn c4 = _sourceList.getColumnModel().getColumn(4);
248                        c4.setMinWidth(200);
249                        c4.setPreferredWidth(600);
250                        c4.setMaxWidth(2000);
251                        c4.setResizable(true);
252
253                        JScrollPane sourceListSP = new JScrollPane(_sourceList);
254                        sourceListSP
255                                        .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
256                        sourceListSP
257                                        .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
258                        sourceListSP.setBackground(TabManager.BGCOLOR);
259                        add(sourceListSP);
260                } catch (Exception e) {
261                        System.out.println(e.toString());
262                }
263
264        }
265
266        /**
267         * Initialize the top panel that contains controls for the table.
268         */
269        private void initDoc() {
270
271                String header = 
272                        StaticResources.getDisplayString(
273                                        "preferences.description1",
274                                        "The Component Library is built using KAR files found"
275                                        + " in the following local directories.  Adding or removing local directories will rebuild"
276                                        + " the component library.")
277                        + "\n\n"
278                        + StaticResources.getDisplayString(
279                                        "preferences.description2",                                     
280                                        "By selecting the search box next to remote"
281                                        + " repositories, components from the remote repositories will be included"
282                                        + " when searching components.")
283                        + "\n\n"                
284                        + StaticResources.getDisplayString(
285                                        "preferences.description3",
286                                        "By selecting the save box next to a local repository, KAR files will be saved"
287                                        + " to that directory by default.")
288                        + "\n\n"                
289                        + StaticResources.getDisplayString(
290                                        "preferences.description4",
291                                        "By selecting the save box next to a remote repository, you will be asked "
292                                        + " if you want to upload the KAR to that repository when it is saved.");
293
294                JTextArea headerTextArea = new JTextArea(header);
295                headerTextArea.setEditable(false);
296                headerTextArea.setLineWrap(true);
297                headerTextArea.setWrapStyleWord(true);
298                headerTextArea.setPreferredSize(new Dimension(300, 400));
299                headerTextArea.setBackground(TabManager.BGCOLOR);
300                JPanel headerPanel = new JPanel(new BorderLayout());
301                headerPanel.setBackground(TabManager.BGCOLOR);
302                headerPanel.add(headerTextArea, BorderLayout.CENTER);
303                JScrollPane headerPane = new JScrollPane(headerPanel);
304                headerPane.setPreferredSize(new Dimension(300, 150));
305
306                add(headerPane);
307        }
308
309        /*
310         * (non-Javadoc)
311         * 
312         * @see
313         * org.kepler.gui.PreferencesTab#setParent(ptolemy.actor.gui.TableauFrame)
314         */
315        public void setParent(TableauFrame frame) {
316                _frame = frame;
317        }
318
319        /**
320         * Called when the preference tab is closed.
321         * 
322         * @see org.kepler.gui.PreferencesTab#onClose()
323         */
324        public void onClose() {
325                if (isDebugging) {
326                        log.debug("onClose()");
327                }
328
329                // If the local directories have changed, rebuild the
330                // library.
331                if (_localRepoManager.changedSinceCheckpoint()) {
332                        _localRepoManager.synchronizeDB();
333                        _buildLibrary.doClick();
334                }
335                
336                try {
337                        // look up the tab by the tab class
338                        TabPane tp = 
339                                TabManager.getInstance().getTab(_frame, ComponentLibraryTab.class);
340                        if (tp instanceof ComponentLibraryTab) {
341                                ((ComponentLibraryTab)tp).reinitializeTab();
342                        }
343                } catch (Exception e) {
344                        e.printStackTrace();
345                }
346
347        }
348
349        /**
350         * Called when the user cancels
351         * 
352         * @see org.kepler.gui.PreferencesTab#onCancel()
353         */
354        public void onCancel() {
355                if (_localRepoManager.changedSinceCheckpoint()) {
356                        _localRepoManager.restoreCheckpoint();
357                }
358        }
359
360        /**
361         * Rebuild the library.
362         */
363        public void rebuildLibrary() {
364
365                try {
366                        
367                        KARCacheManager kcm = KARCacheManager.getInstance();
368                        kcm.clearKARCache();
369                        
370                        LibraryManager lm = LibraryManager.getInstance();
371                        LibIndex index = lm.getIndex();
372                        index.clear();
373                        lm.buildLibrary();
374                        lm.refreshJTrees();
375                        _localRepoManager.setCheckpoint();
376
377                } catch (Exception e1) {
378                        e1.printStackTrace();
379                }
380        }
381
382        public class ComponentSourceTableModel extends AbstractTableModel {
383
384                private String[] columnNames = 
385                        { 
386                                StaticResources.getDisplayString("preferences.search", "Search"), 
387                                StaticResources.getDisplayString("preferences.save", "Save"), 
388                                StaticResources.getDisplayString("preferences.type", "Type"), 
389                                StaticResources.getDisplayString("preferences.name", "Name"),
390                                StaticResources.getDisplayString("preferences.source", "Source") 
391                                };
392
393                private Vector<Boolean> searchSources;
394                private Vector<Boolean> saveSources;
395                private Vector<String> sourceTypes;
396                private Vector<String> sourceNames;
397                private Vector<String> sourceValues;
398
399                public ComponentSourceTableModel() {
400                        refreshData();
401                }
402
403                public void refreshData() {
404                        searchSources = new Vector<Boolean>();
405                        saveSources = new Vector<Boolean>();
406                        sourceTypes = new Vector<String>();
407                        sourceNames = new Vector<String>();
408                        sourceValues = new Vector<String>();
409
410                        /** Local Repositories **/
411                        final File saveDir = _localRepoManager.getSaveRepository();
412                        for (LocalRepositoryManager.LocalRepository localRepo : _localRepoManager.getLocalRepositories()
413                                        .keySet()) {
414                                searchSources.add(new Boolean(true));
415                                if (localRepo.isFileRepoDirectory(saveDir)) {
416                                        saveSources.add(new Boolean(true));
417                                } else {
418                                        saveSources.add(new Boolean(false));
419                                }
420                                sourceTypes.add("local");
421                                sourceNames.add(_localRepoManager.getLocalRepositories().get(
422                                                localRepo));
423                                sourceValues.add(localRepo.getDefaultDirectory().toString());
424                        }
425
426                        /** Remote Repositories **/
427                        for (Repository r : _repositoryManager.getRepositories()) {
428                                searchSources.add(new Boolean(r.includeInSearch()));
429                                sourceTypes.add("remote");
430                                sourceNames.add(r.getName());
431                                sourceValues.add(r.getRepository());
432
433                                // set the save repository
434                                Repository saveRepo = _repositoryManager.getSaveRepository();
435                                if (r == saveRepo) {
436                                        saveSources.add(new Boolean(true));
437                                } else {
438                                        saveSources.add(new Boolean(false));
439                                }
440                        }
441
442                        fireTableDataChanged();
443                }
444
445                public int getColumnCount() {
446                        return 5;
447                }
448
449                public String getColumnName(int col) {
450                        return columnNames[col];
451                }
452
453                public int getRowCount() {
454                        return searchSources.size();
455                }
456
457                public Object getValueAt(int rowIndex, int columnIndex) {
458                        switch (columnIndex) {
459                        case 0:
460                                return searchSources.elementAt(rowIndex);
461                        case 1:
462                                return saveSources.elementAt(rowIndex);
463                        case 2:
464                                return sourceTypes.elementAt(rowIndex);
465                        case 3:
466                                return sourceNames.elementAt(rowIndex);
467                        case 4:
468                                return sourceValues.elementAt(rowIndex);
469                        default:
470                                return null;
471                        }
472                }
473
474                public Class getColumnClass(int col) {
475                        switch (col) {
476                        case 0:
477                                return Boolean.class;
478                        case 1:
479                                return Boolean.class;
480                        case 2:
481                                return String.class;
482                        case 3:
483                                return String.class;
484                        case 4:
485                                return String.class;
486                        default:
487                                return null;
488                        }
489                }
490
491                /*
492                 * Don't need to implement this method unless your table's editable.
493                 */
494                public boolean isCellEditable(int row, int col) {
495                        if (col == 0) {
496                                if (sourceTypes.elementAt(row).equals("remote")) {
497                                        return true;
498                                }
499                        }
500                        if (col == 1) {
501                                if (sourceTypes.elementAt(row).equals("local")
502                                                || sourceTypes.elementAt(row).equals("remote")) {
503                                        return true;
504                                }
505                        }
506                        if (col == 3) {
507                                if (sourceTypes.elementAt(row).equals("local")) {
508                                        return true;
509                                }
510                        }
511                        return false;
512                }
513
514                public void setValueAt(Object value, int row, int col) {
515                        if (col == 0) {
516                                if (sourceTypes.elementAt(row).equals("remote")) {
517                                        if (value instanceof Boolean) {
518                                                Repository r = _repositoryManager
519                                                                .getRepository(sourceNames.elementAt(row));
520                                                r.setIncludeInSearch(((Boolean) value).booleanValue());
521                                                searchSources.setElementAt((Boolean) value, row);
522                                                fireTableCellUpdated(row, col);
523                                        }
524                                }
525                        }
526                        if (col == 1) {
527                                if (sourceTypes.elementAt(row).equals("local")) {
528                                        if (value instanceof Boolean) {
529                                                for (int i = 0; i < saveSources.size(); i++) {
530                                                        if (sourceTypes.elementAt(i).equals("local")) {
531                                                                if (i == row) {
532                                                                        saveSources.setElementAt(new Boolean(true),
533                                                                                        i);
534                                                                        LocalRepositoryManager.getInstance()
535                                                                                        .setLocalSaveRepo(
536                                                                                                        new File(sourceValues
537                                                                                                                        .elementAt(row)));
538                                                                        fireTableCellUpdated(i, col);
539                                                                } else {
540                                                                        saveSources.setElementAt(
541                                                                                        new Boolean(false), i);
542                                                                        fireTableCellUpdated(i, col);
543                                                                }
544                                                        }
545                                                }
546                                        }
547                                } else if (sourceTypes.elementAt(row).equals("remote")) {
548                                        if (value instanceof Boolean) {
549                                                boolean checked = ((Boolean) value).booleanValue();
550                                                for (int i = 0; i < saveSources.size(); i++) {
551                                                        if (sourceTypes.elementAt(i).equals("remote")) {
552                                                                if (checked) {
553                                                                        if (i == row) {
554                                                                                // tell the repositoryManager to save to
555                                                                                // this
556                                                                                // repository
557                                                                                _repositoryManager
558                                                                                                .setSaveRepository(_repositoryManager
559                                                                                                                .getRepository(sourceNames
560                                                                                                                                .elementAt(row)));
561                                                                                saveSources.setElementAt(new Boolean(
562                                                                                                true), i);
563                                                                                fireTableCellUpdated(i, col);
564                                                                        } else {
565                                                                                saveSources.setElementAt(new Boolean(
566                                                                                                false), i);
567                                                                                fireTableCellUpdated(i, col);
568                                                                        }
569                                                                } else {
570                                                                        // Set no save repository
571                                                                        saveSources.setElementAt(
572                                                                                        new Boolean(false), i);
573                                                                        if (i == row) {
574                                                                                _repositoryManager
575                                                                                                .setSaveRepository(null);
576                                                                        }
577                                                                        fireTableCellUpdated(i, col);
578                                                                }
579                                                        }
580                                                }
581                                        }
582                                }
583                        }
584                        if (col == 3) {
585                                if (sourceTypes.elementAt(row).equals("local")) {
586                                        if (isDebugging)
587                                                log.debug(value.getClass().getName());
588                                        if (value instanceof String) {
589                                                try {
590                                                        LocalRepositoryManager.getInstance()
591                                                                        .setLocalRepoName(
592                                                                                        new File(sourceValues
593                                                                                                        .elementAt(row)),
594                                                                                        (String) value);
595                                                        sourceNames.setElementAt((String) value, row);
596                                                } catch (Exception e) {
597                                                        JOptionPane.showMessageDialog(GUIUtil
598                                                                        .getParentWindow(_sourceList), e
599                                                                        .getMessage());
600                                                        fireTableCellUpdated(row, col);
601                                                }
602                                        }
603                                }
604                        }
605                }
606
607        }
608
609        /**
610         * A listener for the Add source button.
611         * 
612         * @author Aaron Schultz
613         */
614        private class AddSourceListener implements ActionListener {
615
616                /** Action for changing the view. */
617                public void actionPerformed(ActionEvent e) {
618
619                        Object c = e.getSource();
620                        if (c instanceof JButton) {
621
622                                JButton jc = (JButton) c;
623
624                                File saveRepo = _localRepoManager.getSaveRepository();
625                                // Avoid white boxes in file chooser, see
626                                // http://bugzilla.ecoinformatics.org/show_bug.cgi?id=3801
627                                JFileChooserBugFix jFileChooserBugFix = new JFileChooserBugFix();
628                                Color background = null;
629                                PtFileChooser chooser = null;
630                                try {
631                                    background = jFileChooserBugFix.saveBackground();
632                                    chooser = new PtFileChooser(_frame, "Add Repository", JFileChooser.OPEN_DIALOG);
633                                    chooser.setCurrentDirectory(saveRepo);
634                                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
635                                
636                                    int returnVal = chooser.showDialog(_frame, "Choose");
637
638                                    if (returnVal == JFileChooser.APPROVE_OPTION) {
639
640                                        File selectedFile = chooser.getSelectedFile();
641                                        try {
642                                                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
643                                                _localRepoManager.addLocalRepoRootDir(selectedFile);
644
645                                                TableModel tm = _sourceList.getModel();
646                                                ComponentSourceTableModel cstm = (ComponentSourceTableModel) tm;
647                                                cstm.refreshData();
648
649                                        } catch (Exception e1) {
650                                                Window parentWindow = GUIUtil.getParentWindow(jc);
651                                                JOptionPane.showMessageDialog(parentWindow, e1.getMessage());
652                                        } finally {
653                                                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
654                                        }
655                                    } else {
656                                        if (isDebugging)
657                                            log.debug("Cancel Add source operation");
658                                    }
659                                } finally {
660                                    jFileChooserBugFix.restoreBackground(background);
661                                }
662                        }
663                }
664
665        }
666
667        /**
668         * A listener for the set default sources button.
669         * 
670         * @author Aaron Schultz
671         * 
672         */
673        private class DefaultSourcesListener implements ActionListener {
674                public void actionPerformed(ActionEvent e) {
675                        Object c = e.getSource();
676                        if (c instanceof JButton) {
677                                JButton jc = (JButton) c;
678                                int choice = JOptionPane.showConfirmDialog(GUIUtil
679                                                .getParentWindow(jc), "Reset to default directories?");
680                                if (choice == JOptionPane.YES_OPTION) {
681                                        _localRepoManager.setDefaultLocalRepos();
682                                        _localRepoManager.setDefaultSaveRepo();
683                                        _repositoryManager.setSearchNone();
684                                        ((ComponentSourceTableModel) _sourceList.getModel())
685                                                        .refreshData();
686                                }
687
688                        }
689                }
690        }
691
692        /**
693         * A listener for the set default sources button.
694         * 
695         * @author Aaron Schultz
696         * 
697         */
698        private class BuildLibraryListener implements ActionListener {
699                public void actionPerformed(ActionEvent e) {
700                        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
701                        Object c = e.getSource();
702                        if (c instanceof JButton) {
703                                rebuildLibrary();
704                        }
705                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
706                }
707        }
708
709        /**
710         * A listener for the Add source button.
711         * 
712         * @author Aaron Schultz
713         */
714        private class RemoveSourceListener implements ActionListener {
715
716                /** Action for changing the view. */
717                public void actionPerformed(ActionEvent e) {
718
719                        Object c = e.getSource();
720                        if (c instanceof JButton) {
721                                JButton jc = (JButton) c;
722                                int currentRow = _sourceList.getSelectedRow();
723
724                                if (currentRow < 0) {
725                                        Window parentWindow = GUIUtil.getParentWindow(jc);
726                                        JOptionPane.showMessageDialog(parentWindow,
727                                                        "Please highlight a source for removal.");
728                                        return;
729                                }
730
731                                TableModel tm = _sourceList.getModel();
732                                ComponentSourceTableModel cstm = (ComponentSourceTableModel) tm;
733
734                                String sourceType = (String) cstm.getValueAt(currentRow, 2);
735                                String removeDir = (String) cstm.getValueAt(currentRow, 4);
736                                if (sourceType.equals("remote")) {
737                                        JOptionPane.showMessageDialog(GUIUtil.getParentWindow(jc),
738                                                        "Cannot remove remote sources: \n" + removeDir
739                                                                        + "\n is a remote source.");
740                                        return;
741                                }
742                                try {
743                                        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
744                                        File removeDirFile = new File(removeDir);
745                                        _localRepoManager.removeLocalRepoRootDir(removeDirFile);
746                                        cstm.refreshData();
747                                } catch (Exception e2) {
748                                        JOptionPane.showMessageDialog(GUIUtil.getParentWindow(jc),
749                                                        "Unable to remove repository root directory.\n"
750                                                                        + e2.getMessage());
751                                } finally {
752                                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
753                                }
754                        }
755                }
756
757        }
758
759        /**
760         * A factory that creates the ServicesListModification panel for the
761         * PreferencesFrame.
762         * 
763         *@author Aaron Schultz
764         */
765        public static class Factory extends PreferencesTabFactory {
766                /**
767                 * Create a factory with the given name and container.
768                 * 
769                 *@param container
770                 *            The container.
771                 *@param name
772                 *            The name of the entity.
773                 *@exception IllegalActionException
774                 *                If the container is incompatible with this attribute.
775                 *@exception NameDuplicationException
776                 *                If the name coincides with an attribute already in the
777                 *                container.
778                 */
779                public Factory(NamedObj container, String name)
780                                throws IllegalActionException, NameDuplicationException {
781                        super(container, name);
782                }
783
784                /**
785                 * Create a PreferencesTab that displays the selected Ecogrid Services.
786                 * 
787                 * @return A new LibraryPaneTab that displays the library
788                 */
789                public PreferencesTab createPreferencesTab() {
790                        ComponentLibraryPreferencesTab clpt = new ComponentLibraryPreferencesTab();
791                        clpt.setTabName(this.getName());
792                        return clpt;
793                }
794        }
795
796}