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.form;
043:
044: import java.util.*;
045: import javax.swing.DefaultListModel;
046:
047: import org.openide.DialogDisplayer;
048: import org.openide.NotifyDescriptor;
049:
050: /**
051: *
052: * @author Pavel Buzek
053: */
054: public class EventCustomEditor extends javax.swing.JPanel {
055:
056: static final long serialVersionUID = -4825059521634962952L;
057:
058: /** Creates new form EventCustomEditor */
059: public EventCustomEditor(EventProperty eventProperty) {
060: this .eventProperty = eventProperty;
061:
062: initComponents();
063: enableButtons();
064:
065: addButton.setMnemonic(FormUtils.getBundleString(
066: "CTL_EE_ADD_Mnemonic").charAt(0)); // NOI18N
067: removeButton.setMnemonic(FormUtils.getBundleString(
068: "CTL_EE_REMOVE_Mnemonic").charAt(0)); // NOI18N
069: editButton.setMnemonic(FormUtils.getBundleString(
070: "CTL_EE_RENAME_Mnemonic").charAt(0)); // NOI18N
071: handlersListLabel.setDisplayedMnemonic(FormUtils
072: .getBundleString("CTL_EE_Handlers_Mnemonic").charAt(0)); // NOI18N
073:
074: handlersList.getAccessibleContext().setAccessibleDescription(
075: FormUtils.getBundleString("ACSD_CTL_EE_Handlers")); // NOI18N
076: addButton.getAccessibleContext().setAccessibleDescription(
077: FormUtils.getBundleString("ACSD_CTL_EE_ADD")); // NOI18N
078: removeButton.getAccessibleContext().setAccessibleDescription(
079: FormUtils.getBundleString("ACSD_CTL_EE_REMOVE")); // NOI18N
080: editButton.getAccessibleContext().setAccessibleDescription(
081: FormUtils.getBundleString("ACSD_CTL_EE_RENAME")); // NOI18N
082: getAccessibleContext().setAccessibleDescription(
083: FormUtils.getBundleString("ACSD_EventCustomEditor")); // NOI18N
084: }
085:
086: /** This method is called from within the constructor to
087: * initialize the form.
088: * WARNING: Do NOT modify this code. The content of this method is
089: * always regenerated by the FormEditor.
090: */
091: private void initComponents() {//GEN-BEGIN:initComponents
092: java.awt.GridBagConstraints gridBagConstraints;
093:
094: handlersListLabel = new javax.swing.JLabel();
095: jScrollPane1 = new javax.swing.JScrollPane();
096: String[] handlers = eventProperty.getEventHandlers();
097: for (int i = 0; i < handlers.length; i++) {
098: handlersModel.addElement(handlers[i]);
099: }
100: handlersList = new javax.swing.JList();
101: handlersList.setModel(handlersModel);
102: if (handlers.length > 0) {
103: handlersList.setSelectedIndex(0);
104: }
105: addButton = new javax.swing.JButton();
106: removeButton = new javax.swing.JButton();
107: editButton = new javax.swing.JButton();
108:
109: setLayout(new java.awt.GridBagLayout());
110:
111: setPreferredSize(new java.awt.Dimension(300, 300));
112: handlersListLabel.setLabelFor(handlersList);
113: handlersListLabel.setText(FormUtils
114: .getBundleString("CTL_EE_Handlers"));
115: gridBagConstraints = new java.awt.GridBagConstraints();
116: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
117: gridBagConstraints.insets = new java.awt.Insets(12, 12, 2, 0);
118: add(handlersListLabel, gridBagConstraints);
119:
120: handlersList
121: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
122: public void valueChanged(
123: javax.swing.event.ListSelectionEvent evt) {
124: handlersListValueChanged(evt);
125: }
126: });
127:
128: jScrollPane1.setViewportView(handlersList);
129:
130: gridBagConstraints = new java.awt.GridBagConstraints();
131: gridBagConstraints.gridx = 0;
132: gridBagConstraints.gridy = 1;
133: gridBagConstraints.gridheight = 4;
134: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
135: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
136: gridBagConstraints.weightx = 0.9;
137: gridBagConstraints.weighty = 1.0;
138: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 11);
139: add(jScrollPane1, gridBagConstraints);
140:
141: addButton.setText(FormUtils.getBundleString("CTL_EE_ADD"));
142: addButton
143: .addActionListener(new java.awt.event.ActionListener() {
144: public void actionPerformed(
145: java.awt.event.ActionEvent evt) {
146: addButtonActionPerformed(evt);
147: }
148: });
149:
150: gridBagConstraints = new java.awt.GridBagConstraints();
151: gridBagConstraints.gridx = 1;
152: gridBagConstraints.gridy = 1;
153: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
154: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
155: gridBagConstraints.weightx = 0.1;
156: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 11);
157: add(addButton, gridBagConstraints);
158:
159: removeButton
160: .setText(FormUtils.getBundleString("CTL_EE_REMOVE"));
161: removeButton
162: .addActionListener(new java.awt.event.ActionListener() {
163: public void actionPerformed(
164: java.awt.event.ActionEvent evt) {
165: removeButtonActionPerformed(evt);
166: }
167: });
168:
169: gridBagConstraints = new java.awt.GridBagConstraints();
170: gridBagConstraints.gridx = 1;
171: gridBagConstraints.gridy = 2;
172: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
173: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
174: gridBagConstraints.weightx = 0.1;
175: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 11);
176: add(removeButton, gridBagConstraints);
177:
178: editButton.setText(FormUtils.getBundleString("CTL_EE_RENAME"));
179: editButton
180: .addActionListener(new java.awt.event.ActionListener() {
181: public void actionPerformed(
182: java.awt.event.ActionEvent evt) {
183: editButtonActionPerformed(evt);
184: }
185: });
186:
187: gridBagConstraints = new java.awt.GridBagConstraints();
188: gridBagConstraints.gridx = 1;
189: gridBagConstraints.gridy = 3;
190: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
191: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
192: gridBagConstraints.weightx = 0.1;
193: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 11);
194: add(editButton, gridBagConstraints);
195:
196: }//GEN-END:initComponents
197:
198: private void handlersListValueChanged(
199: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_handlersListValueChanged
200: enableButtons();
201: }//GEN-LAST:event_handlersListValueChanged
202:
203: private void enableButtons() {
204: if (handlersList.isSelectionEmpty()) {
205: removeButton.setEnabled(false);
206: } else {
207: removeButton.setEnabled(true);
208: }
209: editButton
210: .setEnabled(handlersList.getSelectedIndices().length == 1);
211: }
212:
213: private void editButtonActionPerformed(
214: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
215: // Add your handling code here:
216: int i = handlersList.getSelectedIndex();
217: if (i >= 0) {
218: String oldName = (String) handlersModel.get(i);
219: NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
220: FormUtils.getBundleString("CTL_EE_RENAME_LABEL"), // NOI18N
221: FormUtils.getBundleString("CTL_EE_RENAME_CAPTION")); // NOI18N
222: nd.setInputText(oldName);
223:
224: if (DialogDisplayer.getDefault().notify(nd).equals(
225: NotifyDescriptor.OK_OPTION)) {
226: String newName = nd.getInputText();
227: if (newName.equals(oldName))
228: return; // no change
229:
230: if (!org.openide.util.Utilities
231: .isJavaIdentifier(newName)) { // invalid name
232: NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
233: FormUtils
234: .getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
235: NotifyDescriptor.ERROR_MESSAGE);
236: DialogDisplayer.getDefault().notify(msg);
237: return;
238: }
239:
240: if (handlersModel.indexOf(newName) >= 0) { // already exists
241: NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
242: FormUtils
243: .getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
244: NotifyDescriptor.INFORMATION_MESSAGE);
245: DialogDisplayer.getDefault().notify(msg);
246: return;
247: }
248:
249: int ii = changes.getAdded().indexOf(oldName);
250: if (ii >= 0) { // a newly added handler was renamed
251: changes.getAdded().set(ii, newName);
252: } else {
253: ii = changes.getRenamedNewNames().indexOf(oldName);
254: if (ii >= 0) // this handler has been already renamed
255: changes.getRenamedNewNames().set(ii, newName);
256: else {
257: changes.getRenamedOldNames().add(oldName);
258: changes.getRenamedNewNames().add(newName);
259: }
260: }
261:
262: handlersModel.set(i, newName);
263: handlersList.setSelectedIndex(i);
264: enableButtons();
265: }
266: }
267: }//GEN-LAST:event_editButtonActionPerformed
268:
269: private void removeButtonActionPerformed(
270: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
271: Object[] handlers = handlersList.getSelectedValues();
272: for (int i = 0; i < handlers.length; i++) {
273: int ii = changes.getAdded().indexOf(handlers[i]);
274: if (ii >= 0) { // the handler was previously added - cancel it
275: changes.getAdded().remove(ii);
276: } else {
277: ii = changes.getRenamedNewNames().indexOf(handlers[i]);
278: String toRemove;
279: if (ii >= 0) { // the handler was previously renamed - cancel it
280: changes.getRenamedNewNames().remove(ii);
281: toRemove = (String) changes.getRenamedOldNames()
282: .get(ii);
283: changes.getRenamedOldNames().remove(ii);
284: } else
285: toRemove = (String) handlers[i];
286:
287: changes.getRemoved().add(toRemove);
288: }
289: handlersModel.removeElement(handlers[i]);
290: enableButtons();
291: }
292: }//GEN-LAST:event_removeButtonActionPerformed
293:
294: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
295: NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
296: FormUtils.getBundleString("CTL_EE_ADD_LABEL"), // NOI18N
297: FormUtils.getBundleString("CTL_EE_ADD_CAPTION")); // NOI18N
298: if (DialogDisplayer.getDefault().notify(nd).equals(
299: NotifyDescriptor.OK_OPTION)) {
300: String newHandler = nd.getInputText();
301: if (!org.openide.util.Utilities
302: .isJavaIdentifier(newHandler)) {
303: NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
304: FormUtils
305: .getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
306: NotifyDescriptor.ERROR_MESSAGE);
307: DialogDisplayer.getDefault().notify(msg);
308: return;
309: }
310:
311: if (handlersModel.indexOf(newHandler) >= 0) {
312: NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
313: FormUtils
314: .getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
315: NotifyDescriptor.INFORMATION_MESSAGE);
316: DialogDisplayer.getDefault().notify(msg);
317: return;
318: }
319:
320: int ir = changes.getRemoved().indexOf(newHandler);
321: if (ir >= 0) {
322: changes.getRemoved().remove(ir);
323: } else {
324: changes.getAdded().add(newHandler);
325: }
326: handlersModel.addElement(newHandler);
327: handlersList.setSelectedIndex(handlersModel.size() - 1);
328: enableButtons();
329: }
330: }//GEN-LAST:event_addButtonActionPerformed
331:
332: public void doChanges() {
333: try {
334: eventProperty.setValue(changes);
335: } catch (Exception e) { // should not happen
336: e.printStackTrace();
337: }
338: }
339:
340: // Variables declaration - do not modify//GEN-BEGIN:variables
341: private javax.swing.JButton addButton;
342: private javax.swing.JScrollPane jScrollPane1;
343: private javax.swing.JList handlersList;
344: private javax.swing.JButton editButton;
345: private javax.swing.JButton removeButton;
346: private javax.swing.JLabel handlersListLabel;
347: // End of variables declaration//GEN-END:variables
348:
349: EventProperty eventProperty;
350: DefaultListModel handlersModel = new DefaultListModel();
351: EventProperty.Change changes = new EventProperty.Change();
352: }
|