alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.cvsclient;

import javax.swing.*;
import org.openide.util.*;
import org.openide.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.beans.*;
import java.lang.reflect.*;
import org.netbeans.modules.javacvs.events.*;
import org.netbeans.modules.javacvs.commands.*;
import org.netbeans.modules.javacvs.FsGlobalOptions;
import org.netbeans.modules.vcscore.settings.GeneralVcsSettings;
import javax.accessibility.*;

//import org.netbeans.modules.javacvs.*;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileStateInvalidException;

import org.openide.*;
import org.openide.windows.*;

import org.netbeans.modules.cvsclient.NbJavaCvsFileSystem;
import org.netbeans.modules.cvsclient.NbJavaCvsStatusManager;
import org.netbeans.modules.javacvs.customizers.CustomizerPropChangeSupport;
import org.netbeans.modules.javacvs.customizers.GlobalOptionsCustomizer;
import org.netbeans.modules.cvsclient.actions.*;
import org.netbeans.modules.cvsclient.commands.add.*;
import org.netbeans.modules.cvsclient.commands.*;
import org.netbeans.modules.cvsclient.commands.annotate.*;
import org.netbeans.modules.cvsclient.commands.commit.*;
import org.netbeans.modules.cvsclient.commands.remove.*;
import org.netbeans.modules.cvsclient.commands.checkout.*;
import org.netbeans.modules.cvsclient.commands.diff.*;
import org.netbeans.modules.cvsclient.commands.log.*;
import org.netbeans.modules.cvsclient.commands.tag.*;
import org.netbeans.modules.cvsclient.commands.grouping.*;
import org.netbeans.modules.javacvs.commands.*;
import org.netbeans.modules.vcscore.actions.*;
import org.netbeans.modules.vcscore.grouping.*;
import org.netbeans.modules.cvsclient.commands.status.*;
import org.netbeans.modules.cvsclient.commands.update.*;
import org.netbeans.lib.cvsclient.commandLine.GetOpt;
import org.netbeans.lib.cvsclient.command.*;
import org.openide.util.SharedClassObject;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;
import org.openide.windows.WindowManager;


/** factory that creates instances of commands. Can be used for feeding 
 * the instances with default parameters etc.
 * Should be used only for default cvs commands configurable by user.
 *
 * Static methods included here handle the starting and NbGUI placement of the commands.
 * @author  mkleint
 * 
 */
public class FsCommandFactory extends JavaCvsCommandFactory {
    
   private static final int HIST_LIST_MAX_COUNT = 20; 
   private static transient HashMap historyLists; 
   
   private HashMap runtimeFolders;
   
   private CommandActionSupporter supporter = null;
   
   private PropertyChangeListener propChangeListener;
   
   private java.util.List limitedList;
   
   private JavaCvsRuntimeCommandsProvider independentRuntimeProvider = null;
   
   protected FsCommandFactory() {
       super();
       propChangeListener = new PropertyChangeListener() {
            public void propertyChange(final PropertyChangeEvent event) { 
                settingsChanged(event.getPropertyName(), event.getOldValue(), event.getNewValue());
            }
        };
        JavaCvsSettings settings = (JavaCvsSettings)SharedClassObject.findObject(
                                       JavaCvsSettings.class, true); 
        settings.addPropertyChangeListener(WeakListener.propertyChange(propChangeListener, settings));
        historyLists = new HashMap();
        runtimeFolders = new HashMap();
        limitedList = new LinkedList();
        limitedList.add(CvsCommit.class);
//        limitedList.add(CvsUpdate.class);
        limitedList.add(CvsAdd.class);
        limitedList.add(CvsRemove.class);
//        limitedList.add(CvsCheckout.class);
        limitedList.add(CvsImport.class);
        limitedList.add(CvsTag.class);
        limitedList.add(CvsExport.class);
    }
   
    public boolean showDisplayerWhenLimited(Class commandClass) {
        return !limitedList.contains(commandClass);
    }
    
    /**
     * @deprecated
     */
    public static JavaCvsCommandFactory getInstance() {
        if (instance == null) {
            instance = new FsCommandFactory();
        }
        return instance;
    }
    
    static void  uninstall() {
        instance = null;
        historyLists = null;
    }
    
    public static FsCommandFactory getFsInstance() {
        if (instance == null) {
            instance = new FsCommandFactory();
        }
        return (FsCommandFactory)instance;
    }

    public CommandActionSupporter getSupporter() {
        if (supporter == null) {
            supporter = new JavaCvsActionSupporter();
        }
        return supporter;
    }
    

