1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.form;
import java.util.*;
import javax.swing.DefaultListModel;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
/**
*
* @author Pavel Buzek
*/
public class EventCustomEditor extends javax.swing.JPanel {
static final long serialVersionUID =-4825059521634962952L;
/** Creates new form EventCustomEditor */
public EventCustomEditor(EventProperty eventProperty) {
this.eventProperty = eventProperty;
initComponents();
enableButtons();
addButton.setMnemonic(
FormUtils.getBundleString("CTL_EE_ADD_Mnemonic").charAt(0)); // NOI18N
removeButton.setMnemonic(
FormUtils.getBundleString("CTL_EE_REMOVE_Mnemonic").charAt(0)); // NOI18N
editButton.setMnemonic(
FormUtils.getBundleString("CTL_EE_RENAME_Mnemonic").charAt(0)); // NOI18N
handlersListLabel.setDisplayedMnemonic(
FormUtils.getBundleString("CTL_EE_Handlers_Mnemonic").charAt(0)); // NOI18N
handlersList.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_EE_Handlers")); // NOI18N
addButton.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_EE_ADD")); // NOI18N
removeButton.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_EE_REMOVE")); // NOI18N
editButton.getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_CTL_EE_RENAME")); // NOI18N
getAccessibleContext().setAccessibleDescription(
FormUtils.getBundleString("ACSD_EventCustomEditor")); // NOI18N
}
/** 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 FormEditor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
handlersListLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
String[] handlers = eventProperty.getEventHandlers();
for (int i=0; i < handlers.length; i++) {
handlersModel.addElement(handlers[i]);
}
handlersList = new javax.swing.JList();
handlersList.setModel(handlersModel);
if (handlers.length > 0) {
handlersList.setSelectedIndex(0);
}
addButton = new javax.swing.JButton();
removeButton = new javax.swing.JButton();
editButton = new javax.swing.JButton();
setLayout(new java.awt.GridBagLayout());
setPreferredSize(new java.awt.Dimension(300, 300));
handlersListLabel.setLabelFor(handlersList);
handlersListLabel.setText(FormUtils.getBundleString("CTL_EE_Handlers"));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(12, 12, 2, 0);
add(handlersListLabel, gridBagConstraints);
handlersList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
handlersListValueChanged(evt);
}
});
jScrollPane1.setViewportView(handlersList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridheight = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 0.9;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 11);
add(jScrollPane1, gridBagConstraints);
addButton.setText(FormUtils.getBundleString("CTL_EE_ADD"));
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 11);
add(addButton, gridBagConstraints);
removeButton.setText(FormUtils.getBundleString("CTL_EE_REMOVE"));
removeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 11);
add(removeButton, gridBagConstraints);
editButton.setText(FormUtils.getBundleString("CTL_EE_RENAME"));
editButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 0.1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 11);
add(editButton, gridBagConstraints);
}//GEN-END:initComponents
private void handlersListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_handlersListValueChanged
enableButtons();
}//GEN-LAST:event_handlersListValueChanged
private void enableButtons() {
if (handlersList.isSelectionEmpty()) {
removeButton.setEnabled(false);
} else {
removeButton.setEnabled(true);
}
editButton.setEnabled(handlersList.getSelectedIndices().length == 1);
}
private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
// Add your handling code here:
int i = handlersList.getSelectedIndex();
if (i >= 0) {
String oldName = (String) handlersModel.get(i);
NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
FormUtils.getBundleString("CTL_EE_RENAME_LABEL"), // NOI18N
FormUtils.getBundleString("CTL_EE_RENAME_CAPTION")); // NOI18N
nd.setInputText(oldName);
if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) {
String newName = nd.getInputText();
if (newName.equals(oldName)) return; // no change
if (!org.openide.util.Utilities.isJavaIdentifier(newName)) { // invalid name
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
if (handlersModel.indexOf(newName) >= 0) { // already exists
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
int ii = changes.getAdded().indexOf(oldName);
if (ii >= 0) { // a newly added handler was renamed
changes.getAdded().set(ii,newName);
}
else {
ii = changes.getRenamedNewNames().indexOf(oldName);
if (ii >= 0) // this handler has been already renamed
changes.getRenamedNewNames().set(ii, newName);
else {
changes.getRenamedOldNames().add(oldName);
changes.getRenamedNewNames().add(newName);
}
}
handlersModel.set(i,newName);
handlersList.setSelectedIndex(i);
enableButtons();
}
}
}//GEN-LAST:event_editButtonActionPerformed
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
Object[] handlers = handlersList.getSelectedValues();
for (int i=0; i < handlers.length; i++) {
int ii = changes.getAdded().indexOf(handlers[i]);
if (ii >= 0) { // the handler was previously added - cancel it
changes.getAdded().remove(ii);
}
else {
ii = changes.getRenamedNewNames().indexOf(handlers[i]);
String toRemove;
if (ii >= 0) { // the handler was previously renamed - cancel it
changes.getRenamedNewNames().remove(ii);
toRemove = (String) changes.getRenamedOldNames().get(ii);
changes.getRenamedOldNames().remove(ii);
}
else toRemove = (String) handlers[i];
changes.getRemoved().add(toRemove);
}
handlersModel.removeElement(handlers[i]);
enableButtons();
}
}//GEN-LAST:event_removeButtonActionPerformed
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine(
FormUtils.getBundleString("CTL_EE_ADD_LABEL"), // NOI18N
FormUtils.getBundleString("CTL_EE_ADD_CAPTION")); // NOI18N
if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.OK_OPTION)) {
String newHandler = nd.getInputText();
if (!org.openide.util.Utilities.isJavaIdentifier(newHandler)) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
FormUtils.getBundleString("CTL_EE_NOT_IDENTIFIER"), // NOI18N
NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
if (handlersModel.indexOf(newHandler) >= 0) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
FormUtils.getBundleString("CTL_EE_ALREADY_EXIST"), // NOI18N
NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
return;
}
int ir = changes.getRemoved().indexOf(newHandler);
if (ir >= 0) {
changes.getRemoved().remove(ir);
}
else {
changes.getAdded().add(newHandler);
}
handlersModel.addElement(newHandler);
handlersList.setSelectedIndex(handlersModel.size() - 1);
enableButtons();
}
}//GEN-LAST:event_addButtonActionPerformed
public void doChanges() {
try {
eventProperty.setValue(changes);
} catch (Exception e) { // should not happen
e.printStackTrace();
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList handlersList;
private javax.swing.JButton editButton;
private javax.swing.JButton removeButton;
private javax.swing.JLabel handlersListLabel;
// End of variables declaration//GEN-END:variables
EventProperty eventProperty;
DefaultListModel handlersModel = new DefaultListModel();
EventProperty.Change changes = new EventProperty.Change();
}
|