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.
 *//*
 * ServerTargetSelector.java
 *
 * Created on October 31, 2002, 3:00 PM
 */

package org.netbeans.modules.j2ee.deployment.impl.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
import java.awt.Dialog;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.tree.TreeSelectionModel;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import org.netbeans.modules.j2ee.deployment.impl.*;
import org.openide.nodes.Node;
import org.openide.nodes.Children;
import org.openide.ErrorManager;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.loaders.FolderLookup;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.HelpCtx;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerPanel;
import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
import org.openide.explorer.propertysheet.ExPropertyEditor;
import org.openide.explorer.view.BeanTreeView;
import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;

/**
 *
 * @author  Jeri Lockhart
 */
public class ServerTargetSelector extends javax.swing.JPanel implements EnhancedCustomPropertyEditor {


    private String		  subTitleText;
    private ServerInstance	  curInstance;
    private ArrayList             curTargets;
    private Node[]		  curNodes;
    private ServerRegistryNode svrRegNode = null;
    private Dialog		  dialog;
    private DialogDescriptor      dialogDescriptor;
    private PropertyChangeSupport propSupp;
    private String subTitleMnemonic, subTitleA11yDesc, noInstalledTargetsMsg;
    private boolean hasRegisteredServers = true;
    private JLabel                 errorLbl;

    /** Creates new form ServerTargetSelector */
    public ServerTargetSelector() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents

        setLayout(new java.awt.GridBagLayout());

    }//GEN-END:initComponents

    private void initComponentsMore () {
        getAccessibleContext().setAccessibleName(NbBundle.getMessage(ServerTargetSelector.class, "LBL_ServerTargetSelector"));
        FileSystem defaultFileSystem = Repository.getDefault().getDefaultFileSystem();
        JLabel subTitle = null;
        int gridY = 0;
        GridBagConstraints gbc;

        ServerRegistry svrReg = ServerRegistry.getInstance();
        if (svrReg == null) {
            // No ServerRegistry
            formatNoServersErrorDialog();
            return;
        }
        Collection instances = svrReg.getInstances();
        if (instances.isEmpty()) {
            // No ServerInstances
            formatNoServersErrorDialog();
            return;
        }

        // Get rootNode (ServerRegistryNode) for explorer view
        svrRegNode = ServerRegistryNode.getServerRegistryNode();
        if (svrRegNode == null ) {// ServerRegistryNode not found
            // No registered servers
            formatNoServersErrorDialog();
            return;
        };
        
        svrRegNode.setExpandablePassTargetNode(false);
        
        if (subTitleText != null) {
            getAccessibleContext().setAccessibleDescription(subTitleText);
            subTitle = new javax.swing.JLabel();
            // If the subtitle is too long, split it into two
            if (subTitleText.length() > 110) {
                int mid = subTitleText.length() / 2;
                String temp =   "" +                                                      //NOI18N
                                subTitleText.substring(0, subTitleText.indexOf(" ", mid)) +     //NOI18N
                                "
" + //NOI18N subTitleText.substring(subTitleText.indexOf(" ", mid) + 1) + //NOI18N ""; //NOI18N subTitle.setText(temp); }else { subTitle.setText(subTitleText); } if (subTitleMnemonic != null) { subTitle.setDisplayedMnemonic(subTitleMnemonic.charAt(0)); } if (subTitleA11yDesc != null) { subTitle.getAccessibleContext().setAccessibleDescription(subTitleA11yDesc); } gbc = new GridBagConstraints (); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = new Insets (12, 12, 0, 12); add(subTitle, gbc); } Node rootNode = svrRegNode; curNodes = null; if (curTargets != null) { curNodes = findTargetNodes (rootNode, curTargets); } ExplorerManager mgr = new ExplorerManager (); mgr.addPropertyChangeListener (new PropertyChangeListener () { public void propertyChange (PropertyChangeEvent evt) { if (evt.getPropertyName ().equals (ExplorerManager.PROP_SELECTED_NODES)) { // System.out.println("ServerTargetSelector: nodes selected."); Node [] nodes = (Node []) evt.getNewValue (); Boolean selOK = Boolean.FALSE; curTargets = new ArrayList(); curNodes = null; ServerInstance instance = null; curNodes = nodes ; String msg = " "; //NOI18N if (curNodes != null) { // Check that the selected nodes are all targets under the same instance for (int i = 0; i< curNodes.length; i++) { ServerTarget targ = (ServerTarget) curNodes [i].getCookie (ServerTarget.class); if (targ == null) { // Not a target, clear selection, disable ok button if (curNodes.length == 1) { msg = NbBundle.getMessage(ServerTargetSelector.class, "MSG_Selected_Single_Not_Target"); } else { msg = NbBundle.getMessage(ServerTargetSelector.class, "MSG_Selected_Multi_Not_Target"); } curTargets.clear(); break; } else { if (instance == null) { // Get the server instance instance = targ.getInstance(); } ServerInstance tempInstance = targ.getInstance(); String instanceUrl = instance.getUrl(); String tempInstanceUrl = tempInstance.getUrl(); if (instanceUrl == null || tempInstanceUrl == null) { // Can't select target with null url if (curNodes.length == 1) { msg = NbBundle.getMessage(ServerTargetSelector.class, "MSG_Selected_Target_Has_Null_Url"); } else { msg = NbBundle.getMessage(ServerTargetSelector.class, "MSG_Selected_Targets_Have_Null_Url"); } curTargets.clear(); break; } if (instanceUrl.equals(tempInstanceUrl)) { curTargets.add(targ); } else { // Can't select target of a different instance, quit msg = NbBundle.getMessage(ServerTargetSelector.class, "MSG_Selected_Targets_Not_Same_Instance"); curTargets.clear(); break; } } // end else (targ == null) }// end for loop if (curTargets.isEmpty() && curNodes.length == 1) { curInstance = (ServerInstance) curNodes[0].getCookie(ServerInstance.class); } }// end if curNodes == null if (curTargets.isEmpty() && curInstance == null) { selOK = Boolean.FALSE; errorLbl.setText(msg); } else { selOK = Boolean.TRUE; errorLbl.setText(" "); //NOI18N } enableOKButton (selOK); repaint(); } } }); mgr.setRootContext (rootNode); ExplorerPanel expPanel = new ExplorerPanel (mgr); expPanel.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ServerTargetSelector.class, "LBL_Server_Registry_Explorer_Panel")); expPanel.setPreferredSize (new Dimension (300, 200)); BeanTreeView tView = new BeanTreeView () { public void addNotify () { tree.getSelectionModel ().setSelectionMode (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); super.addNotify (); } }; tView.setPopupAllowed (false); tView.setDefaultActionAllowed (false); tView.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ServerTargetSelector.class, "LBL_Server_Registry_Explorer")); expPanel.add (tView, "Center"); //NOI18N if (subTitle != null) { subTitle.setLabelFor(expPanel); } gbc = new GridBagConstraints (); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = new Insets (12, 12, 12, 12); gbc.fill = GridBagConstraints.BOTH; errorLbl = new JLabel(" "); errorLbl.setForeground(new java.awt.Color(255, 0, 51)); errorLbl.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ServerTargetSelector.class, "LBL_Explorer_Selection_Error_Feedback_Label")); add (expPanel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = GridBagConstraints.REMAINDER; gbc.anchor = java.awt.GridBagConstraints.WEST; gbc.insets = new Insets (0, 12, 0, 12); gbc.fill = GridBagConstraints.HORIZONTAL; add(errorLbl, gbc); if (curNodes != null) { try { mgr.setSelectedNodes (curNodes ); } catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); } } } private void formatNoServersErrorDialog() { // Show the dialog but with only the error message in the middle // System.out.println("formatNoServersErrorDialog"); hasRegisteredServers = false; // flag to treat the OK button like cancel if (noInstalledTargetsMsg == null) { noInstalledTargetsMsg = NbBundle.getMessage (ServerTargetSelector.class, "MSG_NoTargets"); } JLabel l = new JLabel(noInstalledTargetsMsg); GridBagConstraints gbc = new GridBagConstraints (); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets (12, 12, 12, 12); add (l, gbc); } private Node[] findTargetNodes (Node root, ArrayList targets) { Children ch = root.getChildren(); Node[] n = ch.getNodes(); if (n == null || n.length < 1) { return null; } ArrayList selNodes = new ArrayList(); for (int i = 0; i < n.length; i++) { ServerTarget target = (ServerTarget) n[i].getCookie(ServerTarget.class); if (target != null) { // Compare this target with the default targets (in the array) by name and instance url Iterator it = targets.iterator(); while (it.hasNext()) { ServerTarget tempTarget = (ServerTarget)it.next(); // check for null url (test server instance has null url) String targetUrl = target.getInstance().getUrl(); String tempTargetUrl = tempTarget.getInstance().getUrl(); if (targetUrl == null) { targetUrl = ""; //NOI18N } if (tempTargetUrl == null) { tempTargetUrl = ""; //NOI18N } if ( target.getName().equals(tempTarget.getName()) && targetUrl.equals(tempTargetUrl)) { selNodes.add(n[i]); } } } else { Node[] tempArr = findTargetNodes(n[i], targets); if (tempArr != null && tempArr.length > 0) { for (int j =0; j < tempArr.length; j++) { selNodes.add(tempArr[j]); } } } } if (!selNodes.isEmpty()) { return (Node[])selNodes.toArray(new Node[selNodes.size()] ); } return null; } private void enableOKButton (Boolean b) { if (propSupp != null) { propSupp.firePropertyChange (ExPropertyEditor.PROP_VALUE_VALID, null, b); } if (dialogDescriptor != null) { dialogDescriptor.setValid(b.booleanValue()); } } /** Get the customized property value. * * @return The currently selected Server Instance. This * is a org.netbeans.modules.j2ee.server.ServerInstanceData * object. Note that if the user has selected an invalid * node or has not made any selection, null will be * returned. * @exception InvalidStateException when the custom property editor does not contain a valid property value * (and thus it should not be set) */ public Object getPropertyValue () { if (curTargets == null) { NotifyDescriptor.Message msg = new NotifyDescriptor.Message (NbBundle.getMessage (ServerTargetSelector.class, "MSG_NoTargetSelected"), //NOI18N NotifyDescriptor.ERROR_MESSAGE); propSupp.firePropertyChange (ExPropertyEditor.PROP_VALUE_VALID, msg, Boolean.FALSE); throw new IllegalStateException (); } return getServerTargets(); } public void addNotify () { initComponentsMore (); super.addNotify (); } private void closeDialog () { dialog.setVisible (false); dialog.dispose (); dialog = null; svrRegNode.setExpandablePassTargetNode(true); } /** * Embeds this component in a DialogDescriptor and displays * it in a java.awt.Dialog with two buttons, OK and Cancel. * If the user selects OK but has made an invalid selection or * no selection, an error dialog will be displayed and the dialog * will not disappear. * * @param title Title to be used on the dialog. * * @return true if the user has made a valid choice and * selected the OK button. * false if the user selected Cancel. */ public boolean showDialog (String title) { return showDialog (title, null); } public boolean showDialog (String title, String helpId) { ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent evt) { Object o = evt.getSource (); if (o == NotifyDescriptor.CANCEL_OPTION || hasRegisteredServers == false ) { closeDialog (); return; } if (curNodes == null ) { DialogDisplayer.getDefault ().notify (new NotifyDescriptor.Message (NbBundle.getMessage (ServerTargetSelector.class, "MSG_NoTargetSelected"), //NOI18N NotifyDescriptor.ERROR_MESSAGE)); } else if (curTargets == null) { // This shoudln't happen DialogDisplayer.getDefault ().notify (new NotifyDescriptor.Message (NbBundle.getMessage (ServerTargetSelector.class, "MSG_NotServerTarget"), //NOI18N NotifyDescriptor.ERROR_MESSAGE)); } else { closeDialog (); } } }; dialogDescriptor = new DialogDescriptor (this, title, true, listener); if (helpId!=null) dialogDescriptor.setHelpCtx(new HelpCtx(helpId)); dialogDescriptor.setClosingOptions (new Object [] { NotifyDescriptor.CANCEL_OPTION }); dialog = DialogDisplayer.getDefault ().createDialog (dialogDescriptor); dialog.setVisible (true); if (dialogDescriptor.getValue () == NotifyDescriptor.OK_OPTION && hasRegisteredServers == true) { return true; } dialogDescriptor = null; return false; } /** * Retreives the currently selected Server Target objects. * * @return The currently selected Server Target objects. * Note that if the user has selected an invalid * node or has not made any selection, null will be * returned. */ public ServerTarget[] getServerTargets () { if (curTargets == null) { return null; } return (ServerTarget[])curTargets.toArray(new ServerTarget[curTargets.size()]); } public ServerInstance getServerInstance() { return curInstance; } /** * Set a subtitle (line of text) to be displayed above the * server selection area. * * @param subtitle Text to display */ public void setText (String subtitle) { this.subTitleText = subtitle; } /** * Set a mnemonic on the subtitle label * * @param mnemonic string (the first character will be used) */ public void setTextMnemonic(String mnemonic) { subTitleMnemonic = mnemonic; } /** * Set a accessible description on the subtitle * * @param an accessible description string */ public void setTextA11yDesc(String desc) { subTitleA11yDesc = desc; } /** * Set a plug-in specific message instructing the user about what they should * do if there are no installed servers or server targets in the IDE * * @param error message string for "no server targets found" condition */ public void setNoInstalledTargetsMsg(String message) { noInstalledTargetsMsg = message; } public void setServerTargets (ServerTarget[] targets ) { if (targets != null && targets.length > 0) { if (curTargets == null) { curTargets = new ArrayList(); } for (int i =0; i < targets.length; i++) { curTargets.add(targets[i]); } } } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables }
... 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.