     protected String getHomeString() {
          String toReturn;
          JavaCvsSettings settings = (JavaCvsSettings)SharedClassObject.findObject(
                                       JavaCvsSettings.class, true); 
          java.io.File home = settings.getHome();
          if (home != null || !home.exists()) {
              toReturn  = home.getAbsolutePath();
          } else {
          // just to make sure.. should not happen... only when someone deletes the
          // the home dir during work with NB.
            toReturn = System.getProperty("user.home"); // NOI18N
          }
          return toReturn;
     }
     
    /** run when the JavaCvsSettings object fires propertyChange
     */
    private void settingsChanged(String propName, Object oldVal, Object newVal) {
        if (propName == null) return;
        if (propName.equals(GeneralVcsSettings.PROP_HOME)) {
            File newHome = new File(newVal.toString());
            if (newHome != null) {
                location = newHome.getAbsolutePath();
                try {
                    readFromDisk();
                } catch (FileNotFoundException exc) {
                    // ignore
                } catch (IOException exc2) {
                    ErrorManager.getDefault().notify(exc2);
                }
            }
        }
    }  
    
    public void addToHistory(FileSystemCommand command) {
        FileSystemCommand histCom = null;
        try {
           histCom = (FileSystemCommand)command.getClass().newInstance();
           histCom.copySwitchesFrom(command);
           FsGlobalOptionsImpl glImpl = (FsGlobalOptionsImpl)command.getImpl().getGlobalOptions();
           GlobalOptions opts = glImpl.getLibraryGlobalOptions();
           histCom.setGlobalOptions(opts);
        } catch (InstantiationException ex) {
            return;
        } catch (IllegalAccessException exc) {
            return;
        }
        LinkedList histList = (LinkedList)historyLists.get(histCom.getClass());
        if (histList == null) {
            histList = new LinkedList();
            historyLists.put(histCom.getClass(), histList);
        }
        if (histList.size() >= HIST_LIST_MAX_COUNT) {
            histList.remove(0);
        }
        try {
            FileSystemCommand lastCom = (FileSystemCommand)histList.getLast();
            // only if the switches are different, add to history list..
            if (lastCom == null || (!lastCom.getCVSArguments().equals(histCom.getCVSArguments()))) {
                histList.add(histCom);
            }
        } catch (NoSuchElementException exc) {
            histList.add(histCom);
        }
    }
    
    public int getHistoryCount(Class comm) {
        LinkedList histList = (LinkedList)historyLists.get(comm);
        if (histList == null) {
            return 0;
        }
        return histList.size();
    }
    
    public FileSystemCommand getHistoryItem(Class comm, int index) {
        LinkedList histList = (LinkedList)historyLists.get(comm);
        if (histList == null) {
            return null;
        }
        return (FileSystemCommand)histList.get(index);
    }
    
    
    public FileSystemCommand createCommand(Class classa, 
            boolean loadDefaults, File[] files, ClientProvider provider) {
        FileSystemCommand comm = getCommand(classa, loadDefaults);
        if (comm != null) {
            comm.setClientProvider(provider);
            comm.setFiles(files);
        }
        return comm;
    }

