0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.modules.form;
0043:
0044: import java.awt.Component;
0045: import java.awt.Container;
0046: import java.awt.Dimension;
0047: import java.awt.LayoutManager2;
0048: import java.awt.event.ActionEvent;
0049: import java.awt.event.ActionListener;
0050: import java.lang.reflect.Modifier;
0051: import java.util.HashMap;
0052: import java.util.Iterator;
0053: import java.util.LinkedList;
0054: import java.util.List;
0055: import java.util.ListIterator;
0056: import java.util.Map;
0057: import javax.swing.DefaultComboBoxModel;
0058: import javax.swing.JComboBox;
0059: import javax.swing.JPanel;
0060: import javax.swing.event.DocumentEvent;
0061: import javax.swing.event.DocumentListener;
0062: import javax.swing.text.BadLocationException;
0063: import javax.swing.text.Document;
0064: import javax.swing.text.Element;
0065: import javax.swing.text.JTextComponent;
0066: import javax.swing.text.Position;
0067: import javax.swing.text.StyledDocument;
0068: import javax.swing.undo.UndoManager;
0069: import org.netbeans.editor.BaseDocument;
0070:
0071: import org.openide.ErrorManager;
0072: import org.openide.filesystems.FileObject;
0073: import org.openide.text.NbDocument;
0074: import org.openide.util.NbBundle;
0075:
0076: import static org.netbeans.modules.form.CustomCodeData.*;
0077:
0078: /**
0079: * GUI panel of the code customizer.
0080: *
0081: * @author Tomas Pavek
0082: */
0083:
0084: class CustomCodeView extends javax.swing.JPanel {
0085:
0086: private CustomCodeData codeData;
0087: private int lastLocalModifiers = -1;
0088: private int lastFieldModifiers = -1;
0089:
0090: private boolean changed;
0091:
0092: interface Listener {
0093: void componentExchanged(String compName);
0094:
0095: void renameInvoked();
0096:
0097: void declarationChanged();
0098: }
0099:
0100: private Listener controller;
0101:
0102: private DocumentL docListener;
0103:
0104: // flag to recognize user action on JComboBox from our calls
0105: private boolean ignoreComboAction;
0106:
0107: private static class EditBlockInfo {
0108: Position position;
0109: List<EditableLine> lines;
0110: }
0111:
0112: private static class GuardBlockInfo {
0113: Position position;
0114: String customizedCode;
0115: boolean customized;
0116: }
0117:
0118: private Map<CodeCategory, EditBlockInfo[]> editBlockInfos;
0119: private Map<CodeCategory, GuardBlockInfo[]> guardBlockInfos;
0120:
0121: // -----
0122:
0123: CustomCodeView(Listener controller) {
0124: this .controller = controller;
0125:
0126: initComponents();
0127:
0128: variableCombo
0129: .setModel(new DefaultComboBoxModel(variableStrings));
0130: accessCombo.setModel(new DefaultComboBoxModel(accessStrings));
0131:
0132: // create gutter panels - let their layout share the component map so
0133: // they have the same width
0134: Map<Component, Position> positions = new HashMap<Component, Position>();
0135: initGutter = new JPanel();
0136: initGutter
0137: .setLayout(new GutterLayout(initCodeEditor, positions));
0138: declareGutter = new JPanel();
0139: declareGutter.setLayout(new GutterLayout(declareCodeEditor,
0140: positions));
0141: jScrollPane1.setRowHeaderView(initGutter);
0142: jScrollPane2.setRowHeaderView(declareGutter);
0143: // jScrollPane1.setBorder(null);
0144: // jScrollPane2.setBorder(null);
0145: }
0146:
0147: @Override
0148: public void addNotify() {
0149: super .addNotify();
0150: initCodeEditor.requestFocusInWindow();
0151: }
0152:
0153: boolean isChanged() {
0154: return changed;
0155: }
0156:
0157: void setComponentNames(String[] compNames) {
0158: componentCombo.setModel(new DefaultComboBoxModel(compNames));
0159: }
0160:
0161: void setCodeData(String componentName, CustomCodeData codeData,
0162: FileObject srcFile, int[] positions) {
0163: if (this .codeData != null) { // clean up
0164: initCodeEditor.getDocument().removeDocumentListener(
0165: docListener);
0166: declareCodeEditor.getDocument().removeDocumentListener(
0167: docListener);
0168:
0169: initGutter.removeAll();
0170: declareGutter.removeAll();
0171:
0172: revalidate();
0173: repaint();
0174: }
0175:
0176: if (editBlockInfos != null) {
0177: editBlockInfos.clear();
0178: guardBlockInfos.clear();
0179: } else {
0180: editBlockInfos = new HashMap<CodeCategory, EditBlockInfo[]>();
0181: guardBlockInfos = new HashMap<CodeCategory, GuardBlockInfo[]>();
0182: }
0183:
0184: FormUtils
0185: .setupEditorPane(initCodeEditor, srcFile, positions[0]);
0186: FormUtils.setupEditorPane(declareCodeEditor, srcFile,
0187: positions[1]);
0188:
0189: this .codeData = codeData;
0190: selectInComboBox(componentCombo, componentName);
0191:
0192: buildCodeView(CodeCategory.CREATE_AND_INIT);
0193: buildCodeView(CodeCategory.DECLARATION);
0194:
0195: Object um = initCodeEditor.getDocument().getProperty(
0196: BaseDocument.UNDO_MANAGER_PROP);
0197: if (um instanceof UndoManager) {
0198: ((UndoManager) um).discardAllEdits();
0199: }
0200: um = declareCodeEditor.getDocument().getProperty(
0201: BaseDocument.UNDO_MANAGER_PROP);
0202: if (um instanceof UndoManager) {
0203: ((UndoManager) um).discardAllEdits();
0204: }
0205:
0206: VariableDeclaration decl = codeData.getDeclarationData();
0207: boolean local = decl.local;
0208: for (int i = 0; i < variableValues.length; i++) {
0209: if (variableValues[i] == local) {
0210: selectInComboBox(variableCombo, variableStrings[i]);
0211: break;
0212: }
0213: }
0214: int modifiers = decl.modifiers;
0215: int access = modifiers
0216: & (Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC);
0217: for (int i = 0; i < accessValues.length; i++) {
0218: if (accessValues[i] == access) {
0219: selectInComboBox(accessCombo, accessStrings[i]);
0220: break;
0221: }
0222: }
0223: staticCheckBox
0224: .setSelected((modifiers & Modifier.STATIC) == Modifier.STATIC);
0225: finalCheckBox
0226: .setSelected((modifiers & Modifier.FINAL) == Modifier.FINAL);
0227: transientCheckBox
0228: .setSelected((modifiers & Modifier.TRANSIENT) == Modifier.TRANSIENT);
0229: volatileCheckBox
0230: .setSelected((modifiers & Modifier.VOLATILE) == Modifier.VOLATILE);
0231: accessCombo.setEnabled(!local);
0232: staticCheckBox.setEnabled(!local);
0233: transientCheckBox.setEnabled(!local);
0234: volatileCheckBox.setEnabled(!local);
0235:
0236: if (local)
0237: lastLocalModifiers = modifiers;
0238: else
0239: lastFieldModifiers = modifiers;
0240:
0241: changed = false;
0242:
0243: if (docListener == null)
0244: docListener = new DocumentL();
0245: initCodeEditor.getDocument().addDocumentListener(docListener);
0246: declareCodeEditor.getDocument()
0247: .addDocumentListener(docListener);
0248:
0249: initCodeEditor.setCaretPosition(0);
0250: declareCodeEditor.setCaretPosition(0);
0251: }
0252:
0253: private void buildCodeView(CodeCategory category) {
0254: editBlockInfos.put(category, new EditBlockInfo[codeData
0255: .getEditableBlockCount(category)]);
0256: int gCount = codeData.getGuardedBlockCount(category);
0257: guardBlockInfos.put(category, new GuardBlockInfo[gCount]);
0258:
0259: try {
0260: for (int i = 0; i < gCount; i++) {
0261: addEditableCode(category, i);
0262: addGuardedCode(category, i);
0263: }
0264: if (gCount > 0)
0265: addEditableCode(category, gCount);
0266: } catch (BadLocationException ex) { // should not happen
0267: ErrorManager.getDefault().notify(
0268: ErrorManager.INFORMATIONAL, ex);
0269: }
0270:
0271: getEditor(category).setEnabled(gCount > 0);
0272: }
0273:
0274: private void addEditableCode(CodeCategory category, int blockIndex)
0275: throws BadLocationException {
0276: Document doc = getDocument(category);
0277: EditableBlock eBlock = codeData.getEditableBlock(category,
0278: blockIndex);
0279: boolean lastBlock = blockIndex + 1 == codeData
0280: .getEditableBlockCount(category);
0281: List<EditableLine> lineList = new LinkedList<EditableLine>();
0282: int startIndex = doc.getLength();
0283: boolean needLineEnd = false;
0284:
0285: CodeEntry[] entries = eBlock.getEntries();
0286: for (int i = 0; i < entries.length; i++) {
0287: CodeEntry e = entries[i];
0288: String code = e.getCode();
0289: if (code == null)
0290: continue;
0291:
0292: // process lines of the code entry
0293: int lineStart = 0;
0294: int codeLength = code.length();
0295: for (int j = 0; j < codeLength; j++) {
0296: char c = code.charAt(j);
0297: // TODO: filter out subsequent empty lines?
0298: if (c == '\n' || j + 1 == codeLength) { // end of line
0299: if (needLineEnd) // previous line not ended by '\n'
0300: doc.insertString(doc.getLength(), "\n", null); // NOI18N
0301:
0302: boolean lastLine = lastBlock
0303: && i + 1 == entries.length
0304: && j + 1 == codeLength;
0305: needLineEnd = c != '\n' && !lastLine; // missing '\n' - will add it later when needed
0306: int lineEnd = c == '\n' && lastLine ? j : j + 1; // skip '\n' for very last line
0307: int index = doc.getLength();
0308: doc.insertString(index, code.substring(lineStart,
0309: lineEnd), null);
0310: Position pos = NbDocument.createPosition(doc,
0311: index, Position.Bias.Backward);
0312: lineList.add(new EditableLine(pos, eBlock, i,
0313: lineList));
0314:
0315: lineStart = j + 1;
0316: }
0317: }
0318: }
0319:
0320: if (lineList.size() > 0) {
0321: if (needLineEnd) // last line of the block not ended by '\n'
0322: doc.insertString(doc.getLength(), "\n", null); // NOI18N
0323: } else { // no code in whole block - add one empty line
0324: int index = doc.getLength();
0325: if (!lastBlock)
0326: doc.insertString(index, "\n", null); // NOI18N
0327: Position pos = NbDocument.createPosition(doc, index,
0328: Position.Bias.Backward);
0329: lineList.add(new EditableLine(pos, eBlock, eBlock
0330: .getPreferredEntryIndex(), lineList));
0331: }
0332:
0333: updateGutterComponents(lineList, doc, startIndex, doc
0334: .getLength());
0335:
0336: EditBlockInfo eInfo = new EditBlockInfo();
0337: eInfo.position = lineList.get(0).getPosition();
0338: eInfo.lines = lineList;
0339: getEditInfos(category)[blockIndex] = eInfo;
0340: }
0341:
0342: private void addGuardedCode(CodeCategory category, int blockIndex)
0343: throws BadLocationException {
0344: StyledDocument doc = (StyledDocument) getDocument(category);
0345: GuardedBlock gBlock = codeData.getGuardedBlock(category,
0346: blockIndex);
0347: GuardBlockInfo gInfo = new GuardBlockInfo();
0348: int index = doc.getLength();
0349: if (gBlock.isCustomized()) {
0350: String code = gBlock.getCustomCode();
0351: doc.insertString(index, code, null);
0352: if (!code.endsWith("\n")) { // NOI18N
0353: doc.insertString(doc.getLength(), "\n", null); // NOI18N
0354: }
0355: int header = gBlock.getHeaderLength();
0356: int footer = gBlock.getFooterLength();
0357: NbDocument.markGuarded(doc, index, header);
0358: NbDocument.markGuarded(doc, doc.getLength() - footer,
0359: footer);
0360: gInfo.customized = true;
0361: } else {
0362: String code = gBlock.getDefaultCode();
0363: doc.insertString(index, code, null);
0364: if (!code.endsWith("\n")) { // NOI18N
0365: doc.insertString(doc.getLength(), "\n", null); // NOI18N
0366: }
0367: NbDocument.markGuarded(doc, index, doc.getLength() - index);
0368: }
0369:
0370: Position pos = NbDocument.createPosition(doc, index,
0371: Position.Bias.Forward);
0372: gInfo.position = pos;
0373: getGuardInfos(category)[blockIndex] = gInfo;
0374:
0375: if (gBlock.isCustomizable()) {
0376: String[] items = new String[] {
0377: NbBundle.getMessage(CustomCodeView.class,
0378: "CTL_GuardCombo_Default"), // NOI18N
0379: gBlock.getCustomEntry().getDisplayName() };
0380: JComboBox combo = new JComboBox(items);
0381: // combo.setBorder(null);
0382: if (gBlock.isCustomized()) {
0383: selectInComboBox(combo, items[1]);
0384: combo.setToolTipText(gBlock.getCustomEntry()
0385: .getToolTipText());
0386: } else {
0387: selectInComboBox(combo, items[0]);
0388: combo.setToolTipText(NbBundle.getMessage(
0389: CustomCodeView.class,
0390: "CTL_GuardCombo_Default_Hint")); // NOI18N
0391: }
0392: combo.getAccessibleContext().setAccessibleName(
0393: gBlock.getCustomEntry().getName());
0394: combo.addActionListener(new GuardSwitchL(category,
0395: blockIndex));
0396: getGutter(doc).add(combo, pos);
0397: }
0398: }
0399:
0400: /**
0401: * Writes edited code back to the CustomCodeData structure.
0402: */
0403: CustomCodeData retreiveCodeData() {
0404: retreiveCodeData(CodeCategory.CREATE_AND_INIT);
0405: retreiveCodeData(CodeCategory.DECLARATION);
0406:
0407: VariableDeclaration decl = codeData.getDeclarationData();
0408: boolean local = variableValues[variableCombo.getSelectedIndex()];
0409: int modifiers;
0410: if (local != decl.local) {
0411: modifiers = local ? lastLocalModifiers : lastFieldModifiers;
0412: if (finalCheckBox.isSelected()) // only final makes sense for both local and field scope
0413: modifiers |= Modifier.FINAL;
0414: else
0415: modifiers &= ~Modifier.FINAL;
0416: } else {
0417: modifiers = accessValues[accessCombo.getSelectedIndex()];
0418: if (staticCheckBox.isSelected())
0419: modifiers |= Modifier.STATIC;
0420: if (finalCheckBox.isSelected())
0421: modifiers |= Modifier.FINAL;
0422: if (transientCheckBox.isSelected())
0423: modifiers |= Modifier.TRANSIENT;
0424: if (volatileCheckBox.isSelected())
0425: modifiers |= Modifier.VOLATILE;
0426: if (local)
0427: modifiers &= ~(Modifier.STATIC | Modifier.TRANSIENT | Modifier.VOLATILE);
0428: }
0429: decl.local = local;
0430: decl.modifiers = modifiers;
0431:
0432: return codeData;
0433: }
0434:
0435: private void retreiveCodeData(CodeCategory category) {
0436: int gCount = codeData.getGuardedBlockCount(category);
0437: for (int i = 0; i < gCount; i++) {
0438: retreiveEditableBlock(category, i);
0439: retreiveGuardedBlock(category, i);
0440: }
0441: if (gCount > 0)
0442: retreiveEditableBlock(category, gCount);
0443: }
0444:
0445: private void retreiveEditableBlock(CodeCategory category, int index) {
0446: CodeEntry[] entries = codeData
0447: .getEditableBlock(category, index).getEntries();
0448: for (CodeEntry e : entries) {
0449: e.setCode(null);
0450: }
0451:
0452: int[] blockBounds = getEditBlockBounds(category, index);
0453: Document doc = getDocument(category);
0454:
0455: try {
0456: String allCode = doc.getText(blockBounds[0], blockBounds[1]
0457: - blockBounds[0]);
0458: if (allCode.trim().equals("")) // NOI18N
0459: return;
0460:
0461: StringBuilder buf = new StringBuilder();
0462: int selIndex = -1;
0463: EditableLine nextLine = null;
0464: Iterator<EditableLine> it = getEditInfos(category)[index].lines
0465: .iterator();
0466: while (it.hasNext() || nextLine != null) {
0467: EditableLine l = nextLine != null ? nextLine : it
0468: .next();
0469: int startPos = l.getPosition().getOffset();
0470: int endPos;
0471: if (it.hasNext()) {
0472: nextLine = it.next();
0473: endPos = nextLine.getPosition().getOffset();
0474: } else {
0475: nextLine = null;
0476: endPos = blockBounds[1];
0477: }
0478: buf.append(doc.getText(startPos, endPos - startPos));
0479: if (nextLine == null
0480: || nextLine.getSelectedIndex() != l
0481: .getSelectedIndex()) {
0482: String code = buf.toString().trim();
0483: if (!code.equals("")) // NOI18N
0484: entries[l.getSelectedIndex()].setCode(code);
0485: buf.delete(0, buf.length());
0486: }
0487: }
0488: } catch (BadLocationException ex) { // should not happen
0489: ErrorManager.getDefault().notify(
0490: ErrorManager.INFORMATIONAL, ex);
0491: }
0492: }
0493:
0494: private void retreiveGuardedBlock(CodeCategory category, int index) {
0495: GuardedBlock gBlock = codeData.getGuardedBlock(category, index);
0496: if (!gBlock.isCustomizable())
0497: return;
0498:
0499: if (getGuardInfos(category)[index].customized) {
0500: Document doc = getDocument(category);
0501: int[] blockBounds = getGuardBlockBounds(category, index);
0502: try {
0503: int startPos = blockBounds[0]
0504: + gBlock.getHeaderLength();
0505: String code = doc.getText(startPos, blockBounds[1]
0506: - gBlock.getFooterLength() - startPos);
0507: gBlock.setCustomizedCode(code);
0508: } catch (BadLocationException ex) { // should not happen
0509: ErrorManager.getDefault().notify(
0510: ErrorManager.INFORMATIONAL, ex);
0511: }
0512: } else { // reset to default code
0513: gBlock.setCustomizedCode(null);
0514: }
0515: }
0516:
0517: private void selectInComboBox(JComboBox combo, Object item) {
0518: ignoreComboAction = true;
0519: combo.setSelectedItem(item);
0520: ignoreComboAction = false;
0521: }
0522:
0523: // -----
0524: // mapping methods
0525:
0526: private JTextComponent getEditor(CodeCategory category) {
0527: switch (category) {
0528: case CREATE_AND_INIT:
0529: return initCodeEditor;
0530: case DECLARATION:
0531: return declareCodeEditor;
0532: }
0533: return null;
0534: }
0535:
0536: private Document getDocument(CodeCategory category) {
0537: return getEditor(category).getDocument();
0538: }
0539:
0540: private EditBlockInfo[] getEditInfos(CodeCategory category) {
0541: return editBlockInfos.get(category);
0542: }
0543:
0544: private GuardBlockInfo[] getGuardInfos(CodeCategory category) {
0545: return guardBlockInfos.get(category);
0546: }
0547:
0548: private JPanel getGutter(Document doc) {
0549: if (doc == initCodeEditor.getDocument())
0550: return initGutter;
0551: if (doc == declareCodeEditor.getDocument())
0552: return declareGutter;
0553: return null;
0554: }
0555:
0556: private CodeCategory getCategoryForDocument(Document doc) {
0557: if (doc == initCodeEditor.getDocument())
0558: return CodeCategory.CREATE_AND_INIT;
0559: if (doc == declareCodeEditor.getDocument())
0560: return CodeCategory.DECLARATION;
0561: return null;
0562: }
0563:
0564: private static Element getRootElement(Document doc) {
0565: return doc.getRootElements()[0];
0566: }
0567:
0568: // -----
0569:
0570: private class EditableLine {
0571: private Position position;
0572: private JComboBox targetCombo;
0573: private List<EditableLine> linesInBlock;
0574: private CodeEntry[] codeEntries;
0575:
0576: EditableLine(Position pos, EditableBlock eBlock, int selIndex,
0577: List<EditableLine> lines) {
0578: position = pos;
0579: linesInBlock = lines;
0580: codeEntries = eBlock.getEntries();
0581: targetCombo = new JComboBox(codeEntries);
0582: setSelectedIndex(selIndex);
0583: targetCombo.getAccessibleContext().setAccessibleName(
0584: codeEntries[selIndex].getName());
0585: targetCombo.setToolTipText(codeEntries[selIndex]
0586: .getToolTipText());
0587: targetCombo.addActionListener(new EditSwitchL());
0588: }
0589:
0590: Position getPosition() {
0591: return position;
0592: }
0593:
0594: Component getGutterComponent() {
0595: return targetCombo;
0596: }
0597:
0598: // if having visible combobox in gutter
0599: boolean isVisible() {
0600: return targetCombo.getParent() != null
0601: && targetCombo.isVisible();
0602: }
0603:
0604: int getSelectedIndex() {
0605: return targetCombo.getSelectedIndex();
0606: }
0607:
0608: void setSelectedIndex(int index) {
0609: selectInComboBox(targetCombo, targetCombo.getItemAt(index));
0610: }
0611:
0612: class EditSwitchL implements ActionListener {
0613: public void actionPerformed(ActionEvent e) {
0614: if (ignoreComboAction)
0615: return; // not invoked by user, ignore
0616:
0617: changed = true;
0618: // go through all comboboxes in the group and correct selected index
0619: // according to the selection in this combobox (preceding comboboxes
0620: // can't have bigger index and subsequent can't have smaller index)
0621: int selectedIndex = targetCombo.getSelectedIndex();
0622: boolean preceding = true;
0623: for (EditableLine l : linesInBlock) {
0624: if (l != EditableLine.this ) {
0625: if ((preceding && l.getSelectedIndex() > selectedIndex)
0626: || (!preceding && l.getSelectedIndex() < selectedIndex)) { // correct selected index
0627: l.setSelectedIndex(selectedIndex);
0628: }
0629: } else
0630: preceding = false;
0631: }
0632: targetCombo.setToolTipText(codeEntries[selectedIndex]
0633: .getToolTipText());
0634: }
0635: }
0636: }
0637:
0638: private boolean updateGutterComponents(List<EditableLine> lines,
0639: Document doc, int startIndex, int endIndex) {
0640: String text;
0641: try {
0642: text = doc.getText(startIndex, endIndex - startIndex);
0643: } catch (BadLocationException ex) { // should not happen
0644: ErrorManager.getDefault().notify(
0645: ErrorManager.INFORMATIONAL, ex);
0646: return false;
0647: }
0648:
0649: boolean visibility = !text.trim().equals(""); // NOI18N
0650: int prevSelectedIndex = 0;
0651: boolean changed = false;
0652: Container gutter = getGutter(doc);
0653: for (EditableLine l : lines) {
0654: // make sure the selected index is correct (ascending in the group)
0655: if (l.getSelectedIndex() < prevSelectedIndex)
0656: l.setSelectedIndex(prevSelectedIndex);
0657: else
0658: prevSelectedIndex = l.getSelectedIndex();
0659: // add the component to the gutter if not there yet
0660: Component comp = l.getGutterComponent();
0661: if (comp.getParent() == null)
0662: gutter.add(comp, l.getPosition());
0663: // show/hide the component
0664: if (visibility != l.isVisible()) {
0665: comp.setVisible(visibility);
0666: changed = true;
0667: }
0668: }
0669: return changed;
0670: }
0671:
0672: // -----
0673: // document changes
0674:
0675: private class DocumentL implements DocumentListener {
0676: boolean active = true;
0677:
0678: public void insertUpdate(DocumentEvent e) {
0679: if (active)
0680: contentChange(e);
0681: }
0682:
0683: public void removeUpdate(DocumentEvent e) {
0684: if (active)
0685: contentChange(e);
0686: }
0687:
0688: public void changedUpdate(DocumentEvent e) {
0689: }
0690:
0691: private void contentChange(DocumentEvent e) {
0692: changed = true;
0693:
0694: Document doc = e.getDocument();
0695: CodeCategory category = getCategoryForDocument(doc);
0696: int eBlockIndex = getEditBlockIndex(category, e.getOffset());
0697: if (eBlockIndex < 0)
0698: return;
0699:
0700: List<EditableLine> lines = getEditInfos(category)[eBlockIndex].lines;
0701: int[] blockBounds = getEditBlockBounds(category,
0702: eBlockIndex);
0703:
0704: boolean repaint = false;
0705: DocumentEvent.ElementChange change = e
0706: .getChange(getRootElement(doc));
0707: if (change != null) {
0708: Element[] added = change.getChildrenAdded();
0709: Element[] removed = change.getChildrenRemoved();
0710: if (added.length != removed.length) {
0711: Element rootEl = getRootElement(doc);
0712: int elIndex = rootEl.getElementIndex(e.getOffset());
0713: if (added.length > removed.length) { // lines added
0714: processAddedLines(rootEl, elIndex, lines,
0715: blockBounds, codeData.getEditableBlock(
0716: category, eBlockIndex));
0717: repaint = true;
0718: } else if (added.length < removed.length) { // lines removed
0719: processRemovedLines(rootEl.getElement(elIndex),
0720: lines, blockBounds);
0721: if (blockBounds[0] == blockBounds[1]) { // whole block's text deleted
0722: try { // keep one empty line
0723: doc.insertString(blockBounds[0], "\n",
0724: null); // NOI18N
0725: getEditor(category).setCaretPosition(
0726: blockBounds[0]);
0727: } catch (BadLocationException ex) { // should not happen
0728: ErrorManager.getDefault().notify(
0729: ErrorManager.INFORMATIONAL, ex);
0730: }
0731: return; // is updated with adding the new line
0732: }
0733: repaint = true;
0734: }
0735: }
0736: }
0737:
0738: repaint |= updateGutterComponents(lines, doc,
0739: blockBounds[0], blockBounds[1]);
0740:
0741: if (repaint) {
0742: JPanel gutter = getGutter(doc);
0743: gutter.revalidate();
0744: gutter.repaint();
0745: }
0746: ((BaseDocument) doc).resetUndoMerge();
0747: }
0748: }
0749:
0750: private void processAddedLines(Element rootEl, int elIndex,
0751: List<EditableLine> lines, int[] blockBounds,
0752: EditableBlock eBlock) {
0753: Document doc = rootEl.getDocument();
0754: int elPos = rootEl.getElement(elIndex).getStartOffset();
0755:
0756: // determine where to insert new lines (and which target should they have selected)
0757: int endPos = -1;
0758: int selIndex = -1;
0759: ListIterator<EditableLine> lineIt = lines.listIterator();
0760: while (lineIt.hasNext()) {
0761: EditableLine l = lineIt.next();
0762: int pos = l.getPosition().getOffset();
0763: if (pos > elPos) {
0764: endPos = pos;
0765: lineIt.previous();
0766: break;
0767: } else {
0768: selIndex = l.getSelectedIndex();
0769: if (pos == elPos) { // already have line for this element (Enter on empty line)
0770: elIndex++;
0771: elPos = rootEl.getElement(elIndex).getStartOffset();
0772: }
0773: }
0774: }
0775: if (endPos < 0) { // adding at the end of the block, don't have boundary line
0776: endPos = blockBounds[1];
0777: }
0778: if (selIndex < 0) {
0779: selIndex = eBlock.getPreferredEntryIndex();
0780: }
0781:
0782: // now create the missing lines
0783: try {
0784: do {
0785: Position pos = NbDocument.createPosition(doc, elPos,
0786: Position.Bias.Backward);
0787: lineIt.add(new EditableLine(pos, eBlock, selIndex,
0788: lines));
0789: if (++elIndex >= rootEl.getElementCount())
0790: break;
0791: elPos = rootEl.getElement(elIndex).getStartOffset();
0792: } while (elPos < endPos);
0793: } catch (BadLocationException ex) { // should not happen
0794: ErrorManager.getDefault().notify(
0795: ErrorManager.INFORMATIONAL, ex);
0796: }
0797: }
0798:
0799: private void processRemovedLines(Element changeEl,
0800: List<EditableLine> lines, int[] blockBounds) {
0801: Document doc = changeEl.getDocument();
0802: int startPos = changeEl.getStartOffset();
0803: int endPos = changeEl.getEndOffset();
0804:
0805: // determine the lines to remove (their positons lie in the affected element)
0806: EditableLine firstLine = null;
0807: EditableLine lastLine = null;
0808: Iterator<EditableLine> it = lines.iterator();
0809: while (it.hasNext()) {
0810: EditableLine l = it.next();
0811: int pos = l.getPosition().getOffset();
0812: if (pos >= startPos) {
0813: if (pos >= endPos)
0814: break;
0815: if (firstLine == null)
0816: firstLine = l;
0817: lastLine = l;
0818: }
0819: }
0820: if (firstLine == null)
0821: return; // no lines affected
0822:
0823: boolean wholeFirstLine = lastLine.getPosition().getOffset() == startPos;
0824: boolean mergedToGuarded = startPos == blockBounds[1]
0825: && startPos != doc.getLength();
0826:
0827: // remove the lines
0828: it = lines.iterator();
0829: while (it.hasNext() && lastLine != null) {
0830: EditableLine l = it.next();
0831: boolean remove;
0832: if (l == firstLine) {
0833: remove = mergedToGuarded || wholeFirstLine;
0834: firstLine = null;
0835: } else if (l == lastLine) {
0836: remove = mergedToGuarded || !wholeFirstLine;
0837: lastLine = null;
0838: } else {
0839: remove = firstLine == null; // all lines in between
0840: }
0841: if (remove) {
0842: it.remove();
0843: Component comp = l.getGutterComponent();
0844: comp.getParent().remove(comp);
0845: }
0846: }
0847: }
0848:
0849: private int[] getEditBlockBounds(CodeCategory category, int index) {
0850: int startIndex = getEditInfos(category)[index].position
0851: .getOffset();
0852: GuardBlockInfo[] gInfos = getGuardInfos(category);
0853: int endIndex = index < gInfos.length ? gInfos[index].position
0854: .getOffset() : getDocument(category).getLength();
0855: return new int[] { startIndex, endIndex };
0856: }
0857:
0858: private int getEditBlockIndex(CodeCategory category, int offset) {
0859: return getBlockIndex(category, offset, true);
0860: }
0861:
0862: private int getGuardBlockIndex(CodeCategory category, int offset) {
0863: return getBlockIndex(category, offset, false);
0864: }
0865:
0866: private int getBlockIndex(CodeCategory category, int offset,
0867: boolean editable) {
0868: EditBlockInfo[] editInfos = getEditInfos(category);
0869: GuardBlockInfo[] guardInfos = getGuardInfos(category);
0870: // assuming editInfo.length == guardInfos.length + 1
0871: for (int i = 0; i < guardInfos.length; i++) {
0872: int editPos = editInfos[i].position.getOffset();
0873: if (editPos > offset) // the offset lies in preceding guarded block
0874: return editable ? -1 : i - 1;
0875: if (editPos == offset
0876: || guardInfos[i].position.getOffset() >= offset)
0877: return editable ? i : -1; // the offset lies in this editable block
0878: }
0879: // otherwise the offset is in the last editable block
0880: return editable ? editInfos.length - 1 : -1;
0881: }
0882:
0883: // -----
0884:
0885: private class GuardSwitchL implements ActionListener {
0886: CodeCategory category;
0887: int blockIndex;
0888:
0889: GuardSwitchL(CodeCategory cat, int index) {
0890: category = cat;
0891: blockIndex = index;
0892: }
0893:
0894: public void actionPerformed(ActionEvent e) {
0895: if (ignoreComboAction)
0896: return; // not invoked by user, ignore
0897:
0898: GuardedBlock gBlock = codeData.getGuardedBlock(category,
0899: blockIndex);
0900: GuardBlockInfo gInfo = getGuardInfos(category)[blockIndex];
0901: int[] blockBounds = getGuardBlockBounds(category,
0902: blockIndex);
0903: int startOffset = blockBounds[0];
0904: int endOffset = blockBounds[1];
0905: int gHead = gBlock.getHeaderLength();
0906: int gFoot = gBlock.getFooterLength();
0907: JTextComponent editor = getEditor(category);
0908: StyledDocument doc = (StyledDocument) editor.getDocument();
0909:
0910: changed = true;
0911:
0912: JComboBox combo = (JComboBox) e.getSource();
0913: try {
0914: docListener.active = false;
0915: if (combo.getSelectedIndex() == 1) { // changing from default to custom
0916: NbDocument.unmarkGuarded(doc, startOffset,
0917: endOffset - startOffset);
0918: // keep last '\n' so we don't destroy next editable block's position
0919: doc
0920: .remove(startOffset, endOffset
0921: - startOffset - 1);
0922: // insert the custom code into the document
0923: String customCode = gBlock.getCustomCode();
0924: int customLength = customCode.length();
0925: if (gInfo.customizedCode != null) { // already was edited before
0926: customCode = customCode.substring(0, gHead)
0927: + gInfo.customizedCode
0928: + customCode.substring(customLength
0929: - gFoot);
0930: customLength = customCode.length();
0931: }
0932: if (customCode.endsWith("\n")) // NOI18N
0933: customCode = customCode.substring(0,
0934: customLength - 1);
0935: doc.insertString(startOffset, customCode, null);
0936: gInfo.customized = true;
0937: // make guarded "header" and "footer", select the text in between
0938: NbDocument.markGuarded(doc, startOffset, gHead);
0939: NbDocument.markGuarded(doc, startOffset
0940: + customLength - gFoot, gFoot);
0941: editor.setSelectionStart(startOffset + gHead);
0942: editor.setSelectionEnd(startOffset + customLength
0943: - gFoot);
0944: editor.requestFocus();
0945: combo.setToolTipText(gBlock.getCustomEntry()
0946: .getToolTipText());
0947: } else { // changing from custom to default
0948: // remember the customized code
0949: gInfo.customizedCode = doc.getText(startOffset
0950: + gHead, endOffset - gFoot - gHead
0951: - startOffset);
0952: NbDocument.unmarkGuarded(doc, endOffset - gFoot,
0953: gFoot);
0954: NbDocument.unmarkGuarded(doc, startOffset, gHead);
0955: // keep last '\n' so we don't destroy next editable block's position
0956: doc
0957: .remove(startOffset, endOffset
0958: - startOffset - 1);
0959: String defaultCode = gBlock.getDefaultCode();
0960: if (defaultCode.endsWith("\n")) // NOI18N
0961: defaultCode = defaultCode.substring(0,
0962: defaultCode.length() - 1);
0963: doc.insertString(startOffset, defaultCode, null);
0964: gInfo.customized = false;
0965: // make the whole text guarded, cancel selection
0966: NbDocument.markGuarded(doc, startOffset,
0967: defaultCode.length() + 1); // including '\n'
0968: if (editor.getSelectionStart() >= startOffset
0969: && editor.getSelectionEnd() <= endOffset)
0970: editor.setCaretPosition(startOffset);
0971: combo.setToolTipText(NbBundle.getMessage(
0972: CustomCodeData.class,
0973: "CTL_GuardCombo_Default_Hint")); // NOI18N
0974: }
0975: // we must create a new Position - current was moved away by inserting new string on it
0976: gInfo.position = NbDocument.createPosition(doc,
0977: startOffset, Position.Bias.Forward);
0978:
0979: docListener.active = true;
0980: } catch (BadLocationException ex) { // should not happen
0981: ErrorManager.getDefault().notify(
0982: ErrorManager.INFORMATIONAL, ex);
0983: }
0984: }
0985: }
0986:
0987: private int[] getGuardBlockBounds(CodeCategory category, int index) {
0988: int startIndex = getGuardInfos(category)[index].position
0989: .getOffset();
0990: int endIndex = getEditInfos(category)[index + 1].position
0991: .getOffset();
0992: return new int[] { startIndex, endIndex };
0993: }
0994:
0995: // -----
0996:
0997: private static class GutterLayout implements LayoutManager2 {
0998:
0999: private JTextComponent editor;
1000: private Map<Component, Position> positions;
1001: private int lineHeight = -1;
1002:
1003: private static final int LEFT_GAP = 2;
1004: private static final int RIGHT_GAP = 4;
1005:
1006: GutterLayout(JTextComponent editor,
1007: Map<Component, Position> positionMap) {
1008: this .editor = editor;
1009: this .positions = positionMap;
1010: }
1011:
1012: public void addLayoutComponent(Component comp,
1013: Object constraints) {
1014: positions.put(comp, (Position) constraints);
1015: }
1016:
1017: public void layoutContainer(Container parent) {
1018: StyledDocument doc = (StyledDocument) editor.getDocument();
1019: for (Component comp : parent.getComponents()) {
1020: Position pos = positions.get(comp);
1021: int line = NbDocument.findLineNumber(doc, pos
1022: .getOffset());
1023: Dimension prefSize = comp.getPreferredSize();
1024: int dy = lineHeight() - prefSize.height;
1025: dy = dy > 0 ? dy / 2 + 1 : 0;
1026: comp.setBounds(LEFT_GAP, line * lineHeight() + dy,
1027: parent.getWidth() - LEFT_GAP - RIGHT_GAP, Math
1028: .min(prefSize.height, lineHeight()));
1029: }
1030: }
1031:
1032: public void removeLayoutComponent(Component comp) {
1033: positions.remove(comp);
1034: }
1035:
1036: public Dimension preferredLayoutSize(Container parent) {
1037: int prefWidth = 0;
1038: for (Component comp : positions.keySet()) {
1039: Dimension prefSize = comp.getPreferredSize();
1040: if (prefSize.width > prefWidth)
1041: prefWidth = prefSize.width;
1042: }
1043: return new Dimension(prefWidth + LEFT_GAP + RIGHT_GAP,
1044: editor.getPreferredSize().height);
1045: }
1046:
1047: public Dimension minimumLayoutSize(Container parent) {
1048: return preferredLayoutSize(parent);
1049: }
1050:
1051: public Dimension maximumLayoutSize(Container parent) {
1052: return preferredLayoutSize(parent);
1053: }
1054:
1055: public float getLayoutAlignmentX(Container target) {
1056: return .5f;
1057: }
1058:
1059: public float getLayoutAlignmentY(Container target) {
1060: return .5f;
1061: }
1062:
1063: public void invalidateLayout(Container target) {
1064: }
1065:
1066: public void addLayoutComponent(String name, Component comp) {
1067: }
1068:
1069: // -----
1070:
1071: private int lineHeight() {
1072: if (lineHeight < 0) {
1073: int lines = getLineCount(editor.getDocument());
1074: lineHeight = editor.getPreferredSize().height / lines;
1075: }
1076: return lineHeight;
1077: }
1078: }
1079:
1080: private static int getLineCount(Document doc) {
1081: return getRootElement(doc).getElementCount();
1082: }
1083:
1084: /** This method is called from within the constructor to
1085: * initialize the form.
1086: * WARNING: Do NOT modify this code. The content of this method is
1087: * always regenerated by the Form Editor.
1088: */
1089: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
1090: private void initComponents() {
1091:
1092: javax.swing.JLabel initCodeLabel = new javax.swing.JLabel();
1093: jScrollPane1 = new javax.swing.JScrollPane();
1094: initCodeEditor = new javax.swing.JEditorPane();
1095: javax.swing.JLabel declarationCodeLabel = new javax.swing.JLabel();
1096: jScrollPane2 = new javax.swing.JScrollPane();
1097: declareCodeEditor = new javax.swing.JEditorPane();
1098: javax.swing.JLabel selectComponentLabel = new javax.swing.JLabel();
1099: componentCombo = new javax.swing.JComboBox();
1100: renameButton = new javax.swing.JButton();
1101: javax.swing.JLabel variableScopeLabel = new javax.swing.JLabel();
1102: variableCombo = new javax.swing.JComboBox();
1103: javax.swing.JLabel variableAccessLabel = new javax.swing.JLabel();
1104: accessCombo = new javax.swing.JComboBox();
1105: staticCheckBox = new javax.swing.JCheckBox();
1106: finalCheckBox = new javax.swing.JCheckBox();
1107: transientCheckBox = new javax.swing.JCheckBox();
1108: volatileCheckBox = new javax.swing.JCheckBox();
1109:
1110: FormListener formListener = new FormListener();
1111:
1112: initCodeLabel.setFont(initCodeLabel.getFont()
1113: .deriveFont(
1114: initCodeLabel.getFont().getStyle()
1115: | java.awt.Font.BOLD));
1116: initCodeLabel.setLabelFor(initCodeEditor);
1117: org.openide.awt.Mnemonics.setLocalizedText(initCodeLabel,
1118: org.openide.util.NbBundle.getMessage(
1119: CustomCodeView.class,
1120: "CustomCodeView.initCodeLabel.text")); // NOI18N
1121:
1122: jScrollPane1.setViewportView(initCodeEditor);
1123:
1124: declarationCodeLabel.setFont(declarationCodeLabel.getFont()
1125: .deriveFont(
1126: declarationCodeLabel.getFont().getStyle()
1127: | java.awt.Font.BOLD));
1128: declarationCodeLabel.setLabelFor(declareCodeEditor);
1129: org.openide.awt.Mnemonics.setLocalizedText(
1130: declarationCodeLabel,
1131: org.openide.util.NbBundle.getMessage(
1132: CustomCodeView.class,
1133: "CustomCodeView.declarationCodeLabel.text")); // NOI18N
1134:
1135: jScrollPane2.setViewportView(declareCodeEditor);
1136:
1137: selectComponentLabel.setLabelFor(componentCombo);
1138: org.openide.awt.Mnemonics.setLocalizedText(
1139: selectComponentLabel,
1140: org.openide.util.NbBundle.getMessage(
1141: CustomCodeView.class,
1142: "CustomCodeView.selectComponentLabel.text")); // NOI18N
1143:
1144: componentCombo.setToolTipText(org.openide.util.NbBundle
1145: .getMessage(CustomCodeView.class,
1146: "CustomCodeView.componentCombo.toolTipText")); // NOI18N
1147: componentCombo.addActionListener(formListener);
1148:
1149: org.openide.awt.Mnemonics.setLocalizedText(renameButton,
1150: org.openide.util.NbBundle.getMessage(
1151: CustomCodeView.class,
1152: "CustomCodeView.renameButton.text")); // NOI18N
1153: renameButton.setToolTipText(org.openide.util.NbBundle
1154: .getMessage(CustomCodeView.class,
1155: "CustomCodeView.renameButton.toolTipText")); // NOI18N
1156: renameButton.addActionListener(formListener);
1157:
1158: variableScopeLabel.setLabelFor(variableCombo);
1159: org.openide.awt.Mnemonics.setLocalizedText(variableScopeLabel,
1160: org.openide.util.NbBundle.getMessage(
1161: CustomCodeView.class,
1162: "CustomCodeView.variableScopeLabel.text")); // NOI18N
1163:
1164: variableCombo.addActionListener(formListener);
1165:
1166: variableAccessLabel.setLabelFor(accessCombo);
1167: org.openide.awt.Mnemonics.setLocalizedText(variableAccessLabel,
1168: org.openide.util.NbBundle.getMessage(
1169: CustomCodeView.class,
1170: "CustomCodeView.variableAccessLabel.text")); // NOI18N
1171:
1172: accessCombo.addActionListener(formListener);
1173:
1174: org.openide.awt.Mnemonics.setLocalizedText(staticCheckBox,
1175: "&static"); // NOI18N
1176: staticCheckBox.setBorder(javax.swing.BorderFactory
1177: .createEmptyBorder(0, 0, 0, 0));
1178: staticCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
1179: staticCheckBox.addActionListener(formListener);
1180:
1181: org.openide.awt.Mnemonics.setLocalizedText(finalCheckBox,
1182: "&final"); // NOI18N
1183: finalCheckBox.setBorder(javax.swing.BorderFactory
1184: .createEmptyBorder(0, 0, 0, 0));
1185: finalCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
1186: finalCheckBox.addActionListener(formListener);
1187:
1188: org.openide.awt.Mnemonics.setLocalizedText(transientCheckBox,
1189: "&transient"); // NOI18N
1190: transientCheckBox.setBorder(javax.swing.BorderFactory
1191: .createEmptyBorder(0, 0, 0, 0));
1192: transientCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
1193: transientCheckBox.addActionListener(formListener);
1194:
1195: org.openide.awt.Mnemonics.setLocalizedText(volatileCheckBox,
1196: "v&olatile"); // NOI18N
1197: volatileCheckBox.setBorder(javax.swing.BorderFactory
1198: .createEmptyBorder(0, 0, 0, 0));
1199: volatileCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
1200: volatileCheckBox.addActionListener(formListener);
1201:
1202: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
1203: this );
1204: this .setLayout(layout);
1205: layout
1206: .setHorizontalGroup(layout
1207: .createParallelGroup(
1208: org.jdesktop.layout.GroupLayout.LEADING)
1209: .add(
1210: org.jdesktop.layout.GroupLayout.TRAILING,
1211: layout
1212: .createSequentialGroup()
1213: .addContainerGap()
1214: .add(
1215: layout
1216: .createParallelGroup(
1217: org.jdesktop.layout.GroupLayout.TRAILING)
1218: .add(
1219: org.jdesktop.layout.GroupLayout.LEADING,
1220: jScrollPane1,
1221: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1222: 680,
1223: Short.MAX_VALUE)
1224: .add(
1225: org.jdesktop.layout.GroupLayout.LEADING,
1226: layout
1227: .createSequentialGroup()
1228: .add(
1229: variableScopeLabel)
1230: .addPreferredGap(
1231: org.jdesktop.layout.LayoutStyle.RELATED)
1232: .add(
1233: variableCombo,
1234: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1235: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1236: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1237: .addPreferredGap(
1238: org.jdesktop.layout.LayoutStyle.UNRELATED)
1239: .add(
1240: variableAccessLabel)
1241: .addPreferredGap(
1242: org.jdesktop.layout.LayoutStyle.RELATED)
1243: .add(
1244: accessCombo,
1245: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1246: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1247: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1248: .addPreferredGap(
1249: org.jdesktop.layout.LayoutStyle.UNRELATED)
1250: .add(
1251: finalCheckBox)
1252: .addPreferredGap(
1253: org.jdesktop.layout.LayoutStyle.RELATED)
1254: .add(
1255: staticCheckBox)
1256: .addPreferredGap(
1257: org.jdesktop.layout.LayoutStyle.RELATED)
1258: .add(
1259: transientCheckBox)
1260: .addPreferredGap(
1261: org.jdesktop.layout.LayoutStyle.RELATED)
1262: .add(
1263: volatileCheckBox))
1264: .add(
1265: org.jdesktop.layout.GroupLayout.LEADING,
1266: jScrollPane2,
1267: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1268: 680,
1269: Short.MAX_VALUE)
1270: .add(
1271: org.jdesktop.layout.GroupLayout.LEADING,
1272: declarationCodeLabel)
1273: .add(
1274: org.jdesktop.layout.GroupLayout.LEADING,
1275: layout
1276: .createSequentialGroup()
1277: .add(
1278: selectComponentLabel)
1279: .addPreferredGap(
1280: org.jdesktop.layout.LayoutStyle.RELATED)
1281: .add(
1282: componentCombo,
1283: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1284: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1285: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1286: .addPreferredGap(
1287: org.jdesktop.layout.LayoutStyle.RELATED)
1288: .add(
1289: renameButton))
1290: .add(
1291: org.jdesktop.layout.GroupLayout.LEADING,
1292: initCodeLabel))
1293: .addContainerGap()));
1294:
1295: layout.linkSize(new java.awt.Component[] { accessCombo,
1296: variableCombo },
1297: org.jdesktop.layout.GroupLayout.HORIZONTAL);
1298:
1299: layout
1300: .setVerticalGroup(layout
1301: .createParallelGroup(
1302: org.jdesktop.layout.GroupLayout.LEADING)
1303: .add(
1304: layout
1305: .createSequentialGroup()
1306: .addContainerGap()
1307: .add(
1308: layout
1309: .createParallelGroup(
1310: org.jdesktop.layout.GroupLayout.BASELINE)
1311: .add(
1312: selectComponentLabel)
1313: .add(
1314: componentCombo,
1315: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1316: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1317: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1318: .add(
1319: renameButton))
1320: .addPreferredGap(
1321: org.jdesktop.layout.LayoutStyle.RELATED,
1322: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1323: Short.MAX_VALUE)
1324: .add(initCodeLabel)
1325: .addPreferredGap(
1326: org.jdesktop.layout.LayoutStyle.RELATED)
1327: .add(
1328: jScrollPane1,
1329: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1330: 348, Short.MAX_VALUE)
1331: .add(11, 11, 11)
1332: .add(declarationCodeLabel)
1333: .addPreferredGap(
1334: org.jdesktop.layout.LayoutStyle.RELATED)
1335: .add(
1336: jScrollPane2,
1337: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1338: 58,
1339: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1340: .addPreferredGap(
1341: org.jdesktop.layout.LayoutStyle.RELATED)
1342: .add(
1343: layout
1344: .createParallelGroup(
1345: org.jdesktop.layout.GroupLayout.BASELINE)
1346: .add(
1347: variableScopeLabel)
1348: .add(
1349: variableCombo,
1350: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1351: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1352: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1353: .add(
1354: variableAccessLabel)
1355: .add(
1356: accessCombo,
1357: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
1358: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
1359: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
1360: .add(
1361: staticCheckBox)
1362: .add(
1363: finalCheckBox)
1364: .add(
1365: transientCheckBox)
1366: .add(
1367: volatileCheckBox))
1368: .addContainerGap()));
1369:
1370: staticCheckBox
1371: .getAccessibleContext()
1372: .setAccessibleDescription(
1373: org.openide.util.NbBundle
1374: .getMessage(CustomCodeView.class,
1375: "CustomCodeView.staticCheckBox.accessibleDescription")); // NOI18N
1376: finalCheckBox
1377: .getAccessibleContext()
1378: .setAccessibleDescription(
1379: org.openide.util.NbBundle
1380: .getMessage(CustomCodeView.class,
1381: "CustomCodeView.finalCheckBox.accessibleDescription")); // NOI18N
1382: transientCheckBox
1383: .getAccessibleContext()
1384: .setAccessibleDescription(
1385: org.openide.util.NbBundle
1386: .getMessage(CustomCodeView.class,
1387: "CustomCodeView.transientCheckBox.accessibleDescription")); // NOI18N
1388: volatileCheckBox
1389: .getAccessibleContext()
1390: .setAccessibleDescription(
1391: org.openide.util.NbBundle
1392: .getMessage(CustomCodeView.class,
1393: "CustomCodeView.volatileCheckBox.accessibleDescription")); // NOI18N
1394:
1395: getAccessibleContext().setAccessibleDescription(
1396: org.openide.util.NbBundle.getMessage(
1397: CustomCodeView.class,
1398: "CustomCodeView.accessibleDescription")); // NOI18N
1399: }
1400:
1401: // Code for dispatching events from components to event handlers.
1402:
1403: private class FormListener implements java.awt.event.ActionListener {
1404: FormListener() {
1405: }
1406:
1407: public void actionPerformed(java.awt.event.ActionEvent evt) {
1408: if (evt.getSource() == componentCombo) {
1409: CustomCodeView.this .componentComboActionPerformed(evt);
1410: } else if (evt.getSource() == renameButton) {
1411: CustomCodeView.this .renameButtonActionPerformed(evt);
1412: } else if (evt.getSource() == variableCombo) {
1413: CustomCodeView.this .declControlActionPerformed(evt);
1414: } else if (evt.getSource() == accessCombo) {
1415: CustomCodeView.this .declControlActionPerformed(evt);
1416: } else if (evt.getSource() == staticCheckBox) {
1417: CustomCodeView.this .declControlActionPerformed(evt);
1418: } else if (evt.getSource() == finalCheckBox) {
1419: CustomCodeView.this .declControlActionPerformed(evt);
1420: } else if (evt.getSource() == transientCheckBox) {
1421: CustomCodeView.this .declControlActionPerformed(evt);
1422: } else if (evt.getSource() == volatileCheckBox) {
1423: CustomCodeView.this .declControlActionPerformed(evt);
1424: }
1425: }
1426: }// </editor-fold>//GEN-END:initComponents
1427:
1428: private void declControlActionPerformed(
1429: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_declControlActionPerformed
1430: if (ignoreComboAction)
1431: return; // not invoked by user, ignore
1432:
1433: changed = true;
1434: controller.declarationChanged();
1435: }//GEN-LAST:event_declControlActionPerformed
1436:
1437: private void renameButtonActionPerformed(
1438: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameButtonActionPerformed
1439: controller.renameInvoked();
1440: }//GEN-LAST:event_renameButtonActionPerformed
1441:
1442: private void componentComboActionPerformed(
1443: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_componentComboActionPerformed
1444: if (ignoreComboAction)
1445: return; // not invoked by user, ignore
1446:
1447: controller.componentExchanged((String) componentCombo
1448: .getSelectedItem());
1449: }//GEN-LAST:event_componentComboActionPerformed
1450:
1451: // Variables declaration - do not modify//GEN-BEGIN:variables
1452: private javax.swing.JComboBox accessCombo;
1453: private javax.swing.JComboBox componentCombo;
1454: private javax.swing.JEditorPane declareCodeEditor;
1455: private javax.swing.JCheckBox finalCheckBox;
1456: private javax.swing.JEditorPane initCodeEditor;
1457: private javax.swing.JScrollPane jScrollPane1;
1458: private javax.swing.JScrollPane jScrollPane2;
1459: private javax.swing.JButton renameButton;
1460: private javax.swing.JCheckBox staticCheckBox;
1461: private javax.swing.JCheckBox transientCheckBox;
1462: private javax.swing.JComboBox variableCombo;
1463: private javax.swing.JCheckBox volatileCheckBox;
1464: // End of variables declaration//GEN-END:variables
1465: private JPanel initGutter;
1466: private JPanel declareGutter;
1467:
1468: private static final boolean[] variableValues = { false, true };
1469: private static final String[] variableStrings = {
1470: NbBundle.getMessage(CustomCodeView.class,
1471: "CTL_VariableCombo_Field"), // NOI18N
1472: NbBundle.getMessage(CustomCodeView.class,
1473: "CTL_VariableCombo_Local") }; // NOI18N
1474: private static final int[] accessValues = { Modifier.PRIVATE, 0,
1475: Modifier.PROTECTED, Modifier.PUBLIC };
1476: private static final String[] accessStrings = {
1477: "private", // NOI18N
1478: NbBundle.getMessage(CustomCodeView.class,
1479: "CTL_AccessCombo_package_private"), // NOI18N
1480: "protected", // NOI18N
1481: "public" }; // NOI18N
1482: }
|