001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.ui.actions;
043:
044: import java.awt.Component;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Insets;
048: import java.awt.Window;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.ActionListener;
051: import java.util.Collections;
052: import java.util.Comparator;
053:
054: import java.util.List;
055:
056: import javax.swing.JComboBox;
057: import javax.swing.JComponent;
058: import javax.swing.JLabel;
059: import javax.swing.JPanel;
060: import javax.swing.JSeparator;
061: import javax.swing.border.EmptyBorder;
062:
063: import org.netbeans.api.debugger.DebuggerManager;
064: import org.netbeans.api.debugger.Properties;
065:
066: import org.netbeans.spi.debugger.ui.AttachType;
067: import org.netbeans.spi.debugger.ui.Controller;
068: import org.openide.awt.Mnemonics;
069:
070: import org.openide.util.NbBundle;
071:
072: public class ConnectorPanel extends JPanel implements ActionListener {
073:
074: public static final String PROP_TYPE = "type";
075:
076: private static final String FIRST_ATTACH_TYPE = "org.netbeans.modules.debugger.jpda.ui.JPDAAttachType"; // NOI18N
077:
078: /** Contains list of AttachType names.*/
079: private JComboBox cbAttachTypes;
080: /** Switches off listenning on cbAttachTypes.*/
081: private boolean doNotListen;
082: /** Contains list of installed AttachTypes.*/
083: private List attachTypes;
084: /** Currentlydisplayed panel.*/
085: private Controller currentPanel;
086: /** Current attach type, which is stored into settings for the next invocation. */
087: private AttachType currentAttachType;
088:
089: public ConnectorPanel() {
090: getAccessibleContext().setAccessibleDescription(
091: NbBundle.getMessage(ConnectorPanel.class,
092: "ACSD_ConnectorPanel"));
093: cbAttachTypes = new JComboBox();
094: cbAttachTypes.getAccessibleContext().setAccessibleDescription(
095: NbBundle.getMessage(ConnectorPanel.class,
096: "ACSD_CTL_Connect_through")// NOI18N
097: );
098: attachTypes = DebuggerManager.getDebuggerManager().lookup(null,
099: AttachType.class);
100: String defaultAttachTypeName = Properties.getDefault()
101: .getProperties("debugger").getString(
102: "last_attach_type", null);
103: int defaultIndex = 0;
104: int i, k = attachTypes.size();
105: Collections.sort(attachTypes, new Comparator() {
106: public int compare(Object o1, Object o2) {
107: if (!(o1 instanceof AttachType)
108: || !(o2 instanceof AttachType))
109: return 0;
110: if (FIRST_ATTACH_TYPE.equals(o1.getClass().getName())) {
111: return -1;
112: }
113: if (FIRST_ATTACH_TYPE.equals(o2.getClass().getName())) {
114: return +1;
115: }
116: return ((AttachType) o1).getTypeDisplayName()
117: .compareTo(
118: ((AttachType) o2).getTypeDisplayName());
119: }
120: });
121: for (i = 0; i < k; i++) {
122: AttachType at = (AttachType) attachTypes.get(i);
123: cbAttachTypes.addItem(at.getTypeDisplayName());
124: if ((defaultAttachTypeName != null)
125: && (defaultAttachTypeName.equals(at.getClass()
126: .getName())))
127: defaultIndex = i;
128: }
129:
130: cbAttachTypes.setActionCommand("SwitchMe!"); // NOI18N
131: cbAttachTypes.addActionListener(this );
132:
133: setLayout(new GridBagLayout());
134: setBorder(new EmptyBorder(11, 11, 0, 10));
135: refresh(defaultIndex);
136: }
137:
138: private void refresh(int index) {
139: JLabel cbLabel = new JLabel();
140: Mnemonics.setLocalizedText(cbLabel, NbBundle.getMessage(
141: ConnectorPanel.class, "CTL_Connect_through"));
142: cbLabel.getAccessibleContext().setAccessibleDescription(
143: NbBundle.getMessage(ConnectorPanel.class,
144: "ACSD_CTL_Connect_through")// NOI18N
145: );
146: cbLabel.setLabelFor(cbAttachTypes);
147:
148: GridBagConstraints c = new GridBagConstraints();
149: c.insets = new Insets(0, 0, 6, 6);
150: add(cbLabel, c);
151: c = new GridBagConstraints();
152: c.weightx = 1.0;
153: c.fill = java.awt.GridBagConstraints.HORIZONTAL;
154: c.gridwidth = 0;
155: c.insets = new Insets(0, 3, 6, 0);
156: doNotListen = true;
157: cbAttachTypes.setSelectedIndex(index);
158: doNotListen = false;
159: add(cbAttachTypes, c);
160: c.insets = new Insets(0, 0, 6, 0);
161: add(new JSeparator(), c);
162: c = new GridBagConstraints();
163: c.weightx = 1.0;
164: c.weighty = 1.0;
165: c.fill = java.awt.GridBagConstraints.BOTH;
166: c.gridwidth = 0;
167: AttachType attachType = (AttachType) attachTypes.get(index);
168: JComponent customizer = attachType.getCustomizer();
169: currentPanel = (Controller) customizer;
170: firePropertyChange(PROP_TYPE, null, customizer);
171: this .currentAttachType = attachType;
172: add(customizer, c);
173: }
174:
175: /**
176: * Called when a user selects debugger type in a combo-box.
177: */
178: public void actionPerformed(ActionEvent e) {
179: if (doNotListen)
180: return;
181: if (e.getActionCommand().equals("SwitchMe!"))
182: ; // NOI18N
183: removeAll();
184: refresh(((JComboBox) e.getSource()).getSelectedIndex());
185: Component w = getParent();
186: while (!(w instanceof Window))
187: w = w.getParent();
188: if (w != null)
189: ((Window) w).pack(); // ugly hack...
190: return;
191: }
192:
193: Controller getController() {
194: return currentPanel;
195: }
196:
197: boolean cancel() {
198: return currentPanel.cancel();
199: }
200:
201: boolean ok() {
202: String defaultAttachTypeName = currentAttachType.getClass()
203: .getName();
204: Properties.getDefault().getProperties("debugger").setString(
205: "last_attach_type", defaultAttachTypeName);
206: return currentPanel.ok();
207: }
208: }
|