    public FileSystemCommand createCommand(Class classa, 
           boolean loadDefaults, FileObject[] files, ClientProvider provider) {
        FileSystemCommand comm = getCommand(classa, loadDefaults);
        if (comm != null) {
            comm.setClientProvider(provider);
            comm.setFileObjects(files);
        }
        return comm;
    }
    
    
    private static Customizer findCustomizer(Object command) {
        if (command == null) {
            return null;
        }
        BeanInfo info;
        try {
            info = Introspector.getBeanInfo(command.getClass());
        } catch (IntrospectionException exc) {
            return null;
        }
        Class clazz = info.getBeanDescriptor().getCustomizerClass();
        if (clazz == null) {
            return null;
        }
        Object o;
        try {
            o = clazz.newInstance();
        } catch (InstantiationException e) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }
        if (! (o instanceof Customizer) ) {
            return null;
        }
        Customizer cust = ((java.beans.Customizer)o);
        return cust;
    }
    
    public void showCustomizerAndRun(FileSystemCommand command, boolean skipParams, boolean skipDisplayers) {
        Customizer cust = null;
        if (!skipParams) {
             cust = findCustomizer(command.getImpl());
        }
        showCustomizerAndRun(command, cust, skipDisplayers);
    }
    
    /**
     * runs commands in the list.. Expects instances of FileSystemCommand..
     * skipDisplayer.. list of Boolean values of 
     */
    public void showCustomizerAndRun(java.util.List commandList, java.util.List skipDisplayers, boolean skipParams) {
        FileSystemCommand comm = (FileSystemCommand)commandList.get(0);
        Customizer cust = null;
        if (!skipParams && comm != null) {
             cust = findCustomizer(comm.getImpl());
        }
        doShowCustomizerAndRun(commandList, cust, skipDisplayers);
    }

    public void showCustomizerAndRun(final FileSystemCommand command, Customizer cust, boolean skipDisplayers) {
       java.util.List list = new LinkedList();
       java.util.List skipList = new LinkedList();
        list.add(command);
        skipList.add(skipDisplayers ? Boolean.TRUE : Boolean.FALSE);
//        FsGlobalOptions globalCom = new FsGlobalOptionsImpl(command.getGlobalOptions());
        doShowCustomizerAndRun(list, cust, skipList);
    }
    
    public void doShowCustomizerAndRun(final java.util.List commandList, Customizer cust, final java.util.List skipDisplayers) {
        final FileSystemCommand command = (FileSystemCommand)commandList.get(0);
        final java.util.List implList = new LinkedList();
        Iterator it = commandList.iterator();
        while (it.hasNext()) {
            FileSystemCommand fsCom = (FileSystemCommand)it.next();
            implList.add(fsCom.getImpl());
        }
        Customizer glComp = findCustomizer(command.getImpl().getGlobalOptions());
        GlobalOptionsCustomizer glOptCustom = null;
        if (glComp instanceof GlobalOptionsCustomizer) {
             glOptCustom = (GlobalOptionsCustomizer)glComp;
        }
        final GlobalOptionsCustomizer finGlOptCustom = glOptCustom;
        if (glComp != null) {
            glComp.setObject(implList);
        }
        if (cust != null && cust instanceof JComponent) {
            // now create option buttons
            final Object[] paramOptions = new Object[2];
            String btnRunText = NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.RunCommandButton"); // NOI18N
            javax.swing.JButton btnRun = new javax.swing.JButton(btnRunText);
            javax.swing.JButton btnCancel = new javax.swing.JButton(NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.CancelButton")); // NOI18N
            AccessibleContext context = btnCancel.getAccessibleContext();
            context.setAccessibleDescription(NbBundle.getBundle(FsCommandFactory.class).getString("ACSD_CancelButton"));
            context = btnRun.getAccessibleContext();
            context.setAccessibleDescription(NbBundle.getBundle(FsCommandFactory.class).getString("ACSD_RunButton"));
            btnRun.setDefaultCapable(true);
            paramOptions[0] = btnRun;
            paramOptions[1] = btnCancel;
            String commandName = ""; // NOI18N
            try {
                BeanInfo beaninfo;
                beaninfo = Introspector.getBeanInfo(command.getImpl().getClass());
                if (beaninfo != null) {
                    commandName = beaninfo.getBeanDescriptor().getDisplayName();
                }
            } catch (IntrospectionException exc) {
                commandName = ""; // NOI18N
            }
            String title = NbBundle.getMessage(FsCommandFactory.class, "FsCommandFactory.CommandCustomizerTitle", commandName); // NOI18N
            
            final JComponent componenta = (JComponent)cust;
            DialogDescriptor dd = new DialogDescriptor(mainParamInput(componenta, commandList, glOptCustom), title);
            cust.setObject(implList);
            dd.setValue(btnRun);
            dd.setClosingOptions(paramOptions);
            dd.setOptions(paramOptions);
            dd.setModal(true);
            dd.setButtonListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (evt.getSource().equals(paramOptions[1])) {
                        return;
                    }
                    if (evt.getSource().equals(paramOptions[0])) {
                        Iterator it = commandList.iterator();
                        while (it.hasNext()) {
                            FileSystemCommand comm = (FileSystemCommand)it.next();
                            String rootString = comm.getGlobalOptions().getCVSRoot();
                            if (rootString != null && !rootString.equals("")) { // NOI18N
                                ((StandardClientProvider)comm.getClientProvider()).setCvsRootString(rootString);
                            } else {
                                comm.getGlobalOptions().setCVSRoot(comm.getClientProvider().getCvsRootString());
                            }
                            
/*                            FsGlobalOptionsImpl opts = new FsGlobalOptionsImpl();
                            opts.setCVSRoot(""); // NOI18N
                            finGlOptCustom.setGlobalData(opts);
                            GlobalOptions libopts = opts.getLibraryGlobalOptions();
                            if (!opts.getCVSRoot().equals("")) { // NOI18N
                                ((StandardClientProvider)comm.getClientProvider()).setCvsRootString(opts.getCVSRoot());
                            } else {
                                libopts.setCVSRoot(comm.getClientProvider().getCvsRootString());
                            }
 */
//                            comm.setGlobalOptions(libopts);
                        }
                        runCommands(commandList, skipDisplayers);
                        return;
                    }
                }
            });
            
            final java.awt.Dialog dial = DialogDisplayer.getDefault().createDialog(dd);
