~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/groovy/groovyproject/src/org/netbeans/modules/groovy/groovyproject/ui/customizer/VisualMainScriptSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.groovy.groovyproject.ui.customizer;
 
43
 
 
44
import java.awt.*;
 
45
import java.awt.event.ActionEvent;
 
46
import java.awt.event.ActionListener;
 
47
import java.awt.event.MouseEvent;
 
48
import java.io.File;
 
49
import java.util.ArrayList;
 
50
import java.util.Enumeration;
 
51
import java.util.HashMap;
 
52
import java.util.Iterator;
 
53
import java.util.List;
 
54
import javax.swing.*;
 
55
import javax.swing.event.ChangeEvent;
 
56
import javax.swing.event.ChangeListener;
 
57
import javax.swing.event.DocumentEvent;
 
58
import javax.swing.event.DocumentListener;
 
59
import javax.swing.event.ListSelectionEvent;
 
60
import javax.swing.event.ListSelectionListener;
 
61
import javax.swing.text.Document;
 
62
import org.netbeans.api.project.ant.AntArtifact;
 
63
import org.netbeans.api.project.libraries.Library;
 
64
import org.openide.DialogDisplayer;
 
65
import org.openide.NotifyDescriptor;
 
66
import org.openide.DialogDescriptor;
 
67
import org.openide.awt.MouseUtils;
 
68
import org.openide.filesystems.FileObject;
 
69
import org.openide.util.NbBundle;
 
70
 
 
71
/** Represents field for main script name and the button to main script chooser.
 
72
 * of classpath editing controls.
 
73
 *
 
74
 * @author Jiri Rechtacek
 
75
 */
 
76
final class VisualMainScriptSupport {
 
77
    
 
78
    private final JTextField mainScriptTextField;
 
79
    private final JButton chooseMainScriptButton;
 
80
    private final FileObject sourceRoot;
 
81
    
 
82
    private final ArrayList actionListeners = new ArrayList();
 
83
    
 
84
    public VisualMainScriptSupport (JTextField mainScriptTextField, JButton chooseMainScriptButton, FileObject sourceRoot) {
 
85
 
 
86
        this.mainScriptTextField = mainScriptTextField;
 
87
        this.chooseMainScriptButton = chooseMainScriptButton;
 
88
        this.sourceRoot = sourceRoot;
 
89
                                     
 
90
        // Register the button listener
 
91
        MainScriptListener actionListener = new MainScriptListener ();
 
92
        
 
93
        chooseMainScriptButton.addActionListener (actionListener);
 
94
        mainScriptTextField.getDocument ().addDocumentListener (actionListener);
 
95
    } 
 
96
    
 
97
    public void setMainScript (String mainScript) {
 
98
        mainScriptTextField.setText (mainScript);
 
99
    }
 
100
    
 
101
    public String getMainScript () {
 
102
        return mainScriptTextField == null ? "" : mainScriptTextField.getText (); // NOI18N
 
103
    }
 
104
    
 
105
    /** Action listeners will be informed when the value of the
 
106
     * list changes.
 
107
     */
 
108
    public void addActionListener( ActionListener listener ) {
 
109
        actionListeners.add( listener );
 
110
    }
 
111
    
 
112
    public void removeActionListener( ActionListener listener ) {
 
113
        actionListeners.remove( listener );
 
114
    }
 
115
    
 
116
    private void fireActionPerformed() {
 
117
        ArrayList listeners;
 
118
        
 
119
        synchronized ( this ) {
 
120
             listeners = new ArrayList( actionListeners );
 
121
        }
 
122
        
 
123
        ActionEvent ae = new ActionEvent( this, 0, null );
 
124
        
 
125
        for( Iterator it = listeners.iterator(); it.hasNext(); ) {
 
126
            ActionListener al = (ActionListener)it.next();
 
127
            al.actionPerformed( ae );
 
128
        }
 
129
        
 
130
    }
 
131
        
 
132
    // Private methods ---------------------------------------------------------
 
133
 
 
134
    // Private innerclasses ----------------------------------------------------
 
135
    
 
136
    private class MainScriptListener implements ActionListener, DocumentListener {
 
137
        private final JButton okButton = new JButton (NbBundle.getMessage (VisualMainScriptSupport.class, "LBL_ChooseMainScript_OK"));
 
138
     
 
139
        // Implementation of ActionListener ------------------------------------
 
140
        
 
141
        /** Handles button events
 
142
         */        
 
143
        public void actionPerformed( ActionEvent e ) {
 
144
            
 
145
            // only chooseMainScriptButton can be performed
 
146
            
 
147
            final MainScriptChooser panel = new MainScriptChooser (sourceRoot);
 
148
            Object[] options = new Object[] {
 
149
                okButton,
 
150
                DialogDescriptor.CANCEL_OPTION
 
151
            };
 
152
            panel.addChangeListener (new ChangeListener () {
 
153
               public void stateChanged(ChangeEvent e) {
 
154
                   if (e.getSource () instanceof MouseEvent && MouseUtils.isDoubleClick (((MouseEvent)e.getSource ()))) {
 
155
                       // click button and finish the dialog with selected class
 
156
                       okButton.doClick ();
 
157
                   } else {
 
158
                       okButton.setEnabled (panel.getSelectedMainScript () != null);
 
159
                   }
 
160
               }
 
161
            });
 
162
            okButton.setEnabled (panel.getSelectedMainScript () != null);
 
163
            DialogDescriptor desc = new DialogDescriptor (panel,
 
164
                    NbBundle.getMessage (VisualMainScriptSupport.class, "LBL_ChooseMainScript_Title" ),
 
165
                true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null);
 
166
            //desc.setMessageType (DialogDescriptor.INFORMATION_MESSAGE);
 
167
            Dialog dlg = DialogDisplayer.getDefault ().createDialog (desc);
 
168
            dlg.setVisible (true);
 
169
            if (desc.getValue() == options[0]) {
 
170
               mainScriptTextField.setText (panel.getSelectedMainScript ());
 
171
            } 
 
172
            dlg.dispose();
 
173
        }
 
174
        
 
175
        // Implementation of document listener ---------------------------------
 
176
        
 
177
        public void changedUpdate (DocumentEvent e) {
 
178
            fireActionPerformed ();
 
179
        }
 
180
        
 
181
        public void insertUpdate( DocumentEvent e ) {
 
182
            changedUpdate( e );
 
183
        }
 
184
        
 
185
        public void removeUpdate( DocumentEvent e ) {
 
186
            changedUpdate( e );
 
187
        }
 
188
        
 
189
        
 
190
    }
 
191
    
 
192
}