//            dial.setModal(true);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    componenta.requestFocus();
                    dial.show();
                }
            });
        } else {
            if (cust != null) {
                cust.setObject(commandList);
            }
            it = commandList.iterator();
            while (it.hasNext()) {
                FileSystemCommand comm = (FileSystemCommand)it.next();
                //                    comm.copySwitchesFrom(command);
                String rootString = comm.getGlobalOptions().getCVSRoot();
                if (rootString != null && !rootString.equals("")) { // NOI18N
                    ((StandardClientProvider)comm.getClientProvider()).setCvsRootString(rootString);
                } else {
                    comm.getGlobalOptions().setCVSRoot(comm.getClientProvider().getCvsRootString());
                }
            }
            runCommands(commandList, skipDisplayers);
        }
    }
    
    private void runCommands(java.util.List commandList, java.util.List skipDisplayersList) {
        Iterator it = commandList.iterator();
        Iterator it2 = skipDisplayersList.iterator();
        boolean skipDisplayers = false;
        boolean isFirstInRow = true;
        while (it.hasNext()) {
            FileSystemCommand command = (FileSystemCommand)it.next();
            Boolean skip = (Boolean)it2.next();
            skipDisplayers = skip.booleanValue();
            CommandDisplayerListener dis = null;
            //        if (!command.getClientProvider() instanceof IndependantClient) return;
            IndependantClient provider = (IndependantClient)command.getClientProvider();
            
            if (!skipDisplayers) {
                if (provider.getDisplayType() == NbJavaCvsFileSystem.DISP_TYPE_SIMPLE) {
                    dis = new CommandLineInfoPanel(command);
                } else {
                    String methodName = command.getClass().getName();
                    int ind = methodName.lastIndexOf('.');
                    methodName = "add" + methodName.substring(ind + 1) + "Displayer"; // NOI18N
                    try {
                        Method metoda = FsCommandFactory.class.getDeclaredMethod(methodName, new Class[] {command.getClass()});
                        dis = (CommandDisplayerListener)metoda.invoke(FsCommandFactory.class, new Object[] {command});
                    } catch (Exception exc) {
//DEBUG only.                        Thread.dumpStack();
// the exception is ok, IF the displayer is not defined...                        
                    }
                }
                if (dis != null) {
                    command.addDisplayerListener(dis);
                }
            }
            // error handling listener added
            dis = new ErrorLogPanel(command);
            command.addDisplayerListener(dis);
            if (isFirstInRow) {
                addToHistory(command);
                isFirstInRow = false;
            }
            command.startCommand();
        }
    }

    public static CommandDisplayerListener addCvsAnnotateDisplayer(CvsAnnotate command) {
        CommandDisplayerListener dis = null;
        dis = new AnnotateDisplayer(command);
        return dis;
    } 
    
    public static CommandDisplayerListener addCvsStatusDisplayer(CvsStatus command) {
        CommandDisplayerListener dis = null;
        dis = new StatusDisplayer(command);
        return dis;
    }    

    public static CommandDisplayerListener addCvsDiffDisplayer(CvsDiff command) {
        CommandDisplayerListener dis = null;
        if (command.isContextDiff() || command.isUnifiedDiff()) {
            command.setIncludeStatusAndCheckout(false);
            dis = new CommandLineInfoPanel(command);
            return dis;
        }
        dis = new DiffCommandDisplayer(command);
        command.setIncludeStatusAndCheckout(true);
        return dis;
    }    

    public static CommandDisplayerListener addCvsLogDisplayer(CvsLog command) {
        CommandDisplayerListener dis = null;
        dis = new LogDisplayer(command);
        return dis;
    }        
    
    public static CommandDisplayerListener addCvsRemoveDisplayer(CvsRemove command) {
        CommandDisplayerListener dis = null;
//        if (!command.getClientProvider() instanceof IndependantClient) return;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
            dis = new RemoveInfoPanel(command);
        }
        return dis;
    }   
    
    public static CommandDisplayerListener addCvsUpdateDisplayer(CvsUpdate command) {
        CommandDisplayerListener dis = null;
        if (command.isPipeToOutput()) {
            dis = new PipeToOutputDisplayer(command);
        } else {
//        if (!command.getClientProvider() instanceof IndependantClient) return;
            IndependantClient provider = (IndependantClient)command.getClientProvider();
            if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
                 dis = new UpdateInfoPanel(command);
            }
        }
        return dis;
    }
    
    public static CommandDisplayerListener addCvsImportDisplayer(CvsImport command) {
        CommandDisplayerListener dis = null;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
              dis = new CommandLineInfoPanel(command);
        }
        return dis;
    }

    public static CommandDisplayerListener addCvsExportDisplayer(CvsExport command) {
        CommandDisplayerListener dis = null;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
              dis = new CommandLineInfoPanel(command);
        }
        return dis;
    }    
    
    public static CommandDisplayerListener addCvsCheckoutDisplayer(CvsCheckout command) {
        CommandDisplayerListener dis = null;
        if (command.isShowModules() || command.isShowModulesWithStatus()) {
            dis = new ModulesListPanel();
        }
        else if (command.isPipeToOutput()) {
            dis = new PipeToOutputDisplayer(command);
        } else {
//            if (!command.getClientProvider() instanceof IndependantClient) return;
            IndependantClient provider = (IndependantClient)command.getClientProvider();
            if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
                 dis = new CommandLineInfoPanel(command);
            }
        }
        return dis;
    }
    public static CommandDisplayerListener addCvsTagDisplayer(CvsTag command) {
        CommandDisplayerListener dis = null;
//        if (!command.getClientProvider() instanceof IndependantClient) return;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
            dis = new CommandLineInfoPanel(command);
        }
        return dis;
    }
    public static CommandDisplayerListener addCvsCommitDisplayer(CvsCommit command) {
        CommandDisplayerListener dis = null;
//        if (!command.getClientProvider() instanceof IndependantClient) return;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
            dis = new CommitInfoPanel(command);
        }
        return dis;
    }    
    public static CommandDisplayerListener addCvsAddDisplayer(CvsAdd command) {
        CommandDisplayerListener dis = null;
//        if (!command.getClientProvider() instanceof IndependantClient) return;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_LIMITED) {
            dis = new AddInfoPanel(command);
        }
        return dis;
    }

    public static CommandDisplayerListener addCvsHistoryDisplayer(CvsHistory command) {
        CommandDisplayerListener dis = new CommandLineInfoPanel(command);
        return dis;
    }

  
    
    public static CommandDisplayerListener addCvsWatchersDisplayer(CvsWatchers command) {
        // temporary command-line display.. 
        CommandDisplayerListener dis = new CommandLineInfoPanel(command);
        return dis;
    }
    
    private JPanel mainParamInput(JComponent concrete, final java.util.List commandList, GlobalOptionsCustomizer globalCust) {
        final FileSystemCommand fCommand = (FileSystemCommand)commandList.get(0);
        final GlobalOptionsCustomizer globalCustom = globalCust;
        JPanel main = new JPanel();
        JPanel pnlButtons = new javax.swing.JPanel();
        final JTabbedPane tbTabs = new JTabbedPane();
        java.util.ResourceBundle bundle = NbBundle.getBundle(FsCommandFactory.class);
        tbTabs.addTab(fCommand.getName(), concrete);
        tbTabs.setTabPlacement(SwingConstants.BOTTOM);
        if (globalCustom == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "error while getting customizer for global options.."); //NOI18N
        } else {
            tbTabs.addTab(bundle.getString("FsCommandFactory.globalLabel"), (Component)globalCustom); // NOI18N
        }
        
        final Customizer custom = (Customizer)concrete;
        final JTextField txCommLineEq = new JTextField();
        JLabel lblCommLineEq = new JLabel();
        JButton btnDefaultSwitches = new javax.swing.JButton();
        javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JSeparator jSeparator2 = new javax.swing.JSeparator();
        final JButton btnPrev = new JButton();
        final JButton btnNext = new JButton();
        // ---------------- generated stuff

        btnPrev.setText(bundle.getString("FsCommandFactory.btnPrev")); // NOI18N
//        btnPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/netbeans/modules/cvsclient/.gif")));
        btnPrev.setToolTipText(bundle.getString("FsCommandFactory.btnPrev.tooltip")); // NOI18N
        btnPrev.setMnemonic(bundle.getString("FsCommandFactory.btnPrev.mnemonic").charAt (0)); // NOI18N
        btnNext.setText(bundle.getString("FsCommandFactory.btnNext")); // NOI18N
//        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/netbeans/modules/cvsclient/.gif")));
        btnNext.setToolTipText(bundle.getString("FsCommandFactory.btnNext.tooltip")); // NOI18N
        btnNext.setMnemonic(bundle.getString("FsCommandFactory.btnNext.mnemonic").charAt (0)); // NOI18N
        
        main.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gbc;
        gbc = new java.awt.GridBagConstraints();
        
        pnlButtons.setLayout(new java.awt.GridBagLayout());

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = gbc.WEST;
        gbc.insets = new java.awt.Insets(12, 12, 0, 0);
        pnlButtons.add(btnPrev, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new java.awt.Insets(12, 2, 0, 0);
        pnlButtons.add(btnNext, gbc);
        
        btnDefaultSwitches.setText(bundle.getString("FsCommandFactory.cbDefaultSwitches.text")); // NOI18N
        btnDefaultSwitches.setToolTipText(bundle.getString("FsCommandFactory.cbDefaultSwitches.tooltip")); // NOI18N
        btnDefaultSwitches.setMnemonic(bundle.getString("FsCommandFactory.cbDefaultSwitches.mnemonic").charAt (0)); // NOI18N
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new java.awt.Insets(12, 5, 0, 11);
        pnlButtons.add(btnDefaultSwitches, gbc);
 
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.insets = new java.awt.Insets(0, 0, 0, 0);
        main.add(pnlButtons, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.fill = gbc.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new java.awt.Insets(12, 12, 0, 11);
        main.add(tbTabs, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        gbc.insets = new java.awt.Insets(12, 12, 0, 0);
        lblCommLineEq.setText(bundle.getString("FsCommandFactory.btnCommandLine.text")); // NOI18N
        lblCommLineEq.setDisplayedMnemonic(bundle.getString("FsCommandFactory.btnCommandLine.mnemonic").charAt(0)); // NOI18N
        lblCommLineEq.setLabelFor(txCommLineEq);        
        main.add(lblCommLineEq, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.fill = gbc.HORIZONTAL;
        gbc.insets = new java.awt.Insets(12, 12, 0, 11);
        txCommLineEq.setEditable(false);        
        main.add(txCommLineEq, gbc);
        
        //--------------- generated stuff
        custom.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent event) {
                doShowCommandLine(event, txCommLineEq);
            }
        });
        globalCustom.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent event) {
                doShowCommandLine(event, txCommLineEq);
            }
        });
        btnDefaultSwitches.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if (tbTabs.getSelectedIndex() == 0) {
                    doSaveAsDefault(fCommand);
                } else {
                    FsGlobalOptionsImpl opts = new FsGlobalOptionsImpl();
                    opts.setCVSRoot(""); // NOI18N
                    globalCustom.setGlobalData(opts);
                    doSaveAsDefault(opts);
                }
            }
        });
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doNextCommand(commandList, btnPrev, btnNext, custom, globalCustom);
            }
        });
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doPrevCommand(commandList, btnPrev, btnNext, custom, globalCustom);
            }
        });
        btnNext.setEnabled(false);
        int histCount = getHistoryCount(fCommand.getClass());
        if (histCount == 0) {
            btnPrev.setEnabled(false);
        }
        btnNext.putClientProperty("HistIndex", new Integer(histCount)); // NOI18N
        btnNext.putClientProperty("HistCount", new Integer(histCount)); // NOI18N
        FileSystemCommand histCom = null;
        try {
           histCom = (FileSystemCommand)fCommand.getClass().newInstance();
           histCom.copySwitchesFrom(fCommand);
           // copy the global options.. we need 2 different instances..
           FsGlobalOptionsImpl glImpl = (FsGlobalOptionsImpl)fCommand.getImpl().getGlobalOptions();
           histCom.setGlobalOptions(glImpl.getLibraryGlobalOptions());
           btnNext.putClientProperty("HistLastCommand", histCom); // NOI18N
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException exc) {
        }
        
        //a11y description 
        AccessibleContext context = main.getAccessibleContext();
        context.setAccessibleDescription(concrete.getAccessibleContext().getAccessibleDescription());
        
        context = btnPrev.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("FsCommandFactory.btnPrev.tooltip"));
        
        context = btnNext.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("FsCommandFactory.btnNext.tooltip"));
                
        context = btnDefaultSwitches.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("FsCommandFactory.cbDefaultSwitches.tooltip"));
        
        context = txCommLineEq.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_FsCommandFactory.txCommLineEq"));

        context = tbTabs.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_FsCommandFactory.tbTabs"));        
        context.setAccessibleName(bundle.getString("ACSN_FsCommandFactory.tbTabs"));
        
        return main;
    }
    
    private void doPrevCommand(java.util.List commList, JButton btnPrev, JButton btnNext, Customizer cust, Customizer glCust) {
        Integer itemId = (Integer)btnNext.getClientProperty("HistIndex"); // NOI18N
        int index = itemId.intValue();
        btnNext.setEnabled(true);
        index = index - 1;
        if (index == 0) {
            btnPrev.setEnabled(false);
        }
        FileSystemCommand prev = getHistoryItem(commList.get(0).getClass(), index);
        LinkedList implList = new LinkedList();
        Iterator it = commList.iterator();
        while (it.hasNext()) {
            FileSystemCommand comm = (FileSystemCommand)it.next();
            comm.copySwitchesFrom(prev);
            implList.add(comm.getImpl());
            FsGlobalOptionsImpl globalCom = (FsGlobalOptionsImpl)prev.getImpl().getGlobalOptions();
            GlobalOptions opts = globalCom.getLibraryGlobalOptions();
            comm.setGlobalOptions(opts);
        }
        cust.setObject(implList);
        glCust.setObject(implList);
        
        btnNext.putClientProperty("HistIndex", new Integer(index)); // NOI18N
    }

    private void doNextCommand(java.util.List commList, JButton btnPrev, JButton btnNext, Customizer cust, Customizer glCust) {
        Integer itemId = (Integer)btnNext.getClientProperty("HistIndex"); // NOI18N
        Integer itemCount = (Integer)btnNext.getClientProperty("HistCount"); // NOI18N
        int index = itemId.intValue();
        btnPrev.setEnabled(true);
        index = index + 1;
        FileSystemCommand next = null;
        if (index == itemCount.intValue()) {
            btnNext.setEnabled(false);
            FileSystemCommand last = (FileSystemCommand)btnNext.getClientProperty("HistLastCommand"); // NOI18N
            next = last;
        } else {
            next = getHistoryItem(commList.get(0).getClass(), index);
        }
        Iterator it = commList.iterator();
        LinkedList implList = new LinkedList();
        while (it.hasNext()) {
            FileSystemCommand comm = (FileSystemCommand)it.next();
            comm.copySwitchesFrom(next);
            implList.add(comm.getImpl());
            FsGlobalOptionsImpl globalCom = (FsGlobalOptionsImpl)next.getImpl().getGlobalOptions();
            comm.setGlobalOptions(globalCom.getLibraryGlobalOptions());
        }
        cust.setObject(implList);
        glCust.setObject(implList);
        
        btnNext.putClientProperty("HistIndex", new Integer(index)); // NOI18N
    }
    
    private void doShowCommandLine(java.beans.PropertyChangeEvent event, JTextField field) {
        if (CustomizerPropChangeSupport.PROPERTY_CVS_COMMAND_LINE.equals(event.getPropertyName())) {
            String oldVal = event.getOldValue().toString();
            String newVal = event.getNewValue().toString();
            if (!oldVal.equals(newVal)) {
                newVal = newVal.replace('\n', ' ');
                int firstOcc = newVal.indexOf('"'); // NOI18N
                int lastOcc = newVal.lastIndexOf('"'); // NOI18N
                if (lastOcc > firstOcc) {
                    if ((lastOcc - firstOcc) > 10) {
                        String fin = newVal.substring(0,firstOcc + 6);
                        String fin2 = newVal.substring(lastOcc, newVal.length());
                        newVal = fin + "..." + fin2; //NOI18N
                    }
                }
                field.putClientProperty(CustomizerPropChangeSupport.PROPERTY_CVS_COMMAND_LINE, newVal);
                Object glSwitches = field.getClientProperty(CustomizerPropChangeSupport.PROPERTY_GLOBAL_COMMAND_LINE);
                if (glSwitches != null) {
                    newVal = glSwitches.toString() + " " + newVal; // NOI18N
                }
                field.setText(newVal);
            }
        }
        if (CustomizerPropChangeSupport.PROPERTY_GLOBAL_COMMAND_LINE.equals(event.getPropertyName())) {
            String oldVal = event.getOldValue().toString();
            String newVal = event.getNewValue().toString();
            if (!oldVal.equals(newVal)) {
                field.putClientProperty(CustomizerPropChangeSupport.PROPERTY_GLOBAL_COMMAND_LINE, newVal);
                Object commSwitches = field.getClientProperty(CustomizerPropChangeSupport.PROPERTY_CVS_COMMAND_LINE);
                if (commSwitches != null) {
                    newVal = newVal + " " + commSwitches.toString(); // NOI18N
                }
                field.setText(newVal);
            }
        }
    }
    
    private static void doSaveAsDefault(FileSystemCommand command) {
        boolean globalDefault = false;
        String message = NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.globalDefaultQuestion"); // NOI18N
        NotifyDescriptor nd = new NotifyDescriptor.Confirmation(message, org.openide.NotifyDescriptor.OK_CANCEL_OPTION);
        Object returnValue = DialogDisplayer.getDefault().notify(nd);
//        System.out.println("return value=" + returnValue.getClass().getName() + " " + returnValue.toString());
        JavaCvsCommandFactory fact = command.getClientProvider().getCommandFactory();
        if (NotifyDescriptor.Confirmation.CANCEL_OPTION.equals(returnValue)) {
            return;
        }    
        if (NotifyDescriptor.Confirmation.OK_OPTION.equals(returnValue)) {
            try {
//                String arguments = command.getCVSArguments();
//                System.out.println("arguments=" + arguments);
                FileSystemCommand newComm = fact.getCommand(command.getClass(), false);
                newComm.copySwitchesFrom(command);
//                System.out.println("def. object=" + newComm.getCvsrcEntry());
                fact.putCommand(command.getClass(), newComm);
                fact.writeToDisk(false);
            } catch (java.io.IOException ioExc) {
                ErrorManager.getDefault().annotate(ioExc, NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.cannotWriteDefaultSwitches"));
            }
        }
    }    

    private static void doSaveAsDefault(FsGlobalOptionsImpl globs) {
        boolean globalDefault = false;
        String message = NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.globalDefaultQuestionForGlOptions"); // NOI18N
        NotifyDescriptor nd = new NotifyDescriptor.Confirmation(message, org.openide.NotifyDescriptor.OK_CANCEL_OPTION);
        Object returnValue = DialogDisplayer.getDefault().notify(nd);
        if (NotifyDescriptor.Confirmation.CANCEL_OPTION.equals(returnValue)) {
            return;
        }    
        if (NotifyDescriptor.Confirmation.OK_OPTION.equals(returnValue)) {
            try {
                FsCommandFactory.getFsInstance().setGlobalOptions(globs);
                FsCommandFactory.getFsInstance().writeToDisk(false);
            } catch (java.io.IOException ioExc) {
                ErrorManager.getDefault().annotate(ioExc, NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.cannotWriteDefaultSwitches"));
            }
        }
    }    
    
        /** a generic procedure of displaying results in the javacvs Mode.
     * If the subclass wants to change that behaviour for whatever reason, it can change it.
     * Recommended to keep it this way for UI reasons.
     */
    public static void displayOutputPanel(final String title, final PersistentCommandDisplayer component) {
        JavaCvsTopComponent comp = findJavaCvsTopComponent(component);
        if (comp == null) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    org.openide.windows.Workspace workspace = WindowManager.getDefault().getCurrentWorkspace();
                    org.openide.windows.Mode javaMode = NbJavaCvsFileSystem.getDockingMode(workspace);
                    JavaCvsTopComponent comp = new JavaCvsTopComponent();
                    javax.accessibility.AccessibleContext context = comp.getAccessibleContext();
                    javax.accessibility.AccessibleContext compContext = component.getComponent().getAccessibleContext();
                    context.setAccessibleDescription(compContext.getAccessibleDescription());
            
                    comp.setName(title);
                    comp.setLayout(new BorderLayout());
                    comp.addDisplayer(component);
                    javaMode.dockInto(comp);
                    comp.open();
                }
            });
        }
        else {
            comp.requestFocus();
        }
    }

    public static JavaCvsTopComponent findJavaCvsTopComponent(PersistentCommandDisplayer displayer) {
        java.util.Set components = org.openide.windows.TopComponent.getRegistry().getOpened();
        if (components == null || components.size() == 0) return null;
        Iterator it = components.iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (next instanceof JavaCvsTopComponent) {
                JavaCvsTopComponent jcTop = (JavaCvsTopComponent)next;
                if (jcTop.getDisplayer().equals(displayer)) {
                    return jcTop;
                }
            }
        }
        return null;
        
    }
    
    public static PersistentCommandDisplayer findOpenDisplayer(File file, Class type, Object additionalInfo) {
        java.util.Set components = TopComponent.getRegistry().getOpened();
        if (components == null || components.size() == 0) return null;
        PersistentCommandDisplayer toReturn = null;
        Iterator it = components.iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (next instanceof JavaCvsTopComponent) {
                JavaCvsTopComponent jcTop = (JavaCvsTopComponent)next;
                toReturn = jcTop.getEqualDisplayer(file, type, additionalInfo);
                if (toReturn != null) {
                    return toReturn;
                }
            }
        }
        return toReturn;
    }
    
    /** a generic procedure for displaying short-term results like the Update command output
     *
     */
    public static Dialog createDialog(JPanel panel, String title) {
        DialogDescriptor dd = new DialogDescriptor(panel, title);
        Object[] options = new Object[1];
        JButton btnClose = new JButton(NbBundle.getBundle(FsCommandFactory.class)
                           .getString("FsCommandFactory.closeButton")); // NOI18N
        AccessibleContext context = btnClose.getAccessibleContext();
        context.setAccessibleDescription(NbBundle.getBundle(FsCommandFactory.class).getString("ACSD_CloseButton"));
        btnClose.setDefaultCapable(true);
        options[0] = btnClose;
        dd.setValue(btnClose);
        dd.setOptions(options);
        dd.setClosingOptions(null); // all are closing
        Dialog dial = DialogDisplayer.getDefault().createDialog(dd);
        dial.setModal(false);
        return dial;
    }

    /*******************************************************************
 * runtime related stuff..
 *******************************************************************/    

   public void addIndependantRuntime(FileSystemCommand comm) {
       //String indName = NbBundle.getBundle(FsCommandFactory.class).getString("FsCommandFactory.IndependantRuntimeFolder");
       //RuntimeSupport.getInstance().initRuntime(indName);
       synchronized (this) {
           if (independentRuntimeProvider == null) {
               independentRuntimeProvider = new JavaCvsRuntimeCommandsProvider(null);
           }
       }
       JavaCvsRuntimeCommand rCom = new JavaCvsRuntimeCommand(comm, independentRuntimeProvider);
       independentRuntimeProvider.updateCommand(rCom);
       comm.addDisplayerListener(rCom);
       comm.addCommandErrorListener(rCom);
   }
   

   
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.